Query/tests/databases/firebird/FirebirdTest.php

297 lines
7.1 KiB
PHP
Raw Normal View History

2012-03-15 09:25:18 -04:00
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
2012-04-20 13:17:39 -04:00
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
2012-03-15 09:25:18 -04:00
* @link https://github.com/aviat4ion/Query
2012-04-20 13:17:39 -04:00
* @license http://philsturgeon.co.uk/code/dbad-license
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);
self::$db->set_table_prefix('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
$this->tables = self::$db->get_tables();
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
{
2012-03-15 09:25:18 -04:00
$only_system = TRUE;
2014-02-25 13:47:35 -05:00
$tables = self::$db->get_system_tables();
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)
{
$only_system = FALSE;
break;
}
}
2014-02-25 13:47:35 -05:00
2012-03-15 09:25:18 -04:00
$this->assertTrue($only_system);
}
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
$sql = self::$db->get_util()->create_table('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
$this->assertTrue(in_array('create_delete', self::$db->get_tables()));
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
$sql = self::$db->get_util()->delete_table('create_delete');
self::$db->query($sql);
2014-02-25 13:47:35 -05:00
2014-02-07 15:43:25 -05:00
//Check
$table_exists = in_array('create_delete', self::$db->get_tables());
2014-02-07 15:43:25 -05:00
$this->assertFalse($table_exists);
}
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
{
if (version_compare(PHP_VERSION, '7.0.0', '>='))
{
$this->markTestSkipped("Segfaults on this version of PHP");
}
self::$db->truncate('create_test');
2014-02-25 13:47:35 -05:00
$this->assertTrue(self::$db->affected_rows() > 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;
self::$db->prepare_execute($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
{
$this->assertNull(self::$db->prepare_query('', 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
{
$res = self::$db->get_sql()->db_list();
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
}