Query/tests/Drivers/Firebird/FirebirdTest.php

295 lines
7.0 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
2012-03-15 09:25:18 -04:00
/**
* Query
*
2016-10-12 22:12:25 -04:00
* SQL Query Builder / Database Abstraction Layer
2012-03-15 09:25:18 -04:00
*
2016-10-12 22:12:25 -04:00
* PHP version 7
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2012 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
2012-03-15 09:25:18 -04:00
*/
2016-10-12 22:12:25 -04:00
2012-03-15 09:25:18 -04:00
// --------------------------------------------------------------------------
2014-07-14 15:17:16 -04:00
@chmod(QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB', 0777);
2012-03-15 09:25:18 -04:00
/**
2014-02-14 22:08:19 -05:00
* Firebirdtest class.
2014-02-25 13:47:35 -05:00
*
2014-02-14 22:08:19 -05:00
* @extends DBtest
* @requires extension interbase
2012-03-15 09:25:18 -04:00
*/
2014-02-14 22:08:19 -05:00
class FirebirdTest extends DBtest {
2014-02-25 13:47:35 -05:00
public static function setupBeforeClass()
2014-02-25 13:47:35 -05:00
{
2012-04-30 16:06:06 -04:00
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
2014-02-25 13:47:35 -05:00
// test the db driver directly
self::$db = new \Query\Drivers\Firebird\Driver('localhost:'.$dbpath);
2016-10-13 21:55:23 -04:00
self::$db->setTablePrefix('create_');
}
public function setUp()
{
2014-04-02 17:08:50 -04:00
if ( ! function_exists('\\fbird_connect'))
2014-02-14 22:52:56 -05:00
{
$this->markTestSkipped('Firebird extension does not exist');
}
2014-02-25 13:47:35 -05:00
2016-10-13 21:55:23 -04:00
$this->tables = self::$db->getTables();
2012-03-15 09:25:18 -04:00
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
public function tearDown()
2012-03-15 09:25:18 -04:00
{
unset($this->tables);
}
2014-02-25 13:47:35 -05:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-07 15:43:25 -05:00
/**
* coverage for methods in result class that aren't implemented
*/
2014-02-14 22:08:19 -05:00
public function testNullResultMethods()
2014-02-07 15:43:25 -05:00
{
$obj = self::$db->query('SELECT "id" FROM "create_test"');
2014-02-25 13:47:35 -05:00
2014-02-07 15:43:25 -05:00
$val = "bar";
2014-02-25 13:47:35 -05:00
2014-02-07 15:43:25 -05:00
$this->assertNull($obj->bindColumn('foo', $val));
$this->assertNull($obj->bindParam('foo', $val));
$this->assertNull($obj->bindValue('foo', $val));
2014-02-20 11:00:18 -05:00
}
2014-02-25 13:47:35 -05:00
2014-02-20 11:00:18 -05:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testExists()
{
$this->assertTrue(function_exists('ibase_connect'));
$this->assertTrue(function_exists('fbird_connect'));
2014-02-25 13:47:35 -05:00
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-03-15 09:25:18 -04:00
2014-02-14 22:08:19 -05:00
public function testConnection()
2012-03-15 09:25:18 -04:00
{
$this->assertIsA(self::$db, '\\Query\\Drivers\\Firebird\\Driver');
2012-03-15 09:25:18 -04:00
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testGetSystemTables()
2014-02-25 13:47:35 -05:00
{
2016-10-13 21:55:23 -04:00
$onlySystem = TRUE;
2014-02-25 13:47:35 -05:00
2016-10-13 21:55:23 -04:00
$tables = self::$db->getSystemTables();
2014-02-25 13:47:35 -05:00
2012-03-15 09:25:18 -04:00
foreach($tables as $t)
{
if(stripos($t, 'rdb$') !== 0 && stripos($t, 'mon$') !== 0)
{
2016-10-13 21:55:23 -04:00
$onlySystem = FALSE;
2012-03-15 09:25:18 -04:00
break;
}
}
2014-02-25 13:47:35 -05:00
2016-10-13 21:55:23 -04:00
$this->assertTrue($onlySystem);
2012-03-15 09:25:18 -04:00
}
2014-02-25 13:47:35 -05:00
2014-02-07 15:43:25 -05:00
// --------------------------------------------------------------------------
// ! Create / Delete Tables
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-03-15 09:25:18 -04:00
2014-02-14 22:08:19 -05:00
public function testCreateTable()
2012-03-15 09:25:18 -04:00
{
//Attempt to create the table
2016-10-13 21:55:23 -04:00
$sql = self::$db->getUtil()->createTable('create_delete', array(
2014-02-25 13:47:35 -05:00
'id' => 'SMALLINT',
'key' => 'VARCHAR(64)',
2012-03-15 09:25:18 -04:00
'val' => 'BLOB SUB_TYPE TEXT'
));
self::$db->query($sql);
2014-02-25 13:47:35 -05:00
2012-03-15 09:25:18 -04:00
//Check
2016-10-13 21:55:23 -04:00
$this->assertTrue(in_array('create_delete', self::$db->getTables()));
2014-02-07 15:43:25 -05:00
}
2014-02-25 13:47:35 -05:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testDeleteTable()
2014-02-07 15:43:25 -05:00
{
//Attempt to delete the table
2016-10-13 21:55:23 -04:00
$sql = self::$db->getUtil()->deleteTable('create_delete');
self::$db->query($sql);
2014-02-25 13:47:35 -05:00
2014-02-07 15:43:25 -05:00
//Check
2016-10-13 21:55:23 -04:00
$tableExists = in_array('create_delete', self::$db->getTables());
$this->assertFalse($tableExists);
2014-02-07 15:43:25 -05:00
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testTruncate()
2012-03-15 09:25:18 -04:00
{
self::$db->truncate('create_test');
2014-02-25 13:47:35 -05:00
2016-10-13 21:55:23 -04:00
$this->assertTrue(self::$db->affectedRows() > 0);
2012-03-15 09:25:18 -04:00
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testCommitTransaction()
2012-03-15 09:25:18 -04:00
{
$res = self::$db->beginTransaction();
2014-02-25 13:47:35 -05:00
2012-03-15 09:25:18 -04:00
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
self::$db->query($sql);
2014-02-25 13:47:35 -05:00
$res = self::$db->commit();
2012-03-15 09:25:18 -04:00
$this->assertTrue($res);
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testRollbackTransaction()
2012-03-15 09:25:18 -04:00
{
$res = self::$db->beginTransaction();
2014-02-25 13:47:35 -05:00
2012-03-15 09:25:18 -04:00
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
self::$db->query($sql);
2014-02-25 13:47:35 -05:00
$res = self::$db->rollback();
2012-03-15 09:25:18 -04:00
$this->assertTrue($res);
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testPreparedStatements()
2012-03-15 09:25:18 -04:00
{
$sql = <<<SQL
2014-02-25 13:47:35 -05:00
INSERT INTO "create_test" ("id", "key", "val")
2012-03-15 09:25:18 -04:00
VALUES (?,?,?)
SQL;
$query = self::$db->prepare($sql);
$query->execute(array(1,"booger's", "Gross"));
2012-03-15 09:25:18 -04:00
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testPrepareExecute()
2012-03-15 09:25:18 -04:00
{
$sql = <<<SQL
2014-02-25 13:47:35 -05:00
INSERT INTO "create_test" ("id", "key", "val")
2012-03-15 09:25:18 -04:00
VALUES (?,?,?)
SQL;
2016-10-13 21:55:23 -04:00
self::$db->prepareExecute($sql, array(
2012-03-15 09:25:18 -04:00
2, "works", 'also?'
));
2014-02-25 13:47:35 -05:00
2012-03-15 09:25:18 -04:00
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testFetch()
2012-03-15 09:25:18 -04:00
{
$res = self::$db->query('SELECT "key","val" FROM "create_test"');
2014-02-25 13:47:35 -05:00
2014-02-07 15:43:25 -05:00
// Object
$fetchObj = $res->fetchObject();
$this->assertIsA($fetchObj, 'stdClass');
2014-02-25 13:47:35 -05:00
2014-02-07 15:43:25 -05:00
// Associative array
$fetchAssoc = $res->fetch(PDO::FETCH_ASSOC);
$this->assertTrue(array_key_exists('key', $fetchAssoc));
2014-02-25 13:47:35 -05:00
2014-02-07 15:43:25 -05:00
// Numeric array
$res2 = self::$db->query('SELECT "id","key","val" FROM "create_test"');
2014-02-07 15:43:25 -05:00
$fetch = $res2->fetch(PDO::FETCH_NUM);
$this->assertTrue(is_array($fetch));
2012-03-15 09:25:18 -04:00
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testPrepareQuery()
2012-03-15 09:25:18 -04:00
{
2016-10-13 21:55:23 -04:00
$this->assertNull(self::$db->prepareQuery('', array()));
2014-02-07 15:43:25 -05:00
}
2014-02-25 13:47:35 -05:00
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
2014-02-20 11:00:18 -05:00
public function testErrorInfo()
{
$result = self::$db->errorInfo();
2014-02-25 13:47:35 -05:00
2014-02-20 11:00:18 -05:00
$expected = array (
0 => 0,
1 => false,
2 => false,
);
2014-02-25 13:47:35 -05:00
2014-02-20 11:00:18 -05:00
$this->assertEqual($expected, $result);
}
2014-02-25 13:47:35 -05:00
2014-02-20 11:00:18 -05:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
public function testErrorCode()
2014-02-20 11:00:18 -05:00
{
$result = self::$db->errorCode();
2014-02-20 11:00:18 -05:00
$this->assertFalse($result);
}
2014-02-25 13:47:35 -05:00
2014-02-20 11:00:18 -05:00
// --------------------------------------------------------------------------
2014-02-25 13:47:35 -05:00
public function testDBList()
2014-02-20 11:00:18 -05:00
{
2016-10-13 21:55:23 -04:00
$res = self::$db->getSql()->dbList();
2014-02-20 11:00:18 -05:00
$this->assertNULL($res);
}
2014-03-31 16:01:58 -04:00
// --------------------------------------------------------------------------
public function testExec()
{
$res = self::$db->exec('SELECT * FROM "create_test"');
2014-03-31 16:01:58 -04:00
$this->assertEquals(NULL, $res);
}
// --------------------------------------------------------------------------
public function testInTransaction()
{
self::$db->beginTransaction();
$this->assertTrue(self::$db->inTransaction());
self::$db->rollBack();
$this->assertFalse(self::$db->inTransaction());
2014-03-31 16:01:58 -04:00
}
// --------------------------------------------------------------------------
public function testGetAttribute()
{
$res = self::$db->getAttribute("foo");
2014-03-31 16:01:58 -04:00
$this->assertEquals(NULL, $res);
}
// --------------------------------------------------------------------------
public function testSetAttribute()
{
$this->assertFalse(self::$db->setAttribute(47, 'foo'));
2014-03-31 16:01:58 -04:00
}
public function testLastInsertId()
{
$this->assertEqual(0, self::$db->lastInsertId('NEWTABLE_SEQ'));
}
2012-03-15 09:25:18 -04:00
}