Skip to content

Commit

Permalink
Added exception for invalid drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
timw4mail committed Jul 26, 2012
1 parent 508c9c3 commit d9a4b5f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
44 changes: 39 additions & 5 deletions classes/query_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* instantiates the specific db driver
Expand Down Expand Up @@ -185,6 +205,12 @@ public function __construct($params)
{
$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
switch($dbtype)
Expand Down Expand Up @@ -213,14 +239,22 @@ public function __construct($params)
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
Expand Down
6 changes: 3 additions & 3 deletions tests/core/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CoreTest extends UnitTestCase {
* @access public
* @return void
*/
function __construct()
public function __construct()
{
parent::__construct();
}
Expand All @@ -37,7 +37,7 @@ function __construct()
* @access public
* @return void
*/
function TestPHPVersion()
public function TestPHPVersion()
{
$this->assertTrue(version_compare(PHP_VERSION, "5.2", "ge"));
}
Expand All @@ -50,7 +50,7 @@ function TestPHPVersion()
* @access public
* @return void
*/
function TestHasPDO()
public function TestHasPDO()
{
// PDO class exists
$this->assertTrue(class_exists('PDO'));
Expand Down
23 changes: 23 additions & 0 deletions tests/core/db_qb_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,29 @@ public function TestNumRows()
$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
Binary file modified tests/db_files/FB_TEST_DB.FDB
Binary file not shown.

0 comments on commit d9a4b5f

Please sign in to comment.