From 89150ceafc31e7c3dff16376a6bdfc8ab06f8cfd Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Mon, 22 Jan 2018 12:03:37 -0500 Subject: [PATCH] Update namespace of unsupported Firebird driver, add assertions to some risky tests --- build/phpunit.xml | 18 +++--- src/AbstractQueryBuilder.php | 62 +++++++++---------- src/common.php | 2 - tests/CoreTest.php | 6 +- ...irebirdTest.php => FirebirdDriverTest.php} | 23 ++++--- ...BTest.php => FirebirdQueryBuilderTest.php} | 13 ++-- tests/Drivers/PgSQL/PgSQLDriverTest.php | 21 ++++++- tests/Drivers/SQLite/SQLiteDriverTest.php | 31 +++++++--- tests/index.php | 8 --- 9 files changed, 107 insertions(+), 77 deletions(-) rename tests/Drivers/Firebird/{FirebirdTest.php => FirebirdDriverTest.php} (91%) rename tests/Drivers/Firebird/{FirebirdQBTest.php => FirebirdQueryBuilderTest.php} (92%) diff --git a/build/phpunit.xml b/build/phpunit.xml index b18682b..e252fcb 100644 --- a/build/phpunit.xml +++ b/build/phpunit.xml @@ -3,26 +3,26 @@ addUncoveredFilesFromWhitelist="true" colors="true" stopOnFailure="false" - bootstrap="../tests/bootstrap.php"> + bootstrap="./../tests/bootstrap.php"> - ../src/* + ./../src/* - ../tests/CoreTest.php - ../tests/ConnectionManagerTest.php - ../tests/QueryParserTest.php + ./../tests/CoreTest.php + ./../tests/ConnectionManagerTest.php + ./../tests/QueryParserTest.php - ../tests/Drivers/MySQL/ + ./../tests/Drivers/MySQL/ - ../tests/Drivers/PgSQL/ + ./../tests/Drivers/PgSQL/ - ../tests/Drivers/SQLite/ + ./../tests/Drivers/SQLite/ - + diff --git a/src/AbstractQueryBuilder.php b/src/AbstractQueryBuilder.php index dee307f..51768a6 100644 --- a/src/AbstractQueryBuilder.php +++ b/src/AbstractQueryBuilder.php @@ -198,7 +198,7 @@ abstract class AbstractQueryBuilder { foreach($arg as $k => $v) { - if (in_array($valType, [self::KEY, self::VALUE])) + if (\in_array($valType, [self::KEY, self::VALUE], TRUE)) { $var[] = ($valType === self::KEY) ? $k @@ -225,7 +225,7 @@ abstract class AbstractQueryBuilder { // Escape the identifiers $field = $this->db->quoteIdent($field); - if ( ! is_string($as)) + if ( ! \is_string($as)) { return $field; } @@ -263,20 +263,20 @@ abstract class AbstractQueryBuilder { * @param string $pos * @param string $like * @param string $conj - * @return QueryBuilderInterface + * @return self */ - protected function _like(string $field, $val, string $pos, string $like='LIKE', string $conj='AND'): QueryBuilderInterface + protected function _like(string $field, $val, string $pos, string $like='LIKE', string $conj='AND'): self { $field = $this->db->quoteIdent($field); // Add the like string into the order map $like = $field. " {$like} ?"; - if ($pos == 'before') + if ($pos === 'before') { $val = "%{$val}"; } - elseif ($pos == 'after') + elseif ($pos === 'after') { $val = "{$val}%"; } @@ -285,7 +285,7 @@ abstract class AbstractQueryBuilder { $val = "%{$val}%"; } - $conj = (empty($this->queryMap)) ? ' WHERE ' : " {$conj} "; + $conj = empty($this->queryMap) ? ' WHERE ' : " {$conj} "; $this->_appendMap($conj, $like, 'like'); // Add to the values array @@ -298,13 +298,13 @@ abstract class AbstractQueryBuilder { * Simplify building having clauses * * @param mixed $key - * @param mixed $val + * @param mixed $values * @param string $conj - * @return QueryBuilderInterface + * @return self */ - protected function _having($key, $val=[], string $conj='AND'): QueryBuilderInterface + protected function _having($key, $values=[], string $conj='AND'): self { - $where = $this->_where($key, $val); + $where = $this->_where($key, $values); // Create key/value placeholders foreach($where as $f => $val) @@ -335,10 +335,10 @@ abstract class AbstractQueryBuilder { * @param mixed $val * @return array */ - protected function _where($key, $val=[]): array + protected function _where($key, array $val=[]): array { $where = []; - $this->_mixedSet($where, $key, $val, self::BOTH); + $this->_mixedSet($where, $key, $val); $this->_mixedSet($this->whereValues, $key, $val, self::VALUE); return $where; } @@ -347,14 +347,14 @@ abstract class AbstractQueryBuilder { * Simplify generating where string * * @param mixed $key - * @param mixed $val + * @param mixed $values * @param string $defaultConj - * @return QueryBuilderInterface + * @return self */ - protected function _whereString($key, $val=[], string $defaultConj='AND'): QueryBuilderInterface + protected function _whereString($key, array $values=[], string $defaultConj='AND'): self { // Create key/value placeholders - foreach($this->_where($key, $val) as $f => $val) + foreach($this->_where($key, $values) as $f => $val) { // Split each key by spaces, in case there // is an operator such as >, <, !=, etc. @@ -394,9 +394,9 @@ abstract class AbstractQueryBuilder { * @param mixed $val * @param string $in - The (not) in fragment * @param string $conj - The where in conjunction - * @return QueryBuilderInterface + * @return self */ - protected function _whereIn($key, $val=[], string $in='IN', string $conj='AND'): QueryBuilderInterface + protected function _whereIn($key, $val=[], string $in='IN', string $conj='AND'): self { $key = $this->db->quoteIdent($key); $params = array_fill(0, count($val), '?'); @@ -426,19 +426,19 @@ abstract class AbstractQueryBuilder { */ protected function _run(string $type, string $table, $sql=NULL, $vals=NULL, bool $reset=TRUE): PDOStatement { - if (is_null($sql)) + if ($sql === NULL) { $sql = $this->_compile($type, $table); } - if (is_null($vals)) + if ($vals === NULL) { $vals = array_merge($this->values, (array) $this->whereValues); } $startTime = microtime(TRUE); - $res = (empty($vals)) + $res = empty($vals) ? $this->db->query($sql) : $this->db->prepareExecute($sql, $vals); @@ -467,11 +467,11 @@ abstract class AbstractQueryBuilder { */ protected function _appendMap(string $conjunction = '', string $string = '', string $type = '') { - array_push($this->queryMap, [ + $this->queryMap[] = [ 'type' => $type, 'conjunction' => $conjunction, 'string' => $string - ]); + ]; } /** @@ -500,7 +500,7 @@ abstract class AbstractQueryBuilder { // Add the interpreted query to the list of executed queries $this->queries[] = [ 'time' => $totalTime, - 'sql' => call_user_func_array('sprintf', $evals), + 'sql' => sprintf(...$evals) ]; $this->queries['total_time'] += $totalTime; @@ -520,7 +520,7 @@ abstract class AbstractQueryBuilder { { switch($type) { - case "insert": + case 'insert': $paramCount = count($this->setArrayKeys); $params = array_fill(0, $paramCount, '?'); $sql = "INSERT INTO {$table} (" @@ -528,16 +528,16 @@ abstract class AbstractQueryBuilder { . ")\nVALUES (".implode(',', $params).')'; break; - case "update": + case 'update': $sql = "UPDATE {$table}\nSET {$this->setString}"; break; - case "replace": + case 'replace': // @TODO implement - $sql = ""; + $sql = ''; break; - case "delete": + case 'delete': $sql = "DELETE FROM {$table}"; break; @@ -580,7 +580,7 @@ abstract class AbstractQueryBuilder { foreach($clauses as $clause) { $param = $this->$clause; - if (is_array($param)) + if (\is_array($param)) { foreach($param as $q) { diff --git a/src/common.php b/src/common.php index 7d1861e..df4990e 100644 --- a/src/common.php +++ b/src/common.php @@ -19,8 +19,6 @@ use Query\{ QueryBuilderInterface }; -require __DIR__ . '/../vendor/autoload.php'; - // -------------------------------------------------------------------------- /** diff --git a/tests/CoreTest.php b/tests/CoreTest.php index 52274e9..5ac2d5b 100644 --- a/tests/CoreTest.php +++ b/tests/CoreTest.php @@ -30,7 +30,7 @@ class CoreTest extends TestCase { * @access public * @return void */ - public function testPHPVersion() + public function testPHPVersion(): void { //$this->assertTrue(version_compare(PHP_VERSION, '7.1', 'ge')); $this->assertTrue(PHP_VERSION_ID >= 70000); @@ -44,7 +44,7 @@ class CoreTest extends TestCase { * @access public * @return void */ - public function testHasPDO() + public function testHasPDO(): void { // PDO class exists $this->assertTrue(class_exists('PDO')); @@ -52,10 +52,8 @@ class CoreTest extends TestCase { // Make sure at least one of the supported drivers is enabled $supported = [ - 'firebird', 'mysql', 'pgsql', - 'odbc', 'sqlite', ]; diff --git a/tests/Drivers/Firebird/FirebirdTest.php b/tests/Drivers/Firebird/FirebirdDriverTest.php similarity index 91% rename from tests/Drivers/Firebird/FirebirdTest.php rename to tests/Drivers/Firebird/FirebirdDriverTest.php index d4f1503..85f451d 100644 --- a/tests/Drivers/Firebird/FirebirdTest.php +++ b/tests/Drivers/Firebird/FirebirdDriverTest.php @@ -13,6 +13,11 @@ * @link https://git.timshomepage.net/aviat4ion/Query */ +namespace Query\Tests\Drivers; + +use PDO; +use Query\Drivers\Firebird\Driver; +use Query\Tests\BaseDriverTest; // -------------------------------------------------------------------------- @@ -24,20 +29,20 @@ * @extends DBtest * @requires extension interbase */ -class FirebirdTest extends DBtest { +class FirebirdDriverTest extends BaseDriverTest { 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 = new Driver('localhost:'.$dbpath); self::$db->setTablePrefix('create_'); } public function setUp() { - if ( ! function_exists('\\fbird_connect')) + if ( ! \function_exists('\\fbird_connect')) { $this->markTestSkipped('Firebird extension does not exist'); } @@ -73,15 +78,15 @@ class FirebirdTest extends DBtest { public function testExists() { - $this->assertTrue(function_exists('ibase_connect')); - $this->assertTrue(function_exists('fbird_connect')); + $this->assertTrue(\function_exists('ibase_connect')); + $this->assertTrue(\function_exists('fbird_connect')); } // -------------------------------------------------------------------------- public function testConnection() { - $this->assertIsA(self::$db, '\\Query\\Drivers\\Firebird\\Driver'); + $this->assertIsA(self::$db, Driver::class); } // -------------------------------------------------------------------------- @@ -119,7 +124,7 @@ class FirebirdTest extends DBtest { self::$db->query($sql); //Check - $this->assertTrue(in_array('create_delete', self::$db->getTables())); + $this->assertTrue(\in_array('create_delete', self::$db->getTables(), TRUE)); } // -------------------------------------------------------------------------- @@ -131,7 +136,7 @@ class FirebirdTest extends DBtest { self::$db->query($sql); //Check - $tableExists = in_array('create_delete', self::$db->getTables()); + $tableExists = \in_array('create_delete', self::$db->getTables(), TRUE); $this->assertFalse($tableExists); } @@ -214,7 +219,7 @@ SQL; // Numeric array $res2 = self::$db->query('SELECT "id","key","val" FROM "create_test"'); $fetch = $res2->fetch(PDO::FETCH_NUM); - $this->assertTrue(is_array($fetch)); + $this->assertTrue(\is_array($fetch)); } // -------------------------------------------------------------------------- diff --git a/tests/Drivers/Firebird/FirebirdQBTest.php b/tests/Drivers/Firebird/FirebirdQueryBuilderTest.php similarity index 92% rename from tests/Drivers/Firebird/FirebirdQBTest.php rename to tests/Drivers/Firebird/FirebirdQueryBuilderTest.php index 174fd58..3bd27a4 100644 --- a/tests/Drivers/Firebird/FirebirdQBTest.php +++ b/tests/Drivers/Firebird/FirebirdQueryBuilderTest.php @@ -13,6 +13,11 @@ * @link https://git.timshomepage.net/aviat4ion/Query */ +namespace Query\Tests\Drivers; + +use PDO; +use Query\Tests\BaseQueryBuilderTest; +use InvalidArgumentException; // -------------------------------------------------------------------------- @@ -20,14 +25,14 @@ * Firebird Query Builder Tests * @requires extension interbase */ -class FirebirdQBTest extends QBTest { +class FirebirdQueryBuilderTest extends BaseQueryBuilderTest { public static function setUpBeforeClass() { $dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB'; // test the query builder - $params = new Stdclass(); + $params = new \Stdclass(); $params->alias = 'fire'; $params->type = 'firebird'; $params->file = $dbpath; @@ -40,7 +45,7 @@ class FirebirdQBTest extends QBTest { public function setUp() { - if ( ! function_exists('\\fbird_connect')) + if ( ! \function_exists('\\fbird_connect')) { $this->markTestSkipped('Firebird extension does not exist'); } @@ -76,7 +81,7 @@ class FirebirdQBTest extends QBTest { $dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB'; // test the query builder - $params = new Stdclass(); + $params = new \Stdclass(); $params->alias = 'wood'; $params->type = 'firebird'; $params->file = $dbpath; diff --git a/tests/Drivers/PgSQL/PgSQLDriverTest.php b/tests/Drivers/PgSQL/PgSQLDriverTest.php index 75ef841..3369d49 100644 --- a/tests/Drivers/PgSQL/PgSQLDriverTest.php +++ b/tests/Drivers/PgSQL/PgSQLDriverTest.php @@ -17,6 +17,7 @@ namespace Query\Tests\Drivers\PgSQL; // -------------------------------------------------------------------------- use InvalidArgumentException; +use PDO; use Query\Drivers\Pgsql\Driver; use Query\Tests\BaseDriverTest; @@ -59,7 +60,7 @@ class PgSQLDriverTest extends BaseDriverTest { public function testExists() { - $drivers = \PDO::getAvailableDrivers(); + $drivers = PDO::getAvailableDrivers(); $this->assertTrue(in_array('pgsql', $drivers, TRUE)); } @@ -147,6 +148,14 @@ SQL; $statement->execute(); + $res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=1') + ->fetch(PDO::FETCH_ASSOC); + + $this->assertEquals([ + 'id' => 1, + 'key' => 'boogers', + 'val' => 'Gross' + ], $res); } // -------------------------------------------------------------------------- @@ -174,9 +183,17 @@ SQL; VALUES (?,?,?) SQL; self::$db->prepareExecute($sql, array( - 2, "works", 'also?' + 2, 'works', 'also?' )); + $res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=2') + ->fetch(PDO::FETCH_ASSOC); + + $this->assertEquals([ + 'id' => 2, + 'key' => 'works', + 'val' => 'also?' + ], $res); } // -------------------------------------------------------------------------- diff --git a/tests/Drivers/SQLite/SQLiteDriverTest.php b/tests/Drivers/SQLite/SQLiteDriverTest.php index 338a62e..41f5102 100644 --- a/tests/Drivers/SQLite/SQLiteDriverTest.php +++ b/tests/Drivers/SQLite/SQLiteDriverTest.php @@ -30,7 +30,6 @@ class SQLiteDriverTest extends BaseDriverTest { public static function setupBeforeClass() { - $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; $params = array( 'type' => 'sqlite', 'file' => ':memory:', @@ -56,13 +55,13 @@ class SQLiteDriverTest extends BaseDriverTest { //Check $dbs = self::$db->getTables(); - $this->assertTrue(in_array('TEST1', $dbs, TRUE)); - $this->assertTrue(in_array('TEST2', $dbs, TRUE)); - $this->assertTrue(in_array('NUMBERS', $dbs, TRUE)); - $this->assertTrue(in_array('NEWTABLE', $dbs, TRUE)); - $this->assertTrue(in_array('create_test', $dbs, TRUE)); - $this->assertTrue(in_array('create_join', $dbs, TRUE)); - $this->assertTrue(in_array('create_delete', $dbs, TRUE)); + $this->assertTrue(\in_array('TEST1', $dbs, TRUE)); + $this->assertTrue(\in_array('TEST2', $dbs, TRUE)); + $this->assertTrue(\in_array('NUMBERS', $dbs, TRUE)); + $this->assertTrue(\in_array('NEWTABLE', $dbs, TRUE)); + $this->assertTrue(\in_array('create_test', $dbs, TRUE)); + $this->assertTrue(\in_array('create_join', $dbs, TRUE)); + $this->assertTrue(\in_array('create_delete', $dbs, TRUE)); } // -------------------------------------------------------------------------- @@ -209,6 +208,14 @@ SQL; $statement->execute(); + $res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=1') + ->fetch(PDO::FETCH_ASSOC); + + $this->assertEquals([ + 'id' => 1, + 'key' => 'boogers', + 'val' => 'Gross' + ], $res); } // -------------------------------------------------------------------------- @@ -223,6 +230,14 @@ SQL; 2, "works", 'also?' )); + $res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=2') + ->fetch(PDO::FETCH_ASSOC); + + $this->assertEquals([ + 'id' => 2, + 'key' => 'works', + 'val' => 'also?' + ], $res); } // -------------------------------------------------------------------------- diff --git a/tests/index.php b/tests/index.php index 7ee008e..28ea36c 100644 --- a/tests/index.php +++ b/tests/index.php @@ -131,18 +131,10 @@ namespace { 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