1
0
mirror of synced 2025-01-18 06:21:40 +03:00

[2.0] DDC-92 - Refactored Doctrine\DBAL\DriverManager some more in regards to exceptions and added some more test-cases.

This commit is contained in:
beberlei 2010-01-23 23:12:27 +00:00
parent bbce4a407a
commit 2036c95834
3 changed files with 69 additions and 2 deletions

View File

@ -35,4 +35,16 @@ class DBALException extends \Exception
return new self("The given 'driver' ".$unknownDriverName." is unknown, ".
"Doctrine currently supports only the following drivers: ".implode(", ", $knownDrivers));
}
public static function invalidWrapperClass($wrapperClass)
{
return new self("The given 'wrapperClass' ".$wrapperClass." has to be a ".
"subtype of \Doctrine\DBAL\Connection.");
}
public static function invalidDriverClass($driverClass)
{
return new self("The given 'driverClass' ".$driverClass." has to implement the ".
"\Doctrine\DBAL\Driver interface.");
}
}

View File

@ -122,8 +122,12 @@ final class DriverManager
$driver = new $className();
$wrapperClass = 'Doctrine\DBAL\Connection';
if (isset($params['wrapperClass']) && is_subclass_of($params['wrapperClass'], $wrapperClass)) {
$wrapperClass = $params['wrapperClass'];
if (isset($params['wrapperClass'])) {
if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
$wrapperClass = $params['wrapperClass'];
} else {
throw DBALException::invalidWrapperClass($params['wrapperClass']);
}
}
return new $wrapperClass($params, $driver, $config, $eventManager);
@ -149,5 +153,9 @@ final class DriverManager
if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
}
if (isset($params['driverClass']) && ! in_array('Doctrine\DBAL\Driver', class_implements($params['driverClass'], true))) {
throw DBALException::invalidDriverClass($params['driverClass']);
}
}
}

View File

@ -53,4 +53,51 @@ class DriverManagerTest extends \Doctrine\Tests\DbalTestCase
$conn = \Doctrine\DBAL\DriverManager::getConnection($options);
$this->assertSame($mockPlatform, $conn->getDatabasePlatform());
}
public function testCustomWrapper()
{
$wrapperMock = $this->getMock('\Doctrine\DBAL\Connection', array(), array(), '', false);
$wrapperClass = get_class($wrapperMock);
$options = array(
'pdo' => new \PDO('sqlite::memory:'),
'wrapperClass' => $wrapperClass
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($options);
$this->assertType($wrapperClass, $conn);
}
public function testInvalidWrapperClass()
{
$this->setExpectedException('\Doctrine\DBAL\DBALException');
$options = array(
'pdo' => new \PDO('sqlite::memory:'),
'wrapperClass' => 'stdClass',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($options);
}
public function testInvalidDriverClass()
{
$this->setExpectedException('\Doctrine\DBAL\DBALException');
$options = array(
'driverClass' => 'stdClass'
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($options);
}
public function testValidDriverClass()
{
$options = array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($options);
$this->assertType('Doctrine\DBAL\Driver\PDOMySql\Driver', $conn->getDriver());
}
}