* @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\Drivers\SQLite; use PDO; use Query\Drivers\Sqlite\Driver; use Query\Tests\BaseDriverTest; // -------------------------------------------------------------------------- /** * SQLiteTest class. * * @extends DBTest * @requires extension pdo_sqlite */ class SQLiteDriverTest extends BaseDriverTest { public static function setupBeforeClass() { $params = array( 'type' => 'sqlite', 'file' => ':memory:', 'prefix' => 'create_', 'alias' => 'test_sqlite', 'options' => array( PDO::ATTR_PERSISTENT => TRUE ) ); self::$db = Query($params); self::$db->setTablePrefix('create_'); } // -------------------------------------------------------------------------- // ! Util Method tests // -------------------------------------------------------------------------- public function testCreateTable() { self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/sqlite.sql')); //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)); } // -------------------------------------------------------------------------- /*public function testBackupData() { $sql = mb_trim(self::$db->getUtil()->backupData(array('create_join', 'create_test'))); $sqlArray = explode("\n", $sql); $expected = <<assertEqual($expectedArray, $sqlArray); }*/ // -------------------------------------------------------------------------- public function testBackupStructure() { $sql = mb_trim(self::$db->getUtil()->backupStructure()); $expected = << 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; $expectedArray = explode("\n", $expected); $resultArray = explode("\n", $sql); $this->assertEqual($expectedArray, $resultArray); } // -------------------------------------------------------------------------- public function testDeleteTable() { $sql = self::$db->getUtil()->deleteTable('create_delete'); self::$db->query($sql); //Check $dbs = self::$db->getTables(); $this->assertFalse(in_array('create_delete', $dbs)); } // -------------------------------------------------------------------------- // ! General tests // -------------------------------------------------------------------------- public function testConnection() { $class = Driver::class; $db = new $class(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'); $this->assertIsA($db, $class); $this->assertIsA(self::$db->driver, $class); unset($db); } // -------------------------------------------------------------------------- public function testTruncate() { self::$db->truncate('create_test'); $this->assertEquals(0, self::$db->countAll('create_test')); } // -------------------------------------------------------------------------- public function testPreparedStatements() { $sql = <<prepareQuery($sql, array(1,"boogers", "Gross")); $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); } // -------------------------------------------------------------------------- public function testPrepareExecute() { $sql = <<prepareExecute($sql, array( 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); } // -------------------------------------------------------------------------- public function testCommitTransaction() { $res = self::$db->beginTransaction(); $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)'; self::$db->query($sql); $res = self::$db->commit(); $this->assertTrue($res); } // -------------------------------------------------------------------------- public function testRollbackTransaction() { $res = self::$db->beginTransaction(); $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)'; self::$db->query($sql); $res = self::$db->rollback(); $this->assertTrue($res); } // -------------------------------------------------------------------------- public function testGetDBs() { $this->assertTrue(is_array(self::$db->getDbs())); } // -------------------------------------------------------------------------- public function testGetSchemas() { $this->assertNull(self::$db->getSchemas()); } // -------------------------------------------------------------------------- // ! SQL tests // -------------------------------------------------------------------------- public function testNullMethods() { $sql = self::$db->getSQL()->functionList(); $this->assertEqual(NULL, $sql); $sql = self::$db->getSQL()->procedureList(); $this->assertEqual(NULL, $sql); $sql = self::$db->getSQL()->sequenceList(); $this->assertEqual(NULL, $sql); } // -------------------------------------------------------------------------- public function testGetSystemTables() { $sql = self::$db->getSystemTables(); $this->assertTrue(\is_array($sql)); } // -------------------------------------------------------------------------- public function testGetSequences() { $this->assertNull(self::$db->getSequences()); } // -------------------------------------------------------------------------- public function testGetFunctions() { $this->assertNull(self::$db->getFunctions()); } // -------------------------------------------------------------------------- public function testGetProcedures() { $this->assertNull(self::$db->getProcedures()); } }