2024-06-03 23:46:39 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Test\Unit\Query;
|
|
|
|
|
2024-06-08 03:06:31 +00:00
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
|
2024-06-03 23:46:39 +00:00
|
|
|
use BitBadger\PDODocument\Query\Definition;
|
2024-06-04 01:09:03 +00:00
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
2024-06-03 23:46:39 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the Definition class
|
|
|
|
*/
|
|
|
|
class DefinitionTest extends TestCase
|
|
|
|
{
|
2024-06-04 01:09:03 +00:00
|
|
|
#[TestDox('Ensure table succeeds for PosgtreSQL')]
|
|
|
|
public function testEnsureTableSucceedsForPostgreSQL(): void
|
2024-06-03 23:46:39 +00:00
|
|
|
{
|
2024-06-04 01:09:03 +00:00
|
|
|
try {
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
$this->assertEquals('CREATE TABLE IF NOT EXISTS documents (data JSONB NOT NULL)',
|
|
|
|
Definition::ensureTable('documents'), 'CREATE TABLE statement not generated correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Ensure table succeeds for SQLite')]
|
|
|
|
public function testEnsureTableSucceedsForSQLite(): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
|
|
|
$this->assertEquals('CREATE TABLE IF NOT EXISTS dox (data TEXT NOT NULL)', Definition::ensureTable('dox'),
|
|
|
|
'CREATE TABLE statement not generated correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnsureTableFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Configuration::$mode = null;
|
|
|
|
Definition::ensureTable('boom');
|
2024-06-03 23:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnsureIndexOnSucceedsWithoutSchemaSingleAscendingField(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_test_fields ON test ((data->>'details'))",
|
|
|
|
Definition::ensureIndexOn('test', 'fields', ['details']), 'CREATE INDEX statement not generated correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnsureIndexOnSucceedsWithSchemaMultipleFields(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
"CREATE INDEX IF NOT EXISTS idx_testing_json ON sch.testing ((data->>'group'), (data->>'sub_group') DESC)",
|
|
|
|
Definition::ensureIndexOn('sch.testing', 'json', ['group', 'sub_group DESC']),
|
|
|
|
'CREATE INDEX statement not generated correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnsureKey(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals("CREATE UNIQUE INDEX IF NOT EXISTS idx_tbl_key ON tbl ((data->>'id'))",
|
|
|
|
Definition::ensureKey('tbl'), 'CREATE INDEX statement for document key not generated correctly');
|
|
|
|
}
|
|
|
|
}
|