Skip to content

Commit

Permalink
Merge pull request #998 from alcaeus/fix-project-dir-workaround
Browse files Browse the repository at this point in the history
Fix workaround for missing getProjectDir method
  • Loading branch information
alcaeus committed Jul 30, 2019
2 parents 2bc0ff3 + 52f574c commit 3fd175a
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions Tests/Command/ImportMappingDoctrineCommandTest.php
Expand Up @@ -13,11 +13,10 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
use function method_exists;

class ImportMappingDoctrineCommandTest extends TestCase
{
/** @var Kernel|null */
/** @var ImportMappingTestingKernel|null */
private $kernel;

/** @var CommandTester|null */
Expand Down Expand Up @@ -84,10 +83,10 @@ public function testExecuteXmlWithNamespace()
{
$this->commandTester->execute([
'name' => 'Some\Namespace\Entity',
'--path' => $this->getProjectDir($this->kernel) . '/config/doctrine',
'--path' => $this->kernel->getProjectDir() . '/config/doctrine',
]);

$expectedMetadataPath = $this->getProjectDir($this->kernel) . '/config/doctrine/Product.orm.xml';
$expectedMetadataPath = $this->kernel->getProjectDir() . '/config/doctrine/Product.orm.xml';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('"Some\Namespace\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
}
Expand All @@ -96,26 +95,21 @@ public function testExecuteAnnotationsWithNamespace()
{
$this->commandTester->execute([
'name' => 'Some\Namespace\Entity',
'--path' => $this->getProjectDir($this->kernel) . '/src/Entity',
'--path' => $this->kernel->getProjectDir() . '/src/Entity',
'mapping-type' => 'annotation',
]);

$expectedMetadataPath = $this->getProjectDir($this->kernel) . '/src/Entity/Product.php';
$expectedMetadataPath = $this->kernel->getProjectDir() . '/src/Entity/Product.php';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('namespace Some\Namespace\Entity;', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
}

/**
* BC layer to support Symfony < 4.2 and Symfony >= 5.0. Once support for Symfony < 4.2 has been removed, this method can be dropped.
*/
private function getProjectDir(Kernel $kernel) : string
{
return method_exists($kernel, 'getProjectDir') ? $kernel->getProjectDir() : $kernel->getRootDir();
}
}

class ImportMappingTestingKernel extends Kernel
{
/** @var string|null */
private $projectDir;

public function __construct()
{
parent::__construct('test', true);
Expand Down Expand Up @@ -150,13 +144,18 @@ public function registerContainerConfiguration(LoaderInterface $loader)
});
}

public function getRootDir()
public function getProjectDir()
{
if ($this->rootDir === null) {
$this->rootDir = sys_get_temp_dir() . '/sf_kernel_' . md5(mt_rand());
if ($this->projectDir === null) {
$this->projectDir = sys_get_temp_dir() . '/sf_kernel_' . md5(mt_rand());
}

return $this->rootDir;
return $this->projectDir;
}

public function getRootDir()
{
return $this->getProjectDir();
}
}

Expand Down

0 comments on commit 3fd175a

Please sign in to comment.