From 45269fe4f2208e6ad03bdcb117d0c061b3643723 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Tue, 27 Nov 2012 14:29:02 -0200 Subject: [PATCH 1/3] Fix DDC-2172 --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 112 ++++++++++-------- .../Tests/ORM/Tools/EntityGeneratorTest.php | 87 ++++++++++++++ 2 files changed, 150 insertions(+), 49 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 0089a83e336..4463997506e 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -139,7 +139,7 @@ class EntityGenerator /** * Hash-map for handle types - * + * * @var array */ private $typeAlias = array( @@ -157,6 +157,38 @@ class EntityGenerator Type::SIMPLE_ARRAY => 'array', ); + /** + * @var array Hash-map to handle generator types string + */ + protected static $generatorStrategyMap = array( + ClassMetadataInfo::GENERATOR_TYPE_AUTO => 'AUTO', + ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE => 'SEQUENCE', + ClassMetadataInfo::GENERATOR_TYPE_TABLE => 'TABLE', + ClassMetadataInfo::GENERATOR_TYPE_IDENTITY => 'IDENTITY', + ClassMetadataInfo::GENERATOR_TYPE_NONE => 'NONE', + ClassMetadataInfo::GENERATOR_TYPE_UUID => 'UUID', + ClassMetadataInfo::GENERATOR_TYPE_CUSTOM => 'CUSTOM' + ); + + /** + * @var array Hash-map to handle the change tracking policy string. + */ + protected static $changeTrackingPolicyMap = array( + ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT => 'DEFERRED_IMPLICIT', + ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT => 'DEFERRED_EXPLICIT', + ClassMetadataInfo::CHANGETRACKING_NOTIFY => 'NOTIFY', + ); + + /** + * @var array Hash-map to handle the inheritance type string. + */ + protected static $inheritanceTypeMap = array( + ClassMetadataInfo::INHERITANCE_TYPE_NONE => 'NONE', + ClassMetadataInfo::INHERITANCE_TYPE_JOINED => 'JOINED', + ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE => 'SINGLE_TABLE', + ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS => 'TABLE_PER_CLASS', + ); + /** * @var string */ @@ -1214,63 +1246,45 @@ private function prefixCodeWithSpaces($code, $num = 1) return implode("\n", $lines); } - private function getInheritanceTypeString($type) + /** + * @param integer $type The inheritance type used by the class and it's subclasses. + * @return string The literal string for the inheritance type. + * @throws \InvalidArgumentException When the inheritance type does not exists. + */ + protected function getInheritanceTypeString($type) { - switch ($type) { - case ClassMetadataInfo::INHERITANCE_TYPE_NONE: - return 'NONE'; - - case ClassMetadataInfo::INHERITANCE_TYPE_JOINED: - return 'JOINED'; - - case ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE: - return 'SINGLE_TABLE'; - - case ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS: - return 'PER_CLASS'; - - default: - throw new \InvalidArgumentException('Invalid provided InheritanceType: ' . $type); + if( ! isset(self::$inheritanceTypeMap[$type])) { + throw new \InvalidArgumentException(sprintf('Invalid provided InheritanceType: %s', $type)); } + + return self::$inheritanceTypeMap[$type]; } - private function getChangeTrackingPolicyString($policy) + /** + * @param integer $type The policy used for change-tracking for the mapped class. + * @return string The literal string for the change-tracking type. + * @throws \InvalidArgumentException When the change-tracking type does not exists. + */ + protected function getChangeTrackingPolicyString($type) { - switch ($policy) { - case ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT: - return 'DEFERRED_IMPLICIT'; - - case ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT: - return 'DEFERRED_EXPLICIT'; - - case ClassMetadataInfo::CHANGETRACKING_NOTIFY: - return 'NOTIFY'; - - default: - throw new \InvalidArgumentException('Invalid provided ChangeTrackingPolicy: ' . $policy); + if( ! isset(self::$changeTrackingPolicyMap[$type])) { + throw new \InvalidArgumentException(sprintf('Invalid provided ChangeTrackingPolicy: %s', $type)); } + + return self::$changeTrackingPolicyMap[$type]; } - private function getIdGeneratorTypeString($type) + /** + * @param integer $type The generator to use for the mapped class. + * @return string The literal string for the generetor type. + * @throws \InvalidArgumentException When the generator type does not exists. + */ + protected function getIdGeneratorTypeString($type) { - switch ($type) { - case ClassMetadataInfo::GENERATOR_TYPE_AUTO: - return 'AUTO'; - - case ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE: - return 'SEQUENCE'; - - case ClassMetadataInfo::GENERATOR_TYPE_TABLE: - return 'TABLE'; - - case ClassMetadataInfo::GENERATOR_TYPE_IDENTITY: - return 'IDENTITY'; - - case ClassMetadataInfo::GENERATOR_TYPE_NONE: - return 'NONE'; - - default: - throw new \InvalidArgumentException('Invalid provided IdGeneratorType: ' . $type); + if( ! isset(self::$generatorStrategyMap[$type])) { + throw new \InvalidArgumentException(sprintf('Invalid provided IdGeneratorType: %s', $type)); } + + return self::$generatorStrategyMap[$type]; } } diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index 142b68a4818..f0a7e37bd31 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -340,6 +340,93 @@ public function testGenerateEntityWithMultipleInverseJoinColumns() } + /** + * @group DDC-2172 + */ + public function testGetInheritanceTypeString() + { + $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata'); + $method = new \ReflectionMethod($this->_generator, 'getInheritanceTypeString'); + $constants = $reflection->getConstants(); + $pattern = '/^INHERITANCE_TYPE_/'; + $map = array(); + + $method->setAccessible(true); + + foreach ($constants as $name => $value) { + if(preg_match($pattern, $name)) { + $map[preg_replace($pattern, '', $name)] = $value; + } + } + + foreach ($map as $expected => $type) { + $actual = $method->invoke($this->_generator, $type); + + $this->assertEquals($expected, $actual); + } + + $this->setExpectedException('\InvalidArgumentException', 'Invalid provided InheritanceType: INVALID'); + $method->invoke($this->_generator, 'INVALID'); + } + + /** + * @group DDC-2172 + */ + public function testGetChangeTrackingPolicyString() + { + $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata'); + $method = new \ReflectionMethod($this->_generator, 'getChangeTrackingPolicyString'); + $constants = $reflection->getConstants(); + $pattern = '/^CHANGETRACKING_/'; + $map = array(); + + $method->setAccessible(true); + + foreach ($constants as $name => $value) { + if(preg_match($pattern, $name)) { + $map[preg_replace($pattern, '', $name)] = $value; + } + } + + foreach ($map as $expected => $type) { + $actual = $method->invoke($this->_generator, $type); + + $this->assertEquals($expected, $actual); + } + + $this->setExpectedException('\InvalidArgumentException', 'Invalid provided ChangeTrackingPolicy: INVALID'); + $method->invoke($this->_generator, 'INVALID'); + } + + /** + * @group DDC-2172 + */ + public function testGetIdGeneratorTypeString() + { + $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata'); + $method = new \ReflectionMethod($this->_generator, 'getIdGeneratorTypeString'); + $constants = $reflection->getConstants(); + $pattern = '/^GENERATOR_TYPE_/'; + $map = array(); + + $method->setAccessible(true); + + foreach ($constants as $name => $value) { + if(preg_match($pattern, $name)) { + $map[preg_replace($pattern, '', $name)] = $value; + } + } + + foreach ($map as $expected => $type) { + $actual = $method->invoke($this->_generator, $type); + + $this->assertEquals($expected, $actual); + } + + $this->setExpectedException('\InvalidArgumentException', 'Invalid provided IdGeneratorType: INVALID'); + $method->invoke($this->_generator, 'INVALID'); + } + /** * @dataProvider getEntityTypeAliasDataProvider * From faedbfc09ac3c03b358807b345c569bd40d3045a Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Tue, 27 Nov 2012 14:38:18 -0200 Subject: [PATCH 2/3] refactoring tests --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 4 +- .../Tests/ORM/Tools/EntityGeneratorTest.php | 48 ++++++++----------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 4463997506e..cb9781ec513 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -139,7 +139,7 @@ class EntityGenerator /** * Hash-map for handle types - * + * * @var array */ private $typeAlias = array( @@ -158,7 +158,7 @@ class EntityGenerator ); /** - * @var array Hash-map to handle generator types string + * @var array Hash-map to handle generator types string. */ protected static $generatorStrategyMap = array( ClassMetadataInfo::GENERATOR_TYPE_AUTO => 'AUTO', diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index f0a7e37bd31..a55b603d572 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -349,53 +349,49 @@ public function testGetInheritanceTypeString() $method = new \ReflectionMethod($this->_generator, 'getInheritanceTypeString'); $constants = $reflection->getConstants(); $pattern = '/^INHERITANCE_TYPE_/'; - $map = array(); $method->setAccessible(true); foreach ($constants as $name => $value) { - if(preg_match($pattern, $name)) { - $map[preg_replace($pattern, '', $name)] = $value; + if( ! preg_match($pattern, $name)) { + continue; } - } - foreach ($map as $expected => $type) { - $actual = $method->invoke($this->_generator, $type); + $expected = preg_replace($pattern, '', $name); + $actual = $method->invoke($this->_generator, $value); $this->assertEquals($expected, $actual); } - $this->setExpectedException('\InvalidArgumentException', 'Invalid provided InheritanceType: INVALID'); - $method->invoke($this->_generator, 'INVALID'); + $this->setExpectedException('\InvalidArgumentException', 'Invalid provided InheritanceType: INVALID'); + $method->invoke($this->_generator, 'INVALID'); } - /** - * @group DDC-2172 - */ + /** + * @group DDC-2172 + */ public function testGetChangeTrackingPolicyString() { $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata'); $method = new \ReflectionMethod($this->_generator, 'getChangeTrackingPolicyString'); $constants = $reflection->getConstants(); $pattern = '/^CHANGETRACKING_/'; - $map = array(); $method->setAccessible(true); foreach ($constants as $name => $value) { - if(preg_match($pattern, $name)) { - $map[preg_replace($pattern, '', $name)] = $value; + if( ! preg_match($pattern, $name)) { + continue; } - } - foreach ($map as $expected => $type) { - $actual = $method->invoke($this->_generator, $type); + $expected = preg_replace($pattern, '', $name); + $actual = $method->invoke($this->_generator, $value); $this->assertEquals($expected, $actual); } - $this->setExpectedException('\InvalidArgumentException', 'Invalid provided ChangeTrackingPolicy: INVALID'); - $method->invoke($this->_generator, 'INVALID'); + $this->setExpectedException('\InvalidArgumentException', 'Invalid provided ChangeTrackingPolicy: INVALID'); + $method->invoke($this->_generator, 'INVALID'); } /** @@ -407,24 +403,22 @@ public function testGetIdGeneratorTypeString() $method = new \ReflectionMethod($this->_generator, 'getIdGeneratorTypeString'); $constants = $reflection->getConstants(); $pattern = '/^GENERATOR_TYPE_/'; - $map = array(); $method->setAccessible(true); foreach ($constants as $name => $value) { - if(preg_match($pattern, $name)) { - $map[preg_replace($pattern, '', $name)] = $value; + if( ! preg_match($pattern, $name)) { + continue; } - } - foreach ($map as $expected => $type) { - $actual = $method->invoke($this->_generator, $type); + $expected = preg_replace($pattern, '', $name); + $actual = $method->invoke($this->_generator, $value); $this->assertEquals($expected, $actual); } - $this->setExpectedException('\InvalidArgumentException', 'Invalid provided IdGeneratorType: INVALID'); - $method->invoke($this->_generator, 'INVALID'); + $this->setExpectedException('\InvalidArgumentException', 'Invalid provided IdGeneratorType: INVALID'); + $method->invoke($this->_generator, 'INVALID'); } /** From 6e7e4dd35ab72da6106b8fc7363a6aa78627830f Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Tue, 27 Nov 2012 16:58:48 -0200 Subject: [PATCH 3/3] Fix CS --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index cb9781ec513..f7e6048003d 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -1253,7 +1253,7 @@ private function prefixCodeWithSpaces($code, $num = 1) */ protected function getInheritanceTypeString($type) { - if( ! isset(self::$inheritanceTypeMap[$type])) { + if ( ! isset(self::$inheritanceTypeMap[$type])) { throw new \InvalidArgumentException(sprintf('Invalid provided InheritanceType: %s', $type)); } @@ -1267,7 +1267,7 @@ protected function getInheritanceTypeString($type) */ protected function getChangeTrackingPolicyString($type) { - if( ! isset(self::$changeTrackingPolicyMap[$type])) { + if ( ! isset(self::$changeTrackingPolicyMap[$type])) { throw new \InvalidArgumentException(sprintf('Invalid provided ChangeTrackingPolicy: %s', $type)); } @@ -1281,7 +1281,7 @@ protected function getChangeTrackingPolicyString($type) */ protected function getIdGeneratorTypeString($type) { - if( ! isset(self::$generatorStrategyMap[$type])) { + if ( ! isset(self::$generatorStrategyMap[$type])) { throw new \InvalidArgumentException(sprintf('Invalid provided IdGeneratorType: %s', $type)); }