From cd0915deb57c7179dfefde152bfb66f270ec1aba Mon Sep 17 00:00:00 2001 From: Vitali Date: Wed, 23 Nov 2011 13:06:34 +0300 Subject: [PATCH] Introcude ClassMetadataInfo::GENERATOR_TYPE_CUSTOM for custom generators to follow current implementation --- .../ORM/Mapping/ClassMetadataFactory.php | 14 ++-- .../ORM/Mapping/ClassMetadataInfo.php | 4 ++ .../ORM/Mapping/ClassMetadataFactoryTest.php | 72 ++++++++++--------- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 1b7dbf5be..4d93badad 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -504,12 +504,18 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface case ClassMetadata::GENERATOR_TYPE_TABLE: throw new ORMException("TableGenerator not yet implemented."); break; - default: - if (class_exists($class->generatorType)) { - $class->setIdGenerator(new $class->generatorType); + case ClassMetadata::GENERATOR_TYPE_CUSTOM: + $definition = $class->customGeneratorDefinition; + if (class_exists($definition['class'])) { + $class->setIdGenerator(new $definition['class']); } else { - throw new ORMException("Unknown generator type: " . $class->generatorType); + throw new ORMException("Can't find custom generator class: " . + $definition['class']); } + break; + default: + throw new ORMException("Unknown generator type: " . $class->generatorType); + } } diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 1f54a095b..56eaaa606 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -94,6 +94,10 @@ class ClassMetadataInfo implements ClassMetadata * must have a natural, manually assigned id. */ const GENERATOR_TYPE_NONE = 5; + /** + * CUSTOM means that customer will use own ID generator that supposedly work + */ + const GENERATOR_TYPE_CUSTOM = 6; /** * DEFERRED_IMPLICIT means that changes of entities are calculated at commit-time * by doing a property-by-property comparison with the original data. This will diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index ea38eabcd..91f94aa75 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -25,27 +25,12 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase $mockPlatform->setPrefersSequences(true); $mockPlatform->setPrefersIdentityColumns(false); - // Self-made metadata - $cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1'); - $cm1->setPrimaryTable(array('name' => '`group`')); - // Add a mapped field - $cm1->mapField(array('fieldName' => 'name', 'type' => 'varchar')); - // Add a mapped field - $cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); - // and a mapped association - $cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'Other', 'mappedBy' => 'this')); - // and an association on the owning side - $joinColumns = array( - array('name' => 'other_id', 'referencedColumnName' => 'id') - ); - $cm1->mapOneToOne(array('fieldName' => 'association', 'targetEntity' => 'Other', 'joinColumns' => $joinColumns)); - // and an id generator type - $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); + $cm1 = $this->_createValidClassMetadata(); // SUT $cmf = new ClassMetadataFactoryTestSubject(); $cmf->setEntityManager($entityManager); - $cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1); + $cmf->setMetadataForClass($cm1->name, $cm1); // Prechecks $this->assertEquals(array(), $cm1->parentClasses); @@ -55,7 +40,7 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $cm1->generatorType); // Go - $cm1 = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1'); + $cm1 = $cmf->getMetadataFor($cm1->name); $this->assertEquals('group', $cm1->table['name']); $this->assertTrue($cm1->table['quoted']); @@ -64,33 +49,31 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType); } - public function testGetMetadataFor_ReturnLoadedCustomIdGenerator() { - $cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1'); - $cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); - $cm1->setIdGeneratorType("Doctrine\Tests\ORM\Mapping\CustomIdGenerator"); + public function testGetMetadataFor_ReturnsLoadedCustomIdGenerator() { + $cm1 = $this->_createValidClassMetadata(); + $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_CUSTOM); + $cm1->customGeneratorDefinition = array( + "class" => "Doctrine\Tests\ORM\Mapping\CustomIdGenerator"); $cmf = $this->_createTestFactory(); - $cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1); + $cmf->setMetadataForClass($cm1->name, $cm1); - $actual = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1'); + $actual = $cmf->getMetadataFor($cm1->name); - $this->assertEquals("Doctrine\Tests\ORM\Mapping\CustomIdGenerator", + $this->assertEquals(ClassMetadata::GENERATOR_TYPE_CUSTOM, $actual->generatorType); $this->assertInstanceOf("Doctrine\Tests\ORM\Mapping\CustomIdGenerator", $actual->idGenerator); - /** - * @GeneratedValue(type=@CustomIdGenerator(table="test")) - */ } public function testGetMetadataFor_ThrowsExceptionOnUnknownCustomGeneratorClass() { - $cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1'); - $cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); - $cm1->setIdGeneratorType("NotExistingIdGenerator"); + $cm1 = $this->_createValidClassMetadata(); + $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_CUSTOM); + $cm1->customGeneratorDefinition = array("class" => "NotExistingGenerator"); $cmf = $this->_createTestFactory(); - $cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1); + $cmf->setMetadataForClass($cm1->name, $cm1); $this->setExpectedException("Doctrine\ORM\ORMException"); - $actual = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1'); + $actual = $cmf->getMetadataFor($cm1->name); } public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized() @@ -183,6 +166,29 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase $cmf->setEntityManager($entityManager); return $cmf; } + + /** + * @param string $class + * @return ClassMetadata + */ + protected function _createValidClassMetadata() { + $cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1'); + $cm1->setPrimaryTable(array('name' => '`group`')); + // Add a mapped field + $cm1->mapField(array('fieldName' => 'name', 'type' => 'varchar')); + // Add a mapped field + $cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); + // and a mapped association + $cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'Other', 'mappedBy' => 'this')); + // and an association on the owning side + $joinColumns = array( + array('name' => 'other_id', 'referencedColumnName' => 'id') + ); + $cm1->mapOneToOne(array('fieldName' => 'association', 'targetEntity' => 'Other', 'joinColumns' => $joinColumns)); + // and an id generator type + $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); + return $cm1; + } } /* Test subject class with overriden factory method for mocking purposes */