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
|
|
|
*
|
2022-09-29 11:33:08 -04:00
|
|
|
* PHP version 8.1
|
2016-10-12 22:12:25 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
2023-01-20 11:30:51 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshome.page>
|
|
|
|
* @copyright 2012 - 2023 Timothy J. Warren
|
2016-10-12 22:12:25 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-11 16:49:42 -05:00
|
|
|
* @link https://git.timshomepage.net/aviat/Query
|
2023-03-17 16:34:21 -04:00
|
|
|
* @version 4.1.0
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2023-03-17 15:30:36 -04:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
namespace Query\Tests\Drivers\MySQL;
|
|
|
|
|
|
|
|
use PDO;
|
2016-10-13 21:55:23 -04:00
|
|
|
use Query\Drivers\Mysql\Driver;
|
2018-01-19 16:48:52 -05:00
|
|
|
use Query\Tests\BaseDriverTest;
|
2018-01-24 13:14:03 -05:00
|
|
|
use TypeError;
|
2023-03-17 15:30:36 -04:00
|
|
|
use UnitTestCase;
|
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
|
|
|
* @requires extension pdo_mysql
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2023-03-17 15:30:36 -04:00
|
|
|
class MySQLDriverTest extends BaseDriverTest
|
|
|
|
{
|
2019-12-10 12:17:40 -05:00
|
|
|
public static function setUpBeforeClass(): void
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2015-11-10 16:29:17 -05:00
|
|
|
$params = get_json_config();
|
2018-01-19 16:48:52 -05:00
|
|
|
if ($var = getenv('TRAVIS'))
|
2015-11-10 16:29:17 -05:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
self::$db = new Driver('host=127.0.0.1;port=3306;dbname=test', 'root');
|
2015-11-10 16:29:17 -05:00
|
|
|
}
|
2012-03-15 15:46:04 -04:00
|
|
|
// Attempt to connect, if there is a test config file
|
2023-03-17 15:30:36 -04:00
|
|
|
elseif ($params !== FALSE)
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2012-07-05 14:19:49 -04:00
|
|
|
$params = $params->mysql;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2018-01-26 08:39:30 -05:00
|
|
|
self::$db = new Driver("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, [
|
2023-03-17 15:30:36 -04:00
|
|
|
PDO::ATTR_PERSISTENT => TRUE,
|
2018-01-26 08:39:30 -05:00
|
|
|
]);
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2014-04-09 10:30:01 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
self::$db->setTablePrefix('create_');
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testExists(): void
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
2018-01-22 15:43:56 -05:00
|
|
|
$this->assertTrue(\in_array('mysql', PDO::getAvailableDrivers(), TRUE));
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testConnection(): void
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertIsA(self::$db, Driver::class);
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testCreateTable(): void
|
2012-03-15 15:46:04 -04:00
|
|
|
{
|
2023-03-17 15:30:36 -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
|
2023-03-17 15:30:36 -04:00
|
|
|
$sql = self::$db->getUtil()->createTable(
|
|
|
|
'test',
|
2018-01-26 08:39:30 -05:00
|
|
|
[
|
2012-03-15 16:12:20 -04:00
|
|
|
'id' => 'int(10)',
|
2012-03-15 15:46:04 -04:00
|
|
|
'key' => 'TEXT',
|
|
|
|
'val' => 'TEXT',
|
2018-01-26 08:39:30 -05:00
|
|
|
],
|
|
|
|
[
|
2023-03-17 15:30:36 -04:00
|
|
|
'id' => 'PRIMARY KEY',
|
2022-09-29 11:31:25 -04:00
|
|
|
],
|
2012-03-15 15:46:04 -04:00
|
|
|
);
|
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
|
2023-03-17 15:30:36 -04:00
|
|
|
$sql = self::$db->getUtil()->createTable(
|
|
|
|
'join',
|
2018-01-26 08:39:30 -05:00
|
|
|
[
|
2012-03-15 16:12:20 -04:00
|
|
|
'id' => 'int(10)',
|
2012-03-15 15:46:04 -04:00
|
|
|
'key' => 'TEXT',
|
|
|
|
'val' => 'TEXT',
|
2018-01-26 08:39:30 -05:00
|
|
|
],
|
|
|
|
[
|
2023-03-17 15:30:36 -04:00
|
|
|
'id' => 'PRIMARY KEY',
|
2018-01-26 08:39:30 -05:00
|
|
|
]
|
2012-03-15 15:46:04 -04:00
|
|
|
);
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-03-15 15:46:04 -04:00
|
|
|
|
|
|
|
//Check
|
2016-10-13 21:55:23 -04:00
|
|
|
$dbs = self::$db->getTables();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2018-01-22 15:43:56 -05:00
|
|
|
$this->assertTrue(\in_array('create_test', $dbs, TRUE));
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testTruncate(): void
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->truncate('test');
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertEquals(0, self::$db->countAll('test'));
|
|
|
|
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->truncate('join');
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertEquals(0, self::$db->countAll('join'));
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testPreparedStatements(): void
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2023-03-17 15:30:36 -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;
|
2019-12-10 12:17:40 -05:00
|
|
|
$statement = self::$db->prepareQuery($sql, [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
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testBadPreparedStatement(): void
|
2014-02-18 19:29:58 -05:00
|
|
|
{
|
2023-03-17 15:30:36 -04:00
|
|
|
if (is_a($this, UnitTestCase::class))
|
2018-01-25 15:12:46 -05:00
|
|
|
{
|
|
|
|
$this->markTestSkipped();
|
2023-03-17 15:30:36 -04:00
|
|
|
|
2018-01-25 15:12:46 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:14:03 -05:00
|
|
|
$this->expectException(TypeError::class);
|
2018-01-19 16:48:52 -05:00
|
|
|
|
2023-03-17 15:30:36 -04:00
|
|
|
$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;
|
2018-01-19 16:48:52 -05:00
|
|
|
|
|
|
|
self::$db->prepareQuery($sql, 'foo');
|
2014-02-18 19:29:58 -05:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testPrepareExecute(): void
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2023-03-17 15:30:36 -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;
|
2018-01-26 08:39:30 -05:00
|
|
|
$res = self::$db->prepareExecute($sql, [
|
2023-03-17 15:30:36 -04:00
|
|
|
2, 'works', 'also?',
|
2018-01-26 08:39:30 -05:00
|
|
|
]);
|
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
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testCommitTransaction(): void
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2020-12-03 15:50:32 -05:00
|
|
|
// Make sure we aren't already in a transaction
|
|
|
|
if (self::$db->inTransaction())
|
|
|
|
{
|
|
|
|
self::$db->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertFalse(self::$db->inTransaction());
|
2019-12-11 16:48:43 -05:00
|
|
|
$this->assertTrue(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
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testRollbackTransaction(): void
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2020-12-03 15:50:32 -05:00
|
|
|
// Make sure we aren't already in a transaction
|
|
|
|
if (self::$db->inTransaction())
|
|
|
|
{
|
|
|
|
self::$db->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertFalse(self::$db->inTransaction());
|
2019-12-11 16:48:43 -05:00
|
|
|
$this->assertTrue(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
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGetSchemas(): void
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2023-03-17 15:30:36 -04:00
|
|
|
$this->assertTrue(in_array('test', self::$db->getSchemas(), TRUE));
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGetSequences(): void
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->assertNull(self::$db->getSequences());
|
2012-03-15 15:46:04 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testBackup(): void
|
2012-05-15 13:35:59 -04:00
|
|
|
{
|
2023-03-17 15:30:36 -04:00
|
|
|
$this->assertIsString(self::$db->getUtil()->backupStructure());
|
2012-05-15 13:35:59 -04:00
|
|
|
}
|
2023-03-17 15:30:36 -04:00
|
|
|
}
|