From e0ae7634d5e27b7f903e5b3b80bc56fec0dcc9e6 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 3 Aug 2014 17:02:33 +0300 Subject: [PATCH] #DDC-1590: Fix Inheritance in Code-Generation --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 10 +-- .../Tests/Models/DDC1590/DDC1590Entity.php | 85 +++++++++++++++++++ .../Tests/Models/DDC1590/DDC1590User.php | 41 +++++++++ .../Tests/ORM/Tools/EntityGeneratorTest.php | 79 +++++++++++++++++ 4 files changed, 210 insertions(+), 5 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/DDC1590/DDC1590Entity.php create mode 100644 tests/Doctrine/Tests/Models/DDC1590/DDC1590User.php diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index e6680e53b..c040dff57 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -714,9 +714,9 @@ public function __construct() */ protected function hasProperty($property, ClassMetadataInfo $metadata) { - if ($this->extendsClass()) { + if ($this->extendsClass() || class_exists($metadata->name)) { // don't generate property if its already on the base class. - $reflClass = new \ReflectionClass($this->getClassToExtend()); + $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); if ($reflClass->hasProperty($property)) { return true; } @@ -743,15 +743,15 @@ public function __construct() */ protected function hasMethod($method, ClassMetadataInfo $metadata) { - if ($this->extendsClass()) { + if ($this->extendsClass() || class_exists($metadata->name)) { // don't generate method if its already on the base class. - $reflClass = new \ReflectionClass($this->getClassToExtend()); + $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); if ($reflClass->hasMethod($method)) { return true; } } - + // check traits for existing method foreach ($this->getTraits($metadata) as $trait) { if ($trait->hasMethod($method)) { diff --git a/tests/Doctrine/Tests/Models/DDC1590/DDC1590Entity.php b/tests/Doctrine/Tests/Models/DDC1590/DDC1590Entity.php new file mode 100644 index 000000000..7d6565a28 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC1590/DDC1590Entity.php @@ -0,0 +1,85 @@ +id; + } + + /** + * Set createdAt + * + * @param \DateTime $createdAt + * + * @return DDC1590User + */ + public function setCreatedAt($createdAt) + { + $this->created_at = $createdAt; + + return $this; + } + + /** + * Get createdAt + * + * @return \DateTime + */ + public function getCreatedAt() + { + return $this->created_at; + } + + /** + * Set updatedAt + * + * @param \DateTime $updatedAt + * + * @return DDC1590User + */ + public function setUpdatedAt($updatedAt) + { + $this->updated_at = $updatedAt; + + return $this; + } + + /** + * Get updatedAt + * + * @return \DateTime + */ + public function getUpdatedAt() + { + return $this->updated_at; + } +} diff --git a/tests/Doctrine/Tests/Models/DDC1590/DDC1590User.php b/tests/Doctrine/Tests/Models/DDC1590/DDC1590User.php new file mode 100644 index 000000000..9e521df13 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC1590/DDC1590User.php @@ -0,0 +1,41 @@ +name = $name; + + return $this; + } + + /** + * Get name + * + * @return string + */ + public function getName() + { + return $this->name; + } +} diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index 6394f6399..9e9687e91 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -547,6 +547,85 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertSame($reflClass->hasMethod('getAddress'), false); } + /** + * @group DDC-1590 + */ + public function testMethodsAndPropertiesAreNotDuplicatedInChildClasses() + { + $cmf = new ClassMetadataFactory(); + $em = $this->_getTestEntityManager(); + + $cmf->setEntityManager($em); + + $ns = $this->_namespace; + $nsdir = $this->_tmpDir . '/' . $ns; + + $content = str_replace( + 'namespace Doctrine\Tests\Models\DDC1590', + 'namespace '.$ns, + file_get_contents(__DIR__ . '/../../Models/DDC1590/DDC1590User.php') + ); + + $fname = $nsdir . "/DDC1590User.php"; + file_put_contents($fname, $content); + require $fname; + + + $metadata = $cmf->getMetadataFor($ns . '\DDC1590User'); + $this->_generator->writeEntityClass($metadata, $this->_tmpDir); + + // class DDC1590User extends DDC1590Entity { ... } + $source = file_get_contents($fname); + + // class _DDC1590User extends DDC1590Entity { ... } + $source2 = str_replace('class DDC1590User', 'class _DDC1590User', $source); + $fname2 = $fname = $nsdir . "/_DDC1590User.php"; + file_put_contents($fname2, $source2); + require $fname2; + + // class __DDC1590User { ... } + $source3 = str_replace('class DDC1590User extends DDC1590Entity', 'class __DDC1590User', $source); + $fname3 = $fname = $nsdir . "/__DDC1590User.php"; + file_put_contents($fname3, $source3); + require $fname3; + + + // class _DDC1590User extends DDC1590Entity { ... } + $rc2 = new \ReflectionClass($ns.'\_DDC1590User'); + + $this->assertSame($rc2->hasProperty('name'), true); + $this->assertSame($rc2->hasProperty('id'), true); + $this->assertSame($rc2->hasProperty('created_at'), true); + $this->assertSame($rc2->hasProperty('updated_at'), true); + + $this->assertSame($rc2->hasMethod('getName'), true); + $this->assertSame($rc2->hasMethod('setName'), true); + $this->assertSame($rc2->hasMethod('getId'), true); + $this->assertSame($rc2->hasMethod('setId'), false); + $this->assertSame($rc2->hasMethod('getCreatedAt'), true); + $this->assertSame($rc2->hasMethod('setCreatedAt'), true); + $this->assertSame($rc2->hasMethod('getUpdatedAt'), true); + $this->assertSame($rc2->hasMethod('setUpdatedAt'), true); + + + // class __DDC1590User { ... } + $rc3 = new \ReflectionClass($ns.'\__DDC1590User'); + + $this->assertSame($rc3->hasProperty('name'), true); + $this->assertSame($rc3->hasProperty('id'), false); + $this->assertSame($rc3->hasProperty('created_at'), false); + $this->assertSame($rc3->hasProperty('updated_at'), false); + + $this->assertSame($rc3->hasMethod('getName'), true); + $this->assertSame($rc3->hasMethod('setName'), true); + $this->assertSame($rc3->hasMethod('getId'), false); + $this->assertSame($rc3->hasMethod('setId'), false); + $this->assertSame($rc3->hasMethod('getCreatedAt'), false); + $this->assertSame($rc3->hasMethod('setCreatedAt'), false); + $this->assertSame($rc3->hasMethod('getUpdatedAt'), false); + $this->assertSame($rc3->hasMethod('setUpdatedAt'), false); + } + /** * @return array */