1
0
mirror of synced 2025-01-20 23:41:39 +03:00

Introcude ClassMetadataInfo::GENERATOR_TYPE_CUSTOM for custom generators to follow current implementation

This commit is contained in:
Vitali 2011-11-23 13:06:34 +03:00 committed by Vitali Yakavenka
parent ffc722a334
commit cd0915deb5
3 changed files with 53 additions and 37 deletions

View File

@ -504,12 +504,18 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
case ClassMetadata::GENERATOR_TYPE_TABLE: case ClassMetadata::GENERATOR_TYPE_TABLE:
throw new ORMException("TableGenerator not yet implemented."); throw new ORMException("TableGenerator not yet implemented.");
break; break;
default: case ClassMetadata::GENERATOR_TYPE_CUSTOM:
if (class_exists($class->generatorType)) { $definition = $class->customGeneratorDefinition;
$class->setIdGenerator(new $class->generatorType); if (class_exists($definition['class'])) {
$class->setIdGenerator(new $definition['class']);
} else { } 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);
} }
} }

View File

@ -94,6 +94,10 @@ class ClassMetadataInfo implements ClassMetadata
* must have a natural, manually assigned id. * must have a natural, manually assigned id.
*/ */
const GENERATOR_TYPE_NONE = 5; 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 * 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 * by doing a property-by-property comparison with the original data. This will

View File

@ -25,27 +25,12 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$mockPlatform->setPrefersSequences(true); $mockPlatform->setPrefersSequences(true);
$mockPlatform->setPrefersIdentityColumns(false); $mockPlatform->setPrefersIdentityColumns(false);
// Self-made metadata $cm1 = $this->_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);
// SUT // SUT
$cmf = new ClassMetadataFactoryTestSubject(); $cmf = new ClassMetadataFactoryTestSubject();
$cmf->setEntityManager($entityManager); $cmf->setEntityManager($entityManager);
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1); $cmf->setMetadataForClass($cm1->name, $cm1);
// Prechecks // Prechecks
$this->assertEquals(array(), $cm1->parentClasses); $this->assertEquals(array(), $cm1->parentClasses);
@ -55,7 +40,7 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $cm1->generatorType); $this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $cm1->generatorType);
// Go // Go
$cm1 = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1'); $cm1 = $cmf->getMetadataFor($cm1->name);
$this->assertEquals('group', $cm1->table['name']); $this->assertEquals('group', $cm1->table['name']);
$this->assertTrue($cm1->table['quoted']); $this->assertTrue($cm1->table['quoted']);
@ -64,33 +49,31 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType); $this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType);
} }
public function testGetMetadataFor_ReturnLoadedCustomIdGenerator() { public function testGetMetadataFor_ReturnsLoadedCustomIdGenerator() {
$cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1'); $cm1 = $this->_createValidClassMetadata();
$cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_CUSTOM);
$cm1->setIdGeneratorType("Doctrine\Tests\ORM\Mapping\CustomIdGenerator"); $cm1->customGeneratorDefinition = array(
"class" => "Doctrine\Tests\ORM\Mapping\CustomIdGenerator");
$cmf = $this->_createTestFactory(); $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); $actual->generatorType);
$this->assertInstanceOf("Doctrine\Tests\ORM\Mapping\CustomIdGenerator", $this->assertInstanceOf("Doctrine\Tests\ORM\Mapping\CustomIdGenerator",
$actual->idGenerator); $actual->idGenerator);
/**
* @GeneratedValue(type=@CustomIdGenerator(table="test"))
*/
} }
public function testGetMetadataFor_ThrowsExceptionOnUnknownCustomGeneratorClass() { public function testGetMetadataFor_ThrowsExceptionOnUnknownCustomGeneratorClass() {
$cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1'); $cm1 = $this->_createValidClassMetadata();
$cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_CUSTOM);
$cm1->setIdGeneratorType("NotExistingIdGenerator"); $cm1->customGeneratorDefinition = array("class" => "NotExistingGenerator");
$cmf = $this->_createTestFactory(); $cmf = $this->_createTestFactory();
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1); $cmf->setMetadataForClass($cm1->name, $cm1);
$this->setExpectedException("Doctrine\ORM\ORMException"); $this->setExpectedException("Doctrine\ORM\ORMException");
$actual = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1'); $actual = $cmf->getMetadataFor($cm1->name);
} }
public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized() public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized()
@ -183,6 +166,29 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$cmf->setEntityManager($entityManager); $cmf->setEntityManager($entityManager);
return $cmf; 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 */ /* Test subject class with overriden factory method for mocking purposes */