Reformat test files

This commit is contained in:
Timothy Warren 2023-03-17 15:30:36 -04:00
parent cf2255ce87
commit 282935d9f8
15 changed files with 216 additions and 184 deletions

View File

@ -13,6 +13,7 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests;
use Query\QueryBuilderInterface;
@ -20,8 +21,8 @@ use Query\QueryBuilderInterface;
/**
* Parent Database Test Class
*/
abstract class BaseDriverTest extends TestCase {
abstract class BaseDriverTest extends TestCase
{
/**
* @var QueryBuilderInterface|null
*/
@ -37,34 +38,34 @@ abstract class BaseDriverTest extends TestCase {
public function testGetTables(): void
{
$tables = self::$db->getTables();
$this->assertTrue(\is_array($tables));
$this->assertIsArray($tables);
$this->assertTrue( ! empty($tables));
}
public function testGetSystemTables(): void
{
$tables = self::$db->getSystemTables();
$this->assertTrue(\is_array($tables));
$this->assertIsArray($tables);
$this->assertTrue( ! empty($tables));
}
public function testBackupData(): void
{
$this->assertTrue(\is_string(self::$db->getUtil()->backupData(['create_delete', FALSE])));
$this->assertTrue(\is_string(self::$db->getUtil()->backupData(['create_delete', TRUE])));
$this->assertIsString(self::$db->getUtil()->backupData(['create_delete', FALSE]));
$this->assertIsString(self::$db->getUtil()->backupData(['create_delete', TRUE]));
}
public function testGetColumns(): void
{
$cols = self::$db->getColumns('test');
$this->assertTrue(\is_array($cols));
$this->assertIsArray($cols);
$this->assertTrue( ! empty($cols));
}
public function testGetTypes(): void
{
$types = self::$db->getTypes();
$this->assertTrue(\is_array($types));
$this->assertIsArray($types);
$this->assertTrue( ! empty($types));
}
@ -75,7 +76,7 @@ abstract class BaseDriverTest extends TestCase {
'parent_table' => 'testconstraints',
'parent_column' => 'someid',
'update' => 'CASCADE',
'delete' => 'CASCADE'
'delete' => 'CASCADE',
]];
$keys = self::$db->getFks('testconstraints2');
@ -85,14 +86,15 @@ abstract class BaseDriverTest extends TestCase {
public function testGetIndexes(): void
{
$keys = self::$db->getIndexes('test');
$this->assertTrue(\is_array($keys));
$this->assertIsArray($keys);
}
public function testGetViews(): void
{
$views = self::$db->getViews();
$this->assertTrue(\is_array($views));
$this->assertIsArray($views);
foreach (['numbersview', 'testview'] as $searchView)
{
$this->assertTrue(in_array($searchView, $views, TRUE));
@ -104,7 +106,7 @@ abstract class BaseDriverTest extends TestCase {
$this->markTestSkipped('Deprecated');
$triggers = self::$db->getTriggers();
$this->assertTrue(\is_array($triggers));
$this->assertIsArray($triggers);
}
public function testGetSequences(): void
@ -116,7 +118,7 @@ abstract class BaseDriverTest extends TestCase {
$expected = ['newtable_seq'];
$this->assertTrue(\is_array($seqs));
$this->assertIsArray($seqs);
$this->assertEqual($expected, $seqs);
}
@ -125,7 +127,7 @@ abstract class BaseDriverTest extends TestCase {
$this->markTestSkipped('Deprecated');
$procedures = self::$db->getProcedures();
$this->assertTrue(\is_array($procedures));
$this->assertIsArray($procedures);
}
public function testGetFunctions(): void
@ -133,14 +135,14 @@ abstract class BaseDriverTest extends TestCase {
$this->markTestSkipped('Deprecated');
$funcs = self::$db->getFunctions();
$this->assertTrue(\is_array($funcs));
$this->assertIsArray($funcs);
}
public function testGetVersion(): void
{
$version = self::$db->getVersion();
$this->assertTrue(is_string($version));
$this->assertTrue(strlen($version) > 0);
$this->assertIsString($version);
$this->assertTrue($version !== '');
}
}
// End of db_test.php

View File

@ -13,6 +13,7 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests;
use BadMethodCallException;
@ -23,8 +24,8 @@ use Query\QueryBuilderInterface;
/**
* Query builder parent test class
*/
abstract class BaseQueryBuilderTest extends TestCase {
abstract class BaseQueryBuilderTest extends TestCase
{
/**
* @var QueryBuilderInterface|null
*/
@ -75,7 +76,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
$this->assertIsA($query, 'PDOStatement');
$lastQuery = self::$db->getLastQuery();
$this->assertTrue(\is_string($lastQuery));
$this->assertIsString($lastQuery);
}
public function testPrefixGet(): void
@ -137,6 +138,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
$this->assertIsA($query, 'PDOStatement');
}
// ! Select tests
public function testSelectWhereGet(): void
{
@ -255,6 +257,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
$this->assertIsA($query, 'PDOStatement');
}
// ! Grouping tests
public function testGroup(): void
{
@ -350,6 +353,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
$this->assertIsA($query, 'PDOStatement');
}
// ! Where In tests
public function testWhereIn(): void
{
@ -389,6 +393,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
$this->assertIsA($query, 'PDOStatement');
}
// ! Query modifier tests
public function testOrderBy(): void
{
@ -398,7 +403,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
->where('id <', 9000)
->orderBy('id', 'DESC')
->orderBy('k', 'ASC')
->limit(5,2)
->limit(5, 2)
->get();
$this->assertIsA($query, 'PDOStatement');
@ -411,7 +416,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
->where('id >', 0)
->where('id <', 9000)
->orderBy('id', 'rand')
->limit(5,2)
->limit(5, 2)
->get();
$this->assertIsA($query, 'PDOStatement');
@ -424,10 +429,10 @@ abstract class BaseQueryBuilderTest extends TestCase {
->where('id >', 0)
->where('id <', 9000)
->groupBy('k')
->groupBy(['id','val'])
->groupBy(['id', 'val'])
->orderBy('id', 'DESC')
->orderBy('k', 'ASC')
->limit(5,2)
->limit(5, 2)
->get();
$this->assertIsA($query, 'PDOStatement');
@ -537,7 +542,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
->join('join cj', 'cj.id=ct.id', 'inner')
->where([
'ct.id < ' => 3,
'ct.key' => 'foo'
'ct.key' => 'foo',
])
->get();
@ -616,7 +621,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
->update('test', [
'id' => 7,
'key' => 'gogle',
'val' => 'non-word'
'val' => 'non-word',
]);
$this->assertIsA($query, 'PDOStatement');
@ -635,7 +640,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
->set([
'id' => 7,
'key' => 'gogle',
'val' => 'non-word'
'val' => 'non-word',
])
->returning('key')
->update('test');
@ -661,13 +666,13 @@ abstract class BaseQueryBuilderTest extends TestCase {
[
'id' => 480,
'key' => 49,
'val' => '7x7'
'val' => '7x7',
],
[
'id' => 890,
'key' => 100,
'val' => '10x10'
]
'val' => '10x10',
],
];
$affectedRows = self::$db->updateBatch('test', $data, 'id');
@ -679,7 +684,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
$array = [
'id' => 22,
'key' => 'gogle',
'val' => 'non-word'
'val' => 'non-word',
];
$query = self::$db->set($array)
@ -707,7 +712,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
$this->assertIsA($query, 'PDOStatement');
}
public function testDeleteReturning():void
public function testDeleteReturning(): void
{
$query = self::$db->returning()->delete('test', ['id' => 99]);
@ -718,7 +723,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
{
$query = self::$db->delete('test', [
'id' => 5,
'key' => 'gogle'
'key' => 'gogle',
]);
$this->assertIsA($query, 'PDOStatement');
@ -729,14 +734,14 @@ abstract class BaseQueryBuilderTest extends TestCase {
{
$query = self::$db->countAll('test');
$this->assertTrue(is_numeric($query));
$this->assertIsNumeric($query);
}
public function testCountAllResults(): void
{
$query = self::$db->countAllResults('test');
$this->assertTrue(is_numeric($query));
$this->assertIsNumeric($query);
}
public function testCountAllResults2(): void
@ -748,13 +753,13 @@ abstract class BaseQueryBuilderTest extends TestCase {
->limit(2, 1)
->countAllResults();
$this->assertTrue(is_numeric($query));
$this->assertIsNumeric($query);
}
public function testNumRows(): void
{
self::$db->get('test');
$this->assertTrue(is_numeric(self::$db->numRows()));
$this->assertIsNumeric(self::$db->numRows());
}
// ! Compiled Query tests
@ -764,7 +769,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
$qbRes = self::$db->get('test');
$sqlRes = self::$db->query($sql);
$this->assertIsA($qbRes,'PDOStatement', 'Query Builder Result is a PDO Statement');
$this->assertIsA($qbRes, 'PDOStatement', 'Query Builder Result is a PDO Statement');
$this->assertIsA($sqlRes, 'PDOStatement', 'SQL Result is a PDO Statement');
//$this->assertEquals($qbRes, $sqlRes);
}
@ -774,10 +779,10 @@ abstract class BaseQueryBuilderTest extends TestCase {
$sql = self::$db->set([
'id' => 4,
'key' => 'foo',
'val' => 'baz'
'val' => 'baz',
])->getCompiledUpdate('test');
$this->assertTrue(\is_string($sql));
$this->assertIsString($sql);
}
public function testGetCompiledInsert(): void
@ -785,10 +790,10 @@ abstract class BaseQueryBuilderTest extends TestCase {
$sql = self::$db->set([
'id' => 4,
'key' => 'foo',
'val' => 'baz'
'val' => 'baz',
])->getCompiledInsert('test');
$this->assertTrue(\is_string($sql));
$this->assertIsString($sql);
}
public function testGetCompiledDelete(): void
@ -796,8 +801,9 @@ abstract class BaseQueryBuilderTest extends TestCase {
$sql = self::$db->where('id', 4)
->getCompiledDelete('test');
$this->assertTrue(\is_string($sql));
$this->assertIsString($sql);
}
// ! Error tests
/**
* Handles invalid drivers
@ -810,7 +816,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
'database' => 'test',
'user' => 'root',
'pass' => NULL,
'type' => 'QGYFHGEG'
'type' => 'QGYFHGEG',
];
$this->expectException(BadDBDriverException::class);
@ -830,7 +836,7 @@ abstract class BaseQueryBuilderTest extends TestCase {
self::$db->set([
'id' => 999,
'key' => 'ring',
'val' => 'sale'
'val' => 'sale',
])->insert('test');
$res = self::$db->numRows();

View File

@ -13,13 +13,14 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests;
use DomainException;
use Query\{ConnectionManager, QueryBuilderInterface};
class ConnectionManagerTest extends TestCase {
class ConnectionManagerTest extends TestCase
{
protected static $instance;
public static function setUpBeforeClass(): void
@ -65,7 +66,7 @@ class ConnectionManagerTest extends TestCase {
public $type = 'sqlite';
public $file = ':memory:';
public $options = [
'foo' => 'bar'
'foo' => 'bar',
];
};
@ -73,7 +74,7 @@ class ConnectionManagerTest extends TestCase {
':memory:',
'Sqlite',
$params,
['foo' => 'bar']
['foo' => 'bar'],
];
$this->assertEqual($expected, self::$instance->parseParams($params));
@ -86,7 +87,7 @@ class ConnectionManagerTest extends TestCase {
public $file = ':memory:';
public $prefix = 'create_';
public $options = [
'foo' => 'bar'
'foo' => 'bar',
];
};
@ -104,8 +105,8 @@ class ConnectionManagerTest extends TestCase {
'prefix' => 'create_',
'alias' => 'conn_manager',
'options' => [
'foo' => 'bar'
]
'foo' => 'bar',
],
];
$conn = self::$instance->connect($params);

View File

@ -13,30 +13,28 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests;
use PDO;
use function Query;
use function regexInArray;
use PDO;
/**
* CoreTest class - Compatibility and core functionality tests
*
* @extends UnitTestCase
*/
class CoreTest extends TestCase {
class CoreTest extends TestCase
{
/**
* TestHasPDO function.
*
* @access public
* @return void
*/
public function testHasPDO(): void
{
// PDO class exists
$this->assertTrue(class_exists('PDO'));
// Make sure at least one of the supported drivers is enabled
$supported = [
'mysql',

View File

@ -13,20 +13,22 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests\Drivers\MySQL;
use PDO;
use Query\Drivers\Mysql\Driver;
use Query\Tests\BaseDriverTest;
use TypeError;
use UnitTestCase;
/**
* MySQLTest class.
*
* @requires extension pdo_mysql
*/
class MySQLDriverTest extends BaseDriverTest {
class MySQLDriverTest extends BaseDriverTest
{
public static function setUpBeforeClass(): void
{
$params = get_json_config();
@ -35,12 +37,12 @@ class MySQLDriverTest extends BaseDriverTest {
self::$db = new Driver('host=127.0.0.1;port=3306;dbname=test', 'root');
}
// Attempt to connect, if there is a test config file
else if ($params !== FALSE)
elseif ($params !== FALSE)
{
$params = $params->mysql;
self::$db = new Driver("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, [
PDO::ATTR_PERSISTENT => TRUE
PDO::ATTR_PERSISTENT => TRUE,
]);
}
@ -59,31 +61,33 @@ class MySQLDriverTest extends BaseDriverTest {
public function testCreateTable(): void
{
self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/mysql.sql'));
self::$db->exec(file_get_contents(QTEST_DIR . '/db_files/mysql.sql'));
//Attempt to create the table
$sql = self::$db->getUtil()->createTable('test',
$sql = self::$db->getUtil()->createTable(
'test',
[
'id' => 'int(10)',
'key' => 'TEXT',
'val' => 'TEXT',
],
[
'id' => 'PRIMARY KEY'
'id' => 'PRIMARY KEY',
],
);
self::$db->query($sql);
//Attempt to create the table
$sql = self::$db->getUtil()->createTable('join',
$sql = self::$db->getUtil()->createTable(
'join',
[
'id' => 'int(10)',
'key' => 'TEXT',
'val' => 'TEXT',
],
[
'id' => 'PRIMARY KEY'
'id' => 'PRIMARY KEY',
]
);
self::$db->query($sql);
@ -92,10 +96,8 @@ class MySQLDriverTest extends BaseDriverTest {
$dbs = self::$db->getTables();
$this->assertTrue(\in_array('create_test', $dbs, TRUE));
}
public function testTruncate(): void
{
self::$db->truncate('test');
@ -107,7 +109,7 @@ class MySQLDriverTest extends BaseDriverTest {
public function testPreparedStatements(): void
{
$sql = <<<SQL
$sql = <<<'SQL'
INSERT INTO `create_test` (`id`, `key`, `val`)
VALUES (?,?,?)
SQL;
@ -116,40 +118,38 @@ SQL;
$res = $statement->execute();
$this->assertTrue($res);
}
public function testBadPreparedStatement(): void
{
if (is_a($this, \UnitTestCase::class))
if (is_a($this, UnitTestCase::class))
{
$this->markTestSkipped();
return;
}
$this->expectException(TypeError::class);
$sql = <<<SQL
$sql = <<<'SQL'
INSERT INTO `create_test` (`id`, `key`, `val`)
VALUES (?,?,?)
SQL;
self::$db->prepareQuery($sql, 'foo');
}
public function testPrepareExecute(): void
{
$sql = <<<SQL
$sql = <<<'SQL'
INSERT INTO `create_test` (`id`, `key`, `val`)
VALUES (?,?,?)
SQL;
$res = self::$db->prepareExecute($sql, [
2, 'works', 'also?'
2, 'works', 'also?',
]);
$this->assertInstanceOf('PDOStatement', $res);
}
public function testCommitTransaction(): void
@ -190,7 +190,7 @@ SQL;
public function testGetSchemas(): void
{
$this->assertTrue(in_array('test', self::$db->getSchemas()));
$this->assertTrue(in_array('test', self::$db->getSchemas(), TRUE));
}
public function testGetSequences(): void
@ -200,6 +200,6 @@ SQL;
public function testBackup(): void
{
$this->assertTrue(\is_string(self::$db->getUtil()->backupStructure()));
$this->assertIsString(self::$db->getUtil()->backupStructure());
}
}

View File

@ -13,16 +13,18 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests\Drivers\MySQL;
use PDO;
use Query\Tests\BaseQueryBuilderTest;
use function in_array;
/**
* @requires extension pdo_mysql
*/
class MySQLQueryBuilderTest extends BaseQueryBuilderTest {
class MySQLQueryBuilderTest extends BaseQueryBuilderTest
{
public static function setUpBeforeClass(): void
{
$params = get_json_config();
@ -35,11 +37,11 @@ class MySQLQueryBuilderTest extends BaseQueryBuilderTest {
'prefix' => 'create_',
'user' => 'root',
'pass' => NULL,
'type' => 'mysql'
'type' => 'mysql',
];
}
// Attempt to connect, if there is a test config file
else if ($params !== FALSE)
elseif ($params !== FALSE)
{
$params = $params->mysql;
$params->type = 'MySQL';
@ -52,7 +54,7 @@ class MySQLQueryBuilderTest extends BaseQueryBuilderTest {
public function testExists(): void
{
$this->assertTrue(\in_array('mysql', PDO::getAvailableDrivers(), TRUE));
$this->assertTrue(in_array('mysql', PDO::getAvailableDrivers(), TRUE));
}
public function testQueryExplain(): void
@ -68,9 +70,9 @@ class MySQLQueryBuilderTest extends BaseQueryBuilderTest {
// The exact results are version dependent
// The important thing is that there is an array
// of results returned
$this->assertTrue(\is_array($res));
$this->assertIsArray($res);
$this->assertTrue(count(array_keys($res[0])) > 1);
$this->assertTrue(array_key_exists('table', $res[0]));
$this->assertArrayHasKey('table', $res[0]);
}
public function testInsertReturning(): void

View File

@ -13,12 +13,14 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests\Drivers\PgSQL;
use PDO;
use Query\Drivers\Pgsql\Driver;
use Query\Tests\BaseDriverTest;
use TypeError;
use UnitTestCase;
/**
* PgTest class.
@ -26,9 +28,9 @@ use TypeError;
* @extends DBTest
* @requires extension pdo_pgsql
*/
class PgSQLDriverTest extends BaseDriverTest {
public function setUp(): void
class PgSQLDriverTest extends BaseDriverTest
{
protected function setUp(): void
{
// If the database isn't installed, skip the tests
if ( ! class_exists(Driver::class))
@ -39,14 +41,13 @@ class PgSQLDriverTest extends BaseDriverTest {
public static function setUpBeforeClass(): void
{
$params = get_json_config();
if ($var = getenv('TRAVIS'))
{
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)
elseif ($params !== FALSE)
{
$params = $params->pgsql;
self::$db = new Driver("pgsql:host={$params->host};dbname={$params->database};port=5432", $params->user, $params->pass);
@ -63,7 +64,10 @@ class PgSQLDriverTest extends BaseDriverTest {
public function testConnection(): void
{
if (empty(self::$db)) return;
if (empty(self::$db))
{
return;
}
$this->assertIsA(self::$db, Driver::class);
}
@ -78,30 +82,31 @@ class PgSQLDriverTest extends BaseDriverTest {
$sql = 'DROP TABLE IF EXISTS "create_join"';
self::$db->query($sql);
//Attempt to create the table
$sql = self::$db->getUtil()->createTable('create_test',
$sql = self::$db->getUtil()->createTable(
'create_test',
[
'id' => 'integer',
'key' => 'TEXT',
'val' => 'TEXT',
],
[
'id' => 'PRIMARY KEY'
'id' => 'PRIMARY KEY',
]
);
self::$db->query($sql);
//Attempt to create the table
$sql = self::$db->getUtil()->createTable('create_join',
$sql = self::$db->getUtil()->createTable(
'create_join',
[
'id' => 'integer',
'key' => 'TEXT',
'val' => 'TEXT',
],
[
'id' => 'PRIMARY KEY'
'id' => 'PRIMARY KEY',
]
);
self::$db->query($sql);
@ -115,7 +120,6 @@ class PgSQLDriverTest extends BaseDriverTest {
//Check
$dbs = self::$db->getTables();
$this->assertTrue(in_array('create_test', $dbs, TRUE));
}
public function testTruncate(): void
@ -129,11 +133,11 @@ class PgSQLDriverTest extends BaseDriverTest {
public function testPreparedStatements(): void
{
$sql = <<<SQL
$sql = <<<'SQL'
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement = self::$db->prepareQuery($sql, [1,'boogers', 'Gross']);
$statement = self::$db->prepareQuery($sql, [1, 'boogers', 'Gross']);
$statement->execute();
@ -143,21 +147,22 @@ SQL;
$this->assertEquals([
'id' => 1,
'key' => 'boogers',
'val' => 'Gross'
'val' => 'Gross',
], $res);
}
public function testBadPreparedStatement(): void
{
if (is_a($this, \UnitTestCase::class))
if (is_a($this, UnitTestCase::class))
{
$this->markTestSkipped();
return;
}
$this->expectException(TypeError::class);
$sql = <<<SQL
$sql = <<<'SQL'
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
@ -167,14 +172,17 @@ SQL;
public function testPrepareExecute(): void
{
if (empty(self::$db)) return;
if (empty(self::$db))
{
return;
}
$sql = <<<SQL
$sql = <<<'SQL'
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
self::$db->prepareExecute($sql, [
2, 'works', 'also?'
2, 'works', 'also?',
]);
$res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=2')
@ -183,13 +191,16 @@ SQL;
$this->assertEquals([
'id' => 2,
'key' => 'works',
'val' => 'also?'
'val' => 'also?',
], $res);
}
public function testCommitTransaction(): void
{
if (empty(self::$db)) return;
if (empty(self::$db))
{
return;
}
self::$db->beginTransaction();
@ -202,7 +213,10 @@ SQL;
public function testRollbackTransaction(): void
{
if (empty(self::$db)) return;
if (empty(self::$db))
{
return;
}
self::$db->beginTransaction();
@ -215,12 +229,12 @@ SQL;
public function testGetSchemas(): void
{
$this->assertTrue(\is_array(self::$db->getSchemas()));
$this->assertIsArray(self::$db->getSchemas());
}
public function testGetDBs(): void
{
$this->assertTrue(\is_array(self::$db->getDbs()));
$this->assertIsArray(self::$db->getDbs());
}
public function testGetFunctions(): void

View File

@ -13,16 +13,18 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests\Drivers\PgSQL;
use PDO;
use Query\Tests\BaseQueryBuilderTest;
use function in_array;
/**
* @requires extension pdo_pgsql
*/
class PgSQLQueryBuilderTest extends BaseQueryBuilderTest {
class PgSQLQueryBuilderTest extends BaseQueryBuilderTest
{
public static function setUpBeforeClass(): void
{
$params = get_json_config();
@ -35,11 +37,11 @@ class PgSQLQueryBuilderTest extends BaseQueryBuilderTest {
'user' => 'postgres',
'pass' => '',
'type' => 'pgsql',
'prefix' => 'create_'
'prefix' => 'create_',
];
}
// Attempt to connect, if there is a test config file
else if ($params !== FALSE)
elseif ($params !== FALSE)
{
$params = $params->pgsql;
$params->type = 'pgsql';
@ -52,10 +54,10 @@ class PgSQLQueryBuilderTest extends BaseQueryBuilderTest {
self::$db = Query($params);
}
public function setUp(): void
protected function setUp(): void
{
// If the database isn't installed, skip the tests
if ( ! \in_array('pgsql', PDO::getAvailableDrivers(), TRUE))
if ( ! in_array('pgsql', PDO::getAvailableDrivers(), TRUE))
{
$this->markTestSkipped('Postgres extension for PDO not loaded');
}
@ -63,7 +65,7 @@ class PgSQLQueryBuilderTest extends BaseQueryBuilderTest {
public function testExists(): void
{
$this->assertTrue(\in_array('pgsql', PDO::getAvailableDrivers(), TRUE));
$this->assertTrue(in_array('pgsql', PDO::getAvailableDrivers(), TRUE));
}
public function testQueryExplain(): void
@ -79,9 +81,9 @@ class PgSQLQueryBuilderTest extends BaseQueryBuilderTest {
// The exact results are version dependent
// The important thing is that there is an array
// of results returned
$this->assertTrue(\is_array($res));
$this->assertIsArray($res);
$this->assertTrue(count($res) > 1);
$this->assertTrue(array_key_exists('QUERY PLAN', $res[0]));
$this->assertArrayHasKey('QUERY PLAN', $res[0]);
/*$expected = array (
array (

View File

@ -13,6 +13,7 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests\Drivers\SQLite;
use PDO;
@ -26,8 +27,8 @@ use Query\Tests\BaseDriverTest;
* @extends DBTest
* @requires extension pdo_sqlite
*/
class SQLiteDriverTest extends BaseDriverTest {
class SQLiteDriverTest extends BaseDriverTest
{
public static function setupBeforeClass(): void
{
$params = [
@ -36,8 +37,8 @@ class SQLiteDriverTest extends BaseDriverTest {
'prefix' => 'create_',
'alias' => 'test_sqlite',
'options' => [
PDO::ATTR_PERSISTENT => TRUE
]
PDO::ATTR_PERSISTENT => TRUE,
],
];
self::$db = Query($params);
@ -50,7 +51,7 @@ class SQLiteDriverTest extends BaseDriverTest {
public function testCreateTable(): void
{
self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/sqlite.sql'));
self::$db->exec(file_get_contents(QTEST_DIR . '/db_files/sqlite.sql'));
//Check
$dbs = self::$db->getTables();
@ -84,7 +85,7 @@ SQL;
public function testBackupStructure(): void
{
$sql = mb_trim(self::$db->getUtil()->backupStructure());
$expected = <<<SQL
$expected = <<<'SQL'
CREATE TABLE "create_test" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "key" TEXT, "val" TEXT);
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
@ -164,7 +165,7 @@ SQL;
//Check
$dbs = self::$db->getTables();
$this->assertFalse(in_array('create_delete', $dbs));
$this->assertFalse(in_array('create_delete', $dbs, TRUE));
}
// --------------------------------------------------------------------------
@ -175,7 +176,7 @@ SQL;
{
$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');
$this->assertIsA($db, $class);
@ -190,7 +191,7 @@ SQL;
public function testPreparedStatements(): void
{
$sql = <<<SQL
$sql = <<<'SQL'
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
@ -204,18 +205,18 @@ SQL;
$this->assertEquals([
'id' => 1,
'key' => 'boogers',
'val' => 'Gross'
'val' => 'Gross',
], $res);
}
public function testPrepareExecute(): void
{
$sql = <<<SQL
$sql = <<<'SQL'
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
self::$db->prepareExecute($sql, [
2, 'works', 'also?'
2, 'works', 'also?',
]);
$res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=2')
@ -224,7 +225,7 @@ SQL;
$this->assertEquals([
'id' => 2,
'key' => 'works',
'val' => 'also?'
'val' => 'also?',
], $res);
}
@ -270,7 +271,7 @@ SQL;
public function testGetSystemTables(): void
{
$sql = self::$db->getSystemTables();
$this->assertTrue(\is_array($sql));
$this->assertIsArray($sql);
}
public function testGetSequences(): void

View File

@ -13,6 +13,7 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests\Drivers\SQLite;
use PDO;
@ -23,8 +24,8 @@ use Query\Tests\BaseQueryBuilderTest;
*
* @requires extension pdo_sqlite
*/
class SQLiteQueryBuilderTest extends BaseQueryBuilderTest {
class SQLiteQueryBuilderTest extends BaseQueryBuilderTest
{
public static function setUpBeforeClass(): void
{
// Defined in the SQLiteTest.php file
@ -48,7 +49,7 @@ use Query\Tests\BaseQueryBuilderTest;
$res = $query->fetchAll(PDO::FETCH_ASSOC);
$actualDetail = $res[0]['detail'];
$this->assertTrue(is_string($actualDetail));
$this->assertIsString($actualDetail);
/* $expectedPossibilities = [
'TABLE create_test USING PRIMARY KEY',

View File

@ -13,21 +13,23 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace Query\Tests;
use Query\QueryParser;
use Query\Drivers\Sqlite\Driver;
use Query\QueryParser;
/**
* Tests for the Query Parser
*/
class QueryParserTest extends TestCase {
class QueryParserTest extends TestCase
{
/**
* @var QueryParser
*/
protected $parser;
public function setUp(): void
protected function setUp(): void
{
$db = new Driver('sqlite::memory:');
$this->parser = new QueryParser($db);
@ -37,7 +39,7 @@ class QueryParserTest extends TestCase {
{
$matches = $this->parser->parseJoin('table1.field1=table2.field2');
$this->assertEqual($matches['combined'], [
'table1.field1', '=', 'table2.field2'
'table1.field1', '=', 'table2.field2',
]);
}
@ -45,7 +47,7 @@ class QueryParserTest extends TestCase {
{
$matches = $this->parser->parseJoin('db1.table1.field1!=db2.table2.field2');
$this->assertEqual($matches['combined'], [
'db1.table1.field1','!=','db2.table2.field2'
'db1.table1.field1', '!=', 'db2.table2.field2',
]);
}
@ -53,7 +55,7 @@ class QueryParserTest extends TestCase {
{
$matches = $this->parser->parseJoin('table_1.field1 = tab_le2.field_2');
$this->assertEqual($matches['combined'], [
'table_1.field1', '=', 'tab_le2.field_2'
'table_1.field1', '=', 'tab_le2.field_2',
]);
}
@ -61,7 +63,7 @@ class QueryParserTest extends TestCase {
{
$matches = $this->parser->parseJoin('table1.field1 > SUM(3+5)');
$this->assertEqual($matches['combined'], [
'table1.field1', '>', 'SUM(3+5)'
'table1.field1', '>', 'SUM(3+5)',
]);
}
}

View File

@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* Query
*
@ -15,17 +15,17 @@
* Unit test bootstrap - Using phpunit
*/
define('QTEST_DIR', realpath(__DIR__));
define('QBASE_DIR', realpath(QTEST_DIR.'/../') . '/');
define('QBASE_DIR', realpath(QTEST_DIR . '/../') . '/');
define('QDS', DIRECTORY_SEPARATOR);
function get_json_config()
{
$files = [
__DIR__ . '/settings.json',
__DIR__ . '/settings.json.dist'
__DIR__ . '/settings.json.dist',
];
foreach($files as $file)
foreach ($files as $file)
{
if (is_file($file))
{
@ -36,7 +36,7 @@ function get_json_config()
return FALSE;
}
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
$path = QTEST_DIR . QDS . 'db_files' . QDS . 'test_sqlite.db';
@unlink($path);
require_once __DIR__ . '/TestCase.php';

View File

@ -13,6 +13,7 @@
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
namespace {
/**
* Unit test bootstrap - Using php simpletest
@ -26,14 +27,14 @@ namespace {
}
namespace Query\Tests {
/**
* Base class for TestCases
*/
abstract class TestCase extends \UnitTestCase {
abstract class TestCase extends \UnitTestCase
{
public function __construct()
{
$class = \get_class($this);
$class = static::class;
if (PHP_SAPI !== 'cli')
{
@ -41,7 +42,8 @@ namespace Query\Tests {
flush();
}
if (method_exists($class, 'setupBeforeClass')) {
if (method_exists($class, 'setupBeforeClass'))
{
$class::setupBeforeClass();
}
@ -50,9 +52,10 @@ namespace Query\Tests {
public function __destruct()
{
$class = \get_class($this);
$class = static::class;
if (method_exists($class, 'tearDownAfterClass')) {
if (method_exists($class, 'tearDownAfterClass'))
{
$class::tearDownAfterClass();
}
}
@ -60,8 +63,6 @@ namespace Query\Tests {
/**
* Define assertInstanceOf for simpletest
*
* @param $expected
* @param $actual
* @param string $message
*/
public function assertInstanceOf($expected, $actual, $message = '')
@ -111,16 +112,19 @@ namespace Query\Tests {
/**
* Load the test suites
*/
namespace {
function get_json_config()
{
$files = [
__DIR__ . '/settings.json',
__DIR__ . '/settings.json.dist'
__DIR__ . '/settings.json.dist',
];
foreach ($files as $file) {
if (is_file($file)) {
foreach ($files as $file)
{
if (is_file($file))
{
return json_decode(file_get_contents($file));
}
}
@ -130,7 +134,7 @@ namespace {
// Include db tests
// Load db classes based on capability
$testPath = QTEST_DIR.'/Drivers/';
$testPath = QTEST_DIR . '/Drivers/';
// Require base testing classes
require_once QTEST_DIR . '/CoreTest.php';
@ -145,7 +149,7 @@ namespace {
];
// Determine which testcases to load
foreach($driverTestMap as $name => $doLoad)
foreach ($driverTestMap as $name => $doLoad)
{
$path = $testPath . $name;
@ -157,5 +161,4 @@ namespace {
}
}
// End of index.php