2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
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-21 13:46:41 +00:00
|
|
|
#[TestDox('Query (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class QueryTest extends TestCase
|
|
|
|
{
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('selectFromTable() succeeds')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testSelectFromTableSucceeds(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals('SELECT data FROM testing', Query::selectFromTable('testing'),
|
|
|
|
'Query not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereByFields() succeeds for single field')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testWhereByFieldsSucceedsForSingleField(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals("data->>'test_field' <= :it",
|
2024-09-27 02:15:00 +00:00
|
|
|
Query::whereByFields([Field::lessOrEqual('test_field', '', ':it')]),
|
|
|
|
'WHERE fragment not constructed correctly');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereByFields() succeeds for multiple fields All')]
|
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",
|
2024-09-27 02:15:00 +00:00
|
|
|
Query::whereByFields(
|
|
|
|
[Field::lessOrEqual('test_field', '', ':it'), Field::equal('other_field', '', ':other')]),
|
2024-06-08 23:58:45 +00:00
|
|
|
'WHERE fragment not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereByFields() succeeds for multiple fields Any')]
|
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-09-27 02:15:00 +00:00
|
|
|
Query::whereByFields(
|
|
|
|
[Field::lessOrEqual('test_field', '', ':it'), Field::equal('other_field', '', ':other')],
|
2024-06-11 11:07:56 +00:00
|
|
|
FieldMatch::Any),
|
2024-06-08 23:58:45 +00:00
|
|
|
'WHERE fragment not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereById() succeeds with default parameter')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testWhereByIdSucceedsWithDefaultParameter(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals("data->>'id' = :id", Query::whereById(), 'WHERE fragment not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereById() succeeds with specific parameter')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testWhereByIdSucceedsWithSpecificParameter(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals("data->>'id' = :di", Query::whereById(':di'), 'WHERE fragment not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereDataContains() succeeds with default parameter')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testWhereDataContainsSucceedsWithDefaultParameter(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals('data @> :criteria', Query::whereDataContains(),
|
|
|
|
'WHERE fragment not constructed correctly');
|
2024-06-21 13:46:41 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereDataContains() succeeds with specific parameter')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testWhereDataContainsSucceedsWithSpecifiedParameter(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals('data @> :it', Query::whereDataContains(':it'), 'WHERE fragment not constructed correctly');
|
2024-06-21 13:46:41 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereDataContains() fails if not PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testWhereDataContainsFailsIfNotPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Query::whereDataContains();
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereJsonPathMatches() succeeds with default parameter')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testWhereJsonPathMatchesSucceedsWithDefaultParameter(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals('jsonb_path_exists(data, :path::jsonpath)', Query::whereJsonPathMatches(),
|
|
|
|
'WHERE fragment not constructed correctly');
|
2024-06-21 13:46:41 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereJsonPathMatches() succeeds with specified parameter')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testWhereJsonPathMatchesSucceedsWithSpecifiedParameter(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals('jsonb_path_exists(data, :road::jsonpath)', Query::whereJsonPathMatches(':road'),
|
|
|
|
'WHERE fragment not constructed correctly');
|
2024-06-21 13:46:41 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('whereJsonPathMatches() fails if not PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testWhereJsonPathMatchesFailsIfNotPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Query::whereJsonPathMatches();
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('insert() succeeds with no auto-ID for PostgreSQL')]
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testInsertSucceedsWithNoAutoIdForPostgreSQL(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'),
|
|
|
|
'INSERT statement not constructed correctly');
|
2024-06-11 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('insert() succeeds with no auto-ID for SQLite')]
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testInsertSucceedsWithNoAutoIdForSQLite(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
$this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'),
|
|
|
|
'INSERT statement not constructed correctly');
|
2024-06-11 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('insert() succeeds with auto numeric ID for PostgreSQL')]
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testInsertSucceedsWithAutoNumericIdForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals(
|
|
|
|
"INSERT INTO test_tbl VALUES (:data::jsonb || ('{\"id\":' "
|
|
|
|
. "|| (SELECT COALESCE(MAX((data->>'id')::numeric), 0) + 1 FROM test_tbl) || '}')::jsonb)",
|
|
|
|
Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly');
|
2024-06-11 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('insert() succeeds with auto numeric ID for SQLite')]
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testInsertSucceedsWithAutoNumericIdForSQLite(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
$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');
|
2024-06-11 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('insert() succeeds with auto UUID for PostgreSQL')]
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testInsertSucceedsWithAutoUuidForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$query = Query::insert('test_tbl', AutoId::UUID);
|
|
|
|
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", $query,
|
|
|
|
'INSERT statement not constructed correctly');
|
|
|
|
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
|
2024-06-11 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('insert() succeeds with auto UUID for SQLite')]
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testInsertSucceedsWithAutoUuidForSQLite(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
$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');
|
2024-06-11 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('insert() succeeds with auto random string for PostgreSQL')]
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testInsertSucceedsWithAutoRandomStringForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-11 11:07:56 +00:00
|
|
|
Configuration::$idStringLength = 8;
|
|
|
|
try {
|
|
|
|
$query = Query::insert('test_tbl', AutoId::RandomString);
|
2024-06-21 13:46:41 +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-21 13:46:41 +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::$idStringLength = 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('insert() succeeds with auto random string for SQLite')]
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testInsertSucceedsWithAutoRandomStringForSQLite(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
$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");
|
2024-06-11 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('insert() fails when mode not set')]
|
2024-06-11 11:07:56 +00:00
|
|
|
public function testInsertFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-11 11:07:56 +00:00
|
|
|
Query::insert('kaboom');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('save() succeeds')]
|
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');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('update() succeeds')]
|
|
|
|
public function testUpdateSucceeds(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals("UPDATE testing SET data = :data WHERE data->>'id' = :id", Query::update('testing'),
|
|
|
|
'UPDATE statement not constructed correctly');
|
|
|
|
}
|
2024-09-27 02:15:00 +00:00
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with no fields for PostgreSQL')]
|
|
|
|
public function testOrderBySucceedsWithNoFieldsForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals('', Query::orderBy([]), 'ORDER BY should have been blank');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with no fields for SQLite')]
|
|
|
|
public function testOrderBySucceedsWithNoFieldsForSQLite(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals('', Query::orderBy([]), 'ORDER BY should have been blank');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with one field and no direction for PostgreSQL')]
|
|
|
|
public function testOrderBySucceedsWithOneFieldAndNoDirectionForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals(" ORDER BY data->>'TestField'", Query::orderBy([Field::named('TestField')]),
|
|
|
|
'ORDER BY not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with one field and no direction for SQLite')]
|
|
|
|
public function testOrderBySucceedsWithOneFieldAndNoDirectionForSQLite(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(" ORDER BY data->>'TestField'", Query::orderBy([Field::named('TestField')]),
|
|
|
|
'ORDER BY not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with multiple fields and direction for PostgreSQL')]
|
|
|
|
public function testOrderBySucceedsWithMultipleFieldsAndDirectionForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals(" ORDER BY data#>>'{Nested,Test,Field}' DESC, data->>'AnotherField', data->>'It' DESC",
|
|
|
|
Query::orderBy(
|
|
|
|
[Field::named('Nested.Test.Field DESC'), Field::named('AnotherField'), Field::named('It DESC')]),
|
|
|
|
'ORDER BY not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with multiple fields and direction for SQLite')]
|
|
|
|
public function testOrderBySucceedsWithMultipleFieldsAndDirectionForSQLite(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(" ORDER BY data->'Nested'->'Test'->>'Field' DESC, data->>'AnotherField', data->>'It' DESC",
|
|
|
|
Query::orderBy(
|
|
|
|
[Field::named('Nested.Test.Field DESC'), Field::named('AnotherField'), Field::named('It DESC')]),
|
|
|
|
'ORDER BY not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with numeric field for PostgreSQL')]
|
|
|
|
public function testOrderBySucceedsWithNumericFieldForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals(" ORDER BY (data->>'Test')::numeric", Query::orderBy([Field::named('n:Test')]),
|
|
|
|
'ORDER BY not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with numeric field for SQLite')]
|
|
|
|
public function testOrderBySucceedsWithNumericFieldForSQLite(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(" ORDER BY data->>'Test'", Query::orderBy([Field::named('n:Test')]),
|
|
|
|
'ORDER BY not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with case-insensitive ordering for PostgreSQL')]
|
|
|
|
public function testOrderBySucceedsWithCaseInsensitiveOrderingForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
|
|
$this->assertEquals(" ORDER BY LOWER(data#>>'{Test,Field}') DESC NULLS FIRST",
|
|
|
|
Query::orderBy([Field::named('i:Test.Field DESC NULLS FIRST')]), 'ORDER BY not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('orderBy() succeeds with case-insensitive ordering for SQLite')]
|
|
|
|
public function testOrderBySucceedsWithCaseInsensitiveOrderingForSQLite(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(" ORDER BY data->'Test'->>'Field' COLLATE NOCASE ASC NULLS LAST",
|
|
|
|
Query::orderBy([Field::named('i:Test.Field ASC NULLS LAST')]), 'ORDER BY not constructed correctly');
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|