Skip to content
Browse files

Added exception for invalid drivers

  • Loading branch information...
1 parent 508c9c3 commit d9a4b5fbe5408061265f97255595f90201d61369 @timw4mail timw4mail committed Jul 26, 2012
Showing with 65 additions and 8 deletions.
  1. +39 −5 classes/query_builder.php
  2. +3 −3 tests/core/core.php
  3. +23 −0 tests/core/db_qb_test.php
  4. BIN tests/db_files/FB_TEST_DB.FDB
View
44 classes/query_builder.php
@@ -14,6 +14,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
*
@@ -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)
@@ -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
View
6 tests/core/core.php
@@ -24,7 +24,7 @@ class CoreTest extends UnitTestCase {
* @access public
* @return void
*/
- function __construct()
+ public function __construct()
{
parent::__construct();
}
@@ -37,7 +37,7 @@ function __construct()
* @access public
* @return void
*/
- function TestPHPVersion()
+ public function TestPHPVersion()
{
$this->assertTrue(version_compare(PHP_VERSION, "5.2", "ge"));
}
@@ -50,7 +50,7 @@ function TestPHPVersion()
* @access public
* @return void
*/
- function TestHasPDO()
+ public function TestHasPDO()
{
// PDO class exists
$this->assertTrue(class_exists('PDO'));
View
23 tests/core/db_qb_test.php
@@ -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
View
BIN tests/db_files/FB_TEST_DB.FDB
Binary file not shown.

0 comments on commit d9a4b5f

Please sign in to comment.
Something went wrong with that request. Please try again.