Improve code coverage

This commit is contained in:
Timothy Warren 2014-02-11 14:29:41 -05:00
parent 44dc7f341f
commit bed0d1bb04
7 changed files with 34 additions and 92 deletions

View File

@ -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: <pre>{$i1}:{$i2}\n{$i3}</pre>";
}
// --------------------------------------------------------------------------
/**
* 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;
}

View File

@ -46,12 +46,9 @@ abstract class DB_Util {
* @return mixed
*/
public function __call($method, $args)
{
if (method_exists($this->conn, $method))
{
return call_user_func_array(array($this->conn, $method), $args);
}
}
// --------------------------------------------------------------------------
// ! Abstract Methods

View File

@ -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;

View File

@ -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
@ -83,14 +80,6 @@ class Firebird extends DB_PDO {
$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;
}
// --------------------------------------------------------------------------

View File

@ -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);
}

View File

@ -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);
}

View File

@ -30,6 +30,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()
{
if (empty($this->db)) return;