From c0ee57ae55157d2258549ae5c0d13e4bcd3668a6 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 27 Jul 2014 14:44:31 +0300 Subject: [PATCH 01/14] Default/Custom Entity Repository for Entity Repository Generator --- .../Command/GenerateRepositoriesCommand.php | 6 ++- .../ORM/Tools/EntityRepositoryGenerator.php | 49 ++++++++++++++++--- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php index 975bc6938..917f7ac76 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php @@ -73,6 +73,8 @@ EOT $metadatas = $em->getMetadataFactory()->getAllMetadata(); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); + $repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName(); + // Process destination directory $destPath = realpath($input->getArgument('dest-path')); @@ -91,7 +93,9 @@ EOT if (count($metadatas)) { $numRepositories = 0; $generator = new EntityRepositoryGenerator(); - + + $generator->setDefaultRepositoryName($repositoryName); + foreach ($metadatas as $metadata) { if ($metadata->customRepositoryClassName) { $output->writeln( diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index 5093cd54d..24c30568c 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -32,20 +32,20 @@ namespace Doctrine\ORM\Tools; */ class EntityRepositoryGenerator { + protected $_repositoryName = 'Doctrine\ORM\EntityRepository'; + protected static $_template = ' -use Doctrine\ORM\EntityRepository; - /** * * * This class was generated by the Doctrine ORM. Add your own custom * repository methods below. */ -class extends EntityRepository +class extends { } '; @@ -60,8 +60,9 @@ class extends EntityRepository $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName)); $variables = array( - '' => $this->generateEntityRepositoryNamespace($fullClassName), - '' => $className + '' => $this->generateEntityRepositoryNamespace($fullClassName), + '' => $this->generateEntityRepositoryName(), + '' => $className ); return str_replace(array_keys($variables), array_values($variables), self::$_template); @@ -74,13 +75,27 @@ class extends EntityRepository * * @return string $namespace */ - private function generateEntityRepositoryNamespace($fullClassName) + protected function generateEntityRepositoryNamespace($fullClassName) { $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\')); - + return $namespace ? 'namespace ' . $namespace . ';' : ''; } + /** + * @return string $repositoryName + */ + protected function generateEntityRepositoryName() + { + $repositoryName = $this->getDefaultRepositoryName(); + + if (substr($repositoryName, 0, 1) != '\\') { + $repositoryName = '\\'.$repositoryName; + } + + return $repositoryName; + } + /** * @param string $fullClassName * @param string $outputDirectory @@ -103,4 +118,24 @@ class extends EntityRepository file_put_contents($path, $code); } } + + /** + * @param string $repositoryName + * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator + */ + public function setDefaultRepositoryName($repositoryName) + { + $this->_repositoryName = $repositoryName; + + return $this; + } + + /** + * @return string + */ + public function getDefaultRepositoryName() + { + return $this->_repositoryName; + } + } From 90efaeec42043ae85d68be8291dc7836a2fc2456 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 27 Jul 2014 14:54:35 +0300 Subject: [PATCH 02/14] Code Style fixes --- .../ORM/Tools/Console/Command/GenerateRepositoriesCommand.php | 4 ++-- lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php index 917f7ac76..c6c139914 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php @@ -93,9 +93,9 @@ EOT if (count($metadatas)) { $numRepositories = 0; $generator = new EntityRepositoryGenerator(); - + $generator->setDefaultRepositoryName($repositoryName); - + foreach ($metadatas as $metadata) { if ($metadata->customRepositoryClassName) { $output->writeln( diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index 24c30568c..a5056a452 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -90,7 +90,7 @@ class extends $repositoryName = $this->getDefaultRepositoryName(); if (substr($repositoryName, 0, 1) != '\\') { - $repositoryName = '\\'.$repositoryName; + $repositoryName = '\\' . $repositoryName; } return $repositoryName; From 3dc2a6858389bee62b9cee5f7695818cce77f73c Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 27 Jul 2014 15:22:51 +0300 Subject: [PATCH 03/14] simple fixes --- .../ORM/Tools/EntityRepositoryGenerator.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index a5056a452..32927645d 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -75,10 +75,10 @@ class extends * * @return string $namespace */ - protected function generateEntityRepositoryNamespace($fullClassName) + private function generateEntityRepositoryNamespace($fullClassName) { $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\')); - + return $namespace ? 'namespace ' . $namespace . ';' : ''; } @@ -87,7 +87,7 @@ class extends */ protected function generateEntityRepositoryName() { - $repositoryName = $this->getDefaultRepositoryName(); + $repositoryName = $this->_repositoryName; if (substr($repositoryName, 0, 1) != '\\') { $repositoryName = '\\' . $repositoryName; @@ -130,12 +130,4 @@ class extends return $this; } - /** - * @return string - */ - public function getDefaultRepositoryName() - { - return $this->_repositoryName; - } - } From c04b01cbd7e496dae2d04fbf2cd84dea37498148 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 27 Jul 2014 17:02:19 +0300 Subject: [PATCH 04/14] Added NS checking --- .../ORM/Tools/EntityRepositoryGenerator.php | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index 32927645d..b074afa06 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -61,13 +61,27 @@ class extends $variables = array( '' => $this->generateEntityRepositoryNamespace($fullClassName), - '' => $this->generateEntityRepositoryName(), + '' => $this->generateEntityRepositoryName($fullClassName), '' => $className ); return str_replace(array_keys($variables), array_values($variables), self::$_template); } + /** + * Generates the namespace, if class do not have namespace, return empty string instead. + * + * @param string $fullClassName + * + * @return string $namespace + */ + protected function getClassNamespace($fullClassName) + { + $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\')); + + return $namespace; + } + /** * Generates the namespace statement, if class do not have namespace, return empty string instead. * @@ -77,19 +91,23 @@ class extends */ private function generateEntityRepositoryNamespace($fullClassName) { - $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\')); + $namespace = $this->getClassNamespace($fullClassName); return $namespace ? 'namespace ' . $namespace . ';' : ''; } /** + * @param string $fullClassName + * * @return string $repositoryName */ - protected function generateEntityRepositoryName() + protected function generateEntityRepositoryName($fullClassName) { + $namespace = $this->getClassNamespace($fullClassName); + $repositoryName = $this->_repositoryName; - if (substr($repositoryName, 0, 1) != '\\') { + if ($namespace && $repositoryName[0] !== '\\') { $repositoryName = '\\' . $repositoryName; } @@ -121,6 +139,7 @@ class extends /** * @param string $repositoryName + * * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator */ public function setDefaultRepositoryName($repositoryName) From a4c7a895077f46111a4b63686d87b6b22e66c88d Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 27 Jul 2014 17:28:06 +0300 Subject: [PATCH 05/14] className fix: If namespace is not set then ClassName will be cut of --- .../ORM/Tools/EntityRepositoryGenerator.php | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index b074afa06..e94749069 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -57,12 +57,10 @@ class extends */ public function generateEntityRepositoryClass($fullClassName) { - $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName)); - $variables = array( '' => $this->generateEntityRepositoryNamespace($fullClassName), '' => $this->generateEntityRepositoryName($fullClassName), - '' => $className + '' => $this->generateClassName($fullClassName) ); return str_replace(array_keys($variables), array_values($variables), self::$_template); @@ -82,6 +80,26 @@ class extends return $namespace; } + /** + * Generates the class name + * + * @param string $fullClassName + * + * @return string + */ + protected function generateClassName($fullClassName) + { + $namespace = $this->getClassNamespace($fullClassName); + + $className = $fullClassName; + + if ($namespace) { + $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName)); + } + + return $className; + } + /** * Generates the namespace statement, if class do not have namespace, return empty string instead. * From 5d608b62069eaaa58b4a0d1a7a8f6fd6d0371b10 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 27 Jul 2014 19:05:42 +0300 Subject: [PATCH 06/14] Private method EntityRepositoryGenerator::generateEntityRepositoryNamespace() changed to protected --- lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index e94749069..25ec5aee8 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -107,10 +107,10 @@ class extends * * @return string $namespace */ - private function generateEntityRepositoryNamespace($fullClassName) + protected function generateEntityRepositoryNamespace($fullClassName) { $namespace = $this->getClassNamespace($fullClassName); - + return $namespace ? 'namespace ' . $namespace . ';' : ''; } From df80d82aab48ec70cf4705fa75a9cefe06544abd Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 27 Jul 2014 20:15:18 +0300 Subject: [PATCH 07/14] Reverted visibility for EntityRepositoryGenerator::generateEntityRepositoryNamespace() --- lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index 25ec5aee8..88df4a54d 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -107,7 +107,7 @@ class extends * * @return string $namespace */ - protected function generateEntityRepositoryNamespace($fullClassName) + private function generateEntityRepositoryNamespace($fullClassName) { $namespace = $this->getClassNamespace($fullClassName); From 20b72ef344e1d6af17c89644e667d75904ec9b90 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sat, 9 Aug 2014 13:20:27 +0300 Subject: [PATCH 08/14] Visibility for EntityRepositoryGenerator::$repositoryName --- lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index 88df4a54d..39cf3eb9f 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -32,7 +32,7 @@ namespace Doctrine\ORM\Tools; */ class EntityRepositoryGenerator { - protected $_repositoryName = 'Doctrine\ORM\EntityRepository'; + private $repositoryName = 'Doctrine\ORM\EntityRepository'; protected static $_template = ' extends { $namespace = $this->getClassNamespace($fullClassName); - $repositoryName = $this->_repositoryName; + $repositoryName = $this->repositoryName; if ($namespace && $repositoryName[0] !== '\\') { $repositoryName = '\\' . $repositoryName; @@ -162,7 +162,7 @@ class extends */ public function setDefaultRepositoryName($repositoryName) { - $this->_repositoryName = $repositoryName; + $this->repositoryName = $repositoryName; return $this; } From c8565c2772a848c879f0a8147847bf81c290bc89 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Tue, 26 Aug 2014 21:42:55 +0300 Subject: [PATCH 09/14] Visibility for EntityRepositoryGenerator::generateClassName() --- lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index 39cf3eb9f..e1e0f5b02 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -87,7 +87,7 @@ class extends * * @return string */ - protected function generateClassName($fullClassName) + private function generateClassName($fullClassName) { $namespace = $this->getClassNamespace($fullClassName); From 859a5f88cb042af32a4a9aa468af4fa8765321ac Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sat, 6 Sep 2014 18:14:16 +0300 Subject: [PATCH 10/14] Unit Tests && simple fixes --- .../ORM/Tools/EntityRepositoryGenerator.php | 10 +- .../DDC1089/DDC1089EntityRepository.php | 7 + .../Tests/Models/DDC1089/DDC1089User.php | 23 +++ .../Tests/Models/DDC1089/DDC1089User2.php | 57 ++++++++ .../Tools/EntityRepositoryGeneratorTest.php | 132 ++++++++++++++++++ 5 files changed, 226 insertions(+), 3 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/DDC1089/DDC1089EntityRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC1089/DDC1089User.php create mode 100644 tests/Doctrine/Tests/Models/DDC1089/DDC1089User2.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index e1e0f5b02..4e431b754 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -32,7 +32,7 @@ namespace Doctrine\ORM\Tools; */ class EntityRepositoryGenerator { - private $repositoryName = 'Doctrine\ORM\EntityRepository'; + private $repositoryName; protected static $_template = ' extends { $namespace = $this->getClassNamespace($fullClassName); - $repositoryName = $this->repositoryName; + $repositoryName = $this->repositoryName ?: 'Doctrine\ORM\EntityRepository'; if ($namespace && $repositoryName[0] !== '\\') { $repositoryName = '\\' . $repositoryName; @@ -136,7 +136,7 @@ class extends * @param string $fullClassName * @param string $outputDirectory * - * @return void + * @return string */ public function writeEntityRepositoryClass($fullClassName, $outputDirectory) { @@ -152,7 +152,11 @@ class extends if ( ! file_exists($path)) { file_put_contents($path, $code); + + return $path; } + + return null; } /** diff --git a/tests/Doctrine/Tests/Models/DDC1089/DDC1089EntityRepository.php b/tests/Doctrine/Tests/Models/DDC1089/DDC1089EntityRepository.php new file mode 100644 index 000000000..143b8df60 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC1089/DDC1089EntityRepository.php @@ -0,0 +1,7 @@ +id; + } + + /** + * Set name + * + * @param string $name + * + * @return DDC1089User + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * Get name + * + * @return string + */ + public function getName() + { + return $this->name; + } + +} diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php new file mode 100644 index 000000000..442f034a3 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php @@ -0,0 +1,132 @@ +_namespace = uniqid("doctrine_"); + $this->_tmpDir = \sys_get_temp_dir(); + \mkdir($this->_tmpDir . \DIRECTORY_SEPARATOR . $this->_namespace); + + $this->_generator = new EntityGenerator(); + $this->_generator->setAnnotationPrefix(""); + $this->_generator->setGenerateAnnotations(true); + $this->_generator->setGenerateStubMethods(true); + $this->_generator->setRegenerateEntityIfExists(false); + $this->_generator->setUpdateEntityIfExists(true); + $this->_generator->setFieldVisibility(EntityGenerator::FIELD_VISIBLE_PROTECTED); + + $this->_repositoryGenerator = new EntityRepositoryGenerator(); + } + + /** + * @inheritdoc + */ + public function tearDown() + { + $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir . '/' . $this->_namespace)); + foreach ($ri AS $file) { + /* @var $file \SplFileInfo */ + if ($file->isFile()) { + \unlink($file->getPathname()); + } + } + rmdir($this->_tmpDir . '/' . $this->_namespace); + } + + /** + * @group DDC-1089 + */ + public function testGeneratedEntityRepositoryClass() + { + $em = $this->_getTestEntityManager(); + $ns = $this->_namespace; + + $className = 'DDC1089User'; + $this->writeEntityClass('Doctrine\Tests\Models\DDC1089\\' . $className, $ns . '\\' . $className); + + $rpath = $this->writeRepositoryClass($ns . '\\' . $className, 'Doctrine\Tests\Models\DDC1089\DDC1089EntityRepository'); + + $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\DDC1089\DDC1089EntityRepository', $repo->getParentClass()->getName()); + + + $className2 = 'DDC1089User2'; + $this->writeEntityClass('Doctrine\Tests\Models\DDC1089\\' . $className2, $ns . '\\' . $className2); + + $rpath2 = $this->writeRepositoryClass($ns . '\\' . $className2); + + $this->assertNotNull($rpath2); + $this->assertFileExists($rpath2); + + require $rpath2; + + $repo2 = new \ReflectionClass($em->getRepository($ns . '\\' . $className2)); + + $this->assertSame($ns . '\\' . $className2 . 'Repository', $repo2->getName()); + $this->assertSame('Doctrine\ORM\EntityRepository', $repo2->getParentClass()->getName()); + } + + /** + * @param string $className + * @param string $newClassName + */ + private function writeEntityClass($className, $newClassName) + { + $cmf = new ClassMetadataFactory(); + $em = $this->_getTestEntityManager(); + + $cmf->setEntityManager($em); + + $metadata = $cmf->getMetadataFor($className); + $metadata->namespace = $this->_namespace; + $metadata->name = $newClassName; + $metadata->customRepositoryClassName = $newClassName . "Repository"; + + $this->_generator->writeEntityClass($metadata, $this->_tmpDir); + + require $this->_tmpDir . '/' . str_replace('\\', '/', $newClassName) . ".php"; + } + + /** + * @param string $className + * @param string $defaultRepository + * @return string + */ + private function writeRepositoryClass($className, $defaultRepository = null) + { + $this->_repositoryGenerator->setDefaultRepositoryName($defaultRepository); + + return $this->_repositoryGenerator->writeEntityRepositoryClass($className . 'Repository', $this->_tmpDir); + } + +} From cd547fecea547cd3a60e2bd50c2ce7f8994548af Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sat, 6 Sep 2014 18:29:14 +0300 Subject: [PATCH 11/14] Corrected names to DDC3231 --- .../DDC1089/DDC1089EntityRepository.php | 7 --- .../Tests/Models/DDC1089/DDC1089User2.php | 57 ------------------- .../DDC3231/DDC3231EntityRepository.php | 7 +++ .../DDC3231User.php} | 6 +- .../Tests/Models/DDC3231/DDC3231User2.php | 23 ++++++++ .../Tools/EntityRepositoryGeneratorTest.php | 14 ++--- 6 files changed, 40 insertions(+), 74 deletions(-) delete mode 100644 tests/Doctrine/Tests/Models/DDC1089/DDC1089EntityRepository.php delete mode 100644 tests/Doctrine/Tests/Models/DDC1089/DDC1089User2.php create mode 100644 tests/Doctrine/Tests/Models/DDC3231/DDC3231EntityRepository.php rename tests/Doctrine/Tests/Models/{DDC1089/DDC1089User.php => DDC3231/DDC3231User.php} (68%) create mode 100644 tests/Doctrine/Tests/Models/DDC3231/DDC3231User2.php diff --git a/tests/Doctrine/Tests/Models/DDC1089/DDC1089EntityRepository.php b/tests/Doctrine/Tests/Models/DDC1089/DDC1089EntityRepository.php deleted file mode 100644 index 143b8df60..000000000 --- a/tests/Doctrine/Tests/Models/DDC1089/DDC1089EntityRepository.php +++ /dev/null @@ -1,7 +0,0 @@ -id; - } - - /** - * Set name - * - * @param string $name - * - * @return DDC1089User - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } - - /** - * Get name - * - * @return string - */ - public function getName() - { - return $this->name; - } - -} diff --git a/tests/Doctrine/Tests/Models/DDC3231/DDC3231EntityRepository.php b/tests/Doctrine/Tests/Models/DDC3231/DDC3231EntityRepository.php new file mode 100644 index 000000000..91ca8453c --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC3231/DDC3231EntityRepository.php @@ -0,0 +1,7 @@ +_getTestEntityManager(); $ns = $this->_namespace; - $className = 'DDC1089User'; - $this->writeEntityClass('Doctrine\Tests\Models\DDC1089\\' . $className, $ns . '\\' . $className); + $className = 'DDC3231User'; + $this->writeEntityClass('Doctrine\Tests\Models\DDC3231\\' . $className, $ns . '\\' . $className); - $rpath = $this->writeRepositoryClass($ns . '\\' . $className, 'Doctrine\Tests\Models\DDC1089\DDC1089EntityRepository'); + $rpath = $this->writeRepositoryClass($ns . '\\' . $className, 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository'); $this->assertNotNull($rpath); $this->assertFileExists($rpath); @@ -77,11 +77,11 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase $repo = new \ReflectionClass($em->getRepository($ns . '\\' . $className)); $this->assertSame($ns . '\\' . $className . 'Repository', $repo->getName()); - $this->assertSame('Doctrine\Tests\Models\DDC1089\DDC1089EntityRepository', $repo->getParentClass()->getName()); + $this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo->getParentClass()->getName()); - $className2 = 'DDC1089User2'; - $this->writeEntityClass('Doctrine\Tests\Models\DDC1089\\' . $className2, $ns . '\\' . $className2); + $className2 = 'DDC3231User2'; + $this->writeEntityClass('Doctrine\Tests\Models\DDC3231\\' . $className2, $ns . '\\' . $className2); $rpath2 = $this->writeRepositoryClass($ns . '\\' . $className2); From 409f6b4bc18a599e7693a7e36aefca8ad848a4b7 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 7 Sep 2014 18:26:09 +0300 Subject: [PATCH 12/14] 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"; } /** From e295a6d05e37a920843f3151292f901ce0607bfc Mon Sep 17 00:00:00 2001 From: encoder64 Date: Tue, 9 Sep 2014 22:17:10 +0300 Subject: [PATCH 13/14] visibility changed && few simple fixes --- .../ORM/Tools/EntityRepositoryGenerator.php | 10 +++------- .../ORM/Tools/EntityRepositoryGeneratorTest.php | 13 ++++++++----- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index 4e431b754..f94292afc 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -73,7 +73,7 @@ class extends * * @return string $namespace */ - protected function getClassNamespace($fullClassName) + private function getClassNamespace($fullClassName) { $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\')); @@ -119,7 +119,7 @@ class extends * * @return string $repositoryName */ - protected function generateEntityRepositoryName($fullClassName) + private function generateEntityRepositoryName($fullClassName) { $namespace = $this->getClassNamespace($fullClassName); @@ -136,7 +136,7 @@ class extends * @param string $fullClassName * @param string $outputDirectory * - * @return string + * @return void */ public function writeEntityRepositoryClass($fullClassName, $outputDirectory) { @@ -152,11 +152,7 @@ class extends if ( ! file_exists($path)) { file_put_contents($path, $code); - - return $path; } - - return null; } /** diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php index dbf5d9796..3a96c8183 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php @@ -74,7 +74,7 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase $ns = $this->_namespace; - require_once __DIR__.'/../../Models/DDC3231/DDC3231User1.php'; + require_once __DIR__ . '/../../Models/DDC3231/DDC3231User1.php'; $className = $ns . '\DDC3231User1Tmp'; $this->writeEntityClass('Doctrine\Tests\Models\DDC3231\DDC3231User1', $className); @@ -93,7 +93,7 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertSame('Doctrine\ORM\EntityRepository', $repo->getParentClass()->getName()); - require_once __DIR__.'/../../Models/DDC3231/DDC3231User1NoNamespace.php'; + require_once __DIR__ . '/../../Models/DDC3231/DDC3231User1NoNamespace.php'; $className2 = 'DDC3231User1NoNamespaceTmp'; $this->writeEntityClass('DDC3231User1NoNamespace', $className2); @@ -121,7 +121,7 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase $ns = $this->_namespace; - require_once __DIR__.'/../../Models/DDC3231/DDC3231User2.php'; + require_once __DIR__ . '/../../Models/DDC3231/DDC3231User2.php'; $className = $ns . '\DDC3231User2Tmp'; $this->writeEntityClass('Doctrine\Tests\Models\DDC3231\DDC3231User2', $className); @@ -140,7 +140,7 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo->getParentClass()->getName()); - require_once __DIR__.'/../../Models/DDC3231/DDC3231User2NoNamespace.php'; + require_once __DIR__ . '/../../Models/DDC3231/DDC3231User2NoNamespace.php'; $className2 = 'DDC3231User2NoNamespaceTmp'; $this->writeEntityClass('DDC3231User2NoNamespace', $className2); @@ -162,6 +162,7 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase /** * @param string $className * @param string $newClassName + * @return string */ private function writeEntityClass($className, $newClassName) { @@ -189,7 +190,9 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase { $this->_repositoryGenerator->setDefaultRepositoryName($defaultRepository); - return $this->_repositoryGenerator->writeEntityRepositoryClass($className . 'Repository', $this->_tmpDir); + $this->_repositoryGenerator->writeEntityRepositoryClass($className . 'Repository', $this->_tmpDir); + + return $this->_tmpDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $className) . 'Repository.php'; } } From 6bc003e47d53eb46ff96c2c2f25554c15611cd6c Mon Sep 17 00:00:00 2001 From: encoder64 Date: Wed, 10 Sep 2014 01:07:22 +0300 Subject: [PATCH 14/14] simple fixes --- .../Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php index 3a96c8183..02d6ed5a1 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityRepositoryGeneratorTest.php @@ -81,7 +81,6 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase $rpath = $this->writeRepositoryClass($className); - $this->assertNotNull($rpath); $this->assertFileExists($rpath); require $rpath; @@ -100,7 +99,6 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase $rpath2 = $this->writeRepositoryClass($className2); - $this->assertNotNull($rpath2); $this->assertFileExists($rpath2); require $rpath2;