From d9a4b5fbe5408061265f97255595f90201d61369 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 26 Jul 2012 17:21:24 +0000 Subject: [PATCH] Added exception for invalid drivers --- classes/query_builder.php | 44 ++++++++++++++++++++++++++++++---- tests/core/core.php | 6 ++--- tests/core/db_qb_test.php | 23 ++++++++++++++++++ tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/classes/query_builder.php b/classes/query_builder.php index 0d28e82..d9a22e4 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -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 @@ -185,6 +205,12 @@ class Query_Builder { { $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 @@ class Query_Builder { 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 diff --git a/tests/core/core.php b/tests/core/core.php index 557534c..f645552 100644 --- a/tests/core/core.php +++ b/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 @@ class CoreTest extends UnitTestCase { * @access public * @return void */ - function TestPHPVersion() + public function TestPHPVersion() { $this->assertTrue(version_compare(PHP_VERSION, "5.2", "ge")); } @@ -50,7 +50,7 @@ class CoreTest extends UnitTestCase { * @access public * @return void */ - function TestHasPDO() + public function TestHasPDO() { // PDO class exists $this->assertTrue(class_exists('PDO')); diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index d4b9de8..55279ce 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -425,6 +425,29 @@ abstract class QBTest extends UnitTestCase { $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 \ No newline at end of file diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 702a59673968e8f30dd272b2af26214bb2cfcdd3..08cb9f115e128dd034cde28f5d0a1b1c2787cdc4 100644 GIT binary patch delta 1166 zcmZXTO=uHA6vy9el78f|*(pJc3X%_Ft0E*Ih|q(q!!dsySK|Bc3QwW9P#-@5JLcrObeQk)l?7aDh$@{&pX*P?^W^qoy zg~G=i96S=6kx==zmIbJ*55N%5HUTc5|2nyFL?4h~sMijJP-`vwHem@7wIIqJ$WSB|gAcvOl;E0&JG9D90Ej#;+IT!>^dZ@&MijtIOqUUkz&MYo`y@@O*GW?DeaZ_i$cR>9&WLlv&-y6ZGg8POAN%p4wmS zsQoP}{lo$-bun3D4@O8}%@ME;B5>;Kl_6q96Dyin@rfDQ^zgK(XyW*mBHpAY+e~B+ zOk^DmBVf%$XNc%%qN9ng&3p?1an7nuB1i8@d7IA!u1c%R1TeK(gh5WUu)R%@`XH zK+m$1pn__8SaT4pgCJ39*P1B#059#(WQs` zoDfV9JrzA?S@sY?4}wqCYtn-&lAyHWn*7x^tPg$YeKT>|a;@rjS~Nr0 z>V*6Coe=hS0QiG2SKk89MPsp!%#NNz>jGo|Q7ibD^uo#vzs~EVeH{e+8EHvDO^eqzAoL(}?anRaRp@|wqjRgss!*OI61Z)=* z@_ino$4VAUxP+~7vkM6qm*S=6)Aon zC_EaMQ^+)0H?t-Rt<`0q@Wd;^6W=%K@dGD4?k0V*usi8-H|ceGO8O-}%6+3%vaH=w zXO%FtR%ex5)>>f_Mr;p8b|JP8#v;THj1f~9ZItC*ZEvz-Zn7*lS?6Vbr8<^&Q)6kZ zv9#7$1ADL*VL)NElOI)2s%%=vS(z?iyIfkwsQgpFqw>TCM&!9Xw$5iZ&@`HKIaJr2 zYFUbHUZcX>x`vmjQx}zTR#U1p