Lots of refactoring -- accessors/mutators instead of direct access, reduce query builder test database connections, and simplify some logic

This commit is contained in:
Timothy Warren 2015-07-30 16:40:30 -04:00
parent 8669fcc4be
commit 225017adee
32 changed files with 350 additions and 210 deletions

View File

@ -43,12 +43,7 @@ spl_autoload_register(function ($class)
$path = str_replace('\\', DIRECTORY_SEPARATOR, $class); $path = str_replace('\\', DIRECTORY_SEPARATOR, $class);
$file = QBASE_PATH . "{$path}.php"; $file = QBASE_PATH . "{$path}.php";
// @codeCoverageIgnoreStart if (file_exists($file)) require_once($file);
if (file_exists($file))
{
require_once($file);
}
// @codeCoverageIgnoreEnd
}); });
// End of autoload.php // End of autoload.php

View File

@ -13,6 +13,7 @@
<testsuite name="CoreTests"> <testsuite name="CoreTests">
<file>tests/core/core.php</file> <file>tests/core/core.php</file>
<file>tests/core/db_qp_test.php</file> <file>tests/core/db_qp_test.php</file>
<file>tests/core/connection_manager_test.php</file>
</testsuite> </testsuite>
<testsuite name="MySQLTests"> <testsuite name="MySQLTests">
<file>tests/databases/mysql/MySQLTest.php</file> <file>tests/databases/mysql/MySQLTest.php</file>

View File

