[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:
parent
bbce4a407a
commit
2036c95834
@ -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.");
|
||||
}
|
||||
}
|
@ -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']);
|
||||
}
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user