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);
$file = QBASE_PATH . "{$path}.php";
// @codeCoverageIgnoreStart
if (file_exists($file))
{
require_once($file);
}
// @codeCoverageIgnoreEnd
if (file_exists($file)) require_once($file);
});
// End of autoload.php

View File

@ -13,6 +13,7 @@
<testsuite name="CoreTests">
<file>tests/core/core.php</file>
<file>tests/core/db_qp_test.php</file>
<file>tests/core/connection_manager_test.php</file>
</testsuite>
<testsuite name="MySQLTests">
<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
* @var Abstract_Util
*/
public $util;
protected $util;
/**
* Last query executed
* @var string
*/
public $last_query;
protected $last_query;
/**
* Prefix to apply to table names
* @var string
*/
public $table_prefix = '';
protected $table_prefix = '';
/**
* 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
*

View File

@ -229,10 +229,10 @@ abstract class Abstract_Query_Builder {
// Escape the identifiers
$field = $this->db->quote_ident($field);
$as = ($as !== FALSE)
? $this->db->quote_ident($as)
: $field;
if ( ! is_string($as)) return $field;
$as = $this->db->quote_ident($as);
return "({$field}) AS {$as} ";
}
@ -519,7 +519,7 @@ abstract class Abstract_Query_Builder {
$this->queries['total_time'] += $total_time;
// 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
* @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
* @codeCoverageIgnore
* @throws \DomainException
*/
private function __wakeup()
public function __wakeup()
{
throw new \DomainException("Can't unserialize singleton");
}
@ -74,12 +87,7 @@ final class Connection_Manager {
*/
public static function get_instance()
{
// @codeCoverageIgnoreStart
if (self::$instance === null)
{
self::$instance = new self();
}
// @codeCoverageIgnoreEnd
if (self::$instance === null) self::$instance = new self();
return self::$instance;
}
@ -132,7 +140,7 @@ final class Connection_Manager {
// Set the table prefix, if it exists
if (isset($params->prefix))
{
$db->table_prefix = $params->prefix;
$db->set_table_prefix($params->prefix);
}
// Create Query Builder object
@ -161,7 +169,7 @@ final class Connection_Manager {
* @return array
* @throws BadDBDriverException
*/
private function parse_params(\stdClass $params)
public function parse_params(\stdClass $params)
{
$params->type = strtolower($params->type);
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';

View File

@ -30,7 +30,6 @@ interface Driver_Interface {
* @param string $username
* @param string $password
* @param array $driver_options
* @return void
*/
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);
// 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);
$this->statement = new Result($this->statement_link, $this);

View File

@ -258,17 +258,7 @@ class Result extends \PDOStatement {
*/
public function rowCount()
{
$rows = \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;
return \fbird_affected_rows();
}
// --------------------------------------------------------------------------

View File

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

View File

@ -33,6 +33,7 @@ class Driver extends \Query\Abstract_Driver {
/**
* Connect to MySQL Database
*
* @codeCoverageIgnore
* @param string $dsn
* @param string $username
* @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
* @codeCoverageIgnore
* @param string $db_path
* @param string $new_file
* @return string

View File

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

View File

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

View File

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

View File

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

View File

@ -93,13 +93,16 @@ define('QTEST_DIR', realpath(dirname(__FILE__)));
define('QBASE_DIR', realpath(QTEST_DIR.'/../') . '/');
define('QDS', DIRECTORY_SEPARATOR);
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
@unlink($path);
// Include db classes
require_once(QBASE_DIR . 'autoload.php');
// Require base testing classes
require_once(QTEST_DIR . '/core/core.php');
require_once(QTEST_DIR . '/core/db_test.php');
require_once(QTEST_DIR . '/core/db_qp_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');
// End of bootstrap.php

View File

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

View File

@ -51,7 +51,8 @@ abstract class DBTest extends Query_TestCase {
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 {
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';
// test the query builder
@ -37,9 +32,19 @@ class FirebirdQBTest extends QBTest {
$params->user = 'SYSDBA';
$params->pass = 'masterkey';
$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()
{
try
@ -52,13 +57,17 @@ class FirebirdQBTest extends QBTest {
}
}
// --------------------------------------------------------------------------
public function testQueryFunctionAlias()
{
$db = Query();
$this->assertTrue($this->db === $db);
$this->assertTrue(self::$db === $db);
}
// --------------------------------------------------------------------------
public function testGetNamedConnection()
{
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
@ -82,8 +91,8 @@ class FirebirdQBTest extends QBTest {
public function testTypeList()
{
$sql = $this->db->sql->type_list();
$query = $this->db->query($sql);
$sql = self::$db->sql->type_list();
$query = self::$db->query($sql);
$this->assertIsA($query, 'PDOStatement');
@ -96,14 +105,14 @@ class FirebirdQBTest extends QBTest {
public function testQueryExplain()
{
$res = $this->db->select('id, key as k, val')
$res = self::$db->select('id, key as k, val')
->explain()
->where('id >', 1)
->where('id <', 900)
->limit(2, 1)
->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 <', 900)
->limit(2, 1)
@ -117,7 +126,7 @@ class FirebirdQBTest extends QBTest {
public function testResultErrors()
{
$obj = $this->db->query('SELECT * FROM "create_test"');
$obj = self::$db->query('SELECT * FROM "create_test"');
// Test row count
$this->assertEqual(0, $obj->rowCount());
@ -136,11 +145,13 @@ class FirebirdQBTest extends QBTest {
$this->assertEqual($expected, $error);
}
// --------------------------------------------------------------------------
public function testBackupStructure()
{
$existing = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.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
self::$db = new \Query\Drivers\Firebird\Driver('localhost:'.$dbpath);
self::$db->table_prefix = 'create_';
self::$db->set_table_prefix('create_');
}
public function setUp()
@ -108,7 +108,7 @@ class FirebirdTest extends DBtest {
public function testCreateTable()
{
//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',
'key' => 'VARCHAR(64)',
'val' => 'BLOB SUB_TYPE TEXT'
@ -124,7 +124,7 @@ class FirebirdTest extends DBtest {
public function testDeleteTable()
{
//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);
//Check

View File

@ -18,9 +18,9 @@
*/
class MySQLQBTest extends QBTest {
public function setUp()
{
// Attempt to connect, if there is a test config file
public static function setUpBeforeClass()
{
// Attempt to connect, if there is a test config file
if (is_file(QTEST_DIR . "/settings.json"))
{
$params = json_decode(file_get_contents(QTEST_DIR . "/settings.json"));
@ -42,10 +42,8 @@ class MySQLQBTest extends QBTest {
);
}
$this->db = Query($params);
//echo "Mysql Queries <br />";
}
self::$db = Query($params);
}
// --------------------------------------------------------------------------
@ -58,7 +56,7 @@ class MySQLQBTest extends QBTest {
public function testQueryExplain()
{
$query = $this->db->select('id, key as k, val')
$query = self::$db->select('id, key as k, val')
->explain()
->where('id >', 1)
->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->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'));
//Attempt to create the table
$sql = self::$db->util->create_table('test',
$sql = self::$db->get_util()->create_table('test',
array(
'id' => 'int(10)',
'key' => 'TEXT',
@ -76,7 +76,7 @@ class MySQLTest extends DBTest {
self::$db->query($sql);
//Attempt to create the table
$sql = self::$db->util->create_table('join',
$sql = self::$db->get_util()->create_table('join',
array(
'id' => 'int(10)',
'key' => 'TEXT',
@ -198,7 +198,7 @@ SQL;
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->prefix = 'create_';
$this->db = Query($params);
self::$db = Query($params);
}
public function testQueryFunctionAlias()
{
$this->markTestSkipped();
$this->markTestSkipped("Segfault");
$db = Query();
$this->assertTrue($this->db === $db);
$this->assertTrue(self::$db === $db);
}
public function testGetNamedConnectionException()
@ -84,10 +84,10 @@ $this->markTestSkipped();
public function testTypeList()
{
$this->markTestSkipped();
$this->markTestSkipped("Segfault");
$this->doSetUp();
$sql = $this->db->get_sql()->type_list();
$query = $this->db->query($sql);
$sql = self::$db->get_sql()->type_list();
$query = self::$db->query($sql);
$this->assertIsA('PDOStatement', $query);
@ -100,14 +100,14 @@ $this->markTestSkipped();
public function testQueryExplain()
{
$res = $this->db->select('id, key as k, val')
$res = self::$db->select('id, key as k, val')
->explain()
->where('id >', 1)
->where('id <', 900)
->limit(2, 1)
->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 <', 900)
->limit(2, 1)

View File

@ -29,7 +29,7 @@ class PDOFirebirdTest extends DBtest {
// test the db driver directly
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()
@ -83,6 +83,11 @@ class PDOFirebirdTest extends DBtest {
$this->assertTrue($only_system);
}
public function testBackupStructure()
{
$this->assertNull(self::$db->get_util()->backup_structure());
}
// --------------------------------------------------------------------------
// ! Create / Delete Tables
// --------------------------------------------------------------------------
@ -91,7 +96,7 @@ class PDOFirebirdTest extends DBtest {
{
$this->markTestSkipped();
//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',
'key' => 'VARCHAR(64)',
'val' => 'BLOB SUB_TYPE TEXT'
@ -108,7 +113,7 @@ $this->markTestSkipped();
{
$this->markTestSkipped();
//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);
//Check

View File

@ -17,15 +17,9 @@
*/
class PgSQLQBTest extends QBTest {
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");
}
// Attempt to connect, if there is a test config file
public static function setUpBeforeClass()
{
// Attempt to connect, if there is a test config file
if (is_file(QTEST_DIR . "/settings.json"))
{
$params = json_decode(file_get_contents(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");
}
$query = $this->db->select('id, key as k, val')
$query = self::$db->select('id, key as k, val')
->explain()
->where('id >', 1)
->where('id <', 900)
@ -105,6 +108,6 @@ class PgSQLQBTest extends QBTest {
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->table_prefix = 'create_';
self::$db->set_table_prefix('create_');
}
// --------------------------------------------------------------------------
@ -83,7 +83,7 @@ class PgTest extends DBTest {
//Attempt to create the table
$sql = self::$db->util->create_table('create_test',
$sql = self::$db->get_util()->create_table('create_test',
array(
'id' => 'integer',
'key' => 'TEXT',
@ -97,7 +97,7 @@ class PgTest extends DBTest {
self::$db->query($sql);
//Attempt to create the table
$sql = self::$db->util->create_table('create_join',
$sql = self::$db->get_util()->create_table('create_join',
array(
'id' => 'integer',
'key' => 'TEXT',

View File

@ -19,12 +19,11 @@
*/
class SQLiteQBTest extends QBTest {
public function setUp()
{
// Set up in the bootstrap to mitigate
// connection locking issues
$this->db = Query('test_sqlite');
}
public static function setUpBeforeClass()
{
// Defined in the SQLiteTest.php file
self::$db = Query('test_sqlite');
}
// --------------------------------------------------------------------------
@ -32,14 +31,14 @@
{
$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()
{
$query = $this->db->select('id, key as k, val')
$query = self::$db->select('id, key as k, val')
->explain()
->where('id >', 1)
->where('id <', 900)

View File

@ -34,7 +34,7 @@ class SQLiteTest extends DBTest {
);
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()
{
$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);
@ -80,7 +80,7 @@ SQL;
public function testBackupStructure()
{
$sql = mb_trim(self::$db->util->backup_structure());
$sql = mb_trim(self::$db->get_util()->backup_structure());
$expected = <<<SQL
CREATE TABLE "create_test" ("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()
{
$sql = self::$db->util->delete_table('create_delete');
$sql = self::$db->get_util()->delete_table('create_delete');
self::$db->query($sql);

View File

@ -32,7 +32,7 @@ if ( ! defined('IS_QUERCUS'))
// Include simpletest
// it has to be in the tests folder
require_once('/htdocs/__lib/simpletest/autorun.php');
require_once('simpletest/autorun.php');
/**
* Base class for TestCases
@ -51,6 +51,16 @@ abstract class Query_TestCase extends UnitTestCase {
parent::__construct();
}
public function __destruct()
{
$class = get_class($this);
if (method_exists($class, 'tearDownAfterClass'))
{
$class::tearDownAfterClass();
}
}
/**
* Define assertInstanceOf for simpletest
*
@ -84,6 +94,18 @@ abstract class Query_TestCase extends UnitTestCase {
{
$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_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_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();