Update test layout to better match source layout

This commit is contained in:
Timothy Warren 2018-01-19 16:48:52 -05:00
parent 369ca6eb04
commit 4df07b6c72
20 changed files with 352 additions and 301 deletions

View File

@ -31,7 +31,7 @@
"phploc/phploc": "^4.0", "phploc/phploc": "^4.0",
"phpmd/phpmd": "^2.4", "phpmd/phpmd": "^2.4",
"phpstan/phpstan": "^0.9.1", "phpstan/phpstan": "^0.9.1",
"phpunit/phpunit": "^5.5", "phpunit/phpunit": "^6.5",
"sebastian/phpcpd": "^2.0", "sebastian/phpcpd": "^2.0",
"simpletest/simpletest": "^1.1", "simpletest/simpletest": "^1.1",
"squizlabs/php_codesniffer": "^3.0.0" "squizlabs/php_codesniffer": "^3.0.0"
@ -50,7 +50,7 @@
"scripts": { "scripts": {
"coverage": "phpdbg -qrr -- vendor/bin/phpunit -c build", "coverage": "phpdbg -qrr -- vendor/bin/phpunit -c build",
"phpstan": "phpstan analyse -l 3 -c phpstan.neon src tests", "phpstan": "phpstan analyse -l 3 -c phpstan.neon src tests",
"test": "phpunit -c build" "test": "phpunit -c build --no-coverage"
}, },
"scripts-descriptions": { "scripts-descriptions": {
"coverage": "Generate test coverage report", "coverage": "Generate test coverage report",

View File

@ -1,6 +1,12 @@
parameters: parameters:
autoload_files: autoload_files:
- %rootDir%/../../../tests/bootstrap.php - %rootDir%/../../../tests/bootstrap.php
- %rootDir%/../../../tests/databases/mysql/MySQLTest.php
- %rootDir%/../../../tests/databases/mysql/MySQLQBTest.php
- %rootDir%/../../../tests/databases/pgsql/PgSQLTest.php
- %rootDir%/../../../tests/databases/pgsql/PgSQLQBTest.php
- %rootDir%/../../../tests/databases/sqlite/SQLiteTest.php
- %rootDir%/../../../tests/databases/sqlite/SQLiteQBTest.php
ignoreErrors: ignoreErrors:
- '#Access to an undefined property Aviat\\\Ion\\\Friend::\$[a-zA-Z0-9_]+#' - '#Access to an undefined property Aviat\\\Ion\\\Friend::\$[a-zA-Z0-9_]+#'
- '#Call to an undefined method Aviat\\\Ion\\\Friend::[a-zA-Z0-9_]+\(\)#' - '#Call to an undefined method Aviat\\\Ion\\\Friend::[a-zA-Z0-9_]+\(\)#'

View File

@ -105,7 +105,7 @@ class QueryBuilder extends AbstractQueryBuilder implements QueryBuilderInterface
{ {
if (method_exists($object, $methodName)) if (method_exists($object, $methodName))
{ {
return call_user_func_array([$object, $methodName], $params); return \call_user_func_array([$object, $methodName], $params);
} }
} }
@ -645,12 +645,12 @@ class QueryBuilder extends AbstractQueryBuilder implements QueryBuilderInterface
} }
// Set the limit, if it exists // Set the limit, if it exists
if (is_int($limit)) if (\is_int($limit))
{ {
$this->limit($limit, $offset); $this->limit($limit, $offset);
} }
return $this->_run("get", $table); return $this->_run('get', $table);
} }
/** /**

View File

@ -14,7 +14,10 @@
*/ */
use Query\ConnectionManager; use Query\{
ConnectionManager,
QueryBuilderInterface
};
require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
@ -34,7 +37,7 @@ if ( ! function_exists('mb_trim'))
*/ */
function mb_trim(string $string): string function mb_trim(string $string): string
{ {
return preg_replace("/(^\s+)|(\s+$)/us", "", $string); return preg_replace('/(^\s+)|(\s+$)/u', '', $string);
} }
} }
@ -73,9 +76,10 @@ if ( ! function_exists('to_camel_case'))
function to_camel_case(string $snakeCase): string function to_camel_case(string $snakeCase): string
{ {
$pieces = explode('_', $snakeCase); $pieces = explode('_', $snakeCase);
$numPieces = count($pieces);
$pieces[0] = mb_strtolower($pieces[0]); $pieces[0] = mb_strtolower($pieces[0]);
for($i = 1; $i < count($pieces); $i++) for($i = 1; $i < $numPieces; $i++)
{ {
$pieces[$i] = ucfirst(mb_strtolower($pieces[$i])); $pieces[$i] = ucfirst(mb_strtolower($pieces[$i]));
} }
@ -161,26 +165,27 @@ if ( ! function_exists('Query'))
* connection created. * connection created.
* *
* @param string|object|array $params * @param string|object|array $params
* @return Query\QueryBuilder|null * @return QueryBuilderInterface|null
*/ */
function Query($params = '') function Query($params = ''): ?QueryBuilderInterface
{ {
$manager = ConnectionManager::getInstance(); $manager = ConnectionManager::getInstance();
if ($params === NULL)
{
return NULL;
}
// If you are getting a previously created connection // If you are getting a previously created connection
if (is_scalar($params)) if (is_scalar($params))
{ {
return $manager->getConnection($params); return $manager->getConnection($params);
} }
elseif ( ! is_scalar($params) && ! is_null($params))
{
$paramsObject = (object) $params; $paramsObject = (object) $params;
// Otherwise, return a new connection // Otherwise, return a new connection
return $manager->connect($paramsObject); return $manager->connect($paramsObject);
} }
return NULL;
}
} }
// End of common.php // End of common.php

