From 409f6b4bc18a599e7693a7e36aefca8ad848a4b7 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 7 Sep 2014 18:26:09 +0300 Subject: [PATCH] Unit Tests && simple fixes --- .../{DDC3231User.php => DDC3231User1.php} | 4 +- .../DDC3231/DDC3231User1NoNamespace.php | 21 +++ .../DDC3231/DDC3231User2NoNamespace.php | 21 +++ .../GenerateRepositoriesCommandTest.php | 143 ++++++++++++++++++ .../Tools/EntityRepositoryGeneratorTest.php | 101 ++++++++++--- 5 files changed, 269 insertions(+), 21 deletions(-) rename tests/Doctrine/Tests/Models/DDC3231/{DDC3231User.php => DDC3231User1.php} (79%) create mode 100644 tests/Doctrine/Tests/Models/DDC3231/DDC3231User1NoNamespace.php create mode 100644 tests/Doctrine/Tests/Models/DDC3231/DDC3231User2NoNamespace.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/Console/Command/GenerateRepositoriesCommandTest.php diff --git a/tests/Doctrine/Tests/Models/DDC3231/DDC3231User.php b/tests/Doctrine/Tests/Models/DDC3231/DDC3231User1.php similarity index 79% rename from tests/Doctrine/Tests/Models/DDC3231/DDC3231User.php rename to tests/Doctrine/Tests/Models/DDC3231/DDC3231User1.php index c71f3997e..8713bb0bd 100644 --- a/tests/Doctrine/Tests/Models/DDC3231/DDC3231User.php +++ b/tests/Doctrine/Tests/Models/DDC3231/DDC3231User1.php @@ -3,10 +3,10 @@ namespace Doctrine\Tests\Models\DDC3231; /** - * @Entity(repositoryClass="DDC3231UserRepository") + * @Entity(repositoryClass="DDC3231User1Repository") * @Table(name="users") */ -class DDC3231User +class DDC3231User1 { /** * @Id diff --git a/tests/Doctrine/Tests/Models/DDC3231/DDC3231User1NoNamespace.php b/tests/Doctrine/Tests/Models/DDC3231/DDC3231User1NoNamespace.php new file mode 100644 index 000000000..a8f797549 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC3231/DDC3231User1NoNamespace.php @@ -0,0 +1,21 @@ +path = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('doctrine_'); + + \mkdir($this->path); + + + $metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl(); + + $metadataDriver->addPaths(array( + __DIR__ . '/../../../../Models/DDC3231/' + )); + + $this->application = new Application(); + + $this->application->setHelperSet(new HelperSet(array( + 'em' => new EntityManagerHelper($this->_em) + ))); + + $this->application->add(new GenerateRepositoriesCommand()); + + } + + /** + * @inheritdoc + */ + public function tearDown() + { + $dirs = array(); + + $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path)); + foreach ($ri AS $file) { + /* @var $file \SplFileInfo */ + if ($file->isFile()) { + \unlink($file->getPathname()); + } elseif ($file->getBasename() === '.') { + $dirs[] = $file->getRealpath(); + } + } + + arsort($dirs); + + foreach ($dirs as $dir) { + \rmdir($dir); + } + + parent::tearDown(); + } + + public function testGenerateRepositories() + { + $this->generateRepositories('DDC3231User1'); + + $cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User1Repository'; + $fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php'; + + $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname); + $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php'); + + require $this->path . DIRECTORY_SEPARATOR . $fname; + require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php'; + + $this->assertTrue(class_exists($cname)); + $this->assertTrue(class_exists('DDC3231User1NoNamespaceRepository')); + + $repo1 = new \ReflectionClass($cname); + $repo2 = new \ReflectionClass('DDC3231User1NoNamespaceRepository'); + + $this->assertSame('Doctrine\ORM\EntityRepository', $repo1->getParentClass()->getName()); + $this->assertSame('Doctrine\ORM\EntityRepository', $repo2->getParentClass()->getName()); + } + + public function testGenerateRepositoriesCustomDefaultRepository() + { + $this->generateRepositories('DDC3231User2', 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository'); + + $cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User2Repository'; + $fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php'; + + $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname); + $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php'); + + require $this->path . DIRECTORY_SEPARATOR . $fname; + require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php'; + + $this->assertTrue(class_exists($cname)); + $this->assertTrue(class_exists('DDC3231User2NoNamespaceRepository')); + + $repo1 = new \ReflectionClass($cname); + $repo2 = new \ReflectionClass('DDC3231User2NoNamespaceRepository'); + + $this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo1->getParentClass()->getName()); + $this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo2->getParentClass()->getName()); + } + + /** + * @param string $filter + * @param string $defaultRepository + */ + private function generateRepositories($filter, $defaultRepository = null) + { + if ($defaultRepository) { + $this->_em->getConfiguration()->setDefaultRepositoryClassName($defaultRepository); + } + + $command = $this->application->find('orm:generate-repositories'); + $tester = new CommandTester($command); + $tester->execute(array( + 'command' => $command->getName(), + 'dest-path' => $this->path, + '--filter' => $filter, + )); + } + +} diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php index 002d0d557..dbf5d9796 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php @@ -26,10 +26,10 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase */ public function setUp() { - $this->_namespace = uniqid("doctrine_"); - $this->_tmpDir = \sys_get_temp_dir(); - \mkdir($this->_tmpDir . \DIRECTORY_SEPARATOR . $this->_namespace); - + $this->_namespace = uniqid('doctrine_'); + $this->_tmpDir = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . $this->_namespace; + \mkdir($this->_tmpDir); + $this->_generator = new EntityGenerator(); $this->_generator->setAnnotationPrefix(""); $this->_generator->setGenerateAnnotations(true); @@ -46,14 +46,23 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase */ public function tearDown() { - $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir . '/' . $this->_namespace)); + $dirs = array(); + + $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir)); foreach ($ri AS $file) { /* @var $file \SplFileInfo */ if ($file->isFile()) { \unlink($file->getPathname()); + } elseif ($file->getBasename() === '.') { + $dirs[] = $file->getRealpath(); } } - rmdir($this->_tmpDir . '/' . $this->_namespace); + + arsort($dirs); + + foreach ($dirs as $dir) { + \rmdir($dir); + } } /** @@ -64,38 +73,92 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase $em = $this->_getTestEntityManager(); $ns = $this->_namespace; - $className = 'DDC3231User'; - $this->writeEntityClass('Doctrine\Tests\Models\DDC3231\\' . $className, $ns . '\\' . $className); - $rpath = $this->writeRepositoryClass($ns . '\\' . $className, 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository'); + require_once __DIR__.'/../../Models/DDC3231/DDC3231User1.php'; + + $className = $ns . '\DDC3231User1Tmp'; + $this->writeEntityClass('Doctrine\Tests\Models\DDC3231\DDC3231User1', $className); + + $rpath = $this->writeRepositoryClass($className); $this->assertNotNull($rpath); $this->assertFileExists($rpath); require $rpath; - - $repo = new \ReflectionClass($em->getRepository($ns . '\\' . $className)); - $this->assertSame($ns . '\\' . $className . 'Repository', $repo->getName()); - $this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo->getParentClass()->getName()); + $repo = new \ReflectionClass($em->getRepository($className)); + + $this->assertTrue($repo->inNamespace()); + $this->assertSame($className . 'Repository', $repo->getName()); + $this->assertSame('Doctrine\ORM\EntityRepository', $repo->getParentClass()->getName()); - $className2 = 'DDC3231User2'; - $this->writeEntityClass('Doctrine\Tests\Models\DDC3231\\' . $className2, $ns . '\\' . $className2); + require_once __DIR__.'/../../Models/DDC3231/DDC3231User1NoNamespace.php'; - $rpath2 = $this->writeRepositoryClass($ns . '\\' . $className2); + $className2 = 'DDC3231User1NoNamespaceTmp'; + $this->writeEntityClass('DDC3231User1NoNamespace', $className2); + + $rpath2 = $this->writeRepositoryClass($className2); $this->assertNotNull($rpath2); $this->assertFileExists($rpath2); require $rpath2; - $repo2 = new \ReflectionClass($em->getRepository($ns . '\\' . $className2)); + $repo2 = new \ReflectionClass($em->getRepository($className2)); - $this->assertSame($ns . '\\' . $className2 . 'Repository', $repo2->getName()); + $this->assertFalse($repo2->inNamespace()); + $this->assertSame($className2 . 'Repository', $repo2->getName()); $this->assertSame('Doctrine\ORM\EntityRepository', $repo2->getParentClass()->getName()); } + /** + * @group DDC-3231 + */ + public function testGeneratedEntityRepositoryClassCustomDefaultRepository() + { + $em = $this->_getTestEntityManager(); + $ns = $this->_namespace; + + + require_once __DIR__.'/../../Models/DDC3231/DDC3231User2.php'; + + $className = $ns . '\DDC3231User2Tmp'; + $this->writeEntityClass('Doctrine\Tests\Models\DDC3231\DDC3231User2', $className); + + $rpath = $this->writeRepositoryClass($className, 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository'); + + $this->assertNotNull($rpath); + $this->assertFileExists($rpath); + + require $rpath; + + $repo = new \ReflectionClass($em->getRepository($className)); + + $this->assertTrue($repo->inNamespace()); + $this->assertSame($className . 'Repository', $repo->getName()); + $this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo->getParentClass()->getName()); + + + require_once __DIR__.'/../../Models/DDC3231/DDC3231User2NoNamespace.php'; + + $className2 = 'DDC3231User2NoNamespaceTmp'; + $this->writeEntityClass('DDC3231User2NoNamespace', $className2); + + $rpath2 = $this->writeRepositoryClass($className2, 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository'); + + $this->assertNotNull($rpath2); + $this->assertFileExists($rpath2); + + require $rpath2; + + $repo2 = new \ReflectionClass($em->getRepository($className2)); + + $this->assertFalse($repo2->inNamespace()); + $this->assertSame($className2 . 'Repository', $repo2->getName()); + $this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo2->getParentClass()->getName()); + } + /** * @param string $className * @param string $newClassName @@ -114,7 +177,7 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->_generator->writeEntityClass($metadata, $this->_tmpDir); - require $this->_tmpDir . '/' . str_replace('\\', '/', $newClassName) . ".php"; + require $this->_tmpDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $newClassName) . ".php"; } /**