From bbce4a407a386187795c08833bf6ccbb5dacb5ac Mon Sep 17 00:00:00 2001 From: beberlei Date: Sat, 23 Jan 2010 22:59:48 +0000 Subject: [PATCH] [2.0] DDC-92 - Removed DoctrineException from Doctrine\DBAL\DriverManager and replaced with more specific DBALException's --- lib/Doctrine/DBAL/DBALException.php | 24 ++++++++++++++++++- lib/Doctrine/DBAL/DriverManager.php | 11 ++++----- .../Doctrine/Tests/DBAL/DriverManagerTest.php | 14 +++-------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/DBAL/DBALException.php b/lib/Doctrine/DBAL/DBALException.php index 7d79a87a4..432016879 100644 --- a/lib/Doctrine/DBAL/DBALException.php +++ b/lib/Doctrine/DBAL/DBALException.php @@ -11,6 +11,28 @@ class DBALException extends \Exception public static function invalidPlatformSpecified() { - return new self("Invalid 'platform' option specified, need to give an instance of \Doctrine\DBAL\Platforms\AbstractPlatform."); + return new self( + "Invalid 'platform' option specified, need to give an instance of ". + "\Doctrine\DBAL\Platforms\AbstractPlatform."); + } + + public static function invalidPdoInstance() + { + return new self( + "The 'pdo' option was used in DriverManager::getConnection() but no ". + "instance of PDO was given." + ); + } + + public static function driverRequired() + { + return new self("The options 'driver' or 'driverClass' are mandatory if no PDO ". + "instance is given to DriverManager::getConnection()."); + } + + public static function unknownDriver($unknownDriverName, array $knownDrivers) + { + return new self("The given 'driver' ".$unknownDriverName." is unknown, ". + "Doctrine currently supports only the following drivers: ".implode(", ", $knownDrivers)); } } \ No newline at end of file diff --git a/lib/Doctrine/DBAL/DriverManager.php b/lib/Doctrine/DBAL/DriverManager.php index 21a75c10d..fb093fd79 100644 --- a/lib/Doctrine/DBAL/DriverManager.php +++ b/lib/Doctrine/DBAL/DriverManager.php @@ -48,10 +48,7 @@ final class DriverManager ); /** Private constructor. This class cannot be instantiated. */ - public function __construct() - { - throw \Doctrine\Common\DoctrineException::driverManagerCannotBeInstantiated(); - } + private function __construct() { } /** * Creates a connection object based on the specified parameters. @@ -110,7 +107,7 @@ final class DriverManager // check for existing pdo object if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) { - throw DoctrineException::invalidPdoInstance(); + throw DBALException::invalidPdoInstance(); } else if (isset($params['pdo'])) { $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME); } else { @@ -143,14 +140,14 @@ final class DriverManager // driver if ( ! isset($params['driver']) && ! isset($params['driverClass'])) { - throw DoctrineException::driverRequired(); + throw DBALException::driverRequired(); } // check validity of parameters // driver if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) { - throw DoctrineException::unknownDriver($params['driver']); + throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap)); } } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/DriverManagerTest.php b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php index 6e38009c4..770bf9698 100644 --- a/tests/Doctrine/Tests/DBAL/DriverManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php @@ -7,15 +7,7 @@ require_once __DIR__ . '/../TestInit.php'; class DriverManagerTest extends \Doctrine\Tests\DbalTestCase { /** - * @expectedException \Doctrine\Common\DoctrineException - */ - public function testCantInstantiateDriverManager() - { - $test = new \Doctrine\DBAL\DriverManager(); - } - - /** - * @expectedException \Doctrine\Common\DoctrineException + * @expectedException \Doctrine\DBAL\DBALException */ public function testInvalidPdoInstance() { @@ -35,7 +27,7 @@ class DriverManagerTest extends \Doctrine\Tests\DbalTestCase } /** - * @expectedException \Doctrine\Common\DoctrineException + * @expectedException \Doctrine\DBAL\DBALException */ public function testCheckParams() { @@ -43,7 +35,7 @@ class DriverManagerTest extends \Doctrine\Tests\DbalTestCase } /** - * @expectedException \Doctrine\Common\DoctrineException + * @expectedException \Doctrine\DBAL\DBALException */ public function testInvalidDriver() {