Update namespace of unsupported Firebird driver, add assertions to some risky tests

This commit is contained in:
Timothy Warren 2018-01-22 12:03:37 -05:00
parent 1eb146bb67
commit 89150ceafc
9 changed files with 107 additions and 77 deletions

View File

@ -3,26 +3,26 @@
addUncoveredFilesFromWhitelist="true"
colors="true"
stopOnFailure="false"
bootstrap="../tests/bootstrap.php">
bootstrap="./../tests/bootstrap.php">
<filter>
<whitelist>
<directory suffix=".php">../src/*</directory>
<directory suffix=".php">./../src/*</directory>
</whitelist>
</filter>
<testsuites>
<testsuite name="CoreTests">
<file>../tests/CoreTest.php</file>
<file>../tests/ConnectionManagerTest.php</file>
<file>../tests/QueryParserTest.php</file>
<file>./../tests/CoreTest.php</file>
<file>./../tests/ConnectionManagerTest.php</file>
<file>./../tests/QueryParserTest.php</file>
</testsuite>
<testsuite name="MySQL Tests">
<directory>../tests/Drivers/MySQL/</directory>
<directory>./../tests/Drivers/MySQL/</directory>
</testsuite>
<testsuite name="PgSQL Tests">
<directory>../tests/Drivers/PgSQL/</directory>
<directory>./../tests/Drivers/PgSQL/</directory>
</testsuite>
<testsuite name="SQLite Tests">
<directory>../tests/Drivers/SQLite/</directory>
<directory>./../tests/Drivers/SQLite/</directory>
</testsuite>
<!-- <testsuite name="FirebirdTests">
<file>../tests/databases/firebird/FirebirdTest.php</file>
@ -34,7 +34,7 @@
</testsuite> -->
</testsuites>
<logging>
<log type="coverage-html" target="coverage"/>
<log type="coverage-html" target="./../coverage"/>
<log type="coverage-clover" target="logs/clover.xml"/>
<log type="coverage-xml" target="logs/coverage" />
<log type="xml" target="logs/junit.xml" logIncompleteSkipped="true"/>

View File

@ -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)
{

View File

@ -19,8 +19,6 @@ use Query\{
QueryBuilderInterface
};
require __DIR__ . '/../vendor/autoload.php';
// --------------------------------------------------------------------------
/**

View File

@ -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',
];

View File

@ -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));
}
// --------------------------------------------------------------------------

View File

@ -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;

View File

@ -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);
}
// --------------------------------------------------------------------------

View File

@ -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);
}
// --------------------------------------------------------------------------

View File

@ -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