From 859a5f88cb042af32a4a9aa468af4fa8765321ac Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sat, 6 Sep 2014 18:14:16 +0300 Subject: [PATCH] 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); + } + +}