From 884131e51fa7f14fd30aaed0e0d226dcbf79c51b Mon Sep 17 00:00:00 2001 From: beberlei Date: Wed, 4 Nov 2009 23:06:38 +0000 Subject: [PATCH] [2.0] DDC-115 - Proper fix of global namespace problem was in the ClassMetadataInfo, a namespace plus seperator was appended for reasons of shortcuts. However this broke associations in global namespace. Changes to ClassMetadataFactory were reverted and fix plus tests applied to ClassMetadataInfo. --- .../ORM/Mapping/ClassMetadataFactory.php | 12 ----- .../ORM/Mapping/ClassMetadataInfo.php | 6 +-- .../ORM/Mapping/ClassMetadataFactoryTest.php | 8 ++-- .../Tests/ORM/Mapping/ClassMetadataTest.php | 48 +++++++++++++++++++ .../ORM/Tools/ConvertDoctrine1SchemaTest.php | 2 +- 5 files changed, 57 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 60686bf9d..9c6167d59 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -102,10 +102,6 @@ class ClassMetadataFactory */ public function getMetadataFor($className) { - if($className[0] == "\\") { - $className = substr($className, 1); - } - if ( ! isset($this->_loadedMetadata[$className])) { $cacheKey = "$className\$CLASSMETADATA"; if ($this->_cacheDriver) { @@ -130,10 +126,6 @@ class ClassMetadataFactory */ public function hasMetadataFor($className) { - if($className[0] == "\\") { - $className = substr($className, 1); - } - return isset($this->_loadedMetadata[$className]); } @@ -147,10 +139,6 @@ class ClassMetadataFactory */ public function setMetadataFor($className, $class) { - if($className[0] == "\\") { - $className = substr($className, 1); - } - $this->_loadedMetadata[$className] = $class; } diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 2e09fa0d3..3f46ddf41 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1032,7 +1032,7 @@ class ClassMetadataInfo public function setSubclasses(array $subclasses) { foreach ($subclasses as $subclass) { - if (strpos($subclass, '\\') === false) { + if (strpos($subclass, '\\') === false && strlen($this->namespace)) { $this->subClasses[] = $this->namespace . '\\' . $subclass; } else { $this->subClasses[] = $subclass; @@ -1203,7 +1203,7 @@ class ClassMetadataInfo private function _completeAssociationMapping(array $mapping) { $mapping['sourceEntity'] = $this->name; - if (isset($mapping['targetEntity']) && strpos($mapping['targetEntity'], '\\') === false) { + if (isset($mapping['targetEntity']) && strpos($mapping['targetEntity'], '\\') === false && strlen($this->namespace) > 0) { $mapping['targetEntity'] = $this->namespace . '\\' . $mapping['targetEntity']; } return $mapping; @@ -1480,7 +1480,7 @@ class ClassMetadataInfo public function setDiscriminatorMap(array $map) { foreach ($map as $value => $className) { - if (strpos($className, '\\') === false) { + if (strpos($className, '\\') === false && strlen($this->namespace)) { $className = $this->namespace . '\\' . $className; } $this->discriminatorMap[$value] = $className; diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index 49fa2ecf0..8f972db34 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -59,7 +59,7 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType); } - public function testGetMetadataGlobalNamespaceModel() + public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized() { require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php"; @@ -72,11 +72,13 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase $mf = $entityManager->getMetadataFactory(); $m1 = $mf->getMetadataFor("DoctrineGlobal_Article"); + $h1 = $mf->hasMetadataFor("DoctrineGlobal_Article"); $h2 = $mf->hasMetadataFor("\DoctrineGlobal_Article"); $m2 = $mf->getMetadataFor("\DoctrineGlobal_Article"); - $this->assertSame($m1, $m2); - $this->assertTrue($h2); + $this->assertNotSame($m1, $m2); + $this->assertFalse($h2); + $this->assertTrue($h1); } protected function _createEntityManager($metadataDriver) diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index 592a059f9..c86233354 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -65,4 +65,52 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $cm->mapField(array('fieldName' => 'name', 'type' => 'string', 'length' => 50)); $this->assertFalse($cm->isNullable('name'), "By default a field should not be nullable."); } + + /** + * @group DDC-115 + */ + public function testMapAssocationInGlobalNamespace() + { + require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php"; + + $cm = new ClassMetadata('DoctrineGlobal_Article'); + $cm->mapManyToMany(array( + 'fieldName' => 'author', + 'targetEntity' => 'DoctrineGlobal_User', + 'joinTable' => array( + 'name' => 'bar', + 'joinColumns' => array(array('name' => 'bar_id', 'referencedColumnName' => 'id')), + 'inverseJoinColumns' => array(array('name' => 'baz_id', 'referencedColumnName' => 'id')), + ), + )); + + $this->assertEquals("DoctrineGlobal_User", $cm->associationMappings['author']->targetEntityName); + } + + /** + * @group DDC-115 + */ + public function testSetDiscriminatorMapInGlobalNamespace() + { + require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php"; + + $cm = new ClassMetadata('DoctrineGlobal_User'); + $cm->setDiscriminatorMap(array('descr' => 'DoctrineGlobal_Article', 'foo' => 'DoctrineGlobal_User')); + + $this->assertEquals("DoctrineGlobal_Article", $cm->discriminatorMap['descr']); + $this->assertEquals("DoctrineGlobal_User", $cm->discriminatorMap['foo']); + } + + /** + * @group DDC-115 + */ + public function testSetSubClassesInGlobalNamespace() + { + require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php"; + + $cm = new ClassMetadata('DoctrineGlobal_User'); + $cm->setSubclasses(array('DoctrineGlobal_Article')); + + $this->assertEquals("DoctrineGlobal_Article", $cm->subClasses[0]); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php index dc02f8527..35dd35b5b 100644 --- a/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php @@ -60,7 +60,7 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals(3, count($metadatas['User']->fieldMappings)); $this->assertEquals('Profile', $metadatas['Profile']->associationMappings['User']->sourceEntityName); - $this->assertEquals('\User', $metadatas['Profile']->associationMappings['User']->targetEntityName); + $this->assertEquals('User', $metadatas['Profile']->associationMappings['User']->targetEntityName); $this->assertEquals('username', $metadatas['User']->primaryTable['indexes']['username']['columns'][0]);