From c2ab01bf7ed944481e7b5c24a14b2430c1098716 Mon Sep 17 00:00:00 2001 From: romanb Date: Tue, 6 Jan 2009 17:22:23 +0000 Subject: [PATCH] Added first ClassMetadataFactory tests. --- lib/Doctrine/ORM/Configuration.php | 14 ++- lib/Doctrine/ORM/EntityManager.php | 13 ++- .../ORM/Mapping/AssociationMapping.php | 68 +------------ lib/Doctrine/ORM/Mapping/ClassMetadata.php | 19 +++- .../ORM/Mapping/ClassMetadataFactory.php | 47 ++++++--- .../ORM/Mapping/ManyToManyMapping.php | 1 - lib/Doctrine/ORM/Mapping/OneToManyMapping.php | 2 +- lib/Doctrine/ORM/Mapping/OneToOneMapping.php | 44 ++++----- lib/Doctrine/ORM/VirtualProxy.php | 71 +++++++++++++ tests/Orm/AllTests.php | 4 +- tests/Orm/Mapping/AllTests.php | 32 ++++++ .../Orm/Mapping/ClassMetadataFactoryTest.php | 99 +++++++++++++++++++ tests/Orm/{ => Mapping}/ClassMetadataTest.php | 19 +--- .../lib/mocks/Doctrine_MetadataDriverMock.php | 17 ++++ 14 files changed, 324 insertions(+), 126 deletions(-) create mode 100644 lib/Doctrine/ORM/VirtualProxy.php create mode 100644 tests/Orm/Mapping/AllTests.php create mode 100644 tests/Orm/Mapping/ClassMetadataFactoryTest.php rename tests/Orm/{ => Mapping}/ClassMetadataTest.php (90%) create mode 100644 tests/lib/mocks/Doctrine_MetadataDriverMock.php diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 1030ab697..87eda3b3f 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -22,6 +22,7 @@ #namespace Doctrine\ORM; #use Doctrine\DBAL\Configuration; +#use Doctrine\ORM\Mapping\Driver\AnnotationDriver; /** * Configuration container for all configuration options of Doctrine. @@ -43,9 +44,20 @@ class Doctrine_ORM_Configuration extends Doctrine_DBAL_Configuration $this->_attributes = array_merge($this->_attributes, array( 'resultCacheImpl' => null, 'queryCacheImpl' => null, - 'metadataCacheImpl' => null + 'metadataCacheImpl' => null, + 'metadataDriverImpl' => new Doctrine_ORM_Mapping_Driver_AnnotationDriver() )); } + + public function setMetadataDriverImpl($driverImpl) + { + $this->_attributes['metadataDriverImpl'] = $driverImpl; + } + + public function getMetadataDriverImpl() + { + return $this->_attributes['metadataDriverImpl']; + } public function getResultCacheImpl() { diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index ff15057e3..be7253366 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -135,7 +135,7 @@ class Doctrine_ORM_EntityManager /** * The maintained (cached) Id generators. * - * @var + * @var array */ private $_idGenerators = array(); @@ -148,6 +148,8 @@ class Doctrine_ORM_EntityManager * * @param Doctrine\DBAL\Connection $conn * @param string $name + * @param Doctrine\ORM\Configuration $config + * @param Doctrine\Common\EventManager $eventManager */ protected function __construct( Doctrine_DBAL_Connection $conn, @@ -160,16 +162,16 @@ class Doctrine_ORM_EntityManager $this->_config = $config; $this->_eventManager = $eventManager; $this->_metadataFactory = new Doctrine_ORM_Mapping_ClassMetadataFactory( - new Doctrine_ORM_Mapping_Driver_AnnotationDriver(), + $this->_config->getMetadataDriverImpl(), $this->_conn->getDatabasePlatform()); + $this->_metadataFactory->setCacheDriver($this->_config->getMetadataCacheImpl()); $this->_unitOfWork = new Doctrine_ORM_UnitOfWork($this); - $this->_nullObject = Doctrine_ORM_Internal_Null::$INSTANCE; } /** * Gets the database connection object used by the EntityManager. * - * @return Doctrine_Connection + * @return Doctrine\DBAL\Connection */ public function getConnection() { @@ -178,6 +180,8 @@ class Doctrine_ORM_EntityManager /** * Gets the metadata factory used to gather the metadata of classes. + * + * @return Doctrine\ORM\Mapping\ClassMetadataFactory */ public function getMetadataFactory() { @@ -609,7 +613,6 @@ class Doctrine_ORM_EntityManager } $em = new Doctrine_ORM_EntityManager($conn, $name, $config, $eventManager); - $em->activate(); return $em; } diff --git a/lib/Doctrine/ORM/Mapping/AssociationMapping.php b/lib/Doctrine/ORM/Mapping/AssociationMapping.php index ddff66f15..9192c87b8 100644 --- a/lib/Doctrine/ORM/Mapping/AssociationMapping.php +++ b/lib/Doctrine/ORM/Mapping/AssociationMapping.php @@ -52,9 +52,6 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping protected $_isCascadeSave; protected $_isCascadeRefresh; - protected $_customAccessor; - protected $_customMutator; - /** * The fetch mode used for the association. * @@ -110,14 +107,6 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping */ protected $_mappedByFieldName; - /** - * Identifies the field on the inverse side of a bidirectional association. - * This is only set on the owning side of an association. - * - * @var string - */ - //protected $_inverseSideFieldName; - /** * The name of the join table, if any. * @@ -125,11 +114,8 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping */ protected $_joinTable; - //protected $_mapping = array(); - /** - * Constructor. - * Creates a new AssociationMapping. + * Initializes a new instance of a class derived from AssociationMapping. * * @param array $mapping The mapping definition. */ @@ -151,8 +137,6 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping 'mappedBy' => null, 'joinColumns' => null, 'joinTable' => null, - 'accessor' => null, - 'mutator' => null, 'optional' => true, 'cascades' => array() ); @@ -193,12 +177,6 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping } // Optional attributes for both sides - if (isset($mapping['accessor'])) { - $this->_customAccessor = $mapping['accessor']; - } - if (isset($mapping['mutator'])) { - $this->_customMutator = $mapping['mutator']; - } $this->_isOptional = isset($mapping['optional']) ? (bool)$mapping['optional'] : true; $this->_cascades = isset($mapping['cascade']) ? @@ -387,48 +365,6 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping return $this->_mappedByFieldName || $this->_inverseSideFieldName; }*/ - /** - * Whether the source field of the association has a custom accessor. - * - * @return boolean TRUE if the source field of the association has a custom accessor, - * FALSE otherwise. - */ - public function hasCustomAccessor() - { - return isset($this->_customAccessor); - } - - /** - * Gets the name of the custom accessor method of the source field. - * - * @return string The name of the accessor method or NULL. - */ - public function getCustomAccessor() - { - return $this->_customAccessor; - } - - /** - * Whether the source field of the association has a custom mutator. - * - * @return boolean TRUE if the source field of the association has a custom mutator, - * FALSE otherwise. - */ - public function hasCustomMutator() - { - return isset($this->_customMutator); - } - - /** - * Gets the name of the custom mutator method of the source field. - * - * @return string The name of the mutator method or NULL. - */ - public function getCustomMutator() - { - return $this->_customMutator; - } - public function isOneToOne() { return false; @@ -444,6 +380,6 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping return false; } - abstract public function lazyLoadFor($entity); + abstract public function lazyLoadFor($entity, $entityManager); } diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadata.php b/lib/Doctrine/ORM/Mapping/ClassMetadata.php index e7ec13ec3..ea05720e0 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadata.php @@ -1185,7 +1185,12 @@ class Doctrine_ORM_Mapping_ClassMetadata } $this->_fieldMappings[$mapping['fieldName']] = $mapping; } - + + public function addAssociationMapping(Doctrine_ORM_Mapping_AssociationMapping $mapping) + { + $this->_storeAssociationMapping($mapping); + } + /** * Adds a one-to-one mapping. * @@ -1256,7 +1261,7 @@ class Doctrine_ORM_Mapping_ClassMetadata { $sourceFieldName = $assocMapping->getSourceFieldName(); if (isset($this->_associationMappings[$sourceFieldName])) { - throw Doctrine_MappingException::duplicateFieldMapping(); + throw Doctrine_ORM_Exceptions_MappingException::duplicateFieldMapping(); } $this->_associationMappings[$sourceFieldName] = $assocMapping; $this->_registerMappingIfInverse($assocMapping); @@ -1412,6 +1417,16 @@ class Doctrine_ORM_Mapping_ClassMetadata $this->_discriminatorColumn = $columnDef; } + /** + * + * @param $fieldName + * @param $mapping + */ + /*public function addFieldMapping($fieldName, array $mapping) + { + $this->_fieldMappings[$fieldName] = $mapping; + }*/ + /** * Gets the discriminator column definition. * diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 1854f4ef0..eac161670 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -40,6 +40,7 @@ class Doctrine_ORM_Mapping_ClassMetadataFactory /** The targeted database platform. */ private $_targetPlatform; private $_driver; + private $_cacheDriver; /** * Constructor. @@ -53,16 +54,35 @@ class Doctrine_ORM_Mapping_ClassMetadataFactory $this->_targetPlatform = $targetPlatform; } + public function setCacheDriver($cacheDriver) + { + $this->_cacheDriver = $cacheDriver; + } + + public function getCacheDriver() + { + return $this->_cacheDriver; + } + /** * Returns the metadata object for a class. * * @param string $className The name of the class. - * @return Doctrine_Metadata + * @return Doctrine\ORM\Mapping\ClassMetadata */ public function getMetadataFor($className) { if ( ! isset($this->_loadedMetadata[$className])) { - $this->_loadMetadata($className); + if ($this->_cacheDriver) { + if ($this->_cacheDriver->contains("$className\$CLASSMETADATA")) { + $this->_loadedMetadata[$className] = $this->_cacheDriver->get("$className\$CLASSMETADATA"); + } else { + $this->_loadMetadata($className); + $this->_cacheDriver->put("$className\$CLASSMETADATA", $this->_loadedMetadata[$className]); + } + } else { + $this->_loadMetadata($className); + } } return $this->_loadedMetadata[$className]; } @@ -93,7 +113,7 @@ class Doctrine_ORM_Mapping_ClassMetadataFactory $class = $this->_loadedMetadata[$loadedParentClass]; } else { $rootClassOfHierarchy = count($parentClasses) > 0 ? array_shift($parentClasses) : $name; - $class = new Doctrine_ORM_Mapping_ClassMetadata($rootClassOfHierarchy); + $class = $this->_newClassMetadataInstance($rootClassOfHierarchy); $this->_loadClassMetadata($class, $rootClassOfHierarchy); $this->_loadedMetadata[$rootClassOfHierarchy] = $class; } @@ -108,7 +128,7 @@ class Doctrine_ORM_Mapping_ClassMetadataFactory // Move down the hierarchy of parent classes, starting from the topmost class $parent = $class; foreach ($parentClasses as $subclassName) { - $subClass = new Doctrine_ORM_Mapping_ClassMetadata($subclassName); + $subClass = $this->_newClassMetadataInstance($subclassName); $subClass->setInheritanceType($parent->getInheritanceType()); $subClass->setDiscriminatorMap($parent->getDiscriminatorMap()); $subClass->setDiscriminatorColumn($parent->getDiscriminatorColumn()); @@ -122,12 +142,17 @@ class Doctrine_ORM_Mapping_ClassMetadataFactory $parent = $subClass; } } + + protected function _newClassMetadataInstance($className) + { + return new Doctrine_ORM_Mapping_ClassMetadata($className); + } /** * Adds inherited fields to the subclass mapping. * - * @param Doctrine::ORM::Mapping::ClassMetadata $subClass - * @param Doctrine::ORM::Mapping::ClassMetadata $parentClass + * @param Doctrine\ORM\Mapping\ClassMetadata $subClass + * @param Doctrine\ORM\Mapping\ClassMetadata $parentClass */ private function _addInheritedFields($subClass, $parentClass) { @@ -135,20 +160,20 @@ class Doctrine_ORM_Mapping_ClassMetadataFactory if ( ! isset($mapping['inherited'])) { $mapping['inherited'] = $parentClass->getClassName(); } - $subClass->addFieldMapping($fieldName, $mapping); + $subClass->mapField($mapping); } } /** * Adds inherited associations to the subclass mapping. * - * @param unknown_type $subClass - * @param unknown_type $parentClass + * @param Doctrine\ORM\Mapping\ClassMetadata $subClass + * @param Doctrine\ORM\Mapping\ClassMetadata $parentClass */ private function _addInheritedRelations($subClass, $parentClass) { - foreach ($parentClass->getAssociationMappings() as $fieldName => $mapping) { - $subClass->addAssociationMapping($name, $mapping); + foreach ($parentClass->getAssociationMappings() as $mapping) { + $subClass->addAssociationMapping($mapping); } } diff --git a/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php b/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php index 8bafd546c..dc67ca453 100644 --- a/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php +++ b/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php @@ -85,4 +85,3 @@ class Doctrine_ORM_Mapping_ManyToManyMapping extends Doctrine_ORM_Mapping_Associ } } -?> \ No newline at end of file diff --git a/lib/Doctrine/ORM/Mapping/OneToManyMapping.php b/lib/Doctrine/ORM/Mapping/OneToManyMapping.php index b835b39c1..ec457453e 100644 --- a/lib/Doctrine/ORM/Mapping/OneToManyMapping.php +++ b/lib/Doctrine/ORM/Mapping/OneToManyMapping.php @@ -111,7 +111,7 @@ class Doctrine_ORM_Mapping_OneToManyMapping extends Doctrine_ORM_Mapping_Associa * @param $entity * @override */ - public function lazyLoadFor($entity) + public function lazyLoadFor($entity, $entityManager) { } diff --git a/lib/Doctrine/ORM/Mapping/OneToOneMapping.php b/lib/Doctrine/ORM/Mapping/OneToOneMapping.php index 93c162a00..9fabda4ab 100644 --- a/lib/Doctrine/ORM/Mapping/OneToOneMapping.php +++ b/lib/Doctrine/ORM/Mapping/OneToOneMapping.php @@ -19,9 +19,7 @@ * . */ -#namespace Doctrine::ORM::Mappings; - -#use Doctrine::ORM::Entity; +#namespace Doctrine\ORM\Mappings; /** * A one-to-one mapping describes a uni-directional mapping from one entity @@ -29,7 +27,6 @@ * * @since 2.0 * @author Roman Borschel - * @todo Rename to OneToOneMapping */ class Doctrine_ORM_Mapping_OneToOneMapping extends Doctrine_ORM_Mapping_AssociationMapping { @@ -64,7 +61,12 @@ class Doctrine_ORM_Mapping_OneToOneMapping extends Doctrine_ORM_Mapping_Associat { parent::__construct($mapping); } - + + /** + * {@inheritdoc} + * + * @override + */ protected function _initMappingArray() { parent::_initMappingArray(); @@ -72,7 +74,7 @@ class Doctrine_ORM_Mapping_OneToOneMapping extends Doctrine_ORM_Mapping_Associat } /** - * Validates & completes the mapping. Mapping defaults are applied here. + * {@inheritdoc} * * @param array $mapping The mapping to validate & complete. * @return array The validated & completed mapping. @@ -99,7 +101,7 @@ class Doctrine_ORM_Mapping_OneToOneMapping extends Doctrine_ORM_Mapping_Associat /** * Gets the source-to-target key column mapping. * - * @return unknown + * @return array */ public function getSourceToTargetKeyColumns() { @@ -109,7 +111,7 @@ class Doctrine_ORM_Mapping_OneToOneMapping extends Doctrine_ORM_Mapping_Associat /** * Gets the target-to-source key column mapping. * - * @return unknown + * @return array */ public function getTargetToSourceKeyColumns() { @@ -117,7 +119,7 @@ class Doctrine_ORM_Mapping_OneToOneMapping extends Doctrine_ORM_Mapping_Associat } /** - * Whether the association is one-to-one. + * {@inheritdoc} * * @return boolean * @override @@ -128,37 +130,33 @@ class Doctrine_ORM_Mapping_OneToOneMapping extends Doctrine_ORM_Mapping_Associat } /** - * Lazy-loads the associated entity for a given entity. + * {@inheritdoc} * * @param Doctrine\ORM\Entity $entity * @return void */ - public function lazyLoadFor($entity) + public function lazyLoadFor($entity, $entityManager) { - if ($entity->getClassName() != $this->_sourceClass->getClassName()) { - //error? - } - - $dql = 'SELECT t.* FROM ' . $this->_targetClass->getClassName() . ' t WHERE '; + $sourceClass = $entityManager->getClassMetadata($this->_sourceEntityName); + $targetClass = $entityManager->getClassMetadata($this->_targetEntityName); + + $dql = 'SELECT t.* FROM ' . $targetClass->getClassName() . ' t WHERE '; $params = array(); foreach ($this->_sourceToTargetKeyFields as $sourceKeyField => $targetKeyField) { if ($params) { $dql .= " AND "; } $dql .= "t.$targetKeyField = ?"; - $params[] = $entity->_rawGetField($sourceKeyField); + $params[] = $sourceClass->getReflectionProperty($sourceKeyField)->getValue($entity); } - $otherEntity = $this->_targetClass->getEntityManager() - ->query($dql, $params) - ->getFirst(); + $otherEntity = $entityManager->query($dql, $params)->getFirst(); if ( ! $otherEntity) { - $otherEntity = Doctrine_Null::$INSTANCE; + $otherEntity = null; } - $entity->_internalSetReference($this->_sourceFieldName, $otherEntity); + $sourceClass->getReflectionProperty($this->_sourceFieldName)->setValue($entity, $otherEntity); } } -?> \ No newline at end of file diff --git a/lib/Doctrine/ORM/VirtualProxy.php b/lib/Doctrine/ORM/VirtualProxy.php new file mode 100644 index 000000000..753355c61 --- /dev/null +++ b/lib/Doctrine/ORM/VirtualProxy.php @@ -0,0 +1,71 @@ + $owner + * @param $assoc + * @param $refProp + */ + public function __construct($owner, Doctrine_ORM_Mapping_AssociationMapping $assoc, ReflectionProperty $refProp) + { + $this->_owner = $owner; + $this->_assoc = $assoc; + $this->_refProp = $refProp; + } + + private function _load() + { + $realInstance = $tis->_assoc->lazyLoadFor($this->_owner); + $this->_refProp->setValue($this->_owner, $realInstance); + return $realInstance; + } + + /** All the "magic" interceptors */ + + public function __call($method, $args) + { + $realInstance = $this->_load(); + return call_user_func_array(array($realInstance, $method), $args); + } + + public function __get($prop) + { + $realInstance = $this->_load(); + return $realInstance->$prop; + } + + public function __set($prop, $value) + { + $realInstance = $this->_load(); + $realInstance->$prop = $value; + } + + public function __isset($prop) + { + $realInstance = $this->_load(); + return isset($realInstance->$prop); + } + + public function __unset($prop) + { + $realInstance = $this->_load(); + unset($realInstance->$prop); + } +} +?> diff --git a/tests/Orm/AllTests.php b/tests/Orm/AllTests.php index 885df05fe..d04f53d0f 100644 --- a/tests/Orm/AllTests.php +++ b/tests/Orm/AllTests.php @@ -11,13 +11,13 @@ require_once 'Orm/Hydration/AllTests.php'; require_once 'Orm/Ticket/AllTests.php'; require_once 'Orm/Entity/AllTests.php'; require_once 'Orm/Associations/AllTests.php'; +require_once 'Orm/Mapping/AllTests.php'; // Tests require_once 'Orm/UnitOfWorkTest.php'; require_once 'Orm/EntityManagerTest.php'; require_once 'Orm/EntityPersisterTest.php'; require_once 'Orm/CommitOrderCalculatorTest.php'; -require_once 'Orm/ClassMetadataTest.php'; class Orm_AllTests { @@ -34,13 +34,13 @@ class Orm_AllTests $suite->addTestSuite('Orm_EntityManagerTest'); $suite->addTestSuite('Orm_EntityPersisterTest'); $suite->addTestSuite('Orm_CommitOrderCalculatorTest'); - $suite->addTestSuite('Orm_ClassMetadataTest'); $suite->addTest(Orm_Query_AllTests::suite()); $suite->addTest(Orm_Hydration_AllTests::suite()); $suite->addTest(Orm_Entity_AllTests::suite()); $suite->addTest(Orm_Ticket_AllTests::suite()); $suite->addTest(Orm_Associations_AllTests::suite()); + $suite->addTest(Orm_Mapping_AllTests::suite()); return $suite; } diff --git a/tests/Orm/Mapping/AllTests.php b/tests/Orm/Mapping/AllTests.php new file mode 100644 index 000000000..55f3ef8c6 --- /dev/null +++ b/tests/Orm/Mapping/AllTests.php @@ -0,0 +1,32 @@ +addTestSuite('Orm_Mapping_ClassMetadataTest'); + $suite->addTestSuite('Orm_Mapping_ClassMetadataFactoryTest'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'Orm_Mapping_AllTests::main') { + Orm_Mapping_AllTests::main(); +} diff --git a/tests/Orm/Mapping/ClassMetadataFactoryTest.php b/tests/Orm/Mapping/ClassMetadataFactoryTest.php new file mode 100644 index 000000000..7395c6eb5 --- /dev/null +++ b/tests/Orm/Mapping/ClassMetadataFactoryTest.php @@ -0,0 +1,99 @@ +setInheritanceType('singleTable'); + // Add a mapped field + $cm1->mapField(array('fieldName' => 'name', 'type' => 'string')); + // and a mapped association + $cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'Other', 'mappedBy' => 'this')); + $cm2 = new Doctrine_ORM_Mapping_ClassMetadata('CMFTest_Entity2'); + $cm3 = new Doctrine_ORM_Mapping_ClassMetadata('CMFTest_Entity3'); + + $cmf = new ClassMetadataFactoryTestSubject($mockDriver, $mockPlatform); + // Set self-made metadata + $cmf->setMetadataForClass('CMFTest_Entity1', $cm1); + $cmf->setMetadataForClass('CMFTest_Entity2', $cm2); + $cmf->setMetadataForClass('CMFTest_Entity3', $cm3); + + // 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())); + + // Go + $cm3 = $cmf->getMetadataFor('CMFTest_Entity3'); + + // Metadata gathering should start at the root of the hierarchy, from there on downwards + $this->assertEquals(array('CMFTest_Entity1', 'CMFTest_Entity2', 'CMFTest_Entity3'), $cmf->getRequestedClasses()); + // Parent classes should be assigned by factory + $this->assertEquals(array('CMFTest_Entity2', 'CMFTest_Entity1'), $cm3->getParentClasses()); + $this->assertEquals('CMFTest_Entity1', $cm3->getRootClassName()); + $this->assertEquals('CMFTest_Entity1', $cm2->getRootClassName()); + $this->assertEquals('CMFTest_Entity1', $cm1->getRootClassName()); + // 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')); + } +} + +/* Test subject class with overriden factory method for mocking purposes */ +class ClassMetadataFactoryTestSubject extends Doctrine_ORM_Mapping_ClassMetadataFactory { + 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 */ + +class CMFTest_Entity1 {} +class CMFTest_Entity2 extends CMFTest_Entity1 {} +class CMFTest_Entity3 extends CMFTest_Entity2 {} + diff --git a/tests/Orm/ClassMetadataTest.php b/tests/Orm/Mapping/ClassMetadataTest.php similarity index 90% rename from tests/Orm/ClassMetadataTest.php rename to tests/Orm/Mapping/ClassMetadataTest.php index 581dd535a..404fff836 100644 --- a/tests/Orm/ClassMetadataTest.php +++ b/tests/Orm/Mapping/ClassMetadataTest.php @@ -1,16 +1,11 @@ assertEquals('Bar', $oneOneMapping->getTargetEntityName()); } - public function testTransientEntityIsManaged() - { - ; - } } \ No newline at end of file diff --git a/tests/lib/mocks/Doctrine_MetadataDriverMock.php b/tests/lib/mocks/Doctrine_MetadataDriverMock.php new file mode 100644 index 000000000..da9b12cbb --- /dev/null +++ b/tests/lib/mocks/Doctrine_MetadataDriverMock.php @@ -0,0 +1,17 @@ +