@ -49,19 +49,19 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
* Reference to util class * Reference to util class
* @var Abstract_Util * @var Abstract_Util
*/ */
public $util; protected $util;
/** /**
* Last query executed * Last query executed
* @var string * @var string
*/ */
public $last_query; protected $last_query;
/** /**
* Prefix to apply to table names * Prefix to apply to table names
* @var string * @var string
*/ */
public $table_prefix = ''; protected $table_prefix = '';
/** /**
* Whether the driver supports 'TRUNCATE' * Whether the driver supports 'TRUNCATE'
@ -130,7 +130,32 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Concrete functions that can be overridden in child classes // ! Accessors / Mutators
// --------------------------------------------------------------------------
/**
* Get the last sql query exexcuted
*
* @return string
*/
public function get_last_query()
{
return $this->last_query;
}
// --------------------------------------------------------------------------
/**
* Set the last query sql
*
* @param string $query_string
* @return void
*/
public function set_last_query($query_string)
{
$this->last_query = $query_string;
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
@ -157,6 +182,21 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Set the common table name prefix
*
* @param string
* @return void
*/
public function set_table_prefix($prefix)
{
$this->table_prefix = $prefix;
}
// --------------------------------------------------------------------------
// ! Concrete functions that can be overridden in child classes
// --------------------------------------------------------------------------
/** /**
* Simplifies prepared statements for database queries * Simplifies prepared statements for database queries
* *

View File

@ -229,10 +229,10 @@ abstract class Abstract_Query_Builder {
// Escape the identifiers // Escape the identifiers
$field = $this->db->quote_ident($field); $field = $this->db->quote_ident($field);
$as = ($as !== FALSE) if ( ! is_string($as)) return $field;
? $this->db->quote_ident($as)
: $field;
$as = $this->db->quote_ident($as);
return "({$field}) AS {$as} "; return "({$field}) AS {$as} ";
} }
@ -519,7 +519,7 @@ abstract class Abstract_Query_Builder {
$this->queries['total_time'] += $total_time; $this->queries['total_time'] += $total_time;
// Set the last query to get rowcounts properly // Set the last query to get rowcounts properly
$this->db->last_query = $sql; $this->db->set_last_query($sql);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -48,18 +48,31 @@ final class Connection_Manager {
/** /**
* Private clone method to prevent cloning * Private clone method to prevent cloning
* @codeCoverageIgnore * @throws \DomainException
*/ */
private function __clone() {} public function __clone()
{
throw new \DomainException("Can't clone singleton");
}
// --------------------------------------------------------------------------
/**
* Prevent serialization of this object
* @throws \DomainException
*/
public function __sleep()
{
throw new \DomainException("No serializing of singleton");
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Make sure serialize/deserialize doesn't work * Make sure serialize/deserialize doesn't work
* @codeCoverageIgnore
* @throws \DomainException * @throws \DomainException
*/ */
private function __wakeup() public function __wakeup()
{ {
throw new \DomainException("Can't unserialize singleton"); throw new \DomainException("Can't unserialize singleton");
} }
@ -74,12 +87,7 @@ final class Connection_Manager {
*/ */
public static function get_instance() public static function get_instance()
{ {
// @codeCoverageIgnoreStart if (self::$instance === null) self::$instance = new self();
if (self::$instance === null)
{
self::$instance = new self();
}
// @codeCoverageIgnoreEnd
return self::$instance; return self::$instance;
} }
@ -132,7 +140,7 @@ final class Connection_Manager {
// Set the table prefix, if it exists // Set the table prefix, if it exists
if (isset($params->prefix)) if (isset($params->prefix))
{ {
$db->table_prefix = $params->prefix; $db->set_table_prefix($params->prefix);
} }
// Create Query Builder object // Create Query Builder object
@ -161,7 +169,7 @@ final class Connection_Manager {
* @return array * @return array
* @throws BadDBDriverException * @throws BadDBDriverException
*/ */
private function parse_params(\stdClass $params) public function parse_params(\stdClass $params)
{ {
$params->type = strtolower($params->type); $params->type = strtolower($params->type);
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';

View File

@ -30,7 +30,6 @@ interface Driver_Interface {
* @param string $username * @param string $username
* @param string $password * @param string $password
* @param array $driver_options * @param array $driver_options
* @return void
*/ */
public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options = array()); public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options = array());

View File

@ -190,7 +190,7 @@ class Driver extends \Query\Abstract_Driver {
: \fbird_query($this->conn, $sql); : \fbird_query($this->conn, $sql);
// Throw the error as a exception // Throw the error as a exception
$err_string = \fbird_errmsg() . "Last query:" . $this->last_query; $err_string = \fbird_errmsg() . "Last query:" . $this->get_last_query();
if ($this->statement_link === FALSE) throw new \PDOException($err_string, \fbird_errcode(), NULL); if ($this->statement_link === FALSE) throw new \PDOException($err_string, \fbird_errcode(), NULL);
$this->statement = new Result($this->statement_link, $this); $this->statement = new Result($this->statement_link, $this);

View File

@ -258,17 +258,7 @@ class Result extends \PDOStatement {
*/ */
public function rowCount() public function rowCount()
{ {
$rows = \fbird_affected_rows(); return \fbird_affected_rows();
// Get the number of rows for the select query if you can
if ($rows === 0 && \is_resource($this->statement) && \get_resource_type($this->statement) === "interbase result")
{
// @codeCoverageIgnoreStart
$rows = \count($this->result);
}
// @codeCoverageIgnoreEnd
return $rows;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -53,7 +53,7 @@ class Util extends \Query\Abstract_Util {
/** /**
* Create an SQL backup file for the current database's structure * Create an SQL backup file for the current database's structure
* @codeCoverageIgnore *
* @param string $db_path * @param string $db_path
* @param string $new_file * @param string $new_file
* @return string * @return string
@ -69,7 +69,6 @@ class Util extends \Query\Abstract_Util {
/** /**
* Create an SQL backup file for the current database's data * Create an SQL backup file for the current database's data
* *
* @codeCoverageIgnore
* @param array $exclude * @param array $exclude
* @param bool $system_tables * @param bool $system_tables
* @return string * @return string

View File

@ -33,6 +33,7 @@ class Driver extends \Query\Abstract_Driver {
/** /**
* Connect to MySQL Database * Connect to MySQL Database
* *
* @codeCoverageIgnore
* @param string $dsn * @param string $dsn
* @param string $username * @param string $username
* @param string $password * @param string $password

View File

@ -25,7 +25,6 @@ class Util extends \Query\Drivers\Firebird\Util {
/** /**
* Create an SQL backup file for the current database's structure * Create an SQL backup file for the current database's structure
* @codeCoverageIgnore
* @param string $db_path * @param string $db_path
* @param string $new_file * @param string $new_file
* @return string * @return string

View File

@ -26,6 +26,7 @@ class Driver extends \Query\Abstract_Driver {
/** /**
* Connect to a PosgreSQL database * Connect to a PosgreSQL database
* *
* @codeCoverageIgnore
* @param string $dsn * @param string $dsn
* @param string $username * @param string $username
* @param string $password * @param string $password

View File

@ -25,7 +25,6 @@ class SQL extends \Query\Abstract_SQL {
/** /**
* Get the query plan for the sql query * Get the query plan for the sql query
* *
* @codeCoverageIgnore
* @param string $sql * @param string $sql
* @return string * @return string
*/ */

View File

@ -305,7 +305,7 @@ interface Query_Builder_Interface {
* *
* @param int $limit * @param int $limit
* @param int|bool $offset * @param int|bool $offset
* @return string * @return Query_Builder
*/ */
public function limit($limit, $offset=FALSE); public function limit($limit, $offset=FALSE);

View File

@ -26,7 +26,6 @@ if ( ! function_exists('do_include'))
* Bulk directory loading workaround for use * Bulk directory loading workaround for use
* with array_map and glob * with array_map and glob
* *
* @codeCoverageIgnore
* @param string $path * @param string $path
* @return void * @return void
*/ */
@ -221,8 +220,6 @@ if ( ! function_exists('Query'))
// Otherwise, return a new connection // Otherwise, return a new connection
return $cmanager->connect($params_object); return $cmanager->connect($params_object);
} }
// @codeCoverageIgnoreStart
} }
// @codeCoverageIgnoreEnd
} }
// End of common.php // End of common.php

View File

@ -93,13 +93,16 @@ define('QTEST_DIR', realpath(dirname(__FILE__)));
define('QBASE_DIR', realpath(QTEST_DIR.'/../') . '/'); define('QBASE_DIR', realpath(QTEST_DIR.'/../') . '/');
define('QDS', DIRECTORY_SEPARATOR); define('QDS', DIRECTORY_SEPARATOR);
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
@unlink($path);
// Include db classes // Include db classes
require_once(QBASE_DIR . 'autoload.php'); require_once(QBASE_DIR . 'autoload.php');
// Require base testing classes // Require base testing classes
require_once(QTEST_DIR . '/core/core.php'); require_once(QTEST_DIR . '/core/core.php');
require_once(QTEST_DIR . '/core/db_test.php'); require_once(QTEST_DIR . '/core/db_test.php');
require_once(QTEST_DIR . '/core/db_qp_test.php'); require_once(QTEST_DIR . '/core/query_parser_test.php');
require_once(QTEST_DIR . '/core/db_qb_test.php'); require_once(QTEST_DIR . '/core/base_query_builder_test.php');
// End of bootstrap.php // End of bootstrap.php

View File

@ -18,14 +18,23 @@
*/ */
abstract class QBTest extends Query_TestCase { abstract class QBTest extends Query_TestCase {
protected static $db;
public function __destruct() public function __destruct()
{ {
if (isset($_GET['show_queries'])) if (isset($_GET['show_queries']))
{ {
echo '<pre>' . print_r($this->db->queries, TRUE) . '</pre>'; echo '<pre>' . print_r(self::$db->queries, TRUE) . '</pre>';
} }
} }
// --------------------------------------------------------------------------
public static function tearDownAfterClass()
{
self::$db = NULL;
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Driver-specific results // ! Driver-specific results
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -50,18 +59,9 @@ abstract class QBTest extends Query_TestCase {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testQueryFunctionAlias()
{
$db = Query();
$this->assertTrue($this->db === $db);
}
// --------------------------------------------------------------------------
public function testFunctionGet() public function testFunctionGet()
{ {
$query = $this->db->select('id, COUNT(id) as count') $query = self::$db->select('id, COUNT(id) as count')
->from('test') ->from('test')
->group_by('id') ->group_by('id')
->get(); ->get();
@ -73,7 +73,7 @@ abstract class QBTest extends Query_TestCase {
public function testGet() public function testGet()
{ {
$query = $this->db->get('test'); $query = self::$db->get('test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
@ -82,27 +82,27 @@ abstract class QBTest extends Query_TestCase {
public function testPrefixGet() public function testPrefixGet()
{ {
$query = $this->db->from('test')->get(); $query = self::$db->from('test')->get();
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
$this->assertTrue($this->db->num_rows() > 0); $this->assertTrue(self::$db->num_rows() > 0);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testGetWNumRows() public function testGetWNumRows()
{ {
$query = $this->db->get('test'); $query = self::$db->get('test');
$numrows = count($query->fetchAll(PDO::FETCH_NUM)); $numrows = count($query->fetchAll(PDO::FETCH_NUM));
$this->assertEqual($this->db->num_rows(), $numrows); $this->assertEqual(self::$db->num_rows(), $numrows);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testGetLimit() public function testGetLimit()
{ {
$query = $this->db->get('test', 2); $query = self::$db->get('test', 2);
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
@ -111,7 +111,7 @@ abstract class QBTest extends Query_TestCase {
public function testGetLimitSkip() public function testGetLimitSkip()
{ {
$query = $this->db->get('test', 2, 1); $query = self::$db->get('test', 2, 1);
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
@ -120,7 +120,7 @@ abstract class QBTest extends Query_TestCase {
public function testGetWhere() public function testGetWhere()
{ {
$query = $this->db->get_where('test', array('id !=' => 1), 2, 1); $query = self::$db->get_where('test', array('id !=' => 1), 2, 1);
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
@ -129,7 +129,7 @@ abstract class QBTest extends Query_TestCase {
public function testHaving() public function testHaving()
{ {
$query = $this->db->select('id') $query = self::$db->select('id')
->from('test') ->from('test')
->group_by('id') ->group_by('id')
->having(array('id >' => 1)) ->having(array('id >' => 1))
@ -143,7 +143,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrHaving() public function testOrHaving()
{ {
$query = $this->db->select('id') $query = self::$db->select('id')
->from('test') ->from('test')
->group_by('id') ->group_by('id')
->having(array('id >' => 1)) ->having(array('id >' => 1))
@ -159,7 +159,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectWhereGet() public function testSelectWhereGet()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->where('id >', 1) ->where('id >', 1)
->where('id <', 900) ->where('id <', 900)
->get('test', 2, 1); ->get('test', 2, 1);
@ -171,7 +171,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectAvg() public function testSelectAvg()
{ {
$query = $this->db->select_avg('id', 'di') $query = self::$db->select_avg('id', 'di')
->get('test'); ->get('test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
@ -181,7 +181,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectSum() public function testSelectSum()
{ {
$query = $this->db->select_sum('id', 'di') $query = self::$db->select_sum('id', 'di')
->get('test'); ->get('test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
@ -191,7 +191,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectDistinct() public function testSelectDistinct()
{ {
$query = $this->db->select_sum('id', 'di') $query = self::$db->select_sum('id', 'di')
->distinct() ->distinct()
->get('test'); ->get('test');
@ -202,7 +202,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectGet() public function testSelectGet()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->get('test', 2, 1); ->get('test', 2, 1);
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
@ -212,7 +212,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectFromGet() public function testSelectFromGet()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test ct') ->from('test ct')
->where('id >', 1) ->where('id >', 1)
->get(); ->get();
@ -224,7 +224,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectFromLimitGet() public function testSelectFromLimitGet()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test ct') ->from('test ct')
->where('id >', 1) ->where('id >', 1)
->limit(3) ->limit(3)
@ -238,7 +238,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectWhereGet2() public function testSelectWhereGet2()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->where('id !=', 1) ->where('id !=', 1)
->get('test', 2, 1); ->get('test', 2, 1);
@ -249,7 +249,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectMax() public function testSelectMax()
{ {
$query = $this->db->select_max('id', 'di') $query = self::$db->select_max('id', 'di')
->get('test'); ->get('test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
@ -259,7 +259,7 @@ abstract class QBTest extends Query_TestCase {
public function testSelectMin() public function testSelectMin()
{ {
$query = $this->db->select_min('id', 'di') $query = self::$db->select_min('id', 'di')
->get('test'); ->get('test');
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
@ -269,7 +269,7 @@ abstract class QBTest extends Query_TestCase {
public function testMultiOrderBy() public function testMultiOrderBy()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->order_by('id, key') ->order_by('id, key')
->get(); ->get();
@ -282,7 +282,7 @@ abstract class QBTest extends Query_TestCase {
public function testGroup() public function testGroup()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test') ->from('test')
->group_start() ->group_start()
->where('id >', 1) ->where('id >', 1)
@ -298,7 +298,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrGroup() public function testOrGroup()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test') ->from('test')
->group_start() ->group_start()
->where('id >', 1) ->where('id >', 1)
@ -317,7 +317,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrNotGroup() public function testOrNotGroup()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test') ->from('test')
->group_start() ->group_start()
->where('id >', 1) ->where('id >', 1)
@ -336,7 +336,7 @@ abstract class QBTest extends Query_TestCase {
public function testGroupCamelCase() public function testGroupCamelCase()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test') ->from('test')
->groupStart() ->groupStart()
->where('id >', 1) ->where('id >', 1)
@ -357,7 +357,7 @@ abstract class QBTest extends Query_TestCase {
public function testWhereIn() public function testWhereIn()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->where_in('id', array(0, 6, 56, 563, 341)) ->where_in('id', array(0, 6, 56, 563, 341))
->get(); ->get();
@ -368,7 +368,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrWhereIn() public function testOrWhereIn()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->where('key', 'false') ->where('key', 'false')
->or_where_in('id', array(0, 6, 56, 563, 341)) ->or_where_in('id', array(0, 6, 56, 563, 341))
->get(); ->get();
@ -380,7 +380,7 @@ abstract class QBTest extends Query_TestCase {
public function testWhereNotIn() public function testWhereNotIn()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->where('key', 'false') ->where('key', 'false')
->where_not_in('id', array(0, 6, 56, 563, 341)) ->where_not_in('id', array(0, 6, 56, 563, 341))
->get(); ->get();
@ -392,7 +392,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrWhereNotIn() public function testOrWhereNotIn()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->where('key', 'false') ->where('key', 'false')
->or_where_not_in('id', array(0, 6, 56, 563, 341)) ->or_where_not_in('id', array(0, 6, 56, 563, 341))
->get(); ->get();
@ -406,7 +406,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrderBy() public function testOrderBy()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test') ->from('test')
->where('id >', 0) ->where('id >', 0)
->where('id <', 9000) ->where('id <', 9000)
@ -422,7 +422,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrderByRandom() public function testOrderByRandom()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test') ->from('test')
->where('id >', 0) ->where('id >', 0)
->where('id <', 9000) ->where('id <', 9000)
@ -437,7 +437,7 @@ abstract class QBTest extends Query_TestCase {
public function testGroupBy() public function testGroupBy()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test') ->from('test')
->where('id >', 0) ->where('id >', 0)
->where('id <', 9000) ->where('id <', 9000)
@ -459,7 +459,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrWhere() public function testOrWhere()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test') ->from('test')
->where(' id ', 1) ->where(' id ', 1)
->or_where('key >', 0) ->or_where('key >', 0)
@ -473,7 +473,7 @@ abstract class QBTest extends Query_TestCase {
public function testLike() public function testLike()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->like('key', 'og') ->like('key', 'og')
->get(); ->get();
@ -484,7 +484,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrLike() public function testOrLike()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->like('key', 'og') ->like('key', 'og')
->or_like('key', 'val') ->or_like('key', 'val')
->get(); ->get();
@ -496,7 +496,7 @@ abstract class QBTest extends Query_TestCase {
public function testOrNotLike() public function testOrNotLike()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->like('key', 'og', 'before') ->like('key', 'og', 'before')
->or_not_like('key', 'val') ->or_not_like('key', 'val')
->get(); ->get();
@ -508,7 +508,7 @@ abstract class QBTest extends Query_TestCase {
public function testNotLike() public function testNotLike()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->like('key', 'og', 'before') ->like('key', 'og', 'before')
->not_like('key', 'val') ->not_like('key', 'val')
->get(); ->get();
@ -520,7 +520,7 @@ abstract class QBTest extends Query_TestCase {
public function testLikeBefore() public function testLikeBefore()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->like('key', 'og', 'before') ->like('key', 'og', 'before')
->get(); ->get();
@ -531,7 +531,7 @@ abstract class QBTest extends Query_TestCase {
public function testLikeAfter() public function testLikeAfter()
{ {
$query = $this->db->from('test') $query = self::$db->from('test')
->like('key', 'og', 'after') ->like('key', 'og', 'after')
->get(); ->get();
@ -542,7 +542,7 @@ abstract class QBTest extends Query_TestCase {
public function testJoin() public function testJoin()
{ {
$query = $this->db->from('test ct') $query = self::$db->from('test ct')
->join('join cj', 'cj.id = ct.id') ->join('join cj', 'cj.id = ct.id')
->get(); ->get();
@ -553,7 +553,7 @@ abstract class QBTest extends Query_TestCase {
public function testLeftJoin() public function testLeftJoin()
{ {
$query = $this->db->from('test ct') $query = self::$db->from('test ct')
->join('join cj', 'cj.id = ct.id', 'left') ->join('join cj', 'cj.id = ct.id', 'left')
->get(); ->get();
@ -564,7 +564,7 @@ abstract class QBTest extends Query_TestCase {
public function testInnerJoin() public function testInnerJoin()
{ {
$query = $this->db->from('test ct') $query = self::$db->from('test ct')
->join('join cj', 'cj.id = ct.id', 'inner') ->join('join cj', 'cj.id = ct.id', 'inner')
->get(); ->get();
@ -575,7 +575,7 @@ abstract class QBTest extends Query_TestCase {
public function testJoinWithMultipleWhereValues() public function testJoinWithMultipleWhereValues()
{ {
$query = $this->db->from('test ct') $query = self::$db->from('test ct')
->join('join cj', 'cj.id=ct.id', 'inner') ->join('join cj', 'cj.id=ct.id', 'inner')
->where(array( ->where(array(
'ct.id < ' => 3, 'ct.id < ' => 3,
@ -592,7 +592,7 @@ abstract class QBTest extends Query_TestCase {
public function testInsert() public function testInsert()
{ {
$query = $this->db->set('id', 98) $query = self::$db->set('id', 98)
->set('key', 84) ->set('key', 84)
->set('val', 120) ->set('val', 120)
->insert('test'); ->insert('test');
@ -604,7 +604,7 @@ abstract class QBTest extends Query_TestCase {
public function testInsertArray() public function testInsertArray()
{ {
$query = $this->db->insert('test', array( $query = self::$db->insert('test', array(
'id' => 587, 'id' => 587,
'key' => 1, 'key' => 1,
'val' => 2, 'val' => 2,
@ -635,7 +635,7 @@ abstract class QBTest extends Query_TestCase {
), ),
); );
$query = $this->db->insert_batch('test', $data); $query = self::$db->insert_batch('test', $data);
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
@ -644,7 +644,7 @@ abstract class QBTest extends Query_TestCase {
public function testUpdate() public function testUpdate()
{ {
$query = $this->db->where('id', 7) $query = self::$db->where('id', 7)
->update('test', array( ->update('test', array(
'id' => 7, 'id' => 7,
'key' => 'gogle', 'key' => 'gogle',
@ -664,7 +664,7 @@ abstract class QBTest extends Query_TestCase {
'val' => 'non-word' 'val' => 'non-word'
); );
$query = $this->db->set($array) $query = self::$db->set($array)
->where('id', 22) ->where('id', 22)
->update('test'); ->update('test');
@ -675,7 +675,7 @@ abstract class QBTest extends Query_TestCase {
public function testWhereSetUpdate() public function testWhereSetUpdate()
{ {
$query = $this->db->where('id', 36) $query = self::$db->where('id', 36)
->set('id', 36) ->set('id', 36)
->set('key', 'gogle') ->set('key', 'gogle')
->set('val', 'non-word') ->set('val', 'non-word')
@ -688,8 +688,7 @@ abstract class QBTest extends Query_TestCase {
public function testDelete() public function testDelete()
{ {
//$this->markTestSkipped(); $query = self::$db->delete('test', array('id' => 5));
$query = $this->db->delete('test', array('id' => 5));
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
@ -698,7 +697,7 @@ abstract class QBTest extends Query_TestCase {
public function testDeleteWithMultipleWhereValues() public function testDeleteWithMultipleWhereValues()
{ {
$query = $this->db->delete('test', array( $query = self::$db->delete('test', array(
'id' => 5, 'id' => 5,
'key' => 'gogle' 'key' => 'gogle'
)); ));
@ -712,7 +711,7 @@ abstract class QBTest extends Query_TestCase {
public function testCountAll() public function testCountAll()
{ {
$query = $this->db->count_all('test'); $query = self::$db->count_all('test');
$this->assertTrue(is_numeric($query)); $this->assertTrue(is_numeric($query));
} }
@ -721,7 +720,7 @@ abstract class QBTest extends Query_TestCase {
public function testCountAllResults() public function testCountAllResults()
{ {
$query = $this->db->count_all_results('test'); $query = self::$db->count_all_results('test');
$this->assertTrue(is_numeric($query)); $this->assertTrue(is_numeric($query));
} }
@ -730,7 +729,7 @@ abstract class QBTest extends Query_TestCase {
public function testCountAllResults2() public function testCountAllResults2()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->from('test') ->from('test')
->where(' id ', 1) ->where(' id ', 1)
->or_where('key >', 0) ->or_where('key >', 0)
@ -744,9 +743,9 @@ abstract class QBTest extends Query_TestCase {
public function testNumRows() public function testNumRows()
{ {
$query = $this->db->get('test'); $query = self::$db->get('test');
$this->assertTrue(is_numeric($this->db->num_rows())); $this->assertTrue(is_numeric(self::$db->num_rows()));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -755,9 +754,9 @@ abstract class QBTest extends Query_TestCase {
public function testGetCompiledSelect() public function testGetCompiledSelect()
{ {
$sql = $this->db->get_compiled_select('test'); $sql = self::$db->get_compiled_select('test');
$qb_res = $this->db->get('test'); $qb_res = self::$db->get('test');
$sql_res = $this->db->query($sql); $sql_res = self::$db->query($sql);
$this->assertIsA($qb_res,'PDOStatement', "Query Builder Result is a PDO Statement"); $this->assertIsA($qb_res,'PDOStatement', "Query Builder Result is a PDO Statement");
$this->assertIsA($sql_res, 'PDOStatement', "SQL Result is a PDO Statement"); $this->assertIsA($sql_res, 'PDOStatement', "SQL Result is a PDO Statement");
@ -766,7 +765,7 @@ abstract class QBTest extends Query_TestCase {
public function testGetCompiledUpdate() public function testGetCompiledUpdate()
{ {
$sql = $this->db->set(array( $sql = self::$db->set(array(
'id' => 4, 'id' => 4,
'key' => 'foo', 'key' => 'foo',
'val' => 'baz' 'val' => 'baz'
@ -777,7 +776,7 @@ abstract class QBTest extends Query_TestCase {
public function testGetCompiledInsert() public function testGetCompiledInsert()
{ {
$sql = $this->db->set(array( $sql = self::$db->set(array(
'id' => 4, 'id' => 4,
'key' => 'foo', 'key' => 'foo',
'val' => 'baz' 'val' => 'baz'
@ -788,7 +787,7 @@ abstract class QBTest extends Query_TestCase {
public function testGetCompiledDelete() public function testGetCompiledDelete()
{ {
$sql = $this->db->where('id', 4) $sql = self::$db->where('id', 4)
->get_compiled_delete('test'); ->get_compiled_delete('test');
$this->assertTrue(is_string($sql)); $this->assertTrue(is_string($sql));
@ -814,7 +813,7 @@ abstract class QBTest extends Query_TestCase {
try try
{ {
$this->db = Query($params); self::$db = Query($params);
} }
catch(\Query\BadDBDriverException $e) catch(\Query\BadDBDriverException $e)
{ {
@ -828,7 +827,7 @@ abstract class QBTest extends Query_TestCase {
{ {
try try
{ {
$this->db->foo(); self::$db->foo();
} }
catch(BadMethodCallException $e) catch(BadMethodCallException $e)
{ {
@ -840,13 +839,13 @@ abstract class QBTest extends Query_TestCase {
public function testBadNumRows() public function testBadNumRows()
{ {
$this->db->set(array( self::$db->set(array(
'id' => 999, 'id' => 999,
'key' => 'ring', 'key' => 'ring',
'val' => 'sale' 'val' => 'sale'
))->insert('test'); ))->insert('test');
$res = $this->db->num_rows(); $res = self::$db->num_rows();
$this->assertEqual(NULL, $res); $this->assertEqual(NULL, $res);
} }
} }

View File

@ -0,0 +1,81 @@
<?php
class Connection_Manager_Test extends Query_TestCase {
static $instance = NULL;
public static function setUpBeforeClass()
{
self::$instance = Query\Connection_Manager::get_instance();
}
// --------------------------------------------------------------------------
public function testNoClone()
{
$this->setExpectedException('DomainException', "Can't clone singleton");
$clone = clone self::$instance;
}
// --------------------------------------------------------------------------
public function testNoSerialize()
{
$this->setExpectedException('DomainException', "No serializing of singleton");
$string = serialize(self::$instance);
$this->setExpectedException('DomainException', "No serializing of singleton");
$string = self::$instance->__sleep();
}
// --------------------------------------------------------------------------
public function testNoUnserialize()
{
$this->setExpectedException('DomainException', "Can't unserialize singleton");
$obj = self::$instance->__wakeup();
}
// --------------------------------------------------------------------------
public function testParseParams()
{
$params = (object) array(
'type' => 'sqlite',
'file' => ':memory:',
'options' => array(
'foo' => 'bar'
)
);
$expected = array(
':memory:',
'Sqlite',
$params,
array('foo' => 'bar')
);
$this->assertEqual($expected, self::$instance->parse_params($params));
}
// --------------------------------------------------------------------------
public function testConnect()
{
$params = (object) array(
'type' => 'sqlite',
'file' => ':memory:',
'options' => array(
'foo' => 'bar'
)
);
$conn = self::$instance->connect($params);
$this->assertInstanceOf('Query\\Query_Builder', $conn);
// Check that the connection just made is returned from the get_connection method
$this->assertEqual($conn, self::$instance->get_connection());
}
}
// End of connection_manager_test.php

View File

@ -19,19 +19,6 @@
*/ */
class CoreTest extends Query_TestCase { class CoreTest extends Query_TestCase {
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct()
{
parent::__construct();
}
// --------------------------------------------------------------------------
/** /**
* TestPHPVersion function. * TestPHPVersion function.
* *

View File

@ -51,7 +51,8 @@ abstract class DBTest extends Query_TestCase {
public function testBackupData() public function testBackupData()
{ {
$this->assertTrue(is_string(self::$db->util->backup_data(array('create_delete', TRUE)))); $this->assertTrue(is_string(self::$db->get_util()->backup_data(array('create_delete', FALSE))));
$this->assertTrue(is_string(self::$db->get_util()->backup_data(array('create_delete', TRUE))));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -19,13 +19,8 @@
*/ */
class FirebirdQBTest extends QBTest { class FirebirdQBTest extends QBTest {
public function setUp() public static function setUpBeforeClass()
{ {
if ( ! function_exists('\\fbird_connect'))
{
$this->markTestSkipped('Firebird extension does not exist');
}
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB'; $dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
// test the query builder // test the query builder
@ -37,9 +32,19 @@ class FirebirdQBTest extends QBTest {
$params->user = 'SYSDBA'; $params->user = 'SYSDBA';
$params->pass = 'masterkey'; $params->pass = 'masterkey';
$params->prefix = 'create_'; $params->prefix = 'create_';
$this->db = Query($params); self::$db = Query($params);
} }
public function setUp()
{
if ( ! function_exists('\\fbird_connect'))
{
$this->markTestSkipped('Firebird extension does not exist');
}
}
// --------------------------------------------------------------------------
public function testGetNamedConnectionException() public function testGetNamedConnectionException()
{ {
try try
@ -52,13 +57,17 @@ class FirebirdQBTest extends QBTest {
} }
} }
// --------------------------------------------------------------------------
public function testQueryFunctionAlias() public function testQueryFunctionAlias()
{ {
$db = Query(); $db = Query();
$this->assertTrue($this->db === $db); $this->assertTrue(self::$db === $db);
} }
// --------------------------------------------------------------------------
public function testGetNamedConnection() public function testGetNamedConnection()
{ {
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB'; $dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
@ -82,8 +91,8 @@ class FirebirdQBTest extends QBTest {
public function testTypeList() public function testTypeList()
{ {
$sql = $this->db->sql->type_list(); $sql = self::$db->sql->type_list();
$query = $this->db->query($sql); $query = self::$db->query($sql);
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
@ -96,14 +105,14 @@ class FirebirdQBTest extends QBTest {
public function testQueryExplain() public function testQueryExplain()
{ {
$res = $this->db->select('id, key as k, val') $res = self::$db->select('id, key as k, val')
->explain() ->explain()
->where('id >', 1) ->where('id >', 1)
->where('id <', 900) ->where('id <', 900)
->limit(2, 1) ->limit(2, 1)
->get_compiled_select(); ->get_compiled_select();
$res2 = $this->db->select('id, key as k, val') $res2 = self::$db->select('id, key as k, val')
->where('id >', 1) ->where('id >', 1)
->where('id <', 900) ->where('id <', 900)
->limit(2, 1) ->limit(2, 1)
@ -117,7 +126,7 @@ class FirebirdQBTest extends QBTest {
public function testResultErrors() public function testResultErrors()
{ {
$obj = $this->db->query('SELECT * FROM "create_test"'); $obj = self::$db->query('SELECT * FROM "create_test"');
// Test row count // Test row count
$this->assertEqual(0, $obj->rowCount()); $this->assertEqual(0, $obj->rowCount());
@ -136,11 +145,13 @@ class FirebirdQBTest extends QBTest {
$this->assertEqual($expected, $error); $this->assertEqual($expected, $error);
} }
// --------------------------------------------------------------------------
public function testBackupStructure() public function testBackupStructure()
{ {
$existing = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB'; $existing = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
$backup = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_BKP.FDB'; $backup = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_BKP.FDB';
$this->assertTrue($this->db->util->backup_structure($existing, $backup)); $this->assertTrue(self::$db->get_util()->backup_structure($existing, $backup));
} }
} }

View File

@ -29,7 +29,7 @@ class FirebirdTest extends DBtest {
// test the db driver directly // test the db driver directly
self::$db = new \Query\Drivers\Firebird\Driver('localhost:'.$dbpath); self::$db = new \Query\Drivers\Firebird\Driver('localhost:'.$dbpath);
self::$db->table_prefix = 'create_'; self::$db->set_table_prefix('create_');
} }
public function setUp() public function setUp()
@ -108,7 +108,7 @@ class FirebirdTest extends DBtest {
public function testCreateTable() public function testCreateTable()
{ {
//Attempt to create the table //Attempt to create the table
$sql = self::$db->util->create_table('create_delete', array( $sql = self::$db->get_util()->create_table('create_delete', array(
'id' => 'SMALLINT', 'id' => 'SMALLINT',
'key' => 'VARCHAR(64)', 'key' => 'VARCHAR(64)',
'val' => 'BLOB SUB_TYPE TEXT' 'val' => 'BLOB SUB_TYPE TEXT'
@ -124,7 +124,7 @@ class FirebirdTest extends DBtest {
public function testDeleteTable() public function testDeleteTable()
{ {
//Attempt to delete the table //Attempt to delete the table
$sql = self::$db->util->delete_table('create_delete'); $sql = self::$db->get_util()->delete_table('create_delete');
self::$db->query($sql); self::$db->query($sql);
//Check //Check

View File

@ -18,7 +18,7 @@
*/ */
class MySQLQBTest extends QBTest { class MySQLQBTest extends QBTest {
public function setUp() public static function setUpBeforeClass()
{ {
// Attempt to connect, if there is a test config file // Attempt to connect, if there is a test config file
if (is_file(QTEST_DIR . "/settings.json")) if (is_file(QTEST_DIR . "/settings.json"))
@ -42,9 +42,7 @@ class MySQLQBTest extends QBTest {
); );
} }
$this->db = Query($params); self::$db = Query($params);
//echo "Mysql Queries <br />";
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -58,7 +56,7 @@ class MySQLQBTest extends QBTest {
public function testQueryExplain() public function testQueryExplain()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->explain() ->explain()
->where('id >', 1) ->where('id >', 1)
->where('id <', 900) ->where('id <', 900)

View File

@ -38,7 +38,7 @@ class MySQLTest extends DBTest {
self::$db = new \Query\Drivers\Mysql\Driver('host=127.0.0.1;port=3306;dbname=test', 'root'); self::$db = new \Query\Drivers\Mysql\Driver('host=127.0.0.1;port=3306;dbname=test', 'root');
} }
self::$db->table_prefix = 'create_'; self::$db->set_table_prefix('create_');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -62,7 +62,7 @@ class MySQLTest extends DBTest {
self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/mysql.sql')); self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/mysql.sql'));
//Attempt to create the table //Attempt to create the table
$sql = self::$db->util->create_table('test', $sql = self::$db->get_util()->create_table('test',
array( array(
'id' => 'int(10)', 'id' => 'int(10)',
'key' => 'TEXT', 'key' => 'TEXT',
@ -76,7 +76,7 @@ class MySQLTest extends DBTest {
self::$db->query($sql); self::$db->query($sql);
//Attempt to create the table //Attempt to create the table
$sql = self::$db->util->create_table('join', $sql = self::$db->get_util()->create_table('join',
array( array(
'id' => 'int(10)', 'id' => 'int(10)',
'key' => 'TEXT', 'key' => 'TEXT',
@ -198,7 +198,7 @@ SQL;
public function testBackup() public function testBackup()
{ {
$this->assertTrue(is_string(self::$db->util->backup_structure())); $this->assertTrue(is_string(self::$db->get_util()->backup_structure()));
} }

View File

@ -38,15 +38,15 @@ class PDOFirebirdQBTest extends QBTest {
$params->pass = 'masterkey'; $params->pass = 'masterkey';
$params->prefix = 'create_'; $params->prefix = 'create_';
$this->db = Query($params); self::$db = Query($params);
} }
public function testQueryFunctionAlias() public function testQueryFunctionAlias()
{ {
$this->markTestSkipped(); $this->markTestSkipped("Segfault");
$db = Query(); $db = Query();
$this->assertTrue($this->db === $db); $this->assertTrue(self::$db === $db);
} }
public function testGetNamedConnectionException() public function testGetNamedConnectionException()
@ -84,10 +84,10 @@ $this->markTestSkipped();
public function testTypeList() public function testTypeList()
{ {
$this->markTestSkipped(); $this->markTestSkipped("Segfault");
$this->doSetUp(); $this->doSetUp();
$sql = $this->db->get_sql()->type_list(); $sql = self::$db->get_sql()->type_list();
$query = $this->db->query($sql); $query = self::$db->query($sql);
$this->assertIsA('PDOStatement', $query); $this->assertIsA('PDOStatement', $query);
@ -100,14 +100,14 @@ $this->markTestSkipped();
public function testQueryExplain() public function testQueryExplain()
{ {
$res = $this->db->select('id, key as k, val') $res = self::$db->select('id, key as k, val')
->explain() ->explain()
->where('id >', 1) ->where('id >', 1)
->where('id <', 900) ->where('id <', 900)
->limit(2, 1) ->limit(2, 1)
->get_compiled_select(); ->get_compiled_select();
$res2 = $this->db->select('id, key as k, val') $res2 = self::$db->select('id, key as k, val')
->where('id >', 1) ->where('id >', 1)
->where('id <', 900) ->where('id <', 900)
->limit(2, 1) ->limit(2, 1)

View File

@ -29,7 +29,7 @@ class PDOFirebirdTest extends DBtest {
// test the db driver directly // test the db driver directly
self::$db = new \Query\Drivers\Pdo_firebird\Driver('firebird:host=localhost;dbname='.$dbpath); self::$db = new \Query\Drivers\Pdo_firebird\Driver('firebird:host=localhost;dbname='.$dbpath);
self::$db->table_prefix = 'create_'; self::$db->set_table_prefix('create_');
} }
public function setUp() public function setUp()
@ -83,6 +83,11 @@ class PDOFirebirdTest extends DBtest {
$this->assertTrue($only_system); $this->assertTrue($only_system);
} }
public function testBackupStructure()
{
$this->assertNull(self::$db->get_util()->backup_structure());
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Create / Delete Tables // ! Create / Delete Tables
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -91,7 +96,7 @@ class PDOFirebirdTest extends DBtest {
{ {
$this->markTestSkipped(); $this->markTestSkipped();
//Attempt to create the table //Attempt to create the table
$sql = self::$db->util->create_table('create_delete', array( $sql = self::$db->get_util()->create_table('create_delete', array(
'id' => 'SMALLINT', 'id' => 'SMALLINT',
'key' => 'VARCHAR(64)', 'key' => 'VARCHAR(64)',
'val' => 'BLOB SUB_TYPE TEXT' 'val' => 'BLOB SUB_TYPE TEXT'
@ -108,7 +113,7 @@ $this->markTestSkipped();
{ {
$this->markTestSkipped(); $this->markTestSkipped();
//Attempt to delete the table //Attempt to delete the table
$sql = self::$db->util->delete_table('create_delete'); $sql = self::$db->get_util()->delete_table('create_delete');
self::$db->query($sql); self::$db->query($sql);
//Check //Check

View File

@ -17,14 +17,8 @@
*/ */
class PgSQLQBTest extends QBTest { class PgSQLQBTest extends QBTest {
public function setUp() public static function setUpBeforeClass()
{ {
// If the database isn't installed, skip the tests
if ( ! in_array('pgsql', PDO::getAvailableDrivers()))
{
$this->markTestSkipped("Postgres extension for PDO not loaded");
}
// Attempt to connect, if there is a test config file // Attempt to connect, if there is a test config file
if (is_file(QTEST_DIR . "/settings.json")) if (is_file(QTEST_DIR . "/settings.json"))
{ {
@ -49,7 +43,16 @@ class PgSQLQBTest extends QBTest {
); );
} }
$this->db = Query($params); self::$db = Query($params);
}
public function setUp()
{
// If the database isn't installed, skip the tests
if ( ! in_array('pgsql', PDO::getAvailableDrivers()))
{
$this->markTestSkipped("Postgres extension for PDO not loaded");
}
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -68,7 +71,7 @@ class PgSQLQBTest extends QBTest {
$this->markTestSkipped("Skip this test on CI, because the check is Postgres version dependent"); $this->markTestSkipped("Skip this test on CI, because the check is Postgres version dependent");
} }
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->explain() ->explain()
->where('id >', 1) ->where('id >', 1)
->where('id <', 900) ->where('id <', 900)
@ -105,6 +108,6 @@ class PgSQLQBTest extends QBTest {
public function testBackupStructure() public function testBackupStructure()
{ {
$this->assertEquals('', $this->db->util->backup_structure()); $this->assertEquals('', self::$db->util->backup_structure());
} }
} }

View File

@ -49,7 +49,7 @@ class PgTest extends DBTest {
self::$db = new $class('host=127.0.0.1;port=5432;dbname=test', 'postgres'); self::$db = new $class('host=127.0.0.1;port=5432;dbname=test', 'postgres');
} }
self::$db->table_prefix = 'create_'; self::$db->set_table_prefix('create_');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -83,7 +83,7 @@ class PgTest extends DBTest {
//Attempt to create the table //Attempt to create the table
$sql = self::$db->util->create_table('create_test', $sql = self::$db->get_util()->create_table('create_test',
array( array(
'id' => 'integer', 'id' => 'integer',
'key' => 'TEXT', 'key' => 'TEXT',
@ -97,7 +97,7 @@ class PgTest extends DBTest {
self::$db->query($sql); self::$db->query($sql);
//Attempt to create the table //Attempt to create the table
$sql = self::$db->util->create_table('create_join', $sql = self::$db->get_util()->create_table('create_join',
array( array(
'id' => 'integer', 'id' => 'integer',
'key' => 'TEXT', 'key' => 'TEXT',

View File

@ -19,11 +19,10 @@
*/ */
class SQLiteQBTest extends QBTest { class SQLiteQBTest extends QBTest {
public function setUp() public static function setUpBeforeClass()
{ {
// Set up in the bootstrap to mitigate // Defined in the SQLiteTest.php file
// connection locking issues self::$db = Query('test_sqlite');
$this->db = Query('test_sqlite');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -32,14 +31,14 @@
{ {
$db = Query('test_sqlite'); $db = Query('test_sqlite');
$this->assertTrue($this->db === $db, "Alias passed into query function gives the original object back"); $this->assertTrue(self::$db === $db, "Alias passed into query function gives the original object back");
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testQueryExplain() public function testQueryExplain()
{ {
$query = $this->db->select('id, key as k, val') $query = self::$db->select('id, key as k, val')
->explain() ->explain()
->where('id >', 1) ->where('id >', 1)
->where('id <', 900) ->where('id <', 900)

View File

@ -34,7 +34,7 @@ class SQLiteTest extends DBTest {
); );
self::$db = Query($params); self::$db = Query($params);
self::$db->table_prefix = 'create_'; self::$db->set_table_prefix('create_');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -61,7 +61,7 @@ class SQLiteTest extends DBTest {
/*public function testBackupData() /*public function testBackupData()
{ {
$sql = mb_trim(self::$db->util->backup_data(array('create_join', 'create_test'))); $sql = mb_trim(self::$db->get_util()->backup_data(array('create_join', 'create_test')));
$sql_array = explode("\n", $sql); $sql_array = explode("\n", $sql);
@ -80,7 +80,7 @@ SQL;
public function testBackupStructure() public function testBackupStructure()
{ {
$sql = mb_trim(self::$db->util->backup_structure()); $sql = mb_trim(self::$db->get_util()->backup_structure());
$expected = <<<SQL $expected = <<<SQL
CREATE TABLE "create_test" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT); CREATE TABLE "create_test" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
CREATE TABLE "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT); CREATE TABLE "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
@ -156,7 +156,7 @@ SQL;
public function testDeleteTable() public function testDeleteTable()
{ {
$sql = self::$db->util->delete_table('create_delete'); $sql = self::$db->get_util()->delete_table('create_delete');
self::$db->query($sql); self::$db->query($sql);

View File

@ -32,7 +32,7 @@ if ( ! defined('IS_QUERCUS'))
// Include simpletest // Include simpletest
// it has to be in the tests folder // it has to be in the tests folder
require_once('/htdocs/__lib/simpletest/autorun.php'); require_once('simpletest/autorun.php');
/** /**
* Base class for TestCases * Base class for TestCases
@ -51,6 +51,16 @@ abstract class Query_TestCase extends UnitTestCase {
parent::__construct(); parent::__construct();
} }
public function __destruct()
{
$class = get_class($this);
if (method_exists($class, 'tearDownAfterClass'))
{
$class::tearDownAfterClass();
}
}
/** /**
* Define assertInstanceOf for simpletest * Define assertInstanceOf for simpletest
* *
@ -84,6 +94,18 @@ abstract class Query_TestCase extends UnitTestCase {
{ {
$this->skipUnless(FALSE, $message); $this->skipUnless(FALSE, $message);
} }
/**
* Alias for phpunit method
*
* @param string $name
* @param string $message
* @param int $code
*/
public function setExpectedException($name, $message='', $code=NULL)
{
$this->expectException($name);
}
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -104,8 +126,10 @@ $test_path = QTEST_DIR.'/databases/';
// Require base testing classes // Require base testing classes
require_once(QTEST_DIR . '/core/core.php'); require_once(QTEST_DIR . '/core/core.php');
require_once(QTEST_DIR . '/core/connection_manager_test.php');
require_once(QTEST_DIR . '/core/db_test.php'); require_once(QTEST_DIR . '/core/db_test.php');
require_once(QTEST_DIR . '/core/db_qb_test.php'); //require_once(QTEST_DIR . '/core/query_parser_test.php');
require_once(QTEST_DIR . '/core/base_query_builder_test.php');
$drivers = PDO::getAvailableDrivers(); $drivers = PDO::getAvailableDrivers();