diff --git a/composer.json b/composer.json index fdb83cb..df85e02 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "phploc/phploc": "^4.0", "phpmd/phpmd": "^2.4", "phpstan/phpstan": "^0.9.1", - "phpunit/phpunit": "^5.5", + "phpunit/phpunit": "^6.5", "sebastian/phpcpd": "^2.0", "simpletest/simpletest": "^1.1", "squizlabs/php_codesniffer": "^3.0.0" @@ -50,7 +50,7 @@ "scripts": { "coverage": "phpdbg -qrr -- vendor/bin/phpunit -c build", "phpstan": "phpstan analyse -l 3 -c phpstan.neon src tests", - "test": "phpunit -c build" + "test": "phpunit -c build --no-coverage" }, "scripts-descriptions": { "coverage": "Generate test coverage report", diff --git a/phpstan.neon b/phpstan.neon index eb20b2c..b340330 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,12 @@ parameters: autoload_files: - %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: - '#Access to an undefined property Aviat\\\Ion\\\Friend::\$[a-zA-Z0-9_]+#' - '#Call to an undefined method Aviat\\\Ion\\\Friend::[a-zA-Z0-9_]+\(\)#' diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 91e9bf2..3fd8792 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -105,7 +105,7 @@ class QueryBuilder extends AbstractQueryBuilder implements QueryBuilderInterface { 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 - if (is_int($limit)) + if (\is_int($limit)) { $this->limit($limit, $offset); } - return $this->_run("get", $table); + return $this->_run('get', $table); } /** diff --git a/src/common.php b/src/common.php index f04b489..7d1861e 100644 --- a/src/common.php +++ b/src/common.php @@ -14,7 +14,10 @@ */ -use Query\ConnectionManager; +use Query\{ + ConnectionManager, + QueryBuilderInterface +}; require __DIR__ . '/../vendor/autoload.php'; @@ -34,7 +37,7 @@ if ( ! function_exists('mb_trim')) */ 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 { $pieces = explode('_', $snakeCase); + $numPieces = count($pieces); $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])); } @@ -161,26 +165,27 @@ if ( ! function_exists('Query')) * connection created. * * @param string|object|array $params - * @return Query\QueryBuilder|null + * @return QueryBuilderInterface|null */ - function Query($params = '') + function Query($params = ''): ?QueryBuilderInterface { $manager = ConnectionManager::getInstance(); + if ($params === NULL) + { + return NULL; + } + // If you are getting a previously created connection if (is_scalar($params)) { return $manager->getConnection($params); } - elseif ( ! is_scalar($params) && ! is_null($params)) - { - $paramsObject = (object) $params; - // Otherwise, return a new connection - return $manager->connect($paramsObject); - } + $paramsObject = (object) $params; - return NULL; + // Otherwise, return a new connection + return $manager->connect($paramsObject); } } -// End of common.php \ No newline at end of file +// End of common.php diff --git a/tests/core/base_db_test.php b/tests/BaseDriverTest.php similarity index 96% rename from tests/core/base_db_test.php rename to tests/BaseDriverTest.php index cc7a357..c3eda51 100644 --- a/tests/core/base_db_test.php +++ b/tests/BaseDriverTest.php @@ -13,15 +13,19 @@ * @link https://git.timshomepage.net/aviat4ion/Query */ +namespace Query\Tests; // -------------------------------------------------------------------------- /** * 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(); diff --git a/tests/core/base_query_builder_test.php b/tests/BaseQueryBuilderTest.php similarity index 98% rename from tests/core/base_query_builder_test.php rename to tests/BaseQueryBuilderTest.php index 037a832..c76d9dc 100644 --- a/tests/core/base_query_builder_test.php +++ b/tests/BaseQueryBuilderTest.php @@ -13,10 +13,14 @@ * @link https://git.timshomepage.net/aviat4ion/Query */ +namespace Query\Tests; + +use PDO; + /** * Query builder parent test class */ -abstract class QBTest extends Query_TestCase { +abstract class BaseQueryBuilderTest extends TestCase { protected static $db; @@ -41,7 +45,7 @@ abstract class QBTest extends Query_TestCase { { $this->expectException('InvalidArgumentException'); - $db = Query('foo'); + Query('foo'); } public function testFunctionGet() @@ -506,7 +510,7 @@ abstract class QBTest extends Query_TestCase { $this->assertIsA($query, 'PDOStatement'); } - + // ! DB update tests public function testInsert() { @@ -521,10 +525,10 @@ abstract class QBTest extends Query_TestCase { public function testInsertArray() { $query = self::$db->insert('test', array( - 'id' => 587, - 'key' => 1, - 'val' => 2, - )); + 'id' => 587, + 'key' => 1, + 'val' => 2, + )); $this->assertIsA($query, 'PDOStatement'); } @@ -662,7 +666,7 @@ abstract class QBTest extends Query_TestCase { 'val' => 'baz' ))->getCompiledUpdate('test'); - $this->assertTrue(is_string($sql)); + $this->assertTrue(\is_string($sql)); } public function testGetCompiledInsert() @@ -673,7 +677,7 @@ abstract class QBTest extends Query_TestCase { 'val' => 'baz' ))->getCompiledInsert('test'); - $this->assertTrue(is_string($sql)); + $this->assertTrue(\is_string($sql)); } public function testGetCompiledDelete() @@ -681,7 +685,7 @@ abstract class QBTest extends Query_TestCase { $sql = self::$db->where('id', 4) ->getCompiledDelete('test'); - $this->assertTrue(is_string($sql)); + $this->assertTrue(\is_string($sql)); } // ! Error tests /** diff --git a/tests/core/connection_manager_test.php b/tests/ConnectionManagerTest.php similarity index 65% rename from tests/core/connection_manager_test.php rename to tests/ConnectionManagerTest.php index 04cc1f6..599a140 100644 --- a/tests/core/connection_manager_test.php +++ b/tests/ConnectionManagerTest.php @@ -6,21 +6,25 @@ * * PHP version 7 * - * @package Query - * @author Timothy J. Warren + * @package Query + * @author Timothy J. Warren * @copyright 2012 - 2016 Timothy J. Warren - * @license http://www.opensource.org/licenses/mit-license.html MIT License - * @link https://git.timshomepage.net/aviat4ion/Query + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @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() { - self::$instance = Query\ConnectionManager::getInstance(); + self::$instance = ConnectionManager::getInstance(); } // -------------------------------------------------------------------------- @@ -30,25 +34,29 @@ class Connection_Manager_Test extends Query_TestCase { $this->expectException('DomainException'); $this->expectExceptionMessage("Can't clone singleton"); $clone = clone self::$instance; + $this->assertNull($clone); } // -------------------------------------------------------------------------- public function testNoSerialize() { - $this->setExpectedException('DomainException', "No serializing of singleton"); - $string = serialize(self::$instance); + $this->expectException(DomainException::class); + $this->expectExceptionMessage('No serializing of singleton'); + serialize(self::$instance); - $this->setExpectedException('DomainException', "No serializing of singleton"); - $string = self::$instance->__sleep(); + $this->expectException(DomainException::class); + $this->expectExceptionMessage('No serializing of singleton'); + self::$instance->__sleep(); } // -------------------------------------------------------------------------- public function testNoUnserialize() { - $this->setExpectedException('DomainException', "Can't unserialize singleton"); - $obj = self::$instance->__wakeup(); + $this->expectException(DomainException::class); + $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); - $this->assertInstanceOf('Query\\QueryBuilder', $conn); + $this->assertInstanceOf(QueryBuilderInterface::class, $conn); // Check that the connection just made is returned from the get_connection method @@ -109,9 +117,9 @@ class Connection_Manager_Test extends Query_TestCase { ); $conn = self::$instance->connect($params); - $this->assertInstanceOf('Query\\QueryBuilder', $conn); + $this->assertInstanceOf(QueryBuilderInterface::class, $conn); $this->assertEqual($conn, self::$instance->getConnection('conn_manager')); } } -// End of connection_manager_test.php \ No newline at end of file +// End of connection_manager_test.php diff --git a/tests/core/core_test.php b/tests/CoreTest.php similarity index 83% rename from tests/core/core_test.php rename to tests/CoreTest.php index 333c083..718cbde 100644 --- a/tests/core/core_test.php +++ b/tests/CoreTest.php @@ -13,6 +13,8 @@ * @link https://git.timshomepage.net/aviat4ion/Query */ +namespace Query\Tests; + // -------------------------------------------------------------------------- @@ -21,7 +23,7 @@ * * @extends UnitTestCase */ -class CoreTest extends Query_TestCase { +class CoreTest extends TestCase { /** * TestPHPVersion function. @@ -31,7 +33,8 @@ class CoreTest extends Query_TestCase { */ 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 - $supported = array( + $supported = [ 'firebird', 'mysql', 'pgsql', 'odbc', 'sqlite', - ); + ]; - $drivers = PDO::getAvailableDrivers(); + $drivers = \PDO::getAvailableDrivers(); $numSupported = count(array_intersect($drivers, $supported)); diff --git a/tests/databases/firebird/FirebirdQBTest.php b/tests/Drivers/Firebird/FirebirdQBTest.php similarity index 100% rename from tests/databases/firebird/FirebirdQBTest.php rename to tests/Drivers/Firebird/FirebirdQBTest.php diff --git a/tests/databases/firebird/FirebirdTest.php b/tests/Drivers/Firebird/FirebirdTest.php similarity index 100% rename from tests/databases/firebird/FirebirdTest.php rename to tests/Drivers/Firebird/FirebirdTest.php diff --git a/tests/databases/mysql/MySQLTest.php b/tests/Drivers/MySQL/MySQLDriverTest.php similarity index 90% rename from tests/databases/mysql/MySQLTest.php rename to tests/Drivers/MySQL/MySQLDriverTest.php index d6f3b38..5c21207 100644 --- a/tests/databases/mysql/MySQLTest.php +++ b/tests/Drivers/MySQL/MySQLDriverTest.php @@ -13,7 +13,12 @@ * @link https://git.timshomepage.net/aviat4ion/Query */ +namespace Query\Tests\Drivers\MySQL; + +use InvalidArgumentException; +use PDO; use Query\Drivers\Mysql\Driver; +use Query\Tests\BaseDriverTest; /** * MySQLTest class. @@ -21,12 +26,12 @@ use Query\Drivers\Mysql\Driver; * @extends DBTest * @requires extension pdo_mysql */ -class MySQLTest extends DBTest { +class MySQLDriverTest extends BaseDriverTest { public static function setUpBeforeClass() { $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'); } @@ -54,7 +59,7 @@ class MySQLTest extends DBTest { 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() { self::$db->truncate('test'); + $this->assertEquals(0, self::$db->countAll('test')); + self::$db->truncate('join'); + $this->assertEquals(0, self::$db->countAll('join')); } // -------------------------------------------------------------------------- @@ -125,18 +133,14 @@ SQL; public function testBadPreparedStatement() { + $this->expectException(InvalidArgumentException::class); + $sql = <<prepareQuery($sql, 'foo'); - } - catch(InvalidArgumentException $e) - { - $this->assertTrue(TRUE); - } + + self::$db->prepareQuery($sql, 'foo'); } diff --git a/tests/databases/mysql/MySQLQBTest.php b/tests/Drivers/MySQL/MySQLQueryBuilderTest.php similarity index 89% rename from tests/databases/mysql/MySQLQBTest.php rename to tests/Drivers/MySQL/MySQLQueryBuilderTest.php index a7f2947..521de1c 100644 --- a/tests/databases/mysql/MySQLQBTest.php +++ b/tests/Drivers/MySQL/MySQLQueryBuilderTest.php @@ -13,13 +13,17 @@ * @link https://git.timshomepage.net/aviat4ion/Query */ +namespace Query\Tests\Drivers\MySQL; + +use PDO; +use Query\Tests\BaseQueryBuilderTest; // -------------------------------------------------------------------------- /** * @requires extension pdo_mysql */ -class MySQLQBTest extends QBTest { +class MySQLQueryBuilderTest extends BaseQueryBuilderTest { public static function setUpBeforeClass() { @@ -52,7 +56,7 @@ class MySQLQBTest extends QBTest { public function testExists() { - $this->assertTrue(in_array('mysql', PDO::getAvailableDrivers())); + $this->assertTrue(\in_array('mysql', PDO::getAvailableDrivers(), TRUE)); } // -------------------------------------------------------------------------- diff --git a/tests/databases/pgsql/PgSQLTest.php b/tests/Drivers/PgSQL/PgSQLDriverTest.php similarity index 80% rename from tests/databases/pgsql/PgSQLTest.php rename to tests/Drivers/PgSQL/PgSQLDriverTest.php index 6445c96..3ba913c 100644 --- a/tests/databases/pgsql/PgSQLTest.php +++ b/tests/Drivers/PgSQL/PgSQLDriverTest.php @@ -13,42 +13,44 @@ * @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. * * @extends DBTest * @requires extension pdo_pgsql */ -class PgTest extends DBTest { +class PgSQLDriverTest extends BaseDriverTest { public function setUp() - { - $class = "Query\\Drivers\\Pgsql\\Driver"; - + { // 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() { - $class = "Query\\Drivers\\Pgsql\\Driver"; $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 else if ($params !== FALSE) { $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_'); @@ -59,7 +61,7 @@ class PgTest extends DBTest { public function testExists() { $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; - $this->assertIsA(self::$db, '\\Query\\Drivers\\Pgsql\\Driver'); + $this->assertIsA(self::$db, Driver::class); } // -------------------------------------------------------------------------- @@ -119,7 +121,7 @@ class PgTest extends DBTest { //Check $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() { - self::$db->truncate('create_test'); - self::$db->truncate('create_join'); + self::$db->truncate('test'); + $this->assertEquals(0, self::$db->countAll('test')); - $ctQuery = self::$db->query('SELECT * FROM create_test'); - $cjQuery = self::$db->query('SELECT * FROM create_join'); + self::$db->truncate('join'); + $this->assertEquals(0, self::$db->countAll('join')); } // -------------------------------------------------------------------------- @@ -142,7 +144,7 @@ class PgTest extends DBTest { INSERT INTO "create_test" ("id", "key", "val") VALUES (?,?,?) SQL; - $statement = self::$db->prepareQuery($sql, array(1,"boogers", "Gross")); + $statement = self::$db->prepareQuery($sql, array(1,'boogers', 'Gross')); $statement->execute(); @@ -152,19 +154,14 @@ SQL; public function testBadPreparedStatement() { + $this->expectException(InvalidArgumentException::class); + $sql = <<prepareQuery($sql, 'foo'); - } - catch(InvalidArgumentException $e) - { - $this->assertTrue(TRUE); - } + self::$db->prepareQuery($sql, 'foo'); } // -------------------------------------------------------------------------- @@ -189,7 +186,7 @@ SQL; { if (empty(self::$db)) return; - $res = self::$db->beginTransaction(); + self::$db->beginTransaction(); $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)'; self::$db->query($sql); @@ -204,7 +201,7 @@ SQL; { if (empty(self::$db)) return; - $res = self::$db->beginTransaction(); + self::$db->beginTransaction(); $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)'; self::$db->query($sql); @@ -233,4 +230,4 @@ SQL; { $this->assertNull(self::$db->getFunctions()); } -} \ No newline at end of file +} diff --git a/tests/databases/pgsql/PgSQLQBTest.php b/tests/Drivers/PgSQL/PgSQLQueryBuilderTest.php similarity index 95% rename from tests/databases/pgsql/PgSQLQBTest.php rename to tests/Drivers/PgSQL/PgSQLQueryBuilderTest.php index 0c46c54..d76a906 100644 --- a/tests/databases/pgsql/PgSQLQBTest.php +++ b/tests/Drivers/PgSQL/PgSQLQueryBuilderTest.php @@ -13,13 +13,17 @@ * @link https://git.timshomepage.net/aviat4ion/Query */ +namespace Query\Tests\Drivers\PgSQL; + +use PDO; +use Query\Tests\BaseQueryBuilderTest; // -------------------------------------------------------------------------- /** * @requires extension pdo_pgsql */ -class PgSQLQBTest extends QBTest { +class PgSQLQueryBuilderTest extends BaseQueryBuilderTest { public static function setUpBeforeClass() { diff --git a/tests/databases/sqlite/SQLiteTest.php b/tests/Drivers/SQLite/SQLiteDriverTest.php similarity index 96% rename from tests/databases/sqlite/SQLiteTest.php rename to tests/Drivers/SQLite/SQLiteDriverTest.php index 619b25d..5a97280 100644 --- a/tests/databases/sqlite/SQLiteTest.php +++ b/tests/Drivers/SQLite/SQLiteDriverTest.php @@ -13,6 +13,11 @@ * @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 * @requires extension pdo_sqlite */ -class SQLiteTest extends DBTest { +class SQLiteDriverTest extends BaseDriverTest { public static function setupBeforeClass() { @@ -175,7 +180,7 @@ SQL; public function testConnection() { - $class = '\\Query\\Drivers\\Sqlite\\Driver'; + $class = Driver::class; $db = new $class(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'); @@ -190,6 +195,7 @@ SQL; public function testTruncate() { self::$db->truncate('create_test'); + $this->assertEquals(0, self::$db->countAll('create_test')); } // -------------------------------------------------------------------------- @@ -281,7 +287,7 @@ SQL; public function testGetSystemTables() { $sql = self::$db->getSystemTables(); - $this->assertTrue(is_array($sql)); + $this->assertTrue(\is_array($sql)); } // -------------------------------------------------------------------------- diff --git a/tests/databases/sqlite/SQLiteQBTest.php b/tests/Drivers/SQLite/SQLiteQueryBuilderTest.php similarity index 94% rename from tests/databases/sqlite/SQLiteQBTest.php rename to tests/Drivers/SQLite/SQLiteQueryBuilderTest.php index 3235a81..876a977 100644 --- a/tests/databases/sqlite/SQLiteQBTest.php +++ b/tests/Drivers/SQLite/SQLiteQueryBuilderTest.php @@ -13,6 +13,10 @@ * @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 */ - class SQLiteQBTest extends QBTest { + class SQLiteQueryBuilderTest extends BaseQueryBuilderTest { public static function setUpBeforeClass() { diff --git a/tests/core/query_parser_test.php b/tests/QueryParserTest.php similarity index 80% rename from tests/core/query_parser_test.php rename to tests/QueryParserTest.php index d712ffa..4914cd7 100644 --- a/tests/core/query_parser_test.php +++ b/tests/QueryParserTest.php @@ -4,27 +4,28 @@ * * SQL Query Builder / Database Abstraction Layer * - * PHP version 7 + * PHP version 7.1 * * @package Query * @author Timothy J. Warren - * @copyright 2012 - 2016 Timothy J. Warren + * @copyright 2012 - 2018 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 Query\QueryParser; +use Query\Drivers\Sqlite\Driver; /** * Tests for the Query Parser */ -class Query_Parser_Test extends Query_TestCase { +class QueryParserTest extends TestCase { public function setUp() { - $db = new Query\Drivers\Sqlite\Driver("sqlite::memory:"); - $this->parser = new Query\QueryParser($db); + $db = new Driver('sqlite::memory:'); + $this->parser = new QueryParser($db); } public function testGeneric() diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..201b1a5 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,72 @@ + + * @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); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c02df2b..d0c614f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,8 +11,6 @@ * @license http://philsturgeon.co.uk/code/dbad-license */ -use PHPUnit\Framework\TestCase; - /** * Unit test bootstrap - Using phpunit */ @@ -20,9 +18,6 @@ define('QTEST_DIR', realpath(__DIR__)); define('QBASE_DIR', realpath(QTEST_DIR.'/../') . '/'); define('QDS', DIRECTORY_SEPARATOR); -// Set up autoloader -require_once QBASE_DIR . 'vendor/autoload.php'; - // -------------------------------------------------------------------------- function get_json_config() @@ -43,67 +38,8 @@ function get_json_config() 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'; @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 diff --git a/tests/index.php b/tests/index.php index 9fdb449..3ca3bec 100644 --- a/tests/index.php +++ b/tests/index.php @@ -1,4 +1,4 @@ -'; + + if (method_exists($class, 'setupBeforeClass')) { + $class::setupBeforeClass(); + } + + parent::__construct(); + } + + public function __destruct() + { + $class = \get_class($this); + + if (method_exists($class, 'tearDownAfterClass')) { + $class::tearDownAfterClass(); + } + } + + /** + * Define assertInstanceOf for simpletest + * + * @param $expected + * @param $actual + * @param string $message + */ + public function assertInstanceOf($expected, $actual, $message = '') + { + $this->assertIsA($actual, $expected, $message); + } + + /** + * Alias to assertEqual + * + * @param mixed $expected + * @param mixed $actual + * @param string $message + */ + public function assertEquals($expected, $actual, $message = '') + { + $this->assertEqual($expected, $actual, $message); + } + + /** + * Alias to skipIf in SimpleTest + * + * @param string $message + */ + public function markTestSkipped($message = '') + { + $this->skipUnless(FALSE, $message); + } + + /** + * Alias to the method in PHPUnit + * + * @param string $message + */ + public function expectExceptionMessage($message) + { + // noop } } - return FALSE; } -// -------------------------------------------------------------------------- - -// Set up autoloaders -require_once __DIR__ . '/../vendor/autoload.php'; -require_once __DIR__ . '/../vendor/simpletest/simpletest/autorun.php'; - /** - * Base class for TestCases + * Load the test suites */ -abstract class Query_TestCase extends UnitTestCase { - - public function __construct() +namespace { + function get_json_config() { - $class = get_class($this); + $files = array( + __DIR__ . '/settings.json', + __DIR__ . '/settings.json.dist' + ); - if (method_exists($class, 'setupBeforeClass')) - { - $class::setupBeforeClass(); + foreach ($files as $file) { + if (is_file($file)) { + return json_decode(file_get_contents($file)); + } } - parent::__construct(); + return FALSE; } - public function __destruct() - { - $class = get_class($this); + // Include db tests + // Load db classes based on capability + $testPath = QTEST_DIR.'/Drivers/'; - if (method_exists($class, 'tearDownAfterClass')) + // Require base testing classes + require_once QTEST_DIR . '/CoreTest.php'; + require_once QTEST_DIR . '/ConnectionManagerTest.php'; + require_once QTEST_DIR . '/QueryParserTest.php'; + + $drivers = PDO::getAvailableDrivers(); + + /* if (function_exists('fbird_connect')) + { + $drivers[] = 'interbase'; + } */ + + $driverTestMap = [ + 'MySQL' => \in_array('mysql', $drivers, TRUE), + 'SQLite' => \in_array('sqlite', $drivers, TRUE), + 'PgSQL' => \in_array('pgsql', $drivers, TRUE), + // 'Firebird' => in_array('interbase', $drivers), + //'PDOFirebird' => in_array('firebird', $drivers) + ]; + + // Determine which testcases to load + foreach($driverTestMap as $name => $doLoad) + { + $path = $testPath . $name; + + if ($doLoad) { - $class::tearDownAfterClass(); + require_once "{$path}/{$name}DriverTest.php"; + require_once "{$path}/{$name}QueryBuilderTest.php"; } } - - /** - * Define assertInstanceOf for simpletest - * - * @param $expected - * @param $actual - * @param string $message - */ - public function assertInstanceOf($expected, $actual, $message = '') - { - $this->assertIsA($actual, $expected, $message); - } - - /** - * Alias to assertEqual - * - * @param mixed $expected - * @param mixed $actual - * @param string $message - */ - public function assertEquals($expected, $actual, $message = '') - { - $this->assertEqual($expected, $actual, $message); - } - - /** - * Alias to skipIf in SimpleTest - * - * @param string $message - */ - public function markTestSkipped($message = '') - { - $this->skipUnless(FALSE, $message); - } - - /** - * Alias to the method in PHPUnit - * - * @param string $message - */ - public function expectExceptionMessage($message) - { - // noop - } - - /** - * Alias for phpunit method - * - * @param string $name - * @param string $message - * @param int $code - */ - public function setExpectedException($name, $message='', $code=NULL) - { - $this->expectException($name, $message); - } } -// -------------------------------------------------------------------------- -/** - * Unit test bootstrap - Using php simpletest - */ -define('QTEST_DIR', __DIR__); -define('QBASE_DIR', realpath(__DIR__ . '/../') . '/'); -define('QDS', DIRECTORY_SEPARATOR); - -// Include db tests -// Load db classes based on capability -$testPath = QTEST_DIR.'/databases/'; - -// Require base testing classes -require_once QTEST_DIR . '/core/core_test.php'; -require_once QTEST_DIR . '/core/connection_manager_test.php'; -require_once QTEST_DIR . '/core/base_db_test.php'; -require_once QTEST_DIR . '/core/query_parser_test.php'; -require_once QTEST_DIR . '/core/base_query_builder_test.php'; - -$drivers = PDO::getAvailableDrivers(); - -if (function_exists('fbird_connect')) -{ - $drivers[] = 'interbase'; -} - -$driverTestMap = [ - 'MySQL' => in_array('mysql', $drivers, TRUE), - 'SQLite' => in_array('sqlite', $drivers, TRUE), - 'PgSQL' => in_array('pgsql', $drivers, TRUE), - // 'Firebird' => in_array('interbase', $drivers), - //'PDOFirebird' => in_array('firebird', $drivers) -]; - -// Determine which testcases to load -foreach($driverTestMap as $name => $doLoad) -{ - $path = $testPath . strtolower($name) . '/'; - - if ($doLoad) - { - require_once "{$path}{$name}Test.php"; - require_once "{$path}{$name}QBTest.php"; - } -} // End of index.php