Fix Firebird tests and speed up tests by using one database connection for each query builder datbase test

This commit is contained in:
Timothy Warren 2015-07-17 15:38:06 -04:00
parent 6b20982f61
commit 28f5cb2934
16 changed files with 246 additions and 223 deletions

View File

@ -1,3 +1,5 @@
sudo: false
language: php
php:
@ -15,7 +17,7 @@ before_script:
script:
- mkdir -p build/logs
- cd tests && phpunit --coverage-clover build/logs/clover.xml
- phpunit --coverage-clover build/logs/clover.xml
after_script:
- wget https://scrutinizer-ci.com/ocular.phar

View File

@ -41,13 +41,13 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
/**
* Reference to sql class
* @var SQL\SQL_Interface
* @var SQL_Interface
*/
public $sql;
protected $sql;
/**
* Reference to util class
* @var Util\Abstract_Util
* @var Abstract_Util
*/
public $util;

View File

@ -123,7 +123,6 @@ final class Connection_Manager {
$dbtype = ucfirst($dbtype);
$driver = "\\Query\\Drivers\\{$dbtype}\\Driver";
//echo $driver . "\n";
// Create the database connection
$db = ( ! empty($params->user))
@ -158,7 +157,7 @@ final class Connection_Manager {
/**
* Parses params into a dsn and option array
*
* @param \stdClass $params
* @param \stdClass $set_params
* @return array
* @throws BadDBDriverException
*/
@ -183,7 +182,15 @@ final class Connection_Manager {
}
// Create the dsn for the database to connect to
$dsn = $this->create_dsn($dbtype, $params);
if (strtolower($dbtype) === 'firebird')
{
$dsn = "{$params->host}:{$params->file}";
}
else
{
$dsn = $this->create_dsn($dbtype, $params);
}
return array($dsn, $dbtype, $params, $options);
}
@ -199,10 +206,14 @@ final class Connection_Manager {
*/
private function create_dsn($dbtype, \stdClass $params)
{
if ($dbtype === 'firebird') $dsn = "{$params->host}:{$params->file}";
elseif ($dbtype === 'sqlite') $dsn = $params->file;
if ($dbtype === 'sqlite')
{
$dsn = $params->file;
}
else
{
if (strtolower($dbtype) === 'pdo_firebird') $dbtype = 'firebird';
$dsn = strtolower($dbtype) . ':';
if ( ! empty($params->database))
@ -224,7 +235,7 @@ final class Connection_Manager {
foreach($params as $key => $val)
{
if ( ! isset($skip[$key]))
if (( ! array_key_exists($key, $skip)) && ! empty($val))
{
$dsn .= ";{$key}={$val}";
}

View File

@ -80,7 +80,6 @@ class Driver extends \Query\Abstract_Driver {
*/
public function __construct($dbpath, $user='SYSDBA', $pass='masterkey', array $options = array())
{
$connect_function = (isset($options[\PDO::ATTR_PERSISTENT]) && $options[\PDO::ATTR_PERSISTENT] == TRUE)
? '\\fbird_pconnect'
: '\\fbird_connect';
@ -195,7 +194,7 @@ class Driver extends \Query\Abstract_Driver {
$err_string = \fbird_errmsg() . "Last query:" . $this->last_query;
if ($this->statement_link === FALSE) throw new \PDOException($err_string, \fbird_errcode(), NULL);
$this->statement = new FireBird_Result($this->statement_link, $this);
$this->statement = new Result($this->statement_link, $this);
return $this->statement;
}
@ -217,7 +216,7 @@ class Driver extends \Query\Abstract_Driver {
// Throw the error as an exception
if ($this->statement_link === FALSE) throw new \PDOException(\fbird_errmsg(), \fbird_errcode(), NULL);
$this->statement = new FireBird_Result($this->statement_link, $this);
$this->statement = new Result($this->statement_link, $this);
return $this->statement;
}

View File

@ -60,7 +60,7 @@ class Result extends \PDOStatement {
* @param \Query\Driver\Firebird|null $db
* @return void
*/
public function __construct($link, Firebird $db = NULL)
public function __construct($link, Driver $db = NULL)
{
if ( ! is_null($db)) $this->db = $db;
$this->statement = $link;

View File

@ -13,7 +13,7 @@
// --------------------------------------------------------------------------
namespace Query\Drivers\Util;
namespace Query\Drivers\Firebird;
/**
* Firebird-specific backup, import and creation methods
@ -21,7 +21,7 @@ namespace Query\Drivers\Util;
* @package Query
* @subpackage Drivers
*/
class Firebird extends \Query\Abstract_Util {
class Util extends \Query\Abstract_Util {
/**
* Convenience public function to generate sql for creating a db table

40
phpunit.xml Normal file
View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
stopOnFailure="false"
bootstrap="tests/bootstrap.php">
<filter>
<blacklist>
<directory suffix=".php">coverage</directory>
<directory suffix=".php">docs</directory>
<directory suffix=".php">tests</directory>
</blacklist>
</filter>
<testsuites>
<testsuite name="CoreTests">
<file>tests/core/core.php</file>
<file>tests/core/db_qp_test.php</file>
</testsuite>
<testsuite name="MySQLTests">
<file>tests/databases/mysql/MySQLTest.php</file>
<file>tests/databases/mysql/MySQLQBTest.php</file>
</testsuite>
<testsuite name="PgSQLTests">
<file>tests/databases/pgsql/PgSQLTest.php</file>
<file>tests/databases/pgsql/PgSQLQBTest.php</file>
</testsuite>
<testsuite name="SQLiteTests">
<file>tests/databases/sqlite/SQLiteTest.php</file>
<file>tests/databases/sqlite/SQLiteQBTest.php</file>
</testsuite>
<testsuite name="FirebirdTests">
<file>tests/databases/firebird/FirebirdTest.php</file>
<file>tests/databases/firebird/FirebirdQBTest.php</file>
</testsuite>
<!-- <testsuite name="PDOFirebirdTests">
<file>tests/databases/pdo_firebird/PDOFirebirdTest.php</file>
<file>tests/databases/pdo_firebird/PDOFirebirdQBTest.php</file>
</testsuite> -->
</testsuites>
</phpunit>

View File

@ -102,23 +102,4 @@ 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');
// Preset SQLite connection, so there aren't locking issues
if (extension_loaded('pdo_sqlite'))
{
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
@unlink($path);
$params = array(
'type' => 'sqlite',
'file' => ':memory:',
'host' => 'localhost',
'prefix' => 'create_',
'alias' => 'test_sqlite',
'options' => array(
PDO::ATTR_PERSISTENT => TRUE
)
);
Query($params);
}
// End of bootstrap.php

View File

@ -18,20 +18,22 @@
*/
abstract class DBTest extends Query_TestCase {
protected static $db = NULL;
abstract public function testConnection();
// --------------------------------------------------------------------------
public function tearDown()
public static function tearDownAfterClass()
{
$this->db = NULL;
self::$db = NULL;
}
// --------------------------------------------------------------------------
public function testGetTables()
{
$tables = $this->db->get_tables();
$tables = self::$db->get_tables();
$this->assertTrue(is_array($tables));
$this->assertTrue( ! empty($tables));
}
@ -40,7 +42,7 @@ abstract class DBTest extends Query_TestCase {
public function testGetSystemTables()
{
$tables = $this->db->get_system_tables();
$tables = self::$db->get_system_tables();
$this->assertTrue(is_array($tables));
$this->assertTrue( ! empty($tables));
}
@ -49,14 +51,14 @@ abstract class DBTest extends Query_TestCase {
public function testBackupData()
{
$this->assertTrue(is_string($this->db->util->backup_data(array('create_delete', TRUE))));
$this->assertTrue(is_string(self::$db->util->backup_data(array('create_delete', TRUE))));
}
// --------------------------------------------------------------------------
public function testGetColumns()
{
$cols = $this->db->get_columns('test');
$cols = self::$db->get_columns('test');
$this->assertTrue(is_array($cols));
$this->assertTrue( ! empty($cols));
}
@ -65,7 +67,7 @@ abstract class DBTest extends Query_TestCase {
public function testGetTypes()
{
$types = $this->db->get_types();
$types = self::$db->get_types();
$this->assertTrue(is_array($types));
$this->assertTrue( ! empty($types));
}
@ -82,7 +84,7 @@ abstract class DBTest extends Query_TestCase {
'delete' => 'CASCADE'
));
$keys = $this->db->get_fks('testconstraints2');
$keys = self::$db->get_fks('testconstraints2');
$this->assertEqual($expected, $keys);
}
@ -90,7 +92,7 @@ abstract class DBTest extends Query_TestCase {
public function testGetIndexes()
{
$keys = $this->db->get_indexes('test');
$keys = self::$db->get_indexes('test');
$this->assertTrue(is_array($keys));
}
@ -98,7 +100,7 @@ abstract class DBTest extends Query_TestCase {
public function testGetViews()
{
$views = $this->db->get_views();
$views = self::$db->get_views();
$expected = array('numbersview', 'testview');
$this->assertEqual($expected, array_values($views));
$this->assertTrue(is_array($views));
@ -110,7 +112,7 @@ abstract class DBTest extends Query_TestCase {
{
// @TODO standardize trigger output for different databases
$triggers = $this->db->get_triggers();
$triggers = self::$db->get_triggers();
$this->assertTrue(is_array($triggers));
}
@ -118,7 +120,7 @@ abstract class DBTest extends Query_TestCase {
public function testGetSequences()
{
$seqs = $this->db->get_sequences();
$seqs = self::$db->get_sequences();
// Normalize sequence names
$seqs = array_map('strtolower', $seqs);
@ -133,7 +135,7 @@ abstract class DBTest extends Query_TestCase {
public function testGetProcedures()
{
$procedures = $this->db->get_procedures();
$procedures = self::$db->get_procedures();
$this->assertTrue(is_array($procedures));
}
@ -141,7 +143,7 @@ abstract class DBTest extends Query_TestCase {
public function testGetFunctions()
{
$funcs = $this->db->get_functions();
$funcs = self::$db->get_functions();
$this->assertTrue(is_array($funcs));
}
}

View File

@ -21,13 +21,13 @@ class FirebirdQBTest extends QBTest {
public function setUp()
{
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
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
$params = new Stdclass();
$params->alias = 'fire';
@ -44,7 +44,7 @@ class FirebirdQBTest extends QBTest {
{
try
{
$db = Query('fire');
$db = Query('water');
}
catch(InvalidArgumentException $e)
{
@ -52,13 +52,20 @@ class FirebirdQBTest extends QBTest {
}
}
public function testQueryFunctionAlias()
{
$db = Query();
$this->assertTrue($this->db === $db);
}
public function testGetNamedConnection()
{
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
// test the query builder
$params = new Stdclass();
$params->alias = 'fire';
$params->alias = 'wood';
$params->type = 'firebird';
$params->file = $dbpath;
$params->host = 'localhost';
@ -66,7 +73,7 @@ class FirebirdQBTest extends QBTest {
$params->pass = 'masterkey';
$params->prefix = '';
$f_conn = Query($params);
$q_conn = Query('fire');
$q_conn = Query('wood');
$this->assertReference($f_conn, $q_conn);
}
@ -131,7 +138,6 @@ class FirebirdQBTest extends QBTest {
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';

View File

@ -23,26 +23,29 @@
*/
class FirebirdTest extends DBtest {
public function setUp()
public static function setupBeforeClass()
{
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
// test the db driver directly
self::$db = new \Query\Drivers\Firebird\Driver('localhost:'.$dbpath);
self::$db->table_prefix = 'create_';
}
public function setUp()
{
if ( ! function_exists('\\fbird_connect'))
{
$this->markTestSkipped('Firebird extension does not exist');
}
// test the db driver directly
$this->db = new \Query\Drivers\Firebird\Driver('localhost:'.$dbpath);
$this->db->table_prefix = 'create_';
$this->tables = $this->db->get_tables();
$this->tables = self::$db->get_tables();
}
// --------------------------------------------------------------------------
public function tearDown()
{
unset($this->db);
unset($this->tables);
}
@ -53,7 +56,7 @@ class FirebirdTest extends DBtest {
*/
public function testNullResultMethods()
{
$obj = $this->db->query('SELECT "id" FROM "create_test"');
$obj = self::$db->query('SELECT "id" FROM "create_test"');
$val = "bar";
@ -75,7 +78,7 @@ class FirebirdTest extends DBtest {
public function testConnection()
{
$this->assertIsA($this->db, '\\Query\\Drivers\\Firebird\\Driver');
$this->assertIsA(self::$db, '\\Query\\Drivers\\Firebird\\Driver');
}
// --------------------------------------------------------------------------
@ -84,7 +87,7 @@ class FirebirdTest extends DBtest {
{
$only_system = TRUE;
$tables = $this->db->get_system_tables();
$tables = self::$db->get_system_tables();
foreach($tables as $t)
{
@ -105,15 +108,15 @@ class FirebirdTest extends DBtest {
public function testCreateTable()
{
//Attempt to create the table
$sql = $this->db->util->create_table('create_delete', array(
$sql = self::$db->util->create_table('create_delete', array(
'id' => 'SMALLINT',
'key' => 'VARCHAR(64)',
'val' => 'BLOB SUB_TYPE TEXT'
));
$this->db->query($sql);
self::$db->query($sql);
//Check
$this->assertTrue(in_array('create_delete', $this->db->get_tables()));
$this->assertTrue(in_array('create_delete', self::$db->get_tables()));
}
// --------------------------------------------------------------------------
@ -121,11 +124,11 @@ class FirebirdTest extends DBtest {
public function testDeleteTable()
{
//Attempt to delete the table
$sql = $this->db->util->delete_table('create_delete');
$this->db->query($sql);
$sql = self::$db->util->delete_table('create_delete');
self::$db->query($sql);
//Check
$table_exists = in_array('create_delete', $this->db->get_tables());
$table_exists = in_array('create_delete', self::$db->get_tables());
$this->assertFalse($table_exists);
}
@ -133,21 +136,21 @@ class FirebirdTest extends DBtest {
public function testTruncate()
{
$this->db->truncate('create_test');
self::$db->truncate('create_test');
$this->assertTrue($this->db->affected_rows() > 0);
$this->assertTrue(self::$db->affected_rows() > 0);
}
// --------------------------------------------------------------------------
public function testCommitTransaction()
{
$res = $this->db->beginTransaction();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
$this->db->query($sql);
self::$db->query($sql);
$res = $this->db->commit();
$res = self::$db->commit();
$this->assertTrue($res);
}
@ -155,12 +158,12 @@ class FirebirdTest extends DBtest {
public function testRollbackTransaction()
{
$res = $this->db->beginTransaction();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
$this->db->query($sql);
self::$db->query($sql);
$res = $this->db->rollback();
$res = self::$db->rollback();
$this->assertTrue($res);
}
@ -172,7 +175,7 @@ class FirebirdTest extends DBtest {
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$query = $this->db->prepare($sql);
$query = self::$db->prepare($sql);
$query->execute(array(1,"booger's", "Gross"));
}
@ -185,7 +188,7 @@ SQL;
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
self::$db->prepare_execute($sql, array(
2, "works", 'also?'
));
@ -195,7 +198,7 @@ SQL;
public function testFetch()
{
$res = $this->db->query('SELECT "key","val" FROM "create_test"');
$res = self::$db->query('SELECT "key","val" FROM "create_test"');
// Object
$fetchObj = $res->fetchObject();
@ -206,7 +209,7 @@ SQL;
$this->assertTrue(array_key_exists('key', $fetchAssoc));
// Numeric array
$res2 = $this->db->query('SELECT "id","key","val" FROM "create_test"');
$res2 = self::$db->query('SELECT "id","key","val" FROM "create_test"');
$fetch = $res2->fetch(PDO::FETCH_NUM);
$this->assertTrue(is_array($fetch));
}
@ -215,14 +218,14 @@ SQL;
public function testPrepareQuery()
{
$this->assertNull($this->db->prepare_query('', array()));
$this->assertNull(self::$db->prepare_query('', array()));
}
// --------------------------------------------------------------------------
public function testErrorInfo()
{
$result = $this->db->errorInfo();
$result = self::$db->errorInfo();
$expected = array (
0 => 0,
@ -237,7 +240,7 @@ SQL;
public function testErrorCode()
{
$result = $this->db->errorCode();
$result = self::$db->errorCode();
$this->assertFalse($result);
}
@ -245,7 +248,7 @@ SQL;
public function testDBList()
{
$res = $this->db->sql->db_list();
$res = self::$db->get_sql()->db_list();
$this->assertNULL($res);
}
@ -253,7 +256,7 @@ SQL;
public function testExec()
{
$res = $this->db->exec('SELECT * FROM "create_test"');
$res = self::$db->exec('SELECT * FROM "create_test"');
$this->assertEquals(NULL, $res);
}
@ -261,17 +264,17 @@ SQL;
public function testInTransaction()
{
$this->db->beginTransaction();
$this->assertTrue($this->db->inTransaction());
$this->db->rollBack();
$this->assertFalse($this->db->inTransaction());
self::$db->beginTransaction();
$this->assertTrue(self::$db->inTransaction());
self::$db->rollBack();
$this->assertFalse(self::$db->inTransaction());
}
// --------------------------------------------------------------------------
public function testGetAttribute()
{
$res = $this->db->getAttribute("foo");
$res = self::$db->getAttribute("foo");
$this->assertEquals(NULL, $res);
}
@ -279,11 +282,11 @@ SQL;
public function testSetAttribute()
{
$this->assertFalse($this->db->setAttribute(47, 'foo'));
$this->assertFalse(self::$db->setAttribute(47, 'foo'));
}
public function testLastInsertId()
{
$this->assertEqual(0, $this->db->lastInsertId('NEWTABLE_SEQ'));
$this->assertEqual(0, self::$db->lastInsertId('NEWTABLE_SEQ'));
}
}

View File

@ -21,7 +21,7 @@
*/
class MySQLTest extends DBTest {
public function setUp()
public static function setUpBeforeClass()
{
// Attempt to connect, if there is a test config file
if (is_file(QTEST_DIR . "/settings.json"))
@ -29,16 +29,16 @@ class MySQLTest extends DBTest {
$params = json_decode(file_get_contents(QTEST_DIR . "/settings.json"));
$params = $params->mysql;
$this->db = new \Query\Drivers\Mysql\Driver("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, array(
self::$db = new \Query\Drivers\Mysql\Driver("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, array(
PDO::ATTR_PERSISTENT => TRUE
));
}
elseif (($var = getenv('CI')))
{
$this->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');
}
$this->db->table_prefix = 'create_';
self::$db->table_prefix = 'create_';
}
// --------------------------------------------------------------------------
@ -52,17 +52,17 @@ class MySQLTest extends DBTest {
public function testConnection()
{
$this->assertIsA($this->db, '\\Query\\Drivers\\Mysql\\Driver');
$this->assertIsA(self::$db, '\\Query\\Drivers\\Mysql\\Driver');
}
// --------------------------------------------------------------------------
public function testCreateTable()
{
$this->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
$sql = $this->db->util->create_table('test',
$sql = self::$db->util->create_table('test',
array(
'id' => 'int(10)',
'key' => 'TEXT',
@ -73,10 +73,10 @@ class MySQLTest extends DBTest {
)
);
$this->db->query($sql);
self::$db->query($sql);
//Attempt to create the table
$sql = $this->db->util->create_table('join',
$sql = self::$db->util->create_table('join',
array(
'id' => 'int(10)',
'key' => 'TEXT',
@ -86,10 +86,10 @@ class MySQLTest extends DBTest {
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
self::$db->query($sql);
//Check
$dbs = $this->db->get_tables();
$dbs = self::$db->get_tables();
$this->assertTrue(in_array('create_test', $dbs));
@ -99,8 +99,8 @@ class MySQLTest extends DBTest {
public function testTruncate()
{
$this->db->truncate('test');
$this->db->truncate('join');
self::$db->truncate('test');
self::$db->truncate('join');
}
// --------------------------------------------------------------------------
@ -111,7 +111,7 @@ class MySQLTest extends DBTest {
INSERT INTO `create_test` (`id`, `key`, `val`)
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement = self::$db->prepare_query($sql, array(1,"boogers", "Gross"));
$res = $statement->execute();
@ -129,7 +129,7 @@ SQL;
SQL;
try
{
$statement = $this->db->prepare_query($sql, 'foo');
$statement = self::$db->prepare_query($sql, 'foo');
}
catch(InvalidArgumentException $e)
{
@ -146,7 +146,7 @@ SQL;
INSERT INTO `create_test` (`id`, `key`, `val`)
VALUES (?,?,?)
SQL;
$res = $this->db->prepare_execute($sql, array(
$res = self::$db->prepare_execute($sql, array(
2, "works", 'also?'
));
@ -158,12 +158,12 @@ SQL;
public function testCommitTransaction()
{
$res = $this->db->beginTransaction();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO `create_test` (`id`, `key`, `val`) VALUES (10, 12, 14)';
$this->db->query($sql);
self::$db->query($sql);
$res = $this->db->commit();
$res = self::$db->commit();
$this->assertTrue($res);
}
@ -171,12 +171,12 @@ SQL;
public function testRollbackTransaction()
{
$res = $this->db->beginTransaction();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO `create_test` (`id`, `key`, `val`) VALUES (182, 96, 43)';
$this->db->query($sql);
self::$db->query($sql);
$res = $this->db->rollback();
$res = self::$db->rollback();
$this->assertTrue($res);
}
@ -184,21 +184,21 @@ SQL;
public function testGetSchemas()
{
$this->assertNull($this->db->get_schemas());
$this->assertNull(self::$db->get_schemas());
}
// --------------------------------------------------------------------------
public function testGetSequences()
{
$this->assertNull($this->db->get_sequences());
$this->assertNull(self::$db->get_sequences());
}
// --------------------------------------------------------------------------
public function testBackup()
{
$this->assertTrue(is_string($this->db->util->backup_structure()));
$this->assertTrue(is_string(self::$db->util->backup_structure()));
}

View File

@ -20,7 +20,7 @@ class PgSQLQBTest extends QBTest {
public function setUp()
{
// If the database isn't installed, skip the tests
if ( ! class_exists("Query\\Drivers\\Pgsql\\Driver") && ! IS_QUERCUS)
if ( ! in_array('pgsql', PDO::getAvailableDrivers()))
{
$this->markTestSkipped("Postgres extension for PDO not loaded");
}
@ -65,7 +65,6 @@ class PgSQLQBTest extends QBTest {
{
$this->markTestSkipped();
return;
$query = $this->db->select('id, key as k, val')
->explain()
->where('id >', 1)

View File

@ -30,20 +30,26 @@ class PgTest extends DBTest {
$this->markTestSkipped("Postgres extension for PDO not loaded");
}
}
public static function setUpBeforeClass()
{
$class = "Query\\Drivers\\Pgsql\\Driver";
// 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"));
$params = $params->pgsql;
$this->db = new $class("pgsql:dbname={$params->database};port=5432", $params->user, $params->pass);
self::$db = new $class("pgsql:dbname={$params->database};port=5432", $params->user, $params->pass);
}
elseif (($var = getenv('CI')))
{
$this->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');
}
$this->db->table_prefix = 'create_';
self::$db->table_prefix = 'create_';
}
// --------------------------------------------------------------------------
@ -51,7 +57,6 @@ class PgTest extends DBTest {
public function testExists()
{
$drivers = \PDO::getAvailableDrivers();
print_r($drivers);
$this->assertTrue(in_array('pgsql', $drivers));
}
@ -59,26 +64,26 @@ print_r($drivers);
public function testConnection()
{
if (empty($this->db)) return;
if (empty(self::$db)) return;
$this->assertIsA($this->db, '\\Query\\Drivers\\Pgsql\\Driver');
$this->assertIsA(self::$db, '\\Query\\Drivers\\Pgsql\\Driver');
}
// --------------------------------------------------------------------------
public function testCreateTable()
{
$this->db->exec(file_get_contents(QTEST_DIR.'/db_files/pgsql.sql'));
self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/pgsql.sql'));
// Drop the table(s) if they exist
$sql = 'DROP TABLE IF EXISTS "create_test"';
$this->db->query($sql);
self::$db->query($sql);
$sql = 'DROP TABLE IF EXISTS "create_join"';
$this->db->query($sql);
self::$db->query($sql);
//Attempt to create the table
$sql = $this->db->util->create_table('create_test',
$sql = self::$db->util->create_table('create_test',
array(
'id' => 'integer',
'key' => 'TEXT',
@ -89,10 +94,10 @@ print_r($drivers);
)
);
$this->db->query($sql);
self::$db->query($sql);
//Attempt to create the table
$sql = $this->db->util->create_table('create_join',
$sql = self::$db->util->create_table('create_join',
array(
'id' => 'integer',
'key' => 'TEXT',
@ -102,16 +107,16 @@ print_r($drivers);
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
self::$db->query($sql);
//echo $sql.'<br />';
//Reset
unset($this->db);
$this->setUp();
//unset(self::$db);
//$this->setUp();
//Check
$dbs = $this->db->get_tables();
$dbs = self::$db->get_tables();
$this->assertTrue(in_array('create_test', $dbs));
}
@ -120,11 +125,11 @@ print_r($drivers);
public function testTruncate()
{
$this->db->truncate('create_test');
$this->db->truncate('create_join');
self::$db->truncate('create_test');
self::$db->truncate('create_join');
$ct_query = $this->db->query('SELECT * FROM create_test');
$cj_query = $this->db->query('SELECT * FROM create_join');
$ct_query = self::$db->query('SELECT * FROM create_test');
$cj_query = self::$db->query('SELECT * FROM create_join');
}
// --------------------------------------------------------------------------
@ -135,7 +140,7 @@ print_r($drivers);
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement = self::$db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement->execute();
@ -151,7 +156,7 @@ SQL;
SQL;
try
{
$statement = $this->db->prepare_query($sql, 'foo');
$statement = self::$db->prepare_query($sql, 'foo');
}
catch(InvalidArgumentException $e)
{
@ -164,13 +169,13 @@ SQL;
public function testPrepareExecute()
{
if (empty($this->db)) return;
if (empty(self::$db)) return;
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
self::$db->prepare_execute($sql, array(
2, "works", 'also?'
));
@ -180,14 +185,14 @@ SQL;
public function testCommitTransaction()
{
if (empty($this->db)) return;
if (empty(self::$db)) return;
$res = $this->db->beginTransaction();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
$this->db->query($sql);
self::$db->query($sql);
$res = $this->db->commit();
$res = self::$db->commit();
$this->assertTrue($res);
}
@ -195,14 +200,14 @@ SQL;
public function testRollbackTransaction()
{
if (empty($this->db)) return;
if (empty(self::$db)) return;
$res = $this->db->beginTransaction();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
$this->db->query($sql);
self::$db->query($sql);
$res = $this->db->rollback();
$res = self::$db->rollback();
$this->assertTrue($res);
}
@ -210,20 +215,20 @@ SQL;
public function testGetSchemas()
{
$this->assertTrue(is_array($this->db->get_schemas()));
$this->assertTrue(is_array(self::$db->get_schemas()));
}
// --------------------------------------------------------------------------
public function testGetDBs()
{
$this->assertTrue(is_array($this->db->get_dbs()));
$this->assertTrue(is_array(self::$db->get_dbs()));
}
// --------------------------------------------------------------------------
public function testGetFunctions()
{
$this->assertNull($this->db->get_functions());
$this->assertNull(self::$db->get_functions());
}
}

View File

@ -20,12 +20,23 @@
*/
class SQLiteTest extends DBTest {
public function setUp()
public static function setupBeforeClass()
{
// Set up in the bootstrap to mitigate
// connection locking issues
$this->db = Query('test_sqlite');
$this->db->table_prefix = 'create_';
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
$params = array(
'type' => 'sqlite',
'file' => ':memory:',
'prefix' => 'create_',
'alias' => 'test_sqlite',
'options' => array(
PDO::ATTR_PERSISTENT => TRUE
)
);
Query($params);
self::$db = Query('test_sqlite');
self::$db->table_prefix = 'create_';
}
// --------------------------------------------------------------------------
@ -34,10 +45,10 @@ class SQLiteTest extends DBTest {
public function testCreateTable()
{
$this->db->exec(file_get_contents(QTEST_DIR.'/db_files/sqlite.sql'));
self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/sqlite.sql'));
//Check
$dbs = $this->db->get_tables();
$dbs = self::$db->get_tables();
$this->assertTrue(in_array('TEST1', $dbs));
$this->assertTrue(in_array('TEST2', $dbs));
@ -52,7 +63,7 @@ class SQLiteTest extends DBTest {
/*public function testBackupData()
{
$sql = mb_trim($this->db->util->backup_data(array('create_join', 'create_test')));
$sql = mb_trim(self::$db->util->backup_data(array('create_join', 'create_test')));
$sql_array = explode("\n", $sql);
@ -71,7 +82,7 @@ SQL;
public function testBackupStructure()
{
$sql = mb_trim($this->db->util->backup_structure());
$sql = mb_trim(self::$db->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);
@ -147,12 +158,12 @@ SQL;
public function testDeleteTable()
{
$sql = $this->db->util->delete_table('create_delete');
$sql = self::$db->util->delete_table('create_delete');
$this->db->query($sql);
self::$db->query($sql);
//Check
$dbs = $this->db->get_tables();
$dbs = self::$db->get_tables();
$this->assertFalse(in_array('create_delete', $dbs));
}
@ -167,7 +178,7 @@ SQL;
$db = new $class(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db');
$this->assertIsA($db, $class);
$this->assertIsA($this->db->db, $class);
$this->assertIsA(self::$db->db, $class);
unset($db);
}
@ -176,7 +187,7 @@ SQL;
public function testTruncate()
{
$this->db->truncate('create_test');
self::$db->truncate('create_test');
}
// --------------------------------------------------------------------------
@ -187,7 +198,7 @@ SQL;
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement = self::$db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement->execute();
@ -201,7 +212,7 @@ SQL;
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
self::$db->prepare_execute($sql, array(
2, "works", 'also?'
));
@ -216,12 +227,12 @@ SQL;
$this->markTestSkipped("JDBC Driver doesn't support transactions");
}
$res = $this->db->beginTransaction();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
$this->db->query($sql);
self::$db->query($sql);
$res = $this->db->commit();
$res = self::$db->commit();
$this->assertTrue($res);
}
@ -234,12 +245,12 @@ SQL;
$this->markTestSkipped("JDBC Driver doesn't support transactions");
}
$res = $this->db->beginTransaction();
$res = self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
$this->db->query($sql);
self::$db->query($sql);
$res = $this->db->rollback();
$res = self::$db->rollback();
$this->assertTrue($res);
}
@ -247,14 +258,14 @@ SQL;
public function testGetDBs()
{
$this->assertTrue(is_array($this->db->get_dbs()));
$this->assertTrue(is_array(self::$db->get_dbs()));
}
// --------------------------------------------------------------------------
public function testGetSchemas()
{
$this->assertNull($this->db->get_schemas());
$this->assertNull(self::$db->get_schemas());
}
// --------------------------------------------------------------------------
@ -263,13 +274,13 @@ SQL;
public function testNullMethods()
{
$sql = $this->db->sql->function_list();
$sql = self::$db->sql->function_list();
$this->assertEqual(NULL, $sql);
$sql = $this->db->sql->procedure_list();
$sql = self::$db->sql->procedure_list();
$this->assertEqual(NULL, $sql);
$sql = $this->db->sql->sequence_list();
$sql = self::$db->sql->sequence_list();
$this->assertEqual(NULL, $sql);
}
@ -277,7 +288,7 @@ SQL;
public function testGetSystemTables()
{
$sql = $this->db->get_system_tables();
$sql = self::$db->get_system_tables();
$this->assertTrue(is_array($sql));
}
@ -285,20 +296,20 @@ SQL;
public function testGetSequences()
{
$this->assertNull($this->db->get_sequences());
$this->assertNull(self::$db->get_sequences());
}
// --------------------------------------------------------------------------
public function testGetFunctions()
{
$this->assertNull($this->db->get_functions());
$this->assertNull(self::$db->get_functions());
}
// --------------------------------------------------------------------------
public function testGetProcedures()
{
$this->assertNull($this->db->get_procedures());
$this->assertNull(self::$db->get_procedures());
}
}

View File

@ -1,36 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
stopOnFailure="false"
bootstrap="bootstrap.php">
<filter>
<blacklist>
<directory suffix=".php">../coverage</directory>
<directory suffix=".php">../docs</directory>
<directory suffix=".php">.</directory>
</blacklist>
</filter>
<testsuites>
<testsuite name="CoreTests">
<file>core/core.php</file>
<file>core/db_qp_test.php</file>
</testsuite>
<testsuite name="FirebirdTests">
<file>databases/firebird/FirebirdTest.php</file>
<file>databases/firebird/FirebirdQBTest.php</file>
</testsuite>
<testsuite name="MySQLTests">
<file>databases/mysql/MySQLTest.php</file>
<file>databases/mysql/MySQLQBTest.php</file>
</testsuite>
<testsuite name="PgSQLTests">
<file>databases/pgsql/PgSQLTest.php</file>
<file>databases/pgsql/PgSQLQBTest.php</file>
</testsuite>
<testsuite name="SQLiteTests">
<file>databases/sqlite/SQLiteTest.php</file>
<file>databases/sqlite/SQLiteQBTest.php</file>
</testsuite>
</testsuites>
</phpunit>