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
|
2014-02-18 15:18:01 -05:00
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MySQLTest class.
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2014-02-14 22:08:19 -05:00
|
|
|
* @extends DBTest
|
|
|
|
* @requires extension pdo_mysql
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2012-03-19 13:38:49 -04:00
|
|
|
class MySQLTest extends DBTest {
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
public static function setUpBeforeClass()
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
|
|
|
// Attempt to connect, if there is a test config file
|
2014-02-18 19:29:58 -05:00
|
|
|
if (is_file(QTEST_DIR . "/settings.json"))
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2014-02-18 19:29:58 -05:00
|
|
|
$params = json_decode(file_get_contents(QTEST_DIR . "/settings.json"));
|
2012-07-05 14:19:49 -04:00
|
|
|
$params = $params->mysql;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db = new \Query\Drivers\Mysql\Driver("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, array(
|
2014-02-17 20:10:47 -05:00
|
|
|
PDO::ATTR_PERSISTENT => TRUE
|
|
|
|
));
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2012-03-22 16:33:28 -04:00
|
|
|
elseif (($var = getenv('CI')))
|
2012-03-22 15:59:03 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db = new \Query\Drivers\Mysql\Driver('host=127.0.0.1;port=3306;dbname=test', 'root');
|
2012-03-22 15:59:03 -04:00
|
|
|
}
|
2014-04-09 10:30:01 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->table_prefix = 'create_';
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testExists()
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
2012-07-03 13:00:17 -04:00
|
|
|
$this->assertTrue(in_array('mysql', PDO::getAvailableDrivers()));
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testConnection()
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$this->assertIsA(self::$db, '\\Query\\Drivers\\Mysql\\Driver');
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-15 15:46:04 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testCreateTable()
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/mysql.sql'));
|
2014-04-08 17:13:41 -04:00
|
|
|
|
2012-03-15 15:46:04 -04:00
|
|
|
//Attempt to create the table
|
2015-07-17 15:38:06 -04:00
|
|
|
$sql = self::$db->util->create_table('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
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-15 15:46:04 -04:00
|
|
|
//Attempt to create the table
|
2015-07-17 15:38:06 -04:00
|
|
|
$sql = self::$db->util->create_table('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'
|
|
|
|
)
|
|
|
|
);
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-03-15 15:46:04 -04:00
|
|
|
|
|
|
|
//Check
|
2015-07-17 15:38:06 -04:00
|
|
|
$dbs = self::$db->get_tables();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-09 10:30:01 -04:00
|
|
|
$this->assertTrue(in_array('create_test', $dbs));
|
2012-04-10 14:06:34 -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 testTruncate()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->truncate('test');
|
|
|
|
self::$db->truncate('join');
|
2012-04-10 14:06:34 -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 testPreparedStatements()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
2014-04-09 10:30:01 -04:00
|
|
|
INSERT INTO `create_test` (`id`, `key`, `val`)
|
2012-04-10 14:06:34 -04:00
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2015-07-17 15:38:06 -04:00
|
|
|
$statement = self::$db->prepare_query($sql, array(1,"boogers", "Gross"));
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
$res = $statement->execute();
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
$this->assertTrue($res);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-18 19:29:58 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-18 19:29:58 -05:00
|
|
|
public function testBadPreparedStatement()
|
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
2014-04-09 10:30:01 -04:00
|
|
|
INSERT INTO `create_test` (`id`, `key`, `val`)
|
2014-02-18 19:29:58 -05:00
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2014-02-25 13:47:35 -05:00
|
|
|
try
|
2014-02-18 19:29:58 -05:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$statement = self::$db->prepare_query($sql, 'foo');
|
2014-02-18 19:29:58 -05:00
|
|
|
}
|
|
|
|
catch(InvalidArgumentException $e)
|
|
|
|
{
|
|
|
|
$this->assertTrue(TRUE);
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-18 19:29:58 -05:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testPrepareExecute()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
2014-04-09 10:30:01 -04:00
|
|
|
INSERT INTO `create_test` (`id`, `key`, `val`)
|
2012-04-10 14:06:34 -04:00
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->prepare_execute($sql, array(
|
2012-04-10 14:06:34 -04:00
|
|
|
2, "works", 'also?'
|
|
|
|
));
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
$this->assertInstanceOf('PDOStatement', $res);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testCommitTransaction()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->beginTransaction();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-09 10:30:01 -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);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->commit();
|
2012-04-10 14:06:34 -04:00
|
|
|
$this->assertTrue($res);
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testRollbackTransaction()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->beginTransaction();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-09 10:30:01 -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);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
$res = self::$db->rollback();
|
2012-04-10 14:06:34 -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 testGetSchemas()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$this->assertNull(self::$db->get_schemas());
|
2012-04-10 14:06:34 -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 testGetSequences()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$this->assertNull(self::$db->get_sequences());
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testBackup()
|
2012-05-15 13:35:59 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
$this->assertTrue(is_string(self::$db->util->backup_structure()));
|
2012-05-15 13:35:59 -04:00
|
|
|
}
|
2014-04-28 16:41:46 -04:00
|
|
|
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
}
|