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
|
2012-03-15 09:25:18 -04:00
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MySQLTest class.
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2012-03-15 09:25:18 -04:00
|
|
|
* @extends UnitTestCase
|
|
|
|
*/
|
2012-03-19 13:38:49 -04:00
|
|
|
class MySQLTest extends DBTest {
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function setUp()
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
|
|
|
// Attempt to connect, if there is a test config file
|
2012-04-30 16:06:06 -04:00
|
|
|
if (is_file(QBASE_DIR . "test_config.json"))
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2012-04-30 16:06:06 -04:00
|
|
|
$params = json_decode(file_get_contents(QBASE_DIR . "test_config.json"));
|
2012-03-15 15:46:04 -04:00
|
|
|
$params = $params->mysql;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-15 15:46:04 -04:00
|
|
|
$this->db = new MySQL("host={$params->host};port={$params->port};dbname={$params->database}", $params->user, $params->pass);
|
|
|
|
}
|
2012-03-22 16:33:28 -04:00
|
|
|
elseif (($var = getenv('CI')))
|
2012-03-22 15:59:03 -04:00
|
|
|
{
|
2012-03-22 16:12:54 -04:00
|
|
|
$this->db = new MySQL('host=127.0.0.1;port=3306;dbname=test', 'root');
|
2012-03-22 15:59:03 -04:00
|
|
|
}
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2012-04-24 14:00:44 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestExists()
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
|
|
|
$this->assertTrue(in_array('mysql', pdo_drivers()));
|
|
|
|
}
|
2012-04-24 14:00:44 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestConnection()
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2012-04-10 14:06:34 -04:00
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
2012-03-15 15:46:04 -04:00
|
|
|
$this->assertIsA($this->db, 'MySQL');
|
|
|
|
}
|
2012-04-24 14:00:44 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-03-15 15:46:04 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestCreateTable()
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2012-04-10 14:06:34 -04:00
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
2012-03-15 15:46:04 -04:00
|
|
|
//Attempt to create the table
|
2012-04-18 15:53:06 -04:00
|
|
|
$sql = $this->db->util->create_table('create_test',
|
2012-03-15 15:46:04 -04:00
|
|
|
array(
|
2012-03-15 16:12:20 -04:00
|
|
|
'id' => 'int(10)',
|
2012-03-15 15:46:04 -04:00
|
|
|
'key' => 'TEXT',
|
|
|
|
'val' => 'TEXT',
|
2012-04-10 14:06:34 -04:00
|
|
|
),
|
2012-03-15 15:46:04 -04:00
|
|
|
array(
|
|
|
|
'id' => 'PRIMARY KEY'
|
|
|
|
)
|
|
|
|
);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-15 15:46:04 -04:00
|
|
|
$this->db->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-15 15:46:04 -04:00
|
|
|
//Attempt to create the table
|
2012-04-18 15:53:06 -04:00
|
|
|
$sql = $this->db->util->create_table('create_join',
|
2012-03-15 15:46:04 -04:00
|
|
|
array(
|
2012-03-15 16:12:20 -04:00
|
|
|
'id' => 'int(10)',
|
2012-03-15 15:46:04 -04:00
|
|
|
'key' => 'TEXT',
|
|
|
|
'val' => 'TEXT',
|
2012-04-10 14:06:34 -04:00
|
|
|
),
|
2012-03-15 15:46:04 -04:00
|
|
|
array(
|
|
|
|
'id' => 'PRIMARY KEY'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->db->query($sql);
|
|
|
|
|
|
|
|
//Check
|
|
|
|
$dbs = $this->db->get_tables();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-15 16:12:20 -04:00
|
|
|
$this->assertTrue(in_array('create_test', $dbs));
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestTruncate()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$this->db->truncate('create_test');
|
|
|
|
$this->db->truncate('create_join');
|
|
|
|
|
|
|
|
$ct_query = $this->db->query('SELECT * FROM create_test');
|
|
|
|
$cj_query = $this->db->query('SELECT * FROM create_join');
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestPreparedStatements()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO `create_test` (`id`, `key`, `val`)
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
|
|
|
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
|
|
|
|
|
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
}
|
2012-04-24 14:00:44 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestPrepareExecute()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO `create_test` (`id`, `key`, `val`)
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
|
|
|
$this->db->prepare_execute($sql, array(
|
|
|
|
2, "works", 'also?'
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
2012-04-24 14:00:44 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestCommitTransaction()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$res = $this->db->beginTransaction();
|
|
|
|
|
|
|
|
$sql = 'INSERT INTO `create_test` (`id`, `key`, `val`) VALUES (10, 12, 14)';
|
|
|
|
$this->db->query($sql);
|
|
|
|
|
|
|
|
$res = $this->db->commit();
|
|
|
|
$this->assertTrue($res);
|
|
|
|
}
|
2012-04-24 14:00:44 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestRollbackTransaction()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$res = $this->db->beginTransaction();
|
|
|
|
|
|
|
|
$sql = 'INSERT INTO `create_test` (`id`, `key`, `val`) VALUES (182, 96, 43)';
|
|
|
|
$this->db->query($sql);
|
|
|
|
|
|
|
|
$res = $this->db->rollback();
|
|
|
|
$this->assertTrue($res);
|
|
|
|
}
|
2012-03-15 16:12:20 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestGetSchemas()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$this->assertFalse($this->db->get_schemas());
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestGetsProcedures()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$this->assertTrue(is_array($this->db->get_procedures()));
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestGetTriggers()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$this->assertTrue(is_array($this->db->get_triggers()));
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestGetSequences()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$this->assertFalse($this->db->get_sequences());
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2012-04-24 14:00:44 -04:00
|
|
|
}
|