1
0
mirror of synced 2024-12-14 07:06:04 +03:00

Fix DDC-1412

This commit is contained in:
Fabio B. Silva 2012-01-21 12:42:46 -02:00
parent 1d46d2b9af
commit 0fce3c8f97
2 changed files with 78 additions and 6 deletions

View File

@ -39,7 +39,34 @@ class DriverChain implements Driver
/**
* @var array
*/
private $_drivers = array();
private $drivers = array();
/**
* The default driver
*
* @var Driver
*/
private $defaultDriver;
/**
* Get the default driver.
*
* @return Driver
*/
public function getDefaultDriver()
{
return $this->defaultDriver;
}
/**
* Set the default driver.
*
* @param Driver $driver
*/
public function setDefaultDriver(Driver $driver)
{
$this->defaultDriver = $driver;
}
/**
* Add a nested driver.
@ -49,7 +76,7 @@ class DriverChain implements Driver
*/
public function addDriver(Driver $nestedDriver, $namespace)
{
$this->_drivers[$namespace] = $nestedDriver;
$this->drivers[$namespace] = $nestedDriver;
}
/**
@ -59,7 +86,7 @@ class DriverChain implements Driver
*/
public function getDrivers()
{
return $this->_drivers;
return $this->drivers;
}
/**
@ -70,13 +97,18 @@ class DriverChain implements Driver
*/
public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
{
foreach ($this->_drivers as $namespace => $driver) {
foreach ($this->drivers as $namespace => $driver) {
if (strpos($className, $namespace) === 0) {
$driver->loadMetadataForClass($className, $metadata);
return;
}
}
if ($this->defaultDriver !== null) {
$this->defaultDriver->loadMetadataForClass($className, $metadata);
return;
}
throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
}
@ -89,7 +121,7 @@ class DriverChain implements Driver
{
$classNames = array();
$driverClasses = array();
foreach ($this->_drivers AS $namespace => $driver) {
foreach ($this->drivers AS $namespace => $driver) {
$oid = spl_object_hash($driver);
if (!isset($driverClasses[$oid])) {
$driverClasses[$oid] = $driver->getAllClassNames();
@ -114,12 +146,16 @@ class DriverChain implements Driver
*/
public function isTransient($className)
{
foreach ($this->_drivers AS $namespace => $driver) {
foreach ($this->drivers AS $namespace => $driver) {
if (strpos($className, $namespace) === 0) {
return $driver->isTransient($className);
}
}
if ($this->defaultDriver !== null) {
return $this->defaultDriver->isTransient($className);
}
// class isTransient, i.e. not an entity or mapped superclass
return true;
}

View File

@ -88,6 +88,42 @@ class DriverChainTest extends \Doctrine\Tests\OrmTestCase
$this->assertTrue($chain->isTransient('stdClass'), "stdClass isTransient");
$this->assertFalse($chain->isTransient('Doctrine\Tests\Models\CMS\CmsUser'), "CmsUser is not Transient");
}
/**
* @group DDC-1412
*/
public function testDefaultDriver()
{
$companyDriver = $this->getMock('Doctrine\ORM\Mapping\Driver\Driver');
$dafaultDriver = $this->getMock('Doctrine\ORM\Mapping\Driver\Driver');
$entityClassName = 'Doctrine\Tests\ORM\Mapping\DriverChainEntity';
$managerClassName = 'Doctrine\Tests\Models\Company\CompanyManager';
$chain = new DriverChain();
$companyDriver->expects($this->never())
->method('loadMetadataForClass');
$companyDriver->expects($this->once())
->method('isTransient')
->with($this->equalTo($managerClassName))
->will($this->returnValue(false));
$dafaultDriver->expects($this->never())
->method('loadMetadataForClass');
$dafaultDriver->expects($this->once())
->method('isTransient')
->with($this->equalTo($entityClassName))
->will($this->returnValue(true));
$this->assertNull($chain->getDefaultDriver());
$chain->setDefaultDriver($dafaultDriver);
$chain->addDriver($companyDriver, 'Doctrine\Tests\Models\Company');
$this->assertSame($dafaultDriver, $chain->getDefaultDriver());
$this->assertTrue($chain->isTransient($entityClassName));
$this->assertFalse($chain->isTransient($managerClassName));
}
}
class DriverChainEntity