diff --git a/lib/Doctrine/DBAL/DBALException.php b/lib/Doctrine/DBAL/DBALException.php index 432016879..58728ac56 100644 --- a/lib/Doctrine/DBAL/DBALException.php +++ b/lib/Doctrine/DBAL/DBALException.php @@ -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."); + } } \ No newline at end of file diff --git a/lib/Doctrine/DBAL/DriverManager.php b/lib/Doctrine/DBAL/DriverManager.php index fb093fd79..0b6634dab 100644 --- a/lib/Doctrine/DBAL/DriverManager.php +++ b/lib/Doctrine/DBAL/DriverManager.php @@ -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']); + } } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/DriverManagerTest.php b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php index 770bf9698..d7d0fb94f 100644 --- a/tests/Doctrine/Tests/DBAL/DriverManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php @@ -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()); + } } \ No newline at end of file