From bed0d1bb0422980b426c6c482bcf5d0543e1d78f Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 11 Feb 2014 14:29:41 -0500 Subject: [PATCH] Improve code coverage --- classes/db_pdo.php | 47 +++++----------------------- classes/db_util.php | 5 +-- classes/query_parser.php | 11 ++----- drivers/firebird/firebird_driver.php | 36 ++++----------------- drivers/mysql/mysql_driver.php | 6 +--- drivers/pgsql/pgsql_driver.php | 5 --- tests/core/db_qb_test.php | 16 ++++++++++ 7 files changed, 34 insertions(+), 92 deletions(-) diff --git a/classes/db_pdo.php b/classes/db_pdo.php index 2bf7ac4..d3c20c1 100644 --- a/classes/db_pdo.php +++ b/classes/db_pdo.php @@ -83,26 +83,19 @@ abstract class DB_PDO extends PDO { * @param string $sql * @param array $data * @return mixed PDOStatement / FALSE + * @throws InvalidArgumentException */ public function prepare_query($sql, $data) { // Prepare the sql $query = $this->prepare($sql); - if( ! (is_object($query) || is_resource($query))) - { - $this->get_last_error(); - return NULL; - } - // Set the statement in the class variable for easy later access $this->statement = $query; - if( ! (is_array($data) || is_object($data))) { - trigger_error("Invalid data argument"); - return NULL; + throw new InvalidArgumentException("Invalid data argument"); } // Bind the parameters @@ -114,16 +107,9 @@ abstract class DB_PDO extends PDO { } $res = $query->bindValue($k, $value); - - if( ! $res) - { - trigger_error("Parameter not successfully bound"); - return NULL; - } } return $query; - } // ------------------------------------------------------------------------- @@ -164,20 +150,6 @@ abstract class DB_PDO extends PDO { // -------------------------------------------------------------------------- - /** - * Return the last error for the current database connection - * - * @return string - */ - public function get_last_error() - { - list($i1, $i2, $i3) = $this->errorInfo(); - - echo "Error:
{$i1}:{$i2}\n{$i3}
"; - } - - // -------------------------------------------------------------------------- - /** * Quote database table name, and set prefix * @@ -289,14 +261,11 @@ abstract class DB_PDO extends PDO { */ protected function _quote($str) { - // Don't quote numbers - if ( ! is_string($str) && is_numeric($str)) - { - return $str; - } - - // Don't add additional quotes - if (strpos($str, $this->escape_char) === 0 || strrpos($str, $this->escape_char) === 0) + // Don't add additional quotes, or quote numbers + if (strpos($str, $this->escape_char) === 0 || + strrpos($str, $this->escape_char) === 0 || + ( ! is_string($str) && is_numeric($str)) + ) { return $str; } @@ -552,4 +521,4 @@ abstract class DB_PDO extends PDO { return array($sql, $vals); } } -// End of db_pdo.php +// End of db_pdo.php \ No newline at end of file diff --git a/classes/db_util.php b/classes/db_util.php index c1ba15b..b674413 100644 --- a/classes/db_util.php +++ b/classes/db_util.php @@ -47,10 +47,7 @@ abstract class DB_Util { */ public function __call($method, $args) { - if (method_exists($this->conn, $method)) - { - return call_user_func_array(array($this->conn, $method), $args); - } + return call_user_func_array(array($this->conn, $method), $args); } // -------------------------------------------------------------------------- diff --git a/classes/query_parser.php b/classes/query_parser.php index 1b7dc69..3666c5d 100644 --- a/classes/query_parser.php +++ b/classes/query_parser.php @@ -91,14 +91,7 @@ class Query_Parser { foreach($array as $row) { - if (is_array($row)) - { - $new_array[] = $row[0]; - } - else - { - $new_array[] = $row; - } + $new_array[] = (is_array($row)) ? $row[0] : $row; } return $new_array; @@ -106,4 +99,4 @@ class Query_Parser { } -// End of query_parser.php +// End of query_parser.php \ No newline at end of file diff --git a/drivers/firebird/firebird_driver.php b/drivers/firebird/firebird_driver.php index 66c2064..0505894 100644 --- a/drivers/firebird/firebird_driver.php +++ b/drivers/firebird/firebird_driver.php @@ -64,10 +64,7 @@ class Firebird extends DB_PDO { $this->conn = fbird_connect($dbpath, $user, $pass, 'utf-8', 0); // Throw an exception to make this match other pdo classes - if ( ! is_resource($this->conn)) - { - throw new PDOException(fbird_errmsg()); - } + if ( ! is_resource($this->conn)) throw new PDOException(fbird_errmsg()); // Load these classes here because this // driver does not call the constructor @@ -82,14 +79,6 @@ class Firebird extends DB_PDO { $class = __CLASS__."_util"; $this->util = new $class($this); } - - /** - * Clean up database connections - */ - public function __destruct() - { - fbird_close($this->conn); - } // -------------------------------------------------------------------------- @@ -116,20 +105,15 @@ class Firebird extends DB_PDO { */ public function query($sql) { - if (empty($sql)) - { - throw new PDOException("Query method requires an sql query!"); - } + if (empty($sql)) throw new PDOException("Query method requires an sql query!"); $this->statement_link = (isset($this->trans)) ? fbird_query($this->trans, $sql) : fbird_query($this->conn, $sql); // Throw the error as a exception - if ($this->statement_link === FALSE) - { - throw new PDOException(fbird_errmsg() . "Last query:" . $this->last_query); - } + $err_string = fbird_errmsg() . "Last query:" . $this->last_query; + if ($this->statement_link === FALSE) throw new PDOException($err_string); $this->statement = new FireBird_Result($this->statement_link); @@ -151,10 +135,7 @@ class Firebird extends DB_PDO { $this->statement_link = fbird_prepare($this->conn, $query); // Throw the error as an exception - if ($this->statement_link === FALSE) - { - throw new PDOException(fbird_errmsg()); - } + if ($this->statement_link === FALSE) throw new PDOException(fbird_errmsg()); $this->statement = new FireBird_Result($this->statement_link); @@ -170,12 +151,7 @@ class Firebird extends DB_PDO { */ public function beginTransaction() { - if(($this->trans = fbird_trans($this->conn)) !== NULL) - { - return TRUE; - } - - return NULL; + return (($this->trans = fbird_trans($this->conn)) !== NULL) ? TRUE : NULL; } // -------------------------------------------------------------------------- diff --git a/drivers/mysql/mysql_driver.php b/drivers/mysql/mysql_driver.php index 1710e06..7dd3ab1 100644 --- a/drivers/mysql/mysql_driver.php +++ b/drivers/mysql/mysql_driver.php @@ -38,6 +38,7 @@ class MySQL extends DB_PDO { */ public function __construct($dsn, $username=null, $password=null, $options=array()) { + // Automatically set the charset to UTF-8 if (defined('PDO::MYSQL_ATTR_INIT_COMMAND')) { $options = array_merge($options, array( @@ -45,11 +46,6 @@ class MySQL extends DB_PDO { )); } - if (strpos($dsn, 'mysql') === FALSE) - { - $dsn = 'mysql:'.$dsn; - } - parent::__construct($dsn, $username, $password, $options); } diff --git a/drivers/pgsql/pgsql_driver.php b/drivers/pgsql/pgsql_driver.php index a16610e..c7fa5d2 100644 --- a/drivers/pgsql/pgsql_driver.php +++ b/drivers/pgsql/pgsql_driver.php @@ -31,11 +31,6 @@ class PgSQL extends DB_PDO { */ public function __construct($dsn, $username=null, $password=null, $options=array()) { - if (strpos($dsn, 'pgsql') === FALSE) - { - $dsn = 'pgsql:'.$dsn; - } - parent::__construct($dsn, $username, $password, $options); } diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index c25e53e..426f029 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -29,6 +29,22 @@ abstract class QBTest extends UnitTestCase { // -------------------------------------------------------------------------- // ! Get Tests // -------------------------------------------------------------------------- + + public function TestInvalidConnectionName() + { + if (empty($this->db)) return; + + try + { + $db = Query('foo'); + } + catch (InvalidArgumentException $e) + { + $this->assertTrue(TRUE); + } + } + + // -------------------------------------------------------------------------- public function TestQueryFunctionAlias() {