2016-10-12 22:12:25 -04:00
|
|
|
<?php declare(strict_types=1);
|
2014-06-09 17:02:14 -04:00
|
|
|
/**
|
2016-10-12 22:12:25 -04:00
|
|
|
* Query
|
2014-06-09 17:02:14 -04:00
|
|
|
*
|
2016-10-12 22:12:25 -04:00
|
|
|
* SQL Query Builder / Database Abstraction Layer
|
2014-06-09 17:02:14 -04:00
|
|
|
*
|
2019-12-11 16:49:42 -05:00
|
|
|
* PHP version 7.2
|
2016-10-12 22:12:25 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-03-18 11:31:56 -04:00
|
|
|
* @copyright 2012 - 2020 Timothy J. Warren
|
2016-10-12 22:12:25 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-11 16:49:42 -05:00
|
|
|
* @link https://git.timshomepage.net/aviat/Query
|
|
|
|
* @version 3.0.0
|
2014-06-09 17:02:14 -04:00
|
|
|
*/
|
2018-01-19 16:48:52 -05:00
|
|
|
namespace Query\Tests\Drivers\SQLite;
|
|
|
|
|
|
|
|
use PDO;
|
|
|
|
use Query\Drivers\Sqlite\Driver;
|
2018-01-26 15:45:46 -05:00
|
|
|
use Query\Exception\NotImplementedException;
|
2018-01-19 16:48:52 -05:00
|
|
|
use Query\Tests\BaseDriverTest;
|
2016-10-12 22:12:25 -04:00
|
|
|
|
2014-06-09 17:02:14 -04:00
|
|
|
/**
|
|
|
|
* SQLiteTest class.
|
|
|
|
*
|
|
|
|
* @extends DBTest
|
|
|
|
* @requires extension pdo_sqlite
|
|
|
|
*/
|
2018-01-19 16:48:52 -05:00
|
|
|
class SQLiteDriverTest extends BaseDriverTest {
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public static function setupBeforeClass(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2018-01-26 08:39:30 -05:00
|
|
|
$params = [
|
2015-07-17 15:38:06 -04:00
|
|
|
'type' => 'sqlite',
|
|
|
|
'file' => ':memory:',
|
|
|
|
'prefix' => 'create_',
|
|
|
|
'alias' => 'test_sqlite',
|
2018-01-26 08:39:30 -05:00
|
|
|
'options' => [
|
2015-07-17 15:38:06 -04:00
|
|
|
PDO::ATTR_PERSISTENT => TRUE
|
2018-01-26 08:39:30 -05:00
|
|
|
]
|
|
|
|
];
|
2015-07-17 15:38:06 -04:00
|
|
|
|
2015-07-20 15:24:21 -04:00
|
|
|
self::$db = Query($params);
|
2016-10-13 21:55:23 -04:00
|
|
|
self::$db->setTablePrefix('create_');
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Util Method tests
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testCreateTable(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/sqlite.sql'));
|
2014-06-09 17:02:14 -04:00
|
|
|
|
|
|
|
//Check
|
2016-10-13 21:55:23 -04:00
|
|
|
$dbs = self::$db->getTables();
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2018-01-22 12:03:37 -05:00
|
|
|
$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));
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*public function testBackupData()
|
|
|
|
{
|
2018-01-26 08:39:30 -05:00
|
|
|
$sql = mb_trim(self::$db->getUtil()->backupData(['create_join', 'create_test']));
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$sqlArray = explode("\n", $sql);
|
2014-06-09 17:02:14 -04:00
|
|
|
|
|
|
|
$expected = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id","key","val") VALUES (1,'boogers','Gross');
|
|
|
|
INSERT INTO "create_test" ("id","key","val") VALUES (2,'works','also?');
|
|
|
|
INSERT INTO "create_test" ("id","key","val") VALUES (10,12,14);
|
|
|
|
INSERT INTO "create_test" ("id","key","val") VALUES (587,1,2);
|
|
|
|
INSERT INTO "create_test" ("id","key","val") VALUES (999,'''ring''','''sale''');
|
|
|
|
SQL;
|
2016-10-13 21:55:23 -04:00
|
|
|
$expectedArray = explode("\n", $expected);
|
|
|
|
$this->assertEqual($expectedArray, $sqlArray);
|
2014-06-09 17:02:14 -04:00
|
|
|
}*/
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testBackupStructure(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$sql = mb_trim(self::$db->getUtil()->backupStructure());
|
2014-06-09 17:02:14 -04:00
|
|
|
$expected = <<<SQL
|
2018-01-26 15:45:46 -05:00
|
|
|
CREATE TABLE "create_test" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "key" TEXT, "val" TEXT);
|
|
|
|
CREATE TABLE sqlite_sequence(name,seq);
|
2014-06-09 17:02:14 -04:00
|
|
|
CREATE TABLE "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
|
|
|
|
CREATE TABLE "create_delete" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);
|
|
|
|
CREATE TABLE TEST1 (
|
|
|
|
TEST_NAME TEXT NOT NULL,
|
|
|
|
TEST_ID INTEGER DEFAULT '0' NOT NULL,
|
|
|
|
TEST_DATE TEXT NOT NULL,
|
|
|
|
CONSTRAINT PK_TEST PRIMARY KEY (TEST_ID)
|
|
|
|
);
|
|
|
|
CREATE TABLE TEST2 (
|
|
|
|
ID INTEGER NOT NULL,
|
|
|
|
FIELD1 INTEGER,
|
|
|
|
FIELD2 TEXT,
|
|
|
|
FIELD3 TEXT,
|
|
|
|
FIELD4 INTEGER,
|
|
|
|
FIELD5 INTEGER,
|
|
|
|
ID2 INTEGER NOT NULL,
|
|
|
|
CONSTRAINT PK_TEST2 PRIMARY KEY (ID2),
|
|
|
|
CONSTRAINT TEST2_FIELD1ID_IDX UNIQUE (ID, FIELD1),
|
|
|
|
CONSTRAINT TEST2_FIELD4_IDX UNIQUE (FIELD4)
|
|
|
|
);
|
|
|
|
;
|
|
|
|
;
|
|
|
|
CREATE INDEX TEST2_FIELD5_IDX ON TEST2 (FIELD5);
|
|
|
|
CREATE TABLE NUMBERS (
|
|
|
|
NUMBER INTEGER DEFAULT 0 NOT NULL,
|
|
|
|
EN TEXT NOT NULL,
|
|
|
|
FR TEXT NOT NULL
|
|
|
|
);
|
|
|
|
CREATE TABLE NEWTABLE (
|
|
|
|
ID INTEGER DEFAULT 0 NOT NULL,
|
|
|
|
SOMENAME TEXT,
|
|
|
|
SOMEDATE TEXT NOT NULL,
|
|
|
|
CONSTRAINT PKINDEX_IDX PRIMARY KEY (ID)
|
|
|
|
);
|
|
|
|
CREATE VIEW "testview" AS
|
|
|
|
SELECT *
|
|
|
|
FROM TEST1
|
|
|
|
WHERE TEST_NAME LIKE 't%';
|
|
|
|
CREATE VIEW "numbersview" AS
|
|
|
|
SELECT *
|
|
|
|
FROM NUMBERS
|
|
|
|
WHERE NUMBER > 100;
|
|
|
|
CREATE TABLE "testconstraints" (
|
|
|
|
someid integer NOT NULL,
|
|
|
|
somename TEXT NOT NULL,
|
|
|
|
CONSTRAINT testconstraints_id_pk PRIMARY KEY (someid)
|
|
|
|
);
|
|
|
|
CREATE TABLE "testconstraints2" (
|
|
|
|
ext_id integer NOT NULL,
|
|
|
|
modified text,
|
|
|
|
uniquefield text NOT NULL,
|
|
|
|
usraction integer NOT NULL,
|
|
|
|
CONSTRAINT testconstraints_id_fk FOREIGN KEY (ext_id)
|
|
|
|
REFERENCES testconstraints (someid)
|
|
|
|
ON UPDATE CASCADE
|
|
|
|
ON DELETE CASCADE,
|
|
|
|
CONSTRAINT unique_2_fields_idx UNIQUE (modified, usraction),
|
|
|
|
CONSTRAINT uniquefld_idx UNIQUE (uniquefield)
|
|
|
|
);
|
|
|
|
;
|
|
|
|
;
|
|
|
|
SQL;
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$expectedArray = explode("\n", $expected);
|
|
|
|
$resultArray = explode("\n", $sql);
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->assertEqual($expectedArray, $resultArray);
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testDeleteTable(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$sql = self::$db->getUtil()->deleteTable('create_delete');
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2014-06-09 17:02:14 -04:00
|
|
|
|
|
|
|
//Check
|
2016-10-13 21:55:23 -04:00
|
|
|
$dbs = self::$db->getTables();
|
2014-06-09 17:02:14 -04:00
|
|
|
$this->assertFalse(in_array('create_delete', $dbs));
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! General tests
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testConnection(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2018-01-19 16:48:52 -05:00
|
|
|
$class = Driver::class;
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2015-07-16 16:56:13 -04:00
|
|
|
$db = new $class(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db');
|
|
|
|
|
|
|
|
$this->assertIsA($db, $class);
|
2014-06-09 17:02:14 -04:00
|
|
|
|
|
|
|
unset($db);
|
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testTruncate(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->truncate('create_test');
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertEquals(0, self::$db->countAll('create_test'));
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testPreparedStatements(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2019-12-11 16:48:43 -05:00
|
|
|
$statement = self::$db->prepareQuery($sql, [1, 'boogers', 'Gross']);
|
2014-06-09 17:02:14 -04:00
|
|
|
|
|
|
|
$statement->execute();
|
|
|
|
|
2018-01-22 12:03:37 -05:00
|
|
|
$res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=1')
|
|
|
|
->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'id' => 1,
|
|
|
|
'key' => 'boogers',
|
|
|
|
'val' => 'Gross'
|
|
|
|
], $res);
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testPrepareExecute(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2018-01-26 08:39:30 -05:00
|
|
|
self::$db->prepareExecute($sql, [
|
2019-12-11 16:48:43 -05:00
|
|
|
2, 'works', 'also?'
|
2018-01-26 08:39:30 -05:00
|
|
|
]);
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2018-01-22 12:03:37 -05:00
|
|
|
$res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=2')
|
|
|
|
->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'id' => 2,
|
|
|
|
'key' => 'works',
|
|
|
|
'val' => 'also?'
|
|
|
|
], $res);
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testCommitTransaction(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->beginTransaction();
|
2014-06-09 17:02:14 -04:00
|
|
|
|
|
|
|
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->commit();
|
2014-06-09 17:02:14 -04:00
|
|
|
$this->assertTrue($res);
|
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testRollbackTransaction(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->beginTransaction();
|
2014-06-09 17:02:14 -04:00
|
|
|
|
|
|
|
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->rollback();
|
2014-06-09 17:02:14 -04:00
|
|
|
$this->assertTrue($res);
|
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGetDBs(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2019-12-13 15:11:49 -05:00
|
|
|
$driverSQL = self::$db->getSql()->dbList();
|
|
|
|
$this->assertEqual('', $driverSQL);
|
|
|
|
|
2019-12-11 16:48:43 -05:00
|
|
|
$this->assertNull(self::$db->getDbs());
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGetSchemas(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->assertNull(self::$db->getSchemas());
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! SQL tests
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGetSystemTables(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$sql = self::$db->getSystemTables();
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertTrue(\is_array($sql));
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGetSequences(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
$sql = self::$db->getSequences();
|
|
|
|
$this->assertEquals(['create_test'], $sql);
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGetFunctions(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
$this->expectException(NotImplementedException::class);
|
|
|
|
self::$db->getFunctions();
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGetProcedures(): void
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
$this->expectException(NotImplementedException::class);
|
|
|
|
self::$db->getProcedures();
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
2018-01-19 13:43:19 -05:00
|
|
|
}
|