Derive mode from DSN function

- Add headers in all files
- Minor field name changes
This commit is contained in:
2024-07-20 21:47:21 -04:00
parent 1a37b009ea
commit d8330d828a
81 changed files with 1053 additions and 551 deletions

View File

@@ -1,4 +1,10 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit;
@@ -14,12 +20,12 @@ class QueryTest extends TestCase
{
protected function setUp(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
}
protected function tearDown(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
}
public function testSelectFromTableSucceeds(): void
@@ -63,30 +69,21 @@ class QueryTest extends TestCase
public function testWhereDataContainsSucceedsWithDefaultParameter(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals('data @> :criteria', Query::whereDataContains(),
'WHERE fragment not constructed correctly');
} finally {
Configuration::$mode = null;
}
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('data @> :criteria', Query::whereDataContains(),
'WHERE fragment not constructed correctly');
}
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;
}
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('data @> :it', Query::whereDataContains(':it'), 'WHERE fragment not constructed correctly');
}
#[TestDox('Where data contains fails if not PostgreSQL')]
public function testWhereDataContainsFailsIfNotPostgreSQL(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
$this->expectException(DocumentException::class);
Query::whereDataContains();
}
@@ -94,31 +91,23 @@ class QueryTest extends TestCase
#[TestDox('Where JSON Path matches succeeds with default parameter')]
public function testWhereJsonPathMatchesSucceedsWithDefaultParameter(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals('jsonb_path_exists(data, :path::jsonpath)', Query::whereJsonPathMatches(),
'WHERE fragment not constructed correctly');
} finally {
Configuration::$mode = null;
}
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('jsonb_path_exists(data, :path::jsonpath)', Query::whereJsonPathMatches(),
'WHERE fragment not constructed correctly');
}
#[TestDox('Where JSON Path matches succeeds with specified parameter')]
public function testWhereJsonPathMatchesSucceedsWithSpecifiedParameter(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals('jsonb_path_exists(data, :road::jsonpath)', Query::whereJsonPathMatches(':road'),
'WHERE fragment not constructed correctly');
} finally {
Configuration::$mode = null;
}
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('jsonb_path_exists(data, :road::jsonpath)', Query::whereJsonPathMatches(':road'),
'WHERE fragment not constructed correctly');
}
#[TestDox('Where JSON Path matches fails if not PostgreSQL')]
public function testWhereJsonPathMatchesFailsIfNotPostgreSQL(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
$this->expectException(DocumentException::class);
Query::whereJsonPathMatches();
}
@@ -126,87 +115,60 @@ class QueryTest extends TestCase
#[TestDox('Insert succeeds with no auto-ID for PostgreSQL')]
public function testInsertSucceedsWithNoAutoIdForPostgreSQL(): void
{
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;
}
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'),
'INSERT statement not constructed correctly');
}
#[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;
}
$this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'),
'INSERT statement not constructed correctly');
}
#[TestDox('Insert succeeds with auto numeric ID for PostgreSQL')]
public function testInsertSucceedsWithAutoNumericIdForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$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');
} finally {
Configuration::$mode = null;
}
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');
}
#[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;
}
$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');
}
#[TestDox('Insert succeeds with auto UUID for PostgreSQL')]
public function testInsertSucceedsWithAutoUuidForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$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');
} finally {
Configuration::$mode = null;
}
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');
}
#[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;
}
$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');
}
#[TestDox('Insert succeeds with auto random string for PostgreSQL')]
public function testInsertSucceedsWithAutoRandomStringForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
Configuration::$idStringLength = 8;
try {
$query = Query::insert('test_tbl', AutoId::RandomString);
@@ -216,7 +178,6 @@ class QueryTest extends TestCase
$id = str_replace(["INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", "\"}')"], '', $query);
$this->assertEquals(8, strlen($id), "Generated ID [$id] should have been 8 characters long");
} finally {
Configuration::$mode = null;
Configuration::$idStringLength = 16;
}
}
@@ -224,23 +185,18 @@ class QueryTest extends TestCase
#[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;
}
$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");
}
public function testInsertFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
Configuration::overrideMode(null);
Query::insert('kaboom');
}