From 2290d1ff9b71a0cb5e2b3b5b6dccc28b553ff601 Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 27 Mar 2015 13:10:14 +0200 Subject: [PATCH] fix EntityGenerator RegenerateEntityIfExists --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 4 +-- .../Tests/ORM/Tools/EntityGeneratorTest.php | 33 ++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index a2085365b97..3f634104db1 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -701,7 +701,7 @@ protected function parseTokensInEntityFile($src) */ protected function hasProperty($property, ClassMetadataInfo $metadata) { - if ($this->extendsClass() || class_exists($metadata->name)) { + if ($this->extendsClass() || (!$this->isNew && class_exists($metadata->name))) { // don't generate property if its already on the base class. $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); if ($reflClass->hasProperty($property)) { @@ -723,7 +723,7 @@ protected function hasProperty($property, ClassMetadataInfo $metadata) */ protected function hasMethod($method, ClassMetadataInfo $metadata) { - if ($this->extendsClass() || class_exists($metadata->name)) { + if ($this->extendsClass() || (!$this->isNew && class_exists($metadata->name))) { // don't generate method if its already on the base class. $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index aff4e54b43d..e8218d5ca7c 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -527,7 +527,25 @@ public function testMethodsAndPropertiesAreNotDuplicatedInChildClasses() $this->assertFalse($rc3->hasMethod('getCreatedAt')); $this->assertFalse($rc3->hasMethod('setCreatedAt')); } - + + public function testRegenerateEntityClass() + { + $metadata = $this->generateBookEntityFixture(); + $this->loadEntityClass($metadata); + + $className = basename(str_replace('\\', '/', $metadata->name)); + $path = $this->_tmpDir . '/' . $this->_namespace . '/' . $className . '.php'; + $classTest = file_get_contents($path); + + $this->_generator->setRegenerateEntityIfExists(true); + $this->_generator->setBackupExisting(false); + + $this->_generator->writeEntityClass($metadata, $this->_tmpDir); + $classNew = file_get_contents($path); + + $this->assertSame($classTest,$classNew); + } + /** * @return array */ @@ -639,6 +657,19 @@ public static function someMethod(){ ); } + /** + * @param ClassMetadataInfo $metadata + */ + private function loadEntityClass(ClassMetadataInfo $metadata) + { + $className = basename(str_replace('\\', '/', $metadata->name)); + $path = $this->_tmpDir . '/' . $this->_namespace . '/' . $className . '.php'; + + $this->assertFileExists($path); + + require_once $path; + } + /** * @param string $type * @param \ReflectionProperty $property