From b3414e3c1ab70050d2b0a206e74bf827cb39f8f4 Mon Sep 17 00:00:00 2001 From: Adam Prager Date: Sun, 31 Mar 2013 00:47:45 +0100 Subject: [PATCH] added unit test --- .../Tests/ORM/Tools/EntityGeneratorTest.php | 58 ++++++++++++++----- tests/Doctrine/Tests/TestInit.php | 1 + tools/sandbox/Entities/TraitedUser.php | 34 +++++++++++ .../sandbox/Entities/Traits/AddressTrait.php | 25 ++++++++ 4 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 tools/sandbox/Entities/TraitedUser.php create mode 100644 tools/sandbox/Entities/Traits/AddressTrait.php diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index a55b603d5..694918d13 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -2,10 +2,12 @@ namespace Doctrine\Tests\ORM\Tools; -use Doctrine\ORM\Tools\SchemaTool, - Doctrine\ORM\Tools\EntityGenerator, - Doctrine\ORM\Tools\Export\ClassMetadataExporter, - Doctrine\ORM\Mapping\ClassMetadataInfo; +use Doctrine\ORM\Tools\SchemaTool; +use Doctrine\ORM\Tools\EntityGenerator; +use Doctrine\ORM\Tools\Export\ClassMetadataExporter; +use Doctrine\ORM\Mapping\ClassMetadataInfo; +use Doctrine\ORM\Mapping\ClassMetadataFactory; +use Entities\TraitedUser; require_once __DIR__ . '/../../TestInit.php'; @@ -282,7 +284,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $filename = $this->_tmpDir . DIRECTORY_SEPARATOR . $this->_namespace . DIRECTORY_SEPARATOR . 'DDC1784Entity.php'; - + $this->assertFileExists($filename); require_once $filename; @@ -330,7 +332,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $property = new \ReflectionProperty($metadata->name, 'centroCustos'); $docComment = $property->getDocComment(); - + //joinColumns $this->assertContains('@JoinColumn(name="idorcamento", referencedColumnName="idorcamento"),', $docComment); $this->assertContains('@JoinColumn(name="idunidade", referencedColumnName="idunidade")', $docComment); @@ -436,7 +438,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $entity = new $metadata->name; $reflClass = new \ReflectionClass($metadata->name); - + $type = $field['phpType']; $name = $field['fieldName']; $value = $field['value']; @@ -451,6 +453,34 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals($value, $entity->{$getter}()); } + /** + * @group DDC-2372 + */ + public function testTraitPropertiesAndMethodsAreNotDuplicated() + { + if (function_exists('trait_exists')) { + $cmf = new ClassMetadataFactory(); + $em = $this->_getTestEntityManager(); + $cmf->setEntityManager($em); + + $user = new TraitedUser(); + $metadata = $cmf->getMetadataFor(get_class($user)); + $metadata->name = $this->_namespace . "\TraitedUser"; + $metadata->namespace = $this->_namespace; + + $this->_generator->writeEntityClass($metadata, $this->_tmpDir); + + $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/TraitedUser.php"); + require $this->_tmpDir . "/" . $this->_namespace . "/TraitedUser.php"; + + $reflClass = new \ReflectionClass($metadata->name); + + $this->assertSame($reflClass->hasProperty('address'), false); + $this->assertSame($reflClass->hasMethod('setAddress'), false); + $this->assertSame($reflClass->hasMethod('getAddress'), false); + } + } + /** * @return array */ @@ -470,43 +500,43 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase 'value' => new \DateTime )), array(array( - 'fieldName' => 'date', + 'fieldName' => 'date', 'phpType' => '\\DateTime', 'dbType' => 'date', 'value' => new \DateTime )), array(array( - 'fieldName' => 'time', + 'fieldName' => 'time', 'phpType' => '\DateTime', 'dbType' => 'time', 'value' => new \DateTime )), array(array( - 'fieldName' => 'object', + 'fieldName' => 'object', 'phpType' => '\stdClass', 'dbType' => 'object', 'value' => new \stdClass() )), array(array( - 'fieldName' => 'bigint', + 'fieldName' => 'bigint', 'phpType' => 'integer', 'dbType' => 'bigint', 'value' => 11 )), array(array( - 'fieldName' => 'smallint', + 'fieldName' => 'smallint', 'phpType' => 'integer', 'dbType' => 'smallint', 'value' => 22 )), array(array( - 'fieldName' => 'text', + 'fieldName' => 'text', 'phpType' => 'string', 'dbType' => 'text', 'value' => 'text' )), array(array( - 'fieldName' => 'blob', + 'fieldName' => 'blob', 'phpType' => 'string', 'dbType' => 'blob', 'value' => 'blob' diff --git a/tests/Doctrine/Tests/TestInit.php b/tests/Doctrine/Tests/TestInit.php index c257a75b6..ac7f7354b 100644 --- a/tests/Doctrine/Tests/TestInit.php +++ b/tests/Doctrine/Tests/TestInit.php @@ -17,6 +17,7 @@ if (file_exists(__DIR__ . '/../../../vendor/autoload.php')) { } /* @var $classLoader \Composer\Autoload\ClassLoader */ +$classLoader->add('Entities', __DIR__ . '/../../../tools/sandbox'); $classLoader->add('Doctrine\\Tests\\', __DIR__ . '/../../'); unset($classLoader); diff --git a/tools/sandbox/Entities/TraitedUser.php b/tools/sandbox/Entities/TraitedUser.php new file mode 100644 index 000000000..fe0b68754 --- /dev/null +++ b/tools/sandbox/Entities/TraitedUser.php @@ -0,0 +1,34 @@ +id; + } + + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + } +} \ No newline at end of file diff --git a/tools/sandbox/Entities/Traits/AddressTrait.php b/tools/sandbox/Entities/Traits/AddressTrait.php new file mode 100644 index 000000000..9fb3e23a5 --- /dev/null +++ b/tools/sandbox/Entities/Traits/AddressTrait.php @@ -0,0 +1,25 @@ +address; + } + + public function setAddress(Address $address) + { + if ($this->address !== $address) { + $this->address = $address; + $address->setUser($this); + } + } +} \ No newline at end of file