2009-01-06 20:22:23 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* To change this template, choose Tools | Templates
|
|
|
|
* and open the template in the editor.
|
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Tests\ORM\Mapping;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Mocks\MetadataDriverMock;
|
|
|
|
use Doctrine\Tests\Mocks\DatabasePlatformMock;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
|
|
|
|
|
require_once dirname(__FILE__) . '/../../TestInit.php';
|
2009-01-06 20:22:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Description of ClassMetadataFactoryTest
|
|
|
|
*
|
|
|
|
* @author robo
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase {
|
2009-01-06 21:30:51 +03:00
|
|
|
|
2009-01-06 20:22:23 +03:00
|
|
|
public function testGetMetadataForSingleClass() {
|
2009-01-22 22:38:10 +03:00
|
|
|
$mockPlatform = new DatabasePlatformMock();
|
|
|
|
$mockDriver = new MetadataDriverMock();
|
2009-01-06 21:30:51 +03:00
|
|
|
|
|
|
|
// Self-made metadata
|
2009-01-22 22:38:10 +03:00
|
|
|
$cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
|
2009-01-06 21:30:51 +03:00
|
|
|
// Add a mapped field
|
2009-01-08 14:23:24 +03:00
|
|
|
$cm1->mapField(array('fieldName' => 'name', 'type' => 'varchar'));
|
2009-01-06 21:30:51 +03:00
|
|
|
// and a mapped association
|
|
|
|
$cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'Other', 'mappedBy' => 'this'));
|
|
|
|
// and an id generator type
|
|
|
|
$cm1->setIdGeneratorType('auto');
|
|
|
|
|
|
|
|
// SUT
|
|
|
|
$cmf = new ClassMetadataFactoryTestSubject($mockDriver, $mockPlatform);
|
2009-01-22 22:38:10 +03:00
|
|
|
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1);
|
2009-01-06 21:30:51 +03:00
|
|
|
|
|
|
|
// Prechecks
|
|
|
|
$this->assertEquals(array(), $cm1->getParentClasses());
|
|
|
|
$this->assertEquals('none', $cm1->getInheritanceType());
|
|
|
|
$this->assertTrue($cm1->hasField('name'));
|
|
|
|
$this->assertEquals(1, count($cm1->getAssociationMappings()));
|
|
|
|
$this->assertEquals('auto', $cm1->getIdGeneratorType());
|
|
|
|
|
|
|
|
// Go
|
2009-01-22 22:38:10 +03:00
|
|
|
$cm1 = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1');
|
2009-01-06 21:30:51 +03:00
|
|
|
|
|
|
|
$this->assertEquals(array(), $cm1->getParentClasses());
|
|
|
|
$this->assertTrue($cm1->hasField('name'));
|
|
|
|
// The default fallback for id generation is the table strategy
|
|
|
|
$this->assertEquals('table', $cm1->getIdGeneratorType());
|
2009-01-06 20:22:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMetadataForClassInHierarchy() {
|
2009-01-22 22:38:10 +03:00
|
|
|
$mockPlatform = new DatabasePlatformMock();
|
2009-01-06 21:30:51 +03:00
|
|
|
$mockPlatform->setPrefersIdentityColumns(true);
|
2009-01-22 22:38:10 +03:00
|
|
|
$mockDriver = new MetadataDriverMock();
|
2009-01-06 20:22:23 +03:00
|
|
|
|
|
|
|
// Self-made metadata
|
2009-01-22 22:38:10 +03:00
|
|
|
$cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
|
2009-01-06 20:22:23 +03:00
|
|
|
$cm1->setInheritanceType('singleTable');
|
|
|
|
// Add a mapped field
|
2009-01-08 14:23:24 +03:00
|
|
|
$cm1->mapField(array('fieldName' => 'name', 'type' => 'varchar'));
|
2009-01-06 20:22:23 +03:00
|
|
|
// and a mapped association
|
|
|
|
$cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'Other', 'mappedBy' => 'this'));
|
2009-01-06 21:30:51 +03:00
|
|
|
// and an id generator type
|
|
|
|
$cm1->setIdGeneratorType('auto');
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
$cm2 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity2');
|
|
|
|
$cm3 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity3');
|
2009-01-06 20:22:23 +03:00
|
|
|
|
|
|
|
$cmf = new ClassMetadataFactoryTestSubject($mockDriver, $mockPlatform);
|
|
|
|
// Set self-made metadata
|
2009-01-22 22:38:10 +03:00
|
|
|
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1);
|
|
|
|
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity2', $cm2);
|
|
|
|
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity3', $cm3);
|
2009-01-06 20:22:23 +03:00
|
|
|
|
|
|
|
// Prechecks
|
|
|
|
$this->assertEquals(array(), $cm1->getParentClasses());
|
|
|
|
$this->assertEquals(array(), $cm2->getParentClasses());
|
|
|
|
$this->assertEquals(array(), $cm3->getParentClasses());
|
|
|
|
$this->assertEquals('none', $cm2->getInheritanceType());
|
|
|
|
$this->assertEquals('none', $cm3->getInheritanceType());
|
|
|
|
$this->assertFalse($cm2->hasField('name'));
|
|
|
|
$this->assertFalse($cm3->hasField('name'));
|
|
|
|
$this->assertEquals(1, count($cm1->getAssociationMappings()));
|
|
|
|
$this->assertEquals(0, count($cm2->getAssociationMappings()));
|
|
|
|
$this->assertEquals(0, count($cm3->getAssociationMappings()));
|
2009-01-06 21:30:51 +03:00
|
|
|
$this->assertEquals('none', $cm2->getIdGeneratorType());
|
|
|
|
$this->assertEquals('none', $cm3->getIdGeneratorType());
|
2009-01-06 20:22:23 +03:00
|
|
|
|
|
|
|
// Go
|
2009-01-22 22:38:10 +03:00
|
|
|
$cm3 = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity3');
|
2009-01-06 20:22:23 +03:00
|
|
|
|
|
|
|
// Metadata gathering should start at the root of the hierarchy, from there on downwards
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->assertEquals(array('Doctrine\Tests\ORM\Mapping\TestEntity1', 'Doctrine\Tests\ORM\Mapping\TestEntity2', 'Doctrine\Tests\ORM\Mapping\TestEntity3'), $cmf->getRequestedClasses());
|
2009-01-06 20:22:23 +03:00
|
|
|
// Parent classes should be assigned by factory
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->assertEquals(array('Doctrine\Tests\ORM\Mapping\TestEntity2', 'Doctrine\Tests\ORM\Mapping\TestEntity1'), $cm3->getParentClasses());
|
|
|
|
$this->assertEquals('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm3->getRootClassName());
|
|
|
|
$this->assertEquals('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm2->getRootClassName());
|
|
|
|
$this->assertEquals('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1->getRootClassName());
|
2009-01-06 20:22:23 +03:00
|
|
|
// Inheritance type should be inherited to Entity2
|
|
|
|
$this->assertEquals('singleTable', $cm2->getInheritanceType());
|
|
|
|
$this->assertEquals('singleTable', $cm3->getInheritanceType());
|
|
|
|
// Field mappings should be inherited
|
|
|
|
$this->assertTrue($cm2->hasField('name'));
|
|
|
|
$this->assertTrue($cm3->hasField('name'));
|
|
|
|
// Association mappings should be inherited
|
|
|
|
$this->assertEquals(1, count($cm2->getAssociationMappings()));
|
|
|
|
$this->assertEquals(1, count($cm3->getAssociationMappings()));
|
|
|
|
$this->assertTrue($cm2->hasAssociation('other'));
|
|
|
|
$this->assertTrue($cm3->hasAssociation('other'));
|
2009-01-06 21:30:51 +03:00
|
|
|
// Id generator 'auto' should have been resolved to 'identity' as preferred by our
|
|
|
|
// mock platform (see above). And it should be inherited.
|
|
|
|
$this->assertEquals('identity', $cm1->getIdGeneratorType());
|
|
|
|
$this->assertEquals('identity', $cm2->getIdGeneratorType());
|
|
|
|
$this->assertEquals('identity', $cm3->getIdGeneratorType());
|
2009-01-06 20:22:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test subject class with overriden factory method for mocking purposes */
|
2009-01-22 22:38:10 +03:00
|
|
|
class ClassMetadataFactoryTestSubject extends \Doctrine\ORM\Mapping\ClassMetadataFactory {
|
2009-01-06 20:22:23 +03:00
|
|
|
private $_mockMetadata = array();
|
|
|
|
private $_requestedClasses = array();
|
|
|
|
/** @override */
|
|
|
|
protected function _newClassMetadataInstance($className) {
|
|
|
|
$this->_requestedClasses[] = $className;
|
|
|
|
if ( ! isset($this->_mockMetadata[$className])) {
|
|
|
|
throw new InvalidArgumentException("No mock metadata found for class $className.");
|
|
|
|
}
|
|
|
|
return $this->_mockMetadata[$className];
|
|
|
|
}
|
|
|
|
public function setMetadataForClass($className, $metadata) {
|
|
|
|
$this->_mockMetadata[$className] = $metadata;
|
|
|
|
}
|
|
|
|
public function getRequestedClasses() { return $this->_requestedClasses; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test classes */
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
class TestEntity1 {
|
2009-01-12 16:34:41 +03:00
|
|
|
protected $name;
|
|
|
|
protected $other;
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
class TestEntity2 extends TestEntity1 {}
|
|
|
|
class TestEntity3 extends TestEntity2 {}
|
2009-01-06 20:22:23 +03:00
|
|
|
|