From e0ae7634d5e27b7f903e5b3b80bc56fec0dcc9e6 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 3 Aug 2014 17:02:33 +0300 Subject: [PATCH 1/4] #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 */ From 4e805bb59a766d4741d1197ddd5ca6f4dfff5a98 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 3 Aug 2014 17:07:20 +0300 Subject: [PATCH 2/4] #DDC-1590: Fix Inheritance in Code-Generation: Code Style Fixes --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index c040dff57..918e4162b 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -751,7 +751,7 @@ public function __construct() return true; } } - + // check traits for existing method foreach ($this->getTraits($metadata) as $trait) { if ($trait->hasMethod($method)) { From 94ba6e2dfc455ccdb2505f6d566286cc5eff6b28 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Mon, 4 Aug 2014 20:18:26 +0300 Subject: [PATCH 3/4] #DDC-1590: Simple Fixes --- .../Tests/Models/DDC1590/DDC1590Entity.php | 29 ------------------- .../Tests/Models/DDC1590/DDC1590User.php | 23 --------------- .../Tests/ORM/Tools/EntityGeneratorTest.php | 12 ++------ 3 files changed, 3 insertions(+), 61 deletions(-) diff --git a/tests/Doctrine/Tests/Models/DDC1590/DDC1590Entity.php b/tests/Doctrine/Tests/Models/DDC1590/DDC1590Entity.php index 7d6565a28..a4dff26fa 100644 --- a/tests/Doctrine/Tests/Models/DDC1590/DDC1590Entity.php +++ b/tests/Doctrine/Tests/Models/DDC1590/DDC1590Entity.php @@ -20,11 +20,6 @@ abstract class DDC1590Entity */ protected $created_at; - /** - * @Column(type="datetime") - */ - protected $updated_at; - /** * Get id * @@ -58,28 +53,4 @@ abstract class DDC1590Entity { 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 index 9e521df13..436ab0d54 100644 --- a/tests/Doctrine/Tests/Models/DDC1590/DDC1590User.php +++ b/tests/Doctrine/Tests/Models/DDC1590/DDC1590User.php @@ -15,27 +15,4 @@ class DDC1590User extends DDC1590Entity */ protected $name; - /** - * Set name - * - * @param string $name - * - * @return DDC1590User - */ - 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/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index 9e9687e91..2baecb88d 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -562,7 +562,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $content = str_replace( 'namespace Doctrine\Tests\Models\DDC1590', - 'namespace '.$ns, + 'namespace ' . $ns, file_get_contents(__DIR__ . '/../../Models/DDC1590/DDC1590User.php') ); @@ -579,13 +579,13 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase // class _DDC1590User extends DDC1590Entity { ... } $source2 = str_replace('class DDC1590User', 'class _DDC1590User', $source); - $fname2 = $fname = $nsdir . "/_DDC1590User.php"; + $fname2 = $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"; + $fname3 = $nsdir . "/__DDC1590User.php"; file_put_contents($fname3, $source3); require $fname3; @@ -596,7 +596,6 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $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); @@ -604,8 +603,6 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $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 { ... } @@ -614,7 +611,6 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $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); @@ -622,8 +618,6 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $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); } /** From cd4bc93483b7cd4ebfb2964461acca66680fb43c Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sat, 9 Aug 2014 13:27:38 +0300 Subject: [PATCH 4/4] Simple Fixes --- .../Tests/ORM/Tools/EntityGeneratorTest.php | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index 2baecb88d..1a41dee83 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -593,31 +593,31 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase // 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->assertTrue($rc2->hasProperty('name')); + $this->assertTrue($rc2->hasProperty('id')); + $this->assertTrue($rc2->hasProperty('created_at')); - $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->assertTrue($rc2->hasMethod('getName')); + $this->assertTrue($rc2->hasMethod('setName')); + $this->assertTrue($rc2->hasMethod('getId')); + $this->assertFalse($rc2->hasMethod('setId')); + $this->assertTrue($rc2->hasMethod('getCreatedAt')); + $this->assertTrue($rc2->hasMethod('setCreatedAt')); // 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->assertTrue($rc3->hasProperty('name')); + $this->assertFalse($rc3->hasProperty('id')); + $this->assertFalse($rc3->hasProperty('created_at')); - $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->assertTrue($rc3->hasMethod('getName')); + $this->assertTrue($rc3->hasMethod('setName')); + $this->assertFalse($rc3->hasMethod('getId')); + $this->assertFalse($rc3->hasMethod('setId')); + $this->assertFalse($rc3->hasMethod('getCreatedAt')); + $this->assertFalse($rc3->hasMethod('setCreatedAt')); } /**