View File

@ -13,15 +13,19 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Parent Database Test Class * Parent Database Test Class
*/ */
abstract class DBTest extends Query_TestCase { abstract class BaseDriverTest extends TestCase {
protected static $db = NULL; /**
* @var \Query\QueryBuilder
*/
protected static $db;
abstract public function testConnection(); abstract public function testConnection();

View File

@ -13,10 +13,14 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests;
use PDO;
/** /**
* Query builder parent test class * Query builder parent test class
*/ */
abstract class QBTest extends Query_TestCase { abstract class BaseQueryBuilderTest extends TestCase {
protected static $db; protected static $db;
@ -41,7 +45,7 @@ abstract class QBTest extends Query_TestCase {
{ {
$this->expectException('InvalidArgumentException'); $this->expectException('InvalidArgumentException');
$db = Query('foo'); Query('foo');
} }
public function testFunctionGet() public function testFunctionGet()
@ -662,7 +666,7 @@ abstract class QBTest extends Query_TestCase {
'val' => 'baz' 'val' => 'baz'
))->getCompiledUpdate('test'); ))->getCompiledUpdate('test');
$this->assertTrue(is_string($sql)); $this->assertTrue(\is_string($sql));
} }
public function testGetCompiledInsert() public function testGetCompiledInsert()
@ -673,7 +677,7 @@ abstract class QBTest extends Query_TestCase {
'val' => 'baz' 'val' => 'baz'
))->getCompiledInsert('test'); ))->getCompiledInsert('test');
$this->assertTrue(is_string($sql)); $this->assertTrue(\is_string($sql));
} }
public function testGetCompiledDelete() public function testGetCompiledDelete()
@ -681,7 +685,7 @@ abstract class QBTest extends Query_TestCase {
$sql = self::$db->where('id', 4) $sql = self::$db->where('id', 4)
->getCompiledDelete('test'); ->getCompiledDelete('test');
$this->assertTrue(is_string($sql)); $this->assertTrue(\is_string($sql));
} }
// ! Error tests // ! Error tests
/** /**

View File

@ -13,14 +13,18 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests;
class Connection_Manager_Test extends Query_TestCase { use DomainException;
use Query\{ConnectionManager, QueryBuilderInterface};
protected static $instance = NULL; class ConnectionManagerTest extends TestCase {
protected static $instance;
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
self::$instance = Query\ConnectionManager::getInstance(); self::$instance = ConnectionManager::getInstance();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -30,25 +34,29 @@ class Connection_Manager_Test extends Query_TestCase {
$this->expectException('DomainException'); $this->expectException('DomainException');
$this->expectExceptionMessage("Can't clone singleton"); $this->expectExceptionMessage("Can't clone singleton");
$clone = clone self::$instance; $clone = clone self::$instance;
$this->assertNull($clone);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testNoSerialize() public function testNoSerialize()
{ {
$this->setExpectedException('DomainException', "No serializing of singleton"); $this->expectException(DomainException::class);
$string = serialize(self::$instance); $this->expectExceptionMessage('No serializing of singleton');
serialize(self::$instance);
$this->setExpectedException('DomainException', "No serializing of singleton"); $this->expectException(DomainException::class);
$string = self::$instance->__sleep(); $this->expectExceptionMessage('No serializing of singleton');
self::$instance->__sleep();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testNoUnserialize() public function testNoUnserialize()
{ {
$this->setExpectedException('DomainException', "Can't unserialize singleton"); $this->expectException(DomainException::class);
$obj = self::$instance->__wakeup(); $this->expectExceptionMessage("Can't unserialize singleton");
self::$instance->__wakeup();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -87,7 +95,7 @@ class Connection_Manager_Test extends Query_TestCase {
); );
$conn = self::$instance->connect($params); $conn = self::$instance->connect($params);
$this->assertInstanceOf('Query\\QueryBuilder', $conn); $this->assertInstanceOf(QueryBuilderInterface::class, $conn);
// Check that the connection just made is returned from the get_connection method // Check that the connection just made is returned from the get_connection method
@ -109,7 +117,7 @@ class Connection_Manager_Test extends Query_TestCase {
); );
$conn = self::$instance->connect($params); $conn = self::$instance->connect($params);
$this->assertInstanceOf('Query\\QueryBuilder', $conn); $this->assertInstanceOf(QueryBuilderInterface::class, $conn);
$this->assertEqual($conn, self::$instance->getConnection('conn_manager')); $this->assertEqual($conn, self::$instance->getConnection('conn_manager'));
} }

View File

@ -13,6 +13,8 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -21,7 +23,7 @@
* *
* @extends UnitTestCase * @extends UnitTestCase
*/ */
class CoreTest extends Query_TestCase { class CoreTest extends TestCase {
/** /**
* TestPHPVersion function. * TestPHPVersion function.
@ -31,7 +33,8 @@ class CoreTest extends Query_TestCase {
*/ */
public function testPHPVersion() public function testPHPVersion()
{ {
$this->assertTrue(version_compare(PHP_VERSION, "5.3", "ge")); //$this->assertTrue(version_compare(PHP_VERSION, '7.1', 'ge'));
$this->assertTrue(PHP_VERSION_ID >= 70000);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -49,15 +52,15 @@ class CoreTest extends Query_TestCase {
// Make sure at least one of the supported drivers is enabled // Make sure at least one of the supported drivers is enabled
$supported = array( $supported = [
'firebird', 'firebird',
'mysql', 'mysql',
'pgsql', 'pgsql',
'odbc', 'odbc',
'sqlite', 'sqlite',
); ];
$drivers = PDO::getAvailableDrivers(); $drivers = \PDO::getAvailableDrivers();
$numSupported = count(array_intersect($drivers, $supported)); $numSupported = count(array_intersect($drivers, $supported));

View File

@ -13,7 +13,12 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests\Drivers\MySQL;
use InvalidArgumentException;
use PDO;
use Query\Drivers\Mysql\Driver; use Query\Drivers\Mysql\Driver;
use Query\Tests\BaseDriverTest;
/** /**
* MySQLTest class. * MySQLTest class.
@ -21,12 +26,12 @@ use Query\Drivers\Mysql\Driver;
* @extends DBTest * @extends DBTest
* @requires extension pdo_mysql * @requires extension pdo_mysql
*/ */
class MySQLTest extends DBTest { class MySQLDriverTest extends BaseDriverTest {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
$params = get_json_config(); $params = get_json_config();
if (($var = getenv('TRAVIS'))) if ($var = getenv('TRAVIS'))
{ {
self::$db = new Driver('host=127.0.0.1;port=3306;dbname=test', 'root'); self::$db = new Driver('host=127.0.0.1;port=3306;dbname=test', 'root');
} }
@ -54,7 +59,7 @@ class MySQLTest extends DBTest {
public function testConnection() public function testConnection()
{ {
$this->assertIsA(self::$db, '\\Query\\Drivers\\Mysql\\Driver'); $this->assertIsA(self::$db, Driver::class);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -102,7 +107,10 @@ class MySQLTest extends DBTest {
public function testTruncate() public function testTruncate()
{ {
self::$db->truncate('test'); self::$db->truncate('test');
$this->assertEquals(0, self::$db->countAll('test'));
self::$db->truncate('join'); self::$db->truncate('join');
$this->assertEquals(0, self::$db->countAll('join'));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -125,18 +133,14 @@ SQL;
public function testBadPreparedStatement() public function testBadPreparedStatement()
{ {
$this->expectException(InvalidArgumentException::class);
$sql = <<<SQL $sql = <<<SQL
INSERT INTO `create_test` (`id`, `key`, `val`) INSERT INTO `create_test` (`id`, `key`, `val`)
VALUES (?,?,?) VALUES (?,?,?)
SQL; SQL;
try
{ self::$db->prepareQuery($sql, 'foo');
$statement = self::$db->prepareQuery($sql, 'foo');
}
catch(InvalidArgumentException $e)
{
$this->assertTrue(TRUE);
}
} }

View File

@ -13,13 +13,17 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests\Drivers\MySQL;
use PDO;
use Query\Tests\BaseQueryBuilderTest;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* @requires extension pdo_mysql * @requires extension pdo_mysql
*/ */
class MySQLQBTest extends QBTest { class MySQLQueryBuilderTest extends BaseQueryBuilderTest {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
@ -52,7 +56,7 @@ class MySQLQBTest extends QBTest {
public function testExists() public function testExists()
{ {
$this->assertTrue(in_array('mysql', PDO::getAvailableDrivers())); $this->assertTrue(\in_array('mysql', PDO::getAvailableDrivers(), TRUE));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -13,42 +13,44 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests\Drivers\PgSQL;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
use InvalidArgumentException;
use Query\Drivers\Pgsql\Driver;
use Query\Tests\BaseDriverTest;
/** /**
* PgTest class. * PgTest class.
* *
* @extends DBTest * @extends DBTest
* @requires extension pdo_pgsql * @requires extension pdo_pgsql
*/ */
class PgTest extends DBTest { class PgSQLDriverTest extends BaseDriverTest {
public function setUp() public function setUp()
{ {
$class = "Query\\Drivers\\Pgsql\\Driver";
// If the database isn't installed, skip the tests // If the database isn't installed, skip the tests
if (( ! class_exists($class)) && ! IS_QUERCUS) if ( ! class_exists(Driver::class))
{ {
$this->markTestSkipped("Postgres extension for PDO not loaded"); $this->markTestSkipped('Postgres extension for PDO not loaded');
} }
} }
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
$class = "Query\\Drivers\\Pgsql\\Driver";
$params = get_json_config(); $params = get_json_config();
if (($var = getenv('TRAVIS'))) if ($var = getenv('TRAVIS'))
{ {
self::$db = new $class('host=127.0.0.1;port=5432;dbname=test', 'postgres'); self::$db = new Driver('host=127.0.0.1;port=5432;dbname=test', 'postgres');
} }
// Attempt to connect, if there is a test config file // Attempt to connect, if there is a test config file
else if ($params !== FALSE) else if ($params !== FALSE)
{ {
$params = $params->pgsql; $params = $params->pgsql;
self::$db = new $class("pgsql:host={$params->host};dbname={$params->database};port=5432", $params->user, $params->pass); self::$db = new Driver("pgsql:host={$params->host};dbname={$params->database};port=5432", $params->user, $params->pass);
} }
self::$db->setTablePrefix('create_'); self::$db->setTablePrefix('create_');
@ -59,7 +61,7 @@ class PgTest extends DBTest {
public function testExists() public function testExists()
{ {
$drivers = \PDO::getAvailableDrivers(); $drivers = \PDO::getAvailableDrivers();
$this->assertTrue(in_array('pgsql', $drivers)); $this->assertTrue(in_array('pgsql', $drivers, TRUE));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -68,7 +70,7 @@ class PgTest extends DBTest {
{ {
if (empty(self::$db)) return; if (empty(self::$db)) return;
$this->assertIsA(self::$db, '\\Query\\Drivers\\Pgsql\\Driver'); $this->assertIsA(self::$db, Driver::class);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -119,7 +121,7 @@ class PgTest extends DBTest {
//Check //Check
$dbs = self::$db->getTables(); $dbs = self::$db->getTables();
$this->assertTrue(in_array('create_test', $dbs)); $this->assertTrue(in_array('create_test', $dbs, TRUE));
} }
@ -127,11 +129,11 @@ class PgTest extends DBTest {
public function testTruncate() public function testTruncate()
{ {
self::$db->truncate('create_test'); self::$db->truncate('test');
self::$db->truncate('create_join'); $this->assertEquals(0, self::$db->countAll('test'));
$ctQuery = self::$db->query('SELECT * FROM create_test'); self::$db->truncate('join');
$cjQuery = self::$db->query('SELECT * FROM create_join'); $this->assertEquals(0, self::$db->countAll('join'));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -142,7 +144,7 @@ class PgTest extends DBTest {
INSERT INTO "create_test" ("id", "key", "val") INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?) VALUES (?,?,?)
SQL; SQL;
$statement = self::$db->prepareQuery($sql, array(1,"boogers", "Gross")); $statement = self::$db->prepareQuery($sql, array(1,'boogers', 'Gross'));
$statement->execute(); $statement->execute();
@ -152,19 +154,14 @@ SQL;
public function testBadPreparedStatement() public function testBadPreparedStatement()
{ {
$this->expectException(InvalidArgumentException::class);
$sql = <<<SQL $sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val") INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?) VALUES (?,?,?)
SQL; SQL;
try
{
$statement = self::$db->prepareQuery($sql, 'foo');
}
catch(InvalidArgumentException $e)
{
$this->assertTrue(TRUE);
}
self::$db->prepareQuery($sql, 'foo');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -189,7 +186,7 @@ SQL;
{ {
if (empty(self::$db)) return; if (empty(self::$db)) return;
$res = self::$db->beginTransaction(); self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)'; $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
self::$db->query($sql); self::$db->query($sql);
@ -204,7 +201,7 @@ SQL;
{ {
if (empty(self::$db)) return; if (empty(self::$db)) return;
$res = self::$db->beginTransaction(); self::$db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)'; $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
self::$db->query($sql); self::$db->query($sql);

View File

@ -13,13 +13,17 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests\Drivers\PgSQL;
use PDO;
use Query\Tests\BaseQueryBuilderTest;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* @requires extension pdo_pgsql * @requires extension pdo_pgsql
*/ */
class PgSQLQBTest extends QBTest { class PgSQLQueryBuilderTest extends BaseQueryBuilderTest {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {

View File

@ -13,6 +13,11 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests\Drivers\SQLite;
use PDO;
use Query\Drivers\Sqlite\Driver;
use Query\Tests\BaseDriverTest;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -22,7 +27,7 @@
* @extends DBTest * @extends DBTest
* @requires extension pdo_sqlite * @requires extension pdo_sqlite
*/ */
class SQLiteTest extends DBTest { class SQLiteDriverTest extends BaseDriverTest {
public static function setupBeforeClass() public static function setupBeforeClass()
{ {
@ -175,7 +180,7 @@ SQL;
public function testConnection() public function testConnection()
{ {
$class = '\\Query\\Drivers\\Sqlite\\Driver'; $class = Driver::class;
$db = new $class(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'); $db = new $class(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db');
@ -190,6 +195,7 @@ SQL;
public function testTruncate() public function testTruncate()
{ {
self::$db->truncate('create_test'); self::$db->truncate('create_test');
$this->assertEquals(0, self::$db->countAll('create_test'));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -281,7 +287,7 @@ SQL;
public function testGetSystemTables() public function testGetSystemTables()
{ {
$sql = self::$db->getSystemTables(); $sql = self::$db->getSystemTables();
$this->assertTrue(is_array($sql)); $this->assertTrue(\is_array($sql));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -13,6 +13,10 @@
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests\Drivers\SQLite;
use PDO;
use Query\Tests\BaseQueryBuilderTest;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -21,7 +25,7 @@
* *
* @requires extension pdo_sqlite * @requires extension pdo_sqlite
*/ */
class SQLiteQBTest extends QBTest { class SQLiteQueryBuilderTest extends BaseQueryBuilderTest {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {

View File

@ -4,27 +4,28 @@
* *
* SQL Query Builder / Database Abstraction Layer * SQL Query Builder / Database Abstraction Layer
* *
* PHP version 7 * PHP version 7.1
* *
* @package Query * @package Query
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2012 - 2016 Timothy J. Warren * @copyright 2012 - 2018 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query * @link https://git.timshomepage.net/aviat4ion/Query
*/ */
namespace Query\Tests;
use Query\QueryParser;
// -------------------------------------------------------------------------- use Query\Drivers\Sqlite\Driver;
/** /**
* Tests for the Query Parser * Tests for the Query Parser
*/ */
class Query_Parser_Test extends Query_TestCase { class QueryParserTest extends TestCase {
public function setUp() public function setUp()
{ {
$db = new Query\Drivers\Sqlite\Driver("sqlite::memory:"); $db = new Driver('sqlite::memory:');
$this->parser = new Query\QueryParser($db); $this->parser = new QueryParser($db);
} }
public function testGeneric() public function testGeneric()

72
tests/TestCase.php Normal file
View File

@ -0,0 +1,72 @@
<?php declare(strict_types=1);
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
* PHP version 7
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2012 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
*/
namespace Query\Tests;
use PHPUnit\Framework\TestCase as PHPUnit_TestCase;
/**
* Base class for TestCases
*/
class TestCase extends PHPUnit_TestCase {
/**
* Wrapper for Simpletest's assertEqual
*
* @param mixed $expected
* @param mixed $actual
* @param string $message
*/
public function assertEqual($expected, $actual, $message='')
{
$this->assertEquals($expected, $actual, $message);
}
/**
* Wrapper for SimpleTest's assertIsA
*
* @param mixed $object
* @param string $type
* @param string $message
*/
public function assertIsA($object, $type, $message='')
{
$this->assertTrue(is_a($object, $type), $message);
}
/**
* Implementation of SimpleTest's assertReference
*
* @param mixed $first
* @param mixed $second
* @param string $message
*/
public function assertReference($first, $second, $message='')
{
if (\is_object($first))
{
$res = ($first === $second);
}
else
{
$temp = $first;
$first = uniqid('test', TRUE);
$isRef = ($first === $second);
$first = $temp;
$res = $isRef;
}
$this->assertTrue($res, $message);
}
}

View File

@ -11,8 +11,6 @@
* @license http://philsturgeon.co.uk/code/dbad-license * @license http://philsturgeon.co.uk/code/dbad-license
*/ */
use PHPUnit\Framework\TestCase;
/** /**
* Unit test bootstrap - Using phpunit * Unit test bootstrap - Using phpunit
*/ */
@ -20,9 +18,6 @@ define('QTEST_DIR', realpath(__DIR__));
define('QBASE_DIR', realpath(QTEST_DIR.'/../') . '/'); define('QBASE_DIR', realpath(QTEST_DIR.'/../') . '/');
define('QDS', DIRECTORY_SEPARATOR); define('QDS', DIRECTORY_SEPARATOR);
// Set up autoloader
require_once QBASE_DIR . 'vendor/autoload.php';
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
function get_json_config() function get_json_config()
@ -43,67 +38,8 @@ function get_json_config()
return FALSE; return FALSE;
} }
/**
* Base class for TestCases
*/
class Query_TestCase extends TestCase {
/**
* Wrapper for Simpletest's assertEqual
*
* @param mixed $expected
* @param mixed $actual
* @param string $message
*/
public function assertEqual($expected, $actual, $message='')
{
$this->assertEquals($expected, $actual, $message);
}
/**
* Wrapper for SimpleTest's assertIsA
*
* @param object $object
* @param string $type
* @param string $message
*/
public function assertIsA($object, $type, $message='')
{
$this->assertTrue(is_a($object, $type), $message);
}
/**
* Implementation of SimpleTest's assertReference
*
* @param mixed $first
* @param mixed $second
* @param string $message
*/
public function assertReference($first, $second, $message='')
{
if (is_object($first))
{
$res = ($first === $second);
}
else
{
$temp = $first;
$first = uniqid("test");
$isRef = ($first === $second);
$first = $temp;
$res = $isRef;
}
$this->assertTrue($res, $message);
}
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
@unlink($path); @unlink($path);
// Require base testing classes
require_once(QTEST_DIR . '/core/base_db_test.php');
require_once(QTEST_DIR . '/core/base_query_builder_test.php');
// End of bootstrap.php // End of bootstrap.php

View File

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
/** /**
* Query * Query
* *
@ -11,41 +11,33 @@
* @license http://philsturgeon.co.uk/code/dbad-license * @license http://philsturgeon.co.uk/code/dbad-license
*/ */
function get_json_config() namespace {
{ /**
$files = array( * Unit test bootstrap - Using php simpletest
__DIR__ . '/settings.json', */
__DIR__ . '/settings.json.dist' \define('QTEST_DIR', __DIR__);
); \define('QBASE_DIR', realpath(__DIR__ . '/../') . '/');
\define('QDS', DIRECTORY_SEPARATOR);
foreach($files as $file) require_once QBASE_DIR . 'vendor/simpletest/simpletest/autorun.php';
{ require_once QBASE_DIR . 'vendor/autoload.php';
if (is_file($file))
{
return json_decode(file_get_contents($file));
}
} }
return FALSE; namespace Query\Tests {
}
// --------------------------------------------------------------------------
// Set up autoloaders
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../vendor/simpletest/simpletest/autorun.php';
/** /**
* Base class for TestCases * Base class for TestCases
*/ */
abstract class Query_TestCase extends UnitTestCase { abstract class TestCase extends \UnitTestCase
{
public function __construct() public function __construct()
{ {
$class = get_class($this); $class = \get_class($this);
if (method_exists($class, 'setupBeforeClass')) echo 'Ran test suite: ' . $class . '<br />';
{
if (method_exists($class, 'setupBeforeClass')) {
$class::setupBeforeClass(); $class::setupBeforeClass();
} }
@ -54,10 +46,9 @@ abstract class Query_TestCase extends UnitTestCase {
public function __destruct() public function __destruct()
{ {
$class = get_class($this); $class = \get_class($this);
if (method_exists($class, 'tearDownAfterClass')) if (method_exists($class, 'tearDownAfterClass')) {
{
$class::tearDownAfterClass(); $class::tearDownAfterClass();
} }
} }
@ -105,51 +96,50 @@ abstract class Query_TestCase extends UnitTestCase {
{ {
// noop // noop
} }
}
}
/** /**
* Alias for phpunit method * Load the test suites
*
* @param string $name
* @param string $message
* @param int $code
*/ */
public function setExpectedException($name, $message='', $code=NULL) namespace {
function get_json_config()
{ {
$this->expectException($name, $message); $files = array(
__DIR__ . '/settings.json',
__DIR__ . '/settings.json.dist'
);
foreach ($files as $file) {
if (is_file($file)) {
return json_decode(file_get_contents($file));
} }
} }
// -------------------------------------------------------------------------- return FALSE;
}
/**
* Unit test bootstrap - Using php simpletest
*/
define('QTEST_DIR', __DIR__);
define('QBASE_DIR', realpath(__DIR__ . '/../') . '/');
define('QDS', DIRECTORY_SEPARATOR);
// Include db tests // Include db tests
// Load db classes based on capability // Load db classes based on capability
$testPath = QTEST_DIR.'/databases/'; $testPath = QTEST_DIR.'/Drivers/';
// Require base testing classes // Require base testing classes
require_once QTEST_DIR . '/core/core_test.php'; require_once QTEST_DIR . '/CoreTest.php';
require_once QTEST_DIR . '/core/connection_manager_test.php'; require_once QTEST_DIR . '/ConnectionManagerTest.php';
require_once QTEST_DIR . '/core/base_db_test.php'; require_once QTEST_DIR . '/QueryParserTest.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();
if (function_exists('fbird_connect')) /* if (function_exists('fbird_connect'))
{ {
$drivers[] = 'interbase'; $drivers[] = 'interbase';
} } */
$driverTestMap = [ $driverTestMap = [
'MySQL' => in_array('mysql', $drivers, TRUE), 'MySQL' => \in_array('mysql', $drivers, TRUE),
'SQLite' => in_array('sqlite', $drivers, TRUE), 'SQLite' => \in_array('sqlite', $drivers, TRUE),
'PgSQL' => in_array('pgsql', $drivers, TRUE), 'PgSQL' => \in_array('pgsql', $drivers, TRUE),
// 'Firebird' => in_array('interbase', $drivers), // 'Firebird' => in_array('interbase', $drivers),
//'PDOFirebird' => in_array('firebird', $drivers) //'PDOFirebird' => in_array('firebird', $drivers)
]; ];
@ -157,12 +147,15 @@ $driverTestMap = [
// Determine which testcases to load // Determine which testcases to load
foreach($driverTestMap as $name => $doLoad) foreach($driverTestMap as $name => $doLoad)
{ {
$path = $testPath . strtolower($name) . '/'; $path = $testPath . $name;
if ($doLoad) if ($doLoad)
{ {
require_once "{$path}{$name}Test.php"; require_once "{$path}/{$name}DriverTest.php";
require_once "{$path}{$name}QBTest.php"; require_once "{$path}/{$name}QueryBuilderTest.php";
} }
} }
}
// End of index.php // End of index.php