Added exception for invalid drivers

This commit is contained in:
Timothy Warren 2012-07-26 17:21:24 +00:00
parent 508c9c3a4f
commit d9a4b5fbe5
4 changed files with 65 additions and 8 deletions

View File

@ -13,6 +13,26 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Generic exception for bad drivers
*
* @package Query
* @subpackage Query
*/
class BadDBDriverException extends UnexpectedValueException {}
// --------------------------------------------------------------------------
/**
* Generic exception for bad connection strings
*
* @package Query
* @subpackage Query
*/
class BadConnectionException extends UnexpectedValueException {}
// --------------------------------------------------------------------------
/** /**
* Convienience class for creating sql queries - also the class that * Convienience class for creating sql queries - also the class that
* instantiates the specific db driver * instantiates the specific db driver
@ -186,6 +206,12 @@ class Query_Builder {
$dsn = strtolower($dbtype).':'.$dsn; $dsn = strtolower($dbtype).':'.$dsn;
} }
// Make sure the class exists
if ( ! class_exists($dbtype))
{
throw new BadDBDriverException('Database driver does not exist, or is not supported');
}
// Create the dsn for the database to connect to // Create the dsn for the database to connect to
switch($dbtype) switch($dbtype)
{ {
@ -213,14 +239,22 @@ class Query_Builder {
break; break;
} }
// Create the database connection
if ( ! empty($params->user)) try
{ {
$this->db = new $dbtype($dsn, $params->user, $params->pass); // Create the database connection
if ( ! empty($params->user))
{
$this->db = new $dbtype($dsn, $params->user, $params->pass);
}
else
{
$this->db = new $dbtype($dsn);
}
} }
else catch(Exception $e)
{ {
$this->db = new $dbtype($dsn); throw new BadConnectionException('Connection failed, invalid arguments');
} }
// Set the connection name property, if applicable // Set the connection name property, if applicable

View File

@ -24,7 +24,7 @@ class CoreTest extends UnitTestCase {
* @access public * @access public
* @return void * @return void
*/ */
function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
} }
@ -37,7 +37,7 @@ class CoreTest extends UnitTestCase {
* @access public * @access public
* @return void * @return void
*/ */
function TestPHPVersion() public function TestPHPVersion()
{ {
$this->assertTrue(version_compare(PHP_VERSION, "5.2", "ge")); $this->assertTrue(version_compare(PHP_VERSION, "5.2", "ge"));
} }
@ -50,7 +50,7 @@ class CoreTest extends UnitTestCase {
* @access public * @access public
* @return void * @return void
*/ */
function TestHasPDO() public function TestHasPDO()
{ {
// PDO class exists // PDO class exists
$this->assertTrue(class_exists('PDO')); $this->assertTrue(class_exists('PDO'));

View File

@ -425,6 +425,29 @@ abstract class QBTest extends UnitTestCase {
$this->assertTrue(is_numeric($this->db->num_rows())); $this->assertTrue(is_numeric($this->db->num_rows()));
} }
// --------------------------------------------------------------------------
// ! Other Tests
// --------------------------------------------------------------------------
/**
* Handles invalid drivers
*/
public function TestBadDriver()
{
$params = array(
'host' => '127.0.0.1',
'port' => '3306',
'database' => 'test',
'user' => 'root',
'pass' => NULL,
'type' => 'QGYFHGEG'
);
$this->expectException('BadDBDriverException');
$this->db = new Query_Builder($params);
}
} }
// End of db_qb_test.php // End of db_qb_test.php

Binary file not shown.