From 43ecaf54a2ca84a69b6808afbb0047b02a048869 Mon Sep 17 00:00:00 2001 From: beberlei Date: Thu, 25 Feb 2010 19:58:39 +0000 Subject: [PATCH] [2.0] DDC-374 - Implement and tested DriverChain Mapping Adapter --- .../ORM/Mapping/Driver/DriverChain.php | 14 ++- .../Tests/ORM/Mapping/DriverChainTest.php | 91 +++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Mapping/DriverChainTest.php diff --git a/lib/Doctrine/ORM/Mapping/Driver/DriverChain.php b/lib/Doctrine/ORM/Mapping/Driver/DriverChain.php index b12a53421..08dcdc04f 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DriverChain.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DriverChain.php @@ -22,7 +22,8 @@ namespace Doctrine\ORM\Mapping\Driver; use Doctrine\ORM\Mapping\Driver\Driver, - Doctrine\ORM\Mapping\ClassMetadataInfo; + Doctrine\ORM\Mapping\ClassMetadataInfo, + Doctrine\ORM\Mapping\MappingException; /** * The DriverChain allows you to add multiple other mapping drivers for @@ -44,6 +45,12 @@ class DriverChain implements Driver */ private $_drivers = array(); + /** + * Add a nested driver + * + * @param Driver $nestedDriver + * @param string $namespace + */ public function addDriver(Driver $nestedDriver, $namespace) { $this->_drivers[$namespace] = $nestedDriver; @@ -60,8 +67,11 @@ class DriverChain implements Driver foreach ($this->_drivers AS $namespace => $driver) { if (strpos($className, $namespace) === 0) { $driver->loadMetadataForClass($className, $metadata); + return; } } + + throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); } /** @@ -93,5 +103,7 @@ class DriverChain implements Driver return $driver->isTransient($className); } } + + throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/DriverChainTest.php b/tests/Doctrine/Tests/ORM/Mapping/DriverChainTest.php new file mode 100644 index 000000000..bc00bf72b --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/DriverChainTest.php @@ -0,0 +1,91 @@ +getMock('Doctrine\ORM\Mapping\Driver\Driver'); + $driver1->expects($this->never()) + ->method('loadMetadataForClass'); + $driver1->expectS($this->never()) + ->method('isTransient'); + + $driver2 = $this->getMock('Doctrine\ORM\Mapping\Driver\Driver'); + $driver2->expects($this->at(0)) + ->method('loadMetadataForClass') + ->with($this->equalTo($className), $this->equalTo($classMetadata)); + $driver2->expects($this->at(1)) + ->method('isTransient') + ->with($this->equalTo($className)) + ->will($this->returnValue( true )); + + $chain->addDriver($driver1, 'Doctrine\Tests\Models\Company'); + $chain->addDriver($driver2, 'Doctrine\Tests\ORM\Mapping'); + + $chain->loadMetadataForClass($className, $classMetadata); + + $this->assertTrue( $chain->isTransient($className) ); + } + + public function testLoadMetadata_NoDelegatorFound_ThrowsMappingException() + { + $className = 'Doctrine\Tests\ORM\Mapping\DriverChainEntity'; + $classMetadata = new \Doctrine\ORM\Mapping\ClassMetadata($className); + + $chain = new DriverChain(); + + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException'); + $chain->loadMetadataForClass($className, $classMetadata); + } + + public function testIsTransient_NoDelegatorFound_ThrowsMappingException() + { + $className = 'Doctrine\Tests\ORM\Mapping\DriverChainEntity'; + $classMetadata = new \Doctrine\ORM\Mapping\ClassMetadata($className); + + $chain = new DriverChain(); + + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException'); + $chain->isTransient($className); + } + + public function testGatherAllClassNames() + { + $className = 'Doctrine\Tests\ORM\Mapping\DriverChainEntity'; + $classMetadata = new \Doctrine\ORM\Mapping\ClassMetadata($className); + + $chain = new DriverChain(); + + $driver1 = $this->getMock('Doctrine\ORM\Mapping\Driver\Driver'); + $driver1->expects($this->once()) + ->method('getAllClassNames') + ->will($this->returnValue(array('Foo'))); + + $driver2 = $this->getMock('Doctrine\ORM\Mapping\Driver\Driver'); + $driver2->expects($this->once()) + ->method('getAllClassNames') + ->will($this->returnValue(array('Bar', 'Baz'))); + + $chain->addDriver($driver1, 'Doctrine\Tests\Models\Company'); + $chain->addDriver($driver2, 'Doctrine\Tests\ORM\Mapping'); + + $this->assertEquals(array('Foo', 'Bar', 'Baz'), $chain->getAllClassNames()); + } +} + +class DriverChainEntity +{ + +} \ No newline at end of file