2024-06-08 23:58:45 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Test\Unit;
|
|
|
|
|
2024-06-11 11:07:56 +00:00
|
|
|
use BitBadger\PDODocument\{AutoId, Configuration, DocumentException, Field, FieldMatch, Mode, Query};
|
2024-06-08 23:58:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the Query class
|
|
|
|
*/
|
2024-06-12 21:43:17 +00:00
|
|
|
#[TestDox('Query (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class QueryTest extends TestCase
|
|
|
|
{
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectFromTableSucceeds(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals('SELECT data FROM testing', Query::selectFromTable('testing'),
|
|
|
|
'Query not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhereByFieldsSucceedsForSingleField(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals("data->>'test_field' <= :it",
|
|
|
|
Query::whereByFields([Field::LE('test_field', '', ':it')]), 'WHERE fragment not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testWhereByFieldsSucceedsForMultipleFieldsAll(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals("data->>'test_field' <= :it AND data->>'other_field' = :other",
|
|
|
|
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')]),
|
|
|
|
'WHERE fragment not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testWhereByFieldsSucceedsForMultipleFieldsAny(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals("data->>'test_field' <= :it OR data->>'other_field' = :other",
|
2024-06-11 11:07:56 +00:00
|
|
|
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')],
|
|
|
|
FieldMatch::Any),
|
2024-06-08 23:58:45 +00:00
|
|
|
'WHERE fragment not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Where by ID succeeds with default parameter')]
|
|
|
|
public function testWhereByIdSucceedsWithDefaultParameter(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals("data->>'id' = :id", Query::whereById(), 'WHERE fragment not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Where by ID succeeds with specific parameter')]
|
|
|
|
public function testWhereByIdSucceedsWithSpecificParameter(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals("data->>'id' = :di", Query::whereById(':di'), 'WHERE fragment not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:43:17 +00:00
|
|
|
public function testWhereDataContainsSucceedsWithDefaultParameter(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
try {
|
|
|
|
$this->assertEquals('data @> :criteria', Query::whereDataContains(),
|
|
|
|
'WHERE fragment not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhereDataContainsSucceedsWithSpecifiedParameter(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
try {
|
|
|
|
$this->assertEquals('data @> :it', Query::whereDataContains(':it'),
|
|
|
|
'WHERE fragment not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Where data contains fails if not PostgreSQL')]
|
|
|
|
public function testWhereDataContainsFailsIfNotPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = null;
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Query::whereDataContains();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Where JSON Path matches succeeds with default parameter')]
|
|
|
|
public function testWhereJsonPathMatchesSucceedsWithDefaultParameter(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
try {
|
2024-06-14 01:39:16 +00:00
|
|
|
$this->assertEquals('jsonb_path_exists(data, :path::jsonpath)', Query::whereJsonPathMatches(),
|
2024-06-12 21:43:17 +00:00
|
|
|
'WHERE fragment not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Where JSON Path matches succeeds with specified parameter')]
|
|
|
|
public function testWhereJsonPathMatchesSucceedsWithSpecifiedParameter(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
try {
|
2024-06-14 01:39:16 +00:00
|
|
|
$this->assertEquals('jsonb_path_exists(data, :road::jsonpath)', Query::whereJsonPathMatches(':road'),
|
2024-06-12 21:43:17 +00:00
|
|
|
'WHERE fragment not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Where JSON Path matches fails if not PostgreSQL')]
|
|
|
|
public function testWhereJsonPathMatchesFailsIfNotPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = null;
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Query::whereJsonPathMatches();
|
|
|
|
}
|
|
|
|
|
2024-06-11 11:07:56 +00:00
|
|
|
#[TestDox('Insert succeeds with no auto-ID for PostgreSQL')]
|
|
|
|
public function testInsertSucceedsWithNoAutoIdForPostgreSQL(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
2024-06-11 11:07:56 +00:00
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
try {
|
|
|
|
$this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'),
|
|
|
|
'INSERT statement not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Insert succeeds with no auto-ID for SQLite')]
|
|
|
|
public function testInsertSucceedsWithNoAutoIdForSQLite(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
|
|
|
try {
|
|
|
|
$this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'),
|
|
|
|
'INSERT statement not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Insert succeeds with auto numeric ID for PostgreSQL')]
|
|
|
|
public function testInsertSucceedsWithAutoNumericIdForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
try {
|
|
|
|
$this->assertEquals(
|
2024-06-12 19:21:53 +00:00
|
|
|
"INSERT INTO test_tbl VALUES (:data::jsonb || ('{\"id\":' "
|
|
|
|
. "|| (SELECT COALESCE(MAX((data->>'id')::numeric), 0) + 1 FROM test_tbl) || '}')::jsonb)",
|
2024-06-11 11:07:56 +00:00
|
|
|
Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Insert succeeds with auto numeric ID for SQLite')]
|
|
|
|
public function testInsertSucceedsWithAutoNumericIdForSQLite(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
|
|
|
try {
|
|
|
|
$this->assertEquals(
|
|
|
|
"INSERT INTO test_tbl VALUES (json_set(:data, '$.id', "
|
|
|
|
. "(SELECT coalesce(max(data->>'id'), 0) + 1 FROM test_tbl)))",
|
|
|
|
Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Insert succeeds with auto UUID for PostgreSQL')]
|
|
|
|
public function testInsertSucceedsWithAutoUuidForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
try {
|
|
|
|
$query = Query::insert('test_tbl', AutoId::UUID);
|
2024-06-12 19:21:53 +00:00
|
|
|
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", $query,
|
2024-06-11 11:07:56 +00:00
|
|
|
'INSERT statement not constructed correctly');
|
|
|
|
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Insert succeeds with auto UUID for SQLite')]
|
|
|
|
public function testInsertSucceedsWithAutoUuidForSQLite(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
|
|
|
try {
|
|
|
|
$query = Query::insert('test_tbl', AutoId::UUID);
|
|
|
|
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query,
|
|
|
|
'INSERT statement not constructed correctly');
|
|
|
|
$this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Insert succeeds with auto random string for PostgreSQL')]
|
|
|
|
public function testInsertSucceedsWithAutoRandomStringForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
Configuration::$idStringLength = 8;
|
|
|
|
try {
|
|
|
|
$query = Query::insert('test_tbl', AutoId::RandomString);
|
2024-06-12 19:21:53 +00:00
|
|
|
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", $query,
|
2024-06-11 11:07:56 +00:00
|
|
|
'INSERT statement not constructed correctly');
|
|
|
|
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
|
2024-06-12 19:21:53 +00:00
|
|
|
$id = str_replace(["INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", "\"}')"], '', $query);
|
2024-06-11 11:07:56 +00:00
|
|
|
$this->assertEquals(8, strlen($id), "Generated ID [$id] should have been 8 characters long");
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
Configuration::$idStringLength = 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('Insert succeeds with auto random string for SQLite')]
|
|
|
|
public function testInsertSucceedsWithAutoRandomStringForSQLite(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
|
|
|
try {
|
|
|
|
$query = Query::insert('test_tbl', AutoId::RandomString);
|
|
|
|
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query,
|
|
|
|
'INSERT statement not constructed correctly');
|
|
|
|
$this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly');
|
|
|
|
$id = str_replace(["INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", "'))"], '', $query);
|
|
|
|
$this->assertEquals(16, strlen($id), "Generated ID [$id] should have been 16 characters long");
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInsertFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Configuration::$mode = null;
|
|
|
|
Query::insert('kaboom');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSaveSucceeds(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
"INSERT INTO test_tbl VALUES (:data) ON CONFLICT ((data->>'id')) DO UPDATE SET data = EXCLUDED.data",
|
|
|
|
Query::save('test_tbl'), 'INSERT ON CONFLICT statement not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateSucceeds()
|
|
|
|
{
|
|
|
|
$this->assertEquals("UPDATE testing SET data = :data WHERE data->>'id' = :id", Query::update('testing'),
|
|
|
|
'UPDATE statement not constructed correctly');
|
|
|
|
}
|
|
|
|
}
|