2016-10-12 22:12:25 -04:00
|
|
|
<?php declare(strict_types=1);
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
2016-10-12 22:12:25 -04:00
|
|
|
* Query
|
2012-03-15 09:25:18 -04:00
|
|
|
*
|
2016-10-12 22:12:25 -04:00
|
|
|
* SQL Query Builder / Database Abstraction Layer
|
2012-03-15 09:25:18 -04:00
|
|
|
*
|
2019-12-11 16:49:42 -05:00
|
|
|
* PHP version 7.2
|
2016-10-12 22:12:25 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-03-18 11:31:56 -04:00
|
|
|
* @copyright 2012 - 2020 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
|
|
|
|
* @version 3.0.0
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2018-01-24 15:03:41 -05:00
|
|
|
namespace Query\Tests\Drivers\PgSQL;
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2018-01-22 12:03:37 -05:00
|
|
|
use PDO;
|
2018-01-19 16:48:52 -05:00
|
|
|
use Query\Drivers\Pgsql\Driver;
|
|
|
|
use Query\Tests\BaseDriverTest;
|
2018-01-24 13:14:03 -05:00
|
|
|
use TypeError;
|
2018-01-19 16:48:52 -05:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* PgTest class.
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2014-02-14 22:08:19 -05:00
|
|
|
* @extends DBTest
|
|
|
|
* @requires extension pdo_pgsql
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2018-01-19 16:48:52 -05:00
|
|
|
class PgSQLDriverTest extends BaseDriverTest {
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function setUp(): void
|
2018-01-19 16:48:52 -05:00
|
|
|
{
|
2014-02-17 19:12:53 -05:00
|
|
|
// If the database isn't installed, skip the tests
|
2018-01-19 16:48:52 -05:00
|
|
|
if ( ! class_exists(Driver::class))
|
2014-02-17 19:12:53 -05:00
|
|
|
{
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->markTestSkipped('Postgres extension for PDO not loaded');
|
2014-02-17 19:12:53 -05:00
|
|
|
}
|
2015-07-17 15:38:06 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public static function setUpBeforeClass(): void
|
2015-07-17 15:38:06 -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
|
|
|
{
|
2018-01-19 16:48:52 -05:00
|
|
|
self::$db = new Driver('host=127.0.0.1;port=5432;dbname=test', 'postgres');
|
2015-11-10 16:29:17 -05:00
|
|
|
}
|
2012-03-19 10:40:16 -04:00
|
|
|
// Attempt to connect, if there is a test config file
|
2015-11-10 16:29:17 -05:00
|
|
|
else if ($params !== FALSE)
|
2012-03-19 10:40:16 -04:00
|
|
|
{
|
|
|
|
$params = $params->pgsql;
|
2018-01-19 16:48:52 -05:00
|
|
|
self::$db = new Driver("pgsql:host={$params->host};dbname={$params->database};port=5432", $params->user, $params->pass);
|
2012-03-19 10:40:16 -04:00
|
|
|
}
|
2014-04-09 10:30:01 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
self::$db->setTablePrefix('create_');
|
2012-03-19 10:40:16 -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 12:03:37 -05:00
|
|
|
$drivers = PDO::getAvailableDrivers();
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertTrue(in_array('pgsql', $drivers, 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-19 10:40:16 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
if (empty(self::$db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertIsA(self::$db, Driver::class);
|
2012-03-19 10:40:16 -04:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testCreateTable(): void
|
2012-03-19 10:40:16 -04:00
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->exec(file_get_contents(QTEST_DIR.'/db_files/pgsql.sql'));
|
2014-04-08 14:26:28 -04:00
|
|
|
|
2012-03-22 16:46:47 -04:00
|
|
|
// Drop the table(s) if they exist
|
|
|
|
$sql = 'DROP TABLE IF EXISTS "create_test"';
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-03-22 16:46:47 -04:00
|
|
|
$sql = 'DROP TABLE IF EXISTS "create_join"';
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
|
2012-03-19 10:40:16 -04:00
|
|
|
//Attempt to create the table
|
2016-10-13 21:55:23 -04:00
|
|
|
$sql = self::$db->getUtil()->createTable('create_test',
|
2018-01-26 08:39:30 -05:00
|
|
|
[
|
2012-03-19 10:40:16 -04:00
|
|
|
'id' => 'integer',
|
|
|
|
'key' => 'TEXT',
|
|
|
|
'val' => 'TEXT',
|
2018-01-26 08:39:30 -05:00
|
|
|
],
|
|
|
|
[
|
2012-03-19 10:40:16 -04:00
|
|
|
'id' => 'PRIMARY KEY'
|
2018-01-26 08:39:30 -05:00
|
|
|
]
|
2012-03-19 10:40:16 -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-19 10:40:16 -04:00
|
|
|
//Attempt to create the table
|
2016-10-13 21:55:23 -04:00
|
|
|
$sql = self::$db->getUtil()->createTable('create_join',
|
2018-01-26 08:39:30 -05:00
|
|
|
[
|
2012-03-19 10:40:16 -04:00
|
|
|
'id' => 'integer',
|
|
|
|
'key' => 'TEXT',
|
|
|
|
'val' => 'TEXT',
|
2018-01-26 08:39:30 -05:00
|
|
|
],
|
|
|
|
[
|
2012-03-19 10:40:16 -04:00
|
|
|
'id' => 'PRIMARY KEY'
|
2018-01-26 08:39:30 -05:00
|
|
|
]
|
2012-03-19 10:40:16 -04:00
|
|
|
);
|
2015-07-17 15:38:06 -04:00
|
|
|
self::$db->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-22 16:46:47 -04:00
|
|
|
//echo $sql.'<br />';
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-22 16:46:47 -04:00
|
|
|
//Reset
|
2015-07-17 15:38:06 -04:00
|
|
|
//unset(self::$db);
|
|
|
|
//$this->setUp();
|
2012-03-19 10:40:16 -04:00
|
|
|
|
|
|
|
//Check
|
2016-10-13 21:55:23 -04:00
|
|
|
$dbs = self::$db->getTables();
|
2018-01-19 16:48:52 -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
|
|
|
{
|
2018-01-19 16:48:52 -05:00
|
|
|
self::$db->truncate('test');
|
|
|
|
$this->assertEquals(0, self::$db->countAll('test'));
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
self::$db->truncate('join');
|
|
|
|
$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
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2018-01-26 08:39:30 -05:00
|
|
|
$statement = self::$db->prepareQuery($sql, [1,'boogers', 'Gross']);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
$statement->execute();
|
|
|
|
|
2018-01-22 12:03:37 -05:00
|
|
|
$res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=1')
|
|
|
|
->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'id' => 1,
|
|
|
|
'key' => 'boogers',
|
|
|
|
'val' => 'Gross'
|
|
|
|
], $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
|
|
|
{
|
2018-01-25 15:12:46 -05:00
|
|
|
if (is_a($this, \UnitTestCase::class))
|
|
|
|
{
|
|
|
|
$this->markTestSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:14:03 -05:00
|
|
|
$this->expectException(TypeError::class);
|
2018-01-19 16:48:52 -05:00
|
|
|
|
2014-02-18 19:29:58 -05:00
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2014-02-25 13:47:35 -05:00
|
|
|
|
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
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
if (empty(self::$db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2018-01-26 08:39:30 -05:00
|
|
|
self::$db->prepareExecute($sql, [
|
2018-01-22 12:03:37 -05:00
|
|
|
2, 'works', 'also?'
|
2018-01-26 08:39:30 -05:00
|
|
|
]);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2018-01-22 12:03:37 -05:00
|
|
|
$res = self::$db->query('SELECT * FROM "create_test" WHERE "id"=2')
|
|
|
|
->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'id' => 2,
|
|
|
|
'key' => 'works',
|
|
|
|
'val' => 'also?'
|
|
|
|
], $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
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
if (empty(self::$db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
self::$db->beginTransaction();
|
2012-04-10 14:06:34 -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
|
|
|
{
|
2015-07-17 15:38:06 -04:00
|
|
|
if (empty(self::$db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
self::$db->beginTransaction();
|
2012-04-10 14:06:34 -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
|
|
|
{
|
2018-01-26 08:39:30 -05:00
|
|
|
$this->assertTrue(\is_array(self::$db->getSchemas()));
|
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 testGetDBs(): void
|
2014-02-25 13:47:35 -05:00
|
|
|
{
|
2018-01-26 08:39:30 -05:00
|
|
|
$this->assertTrue(\is_array(self::$db->getDbs()));
|
2014-02-25 13:47:35 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGetFunctions(): void
|
2014-02-25 13:47:35 -05:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->assertNull(self::$db->getFunctions());
|
2014-02-25 13:47:35 -05:00
|
|
|
}
|
2018-01-19 16:48:52 -05:00
|
|
|
}
|