Derive mode from DSN function
- Add headers in all files - Minor field name changes
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -46,7 +52,7 @@ class ConfigurationTest extends TestCase
|
||||
public function testDbConnFailsWhenNoDSNSpecified(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
Configuration::$pdoDSN = '';
|
||||
Configuration::useDSN('');
|
||||
Configuration::dbConn();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit;
|
||||
|
||||
@@ -13,7 +19,7 @@ use PHPUnit\Framework\TestCase;
|
||||
#[TestDox('Document Exception (Unit tests)')]
|
||||
class DocumentExceptionTest extends TestCase
|
||||
{
|
||||
public function testConstructorSucceedsWithCodeAndPriorException()
|
||||
public function testConstructorSucceedsWithCodeAndPriorException(): void
|
||||
{
|
||||
$priorEx = new Exception('Uh oh');
|
||||
$ex = new DocumentException('Test Exception', 17, $priorEx);
|
||||
@@ -23,7 +29,7 @@ class DocumentExceptionTest extends TestCase
|
||||
$this->assertSame($priorEx, $ex->getPrevious(), 'Prior exception not filled properly');
|
||||
}
|
||||
|
||||
public function testConstructorSucceedsWithoutCodeAndPriorException()
|
||||
public function testConstructorSucceedsWithoutCodeAndPriorException(): void
|
||||
{
|
||||
$ex = new DocumentException('Oops');
|
||||
$this->assertNotNull($ex, 'The exception should not have been null');
|
||||
@@ -32,14 +38,14 @@ class DocumentExceptionTest extends TestCase
|
||||
$this->assertNull($ex->getPrevious(), 'Prior exception should have been null');
|
||||
}
|
||||
|
||||
public function testToStringSucceedsWithoutCode()
|
||||
public function testToStringSucceedsWithoutCode(): void
|
||||
{
|
||||
$ex = new DocumentException('Test failure');
|
||||
$this->assertEquals("BitBadger\PDODocument\DocumentException: Test failure\n", "$ex",
|
||||
'toString not generated correctly');
|
||||
}
|
||||
|
||||
public function testToStringSucceedsWithCode()
|
||||
public function testToStringSucceedsWithCode(): void
|
||||
{
|
||||
$ex = new DocumentException('Oof', -6);
|
||||
$this->assertEquals("BitBadger\PDODocument\DocumentException: [-6] Oof\n", "$ex",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -12,13 +18,15 @@ use PHPUnit\Framework\TestCase;
|
||||
#[TestDox('Field Match (Unit tests)')]
|
||||
class FieldMatchTest extends TestCase
|
||||
{
|
||||
public function testToStringSucceedsForAll(): void
|
||||
#[TestDox('To SQL succeeds for all')]
|
||||
public function testToSQLSucceedsForAll(): void
|
||||
{
|
||||
$this->assertEquals('AND', FieldMatch::All->toString(), 'All should have returned AND');
|
||||
$this->assertEquals('AND', FieldMatch::All->toSQL(), 'All should have returned AND');
|
||||
}
|
||||
|
||||
public function testToStringSucceedsForAny(): void
|
||||
#[TestDox('To SQL succeeds for any')]
|
||||
public function testToSQLSucceedsForAny(): void
|
||||
{
|
||||
$this->assertEquals('OR', FieldMatch::Any->toString(), 'Any should have returned OR');
|
||||
$this->assertEquals('OR', FieldMatch::Any->toSQL(), 'Any should have returned OR');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -40,232 +46,232 @@ class FieldTest extends TestCase
|
||||
#[TestDox('To where succeeds for EX without qualifier for PostgreSQL')]
|
||||
public function testToWhereSucceedsForEXWithoutQualifierForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$this->assertEquals("data->>'that_field' IS NOT NULL", Field::EX('that_field')->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for EX without qualifier for SQLite')]
|
||||
public function testToWhereSucceedsForEXWithoutQualifierForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
try {
|
||||
$this->assertEquals("data->>'that_field' IS NOT NULL", Field::EX('that_field')->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for NEX without qualifier for PostgreSQL')]
|
||||
public function testToWhereSucceedsForNEXWithoutQualifierForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$this->assertEquals("data->>'a_field' IS NULL", Field::NEX('a_field')->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for NEX without qualifier for SQLite')]
|
||||
public function testToWhereSucceedsForNEXWithoutQualifierForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
try {
|
||||
$this->assertEquals("data->>'a_field' IS NULL", Field::NEX('a_field')->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for BT without qualifier for SQLite')]
|
||||
public function testToWhereSucceedsForBTWithoutQualifierForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
try {
|
||||
$this->assertEquals("data->>'age' BETWEEN @agemin AND @agemax", Field::BT('age', 13, 17, '@age')->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for BT without qualifier for PostgreSQL with numeric range')]
|
||||
public function testToWhereSucceedsForBTWithoutQualifierForPostgreSQLWithNumericRange(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$this->assertEquals("(data->>'age')::numeric BETWEEN @agemin AND @agemax",
|
||||
Field::BT('age', 13, 17, '@age')->toWhere(), 'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for BT without qualifier for PostgreSQL with non-numeric range')]
|
||||
public function testToWhereSucceedsForBTWithoutQualifierForPostgreSQLWithNonNumericRange(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$this->assertEquals("data->>'city' BETWEEN :citymin AND :citymax",
|
||||
Field::BT('city', 'Atlanta', 'Chicago', ':city')->toWhere(), 'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for BT with qualifier for SQLite')]
|
||||
public function testToWhereSucceedsForBTWithQualifierForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
try {
|
||||
$field = Field::BT('age', 13, 17, '@age');
|
||||
$field->qualifier = 'me';
|
||||
$this->assertEquals("me.data->>'age' BETWEEN @agemin AND @agemax", $field->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for BT with qualifier for PostgreSQL with numeric range')]
|
||||
public function testToWhereSucceedsForBTWithQualifierForPostgreSQLWithNumericRange(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$field = Field::BT('age', 13, 17, '@age');
|
||||
$field->qualifier = 'me';
|
||||
$this->assertEquals("(me.data->>'age')::numeric BETWEEN @agemin AND @agemax", $field->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for BT with qualifier for PostgreSQL with non-numeric range')]
|
||||
public function testToWhereSucceedsForBTWithQualifierForPostgreSQLWithNonNumericRange(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$field = Field::BT('city', 'Atlanta', 'Chicago', ':city');
|
||||
$field->qualifier = 'me';
|
||||
$this->assertEquals("me.data->>'city' BETWEEN :citymin AND :citymax", $field->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for others without qualifier for PostgreSQL')]
|
||||
public function testToWhereSucceedsForOthersWithoutQualifierForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$this->assertEquals("data->>'some_field' = @value", Field::EQ('some_field', '', '@value')->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds for others without qualifier for SQLite')]
|
||||
public function testToWhereSucceedsForOthersWithoutQualifierForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
try {
|
||||
$this->assertEquals("data->>'some_field' = @value", Field::EQ('some_field', '', '@value')->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds with qualifier no parameter for PostgreSQL')]
|
||||
public function testToWhereSucceedsWithQualifierNoParameterForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$field = Field::EX('no_field');
|
||||
$field->qualifier = 'test';
|
||||
$this->assertEquals("test.data->>'no_field' IS NOT NULL", $field->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds with qualifier no parameter for SQLite')]
|
||||
public function testToWhereSucceedsWithQualifierNoParameterForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
try {
|
||||
$field = Field::EX('no_field');
|
||||
$field->qualifier = 'test';
|
||||
$this->assertEquals("test.data->>'no_field' IS NOT NULL", $field->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds with qualifier and parameter for PostgreSQL')]
|
||||
public function testToWhereSucceedsWithQualifierAndParameterForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$field = Field::LE('le_field', 18, '@it');
|
||||
$field->qualifier = 'q';
|
||||
$this->assertEquals("(q.data->>'le_field')::numeric <= @it", $field->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds with qualifier and parameter for SQLite')]
|
||||
public function testToWhereSucceedsWithQualifierAndParameterForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
try {
|
||||
$field = Field::LE('le_field', 18, '@it');
|
||||
$field->qualifier = 'q';
|
||||
$this->assertEquals("q.data->>'le_field' <= @it", $field->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds with sub-document for PostgreSQL')]
|
||||
public function testToWhereSucceedsWithSubDocumentForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
try {
|
||||
$field = Field::EQ('sub.foo.bar', 22, '@it');
|
||||
$this->assertEquals("(data#>>'{sub,foo,bar}')::numeric = @it", $field->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('To where succeeds with sub-document for SQLite')]
|
||||
public function testToWhereSucceedsWithSubDocumentForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
try {
|
||||
$field = Field::EQ('sub.foo.bar', 22, '@it');
|
||||
$this->assertEquals("data->>'sub'->>'foo'->>'bar' = @it", $field->toWhere(),
|
||||
'WHERE fragment not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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\Mapper;
|
||||
|
||||
|
||||
@@ -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\Mapper;
|
||||
|
||||
|
||||
@@ -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\Mapper;
|
||||
|
||||
|
||||
@@ -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\Mapper;
|
||||
|
||||
@@ -17,10 +23,10 @@ class ExistsMapperTest extends TestCase
|
||||
public function testMapSucceedsForPostgreSQL(): void
|
||||
{
|
||||
try {
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertFalse((new ExistsMapper())->map([false, 'nope']), 'Result should have been false');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,17 +34,17 @@ class ExistsMapperTest extends TestCase
|
||||
public function testMapSucceedsForSQLite(): void
|
||||
{
|
||||
try {
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertTrue((new ExistsMapper())->map([1, 'yep']), 'Result should have been true');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
public function testMapFailsWhenModeNotSet(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
(new ExistsMapper())->map(['0']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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\Mapper;
|
||||
|
||||
|
||||
39
tests/unit/ModeTest.php
Normal file
39
tests/unit/ModeTest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit;
|
||||
|
||||
use BitBadger\PDODocument\{DocumentException, Mode};
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the Mode enumeration
|
||||
*/
|
||||
#[TestDox('Mode (Unit tests)')]
|
||||
class ModeTest extends TestCase
|
||||
{
|
||||
#[TestDox('Derive from DSN succeeds for PostgreSQL')]
|
||||
public function testDeriveFromDSNSucceedsForPostgreSQL(): void
|
||||
{
|
||||
$this->assertEquals(Mode::PgSQL, Mode::deriveFromDSN('pgsql:Host=localhost'), 'PostgreSQL mode incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('Derive from DSN succeeds for SQLite')]
|
||||
public function testDeriveFromDSNSucceedsForSQLite(): void
|
||||
{
|
||||
$this->assertEquals(Mode::SQLite, Mode::deriveFromDSN('sqlite:data.db'), 'SQLite mode incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('Derive from DSN fails for MySQL')]
|
||||
public function testDeriveFromDSNFailsForMySQL(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
Mode::deriveFromDSN('mysql:Host=localhost');
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -12,57 +18,57 @@ use PHPUnit\Framework\TestCase;
|
||||
#[TestDox('Op (Unit tests)')]
|
||||
class OpTest extends TestCase
|
||||
{
|
||||
#[TestDox('To string succeeds for EQ')]
|
||||
public function testToStringSucceedsForEQ(): void
|
||||
#[TestDox('To SQL succeeds for EQ')]
|
||||
public function testToSQLSucceedsForEQ(): void
|
||||
{
|
||||
$this->assertEquals('=', Op::EQ->toString(), 'EQ operator incorrect');
|
||||
$this->assertEquals('=', Op::EQ->toSQL(), 'EQ operator incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('To string succeeds for GT')]
|
||||
public function testToStringSucceedsForGT(): void
|
||||
#[TestDox('To SQL succeeds for GT')]
|
||||
public function testToSQLSucceedsForGT(): void
|
||||
{
|
||||
$this->assertEquals('>', Op::GT->toString(), 'GT operator incorrect');
|
||||
$this->assertEquals('>', Op::GT->toSQL(), 'GT operator incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('To string succeeds for GE')]
|
||||
public function testToStringSucceedsForGE(): void
|
||||
#[TestDox('To SQL succeeds for GE')]
|
||||
public function testToSQLSucceedsForGE(): void
|
||||
{
|
||||
$this->assertEquals('>=', Op::GE->toString(), 'GE operator incorrect');
|
||||
$this->assertEquals('>=', Op::GE->toSQL(), 'GE operator incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('To string succeeds for LT')]
|
||||
public function testToStringSucceedsForLT(): void
|
||||
#[TestDox('To SQL succeeds for LT')]
|
||||
public function testToSQLSucceedsForLT(): void
|
||||
{
|
||||
$this->assertEquals('<', Op::LT->toString(), 'LT operator incorrect');
|
||||
$this->assertEquals('<', Op::LT->toSQL(), 'LT operator incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('To string succeeds for LE')]
|
||||
public function testToStringSucceedsForLE(): void
|
||||
#[TestDox('To SQL succeeds for LE')]
|
||||
public function testToSQLSucceedsForLE(): void
|
||||
{
|
||||
$this->assertEquals('<=', Op::LE->toString(), 'LE operator incorrect');
|
||||
$this->assertEquals('<=', Op::LE->toSQL(), 'LE operator incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('To string succeeds for NE')]
|
||||
public function testToStringSucceedsForNE(): void
|
||||
#[TestDox('To SQL succeeds for NE')]
|
||||
public function testToSQLSucceedsForNE(): void
|
||||
{
|
||||
$this->assertEquals('<>', Op::NE->toString(), 'NE operator incorrect');
|
||||
$this->assertEquals('<>', Op::NE->toSQL(), 'NE operator incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('To string succeeds for BT')]
|
||||
public function testToStringSucceedsForBT(): void
|
||||
#[TestDox('To SQL succeeds for BT')]
|
||||
public function testToSQLSucceedsForBT(): void
|
||||
{
|
||||
$this->assertEquals('BETWEEN', Op::BT->toString(), 'BT operator incorrect');
|
||||
$this->assertEquals('BETWEEN', Op::BT->toSQL(), 'BT operator incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('To string succeeds for EX')]
|
||||
public function testToStringSucceedsForEX(): void
|
||||
#[TestDox('To SQL succeeds for EX')]
|
||||
public function testToSQLSucceedsForEX(): void
|
||||
{
|
||||
$this->assertEquals('IS NOT NULL', Op::EX->toString(), 'EX operator incorrect');
|
||||
$this->assertEquals('IS NOT NULL', Op::EX->toSQL(), 'EX operator incorrect');
|
||||
}
|
||||
|
||||
#[TestDox('To string succeeds for NEX')]
|
||||
public function testToStringSucceedsForNEX(): void
|
||||
#[TestDox('To SQL succeeds for NEX')]
|
||||
public function testToSQLSucceedsForNEX(): void
|
||||
{
|
||||
$this->assertEquals('IS NULL', Op::NEX->toString(), 'NEX operator incorrect');
|
||||
$this->assertEquals('IS NULL', Op::NEX->toSQL(), 'NEX operator incorrect');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -91,11 +97,11 @@ class ParametersTest extends TestCase
|
||||
public function testFieldNamesSucceedsForPostgreSQL(): void
|
||||
{
|
||||
try {
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals([':names' => "{one,two,seven}"],
|
||||
Parameters::fieldNames(':names', ['one', 'two', 'seven']), 'Field name parameters not correct');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,18 +109,18 @@ class ParametersTest extends TestCase
|
||||
public function testFieldNamesSucceedsForSQLite(): void
|
||||
{
|
||||
try {
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals([':it0' => '$.test', ':it1' => '$.unit', ':it2' => '$.wow'],
|
||||
Parameters::fieldNames(':it', ['test', 'unit', 'wow']), 'Field name parameters not correct');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
public function testFieldNamesFailsWhenModeNotSet(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
Parameters::fieldNames('', []);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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\Query;
|
||||
|
||||
@@ -15,7 +21,7 @@ class CountTest extends TestCase
|
||||
{
|
||||
public function tearDown(): void
|
||||
{
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
@@ -27,7 +33,7 @@ class CountTest extends TestCase
|
||||
|
||||
public function testByFieldsSucceeds(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors",
|
||||
Count::byFields('somewhere', [Field::GT('errors', 10, ':errors')]),
|
||||
'SELECT statement not generated correctly');
|
||||
@@ -36,7 +42,7 @@ class CountTest extends TestCase
|
||||
#[TestDox('By contains succeeds for PostgreSQL')]
|
||||
public function testByContainsSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('SELECT COUNT(*) FROM the_table WHERE data @> :criteria', Count::byContains('the_table'),
|
||||
'SELECT statement not generated correctly');
|
||||
}
|
||||
@@ -51,7 +57,7 @@ class CountTest extends TestCase
|
||||
#[TestDox('By JSON Path succeeds for PostgreSQL')]
|
||||
public function testByJsonPathSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('SELECT COUNT(*) FROM a_table WHERE jsonb_path_exists(data, :path::jsonpath)',
|
||||
Count::byJsonPath('a_table'), 'SELECT statement not generated correctly');
|
||||
}
|
||||
|
||||
@@ -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\Query;
|
||||
|
||||
@@ -15,14 +21,14 @@ class DefinitionTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
#[TestDox('Ensure table succeeds for PosgtreSQL')]
|
||||
public function testEnsureTableSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('CREATE TABLE IF NOT EXISTS documents (data JSONB NOT NULL)',
|
||||
Definition::ensureTable('documents'), 'CREATE TABLE statement not generated correctly');
|
||||
}
|
||||
@@ -30,7 +36,7 @@ class DefinitionTest extends TestCase
|
||||
#[TestDox('Ensure table succeeds for SQLite')]
|
||||
public function testEnsureTableSucceedsForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals('CREATE TABLE IF NOT EXISTS dox (data TEXT NOT NULL)', Definition::ensureTable('dox'),
|
||||
'CREATE TABLE statement not generated correctly');
|
||||
}
|
||||
@@ -63,14 +69,14 @@ class DefinitionTest extends TestCase
|
||||
|
||||
public function testEnsureDocumentIndexOnSucceedsForSchemaAndFull(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_tbl_document ON my.tbl USING GIN (data)",
|
||||
Definition::ensureDocumentIndexOn('my.tbl', DocumentIndex::Full));
|
||||
}
|
||||
|
||||
public function testEnsureDocumentIndexOnSucceedsForNoSchemaAndOptimized(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_it_document ON it USING GIN (data jsonb_path_ops)",
|
||||
Definition::ensureDocumentIndexOn('it', DocumentIndex::Optimized));
|
||||
}
|
||||
|
||||
@@ -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\Query;
|
||||
|
||||
@@ -15,20 +21,20 @@ class DeleteTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds')]
|
||||
public function testByIdSucceeds(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals("DELETE FROM over_there WHERE data->>'id' = :id", Delete::byId('over_there'),
|
||||
'DELETE statement not constructed correctly');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceeds(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals("DELETE FROM my_table WHERE data->>'value' < :max AND data->>'value' >= :min",
|
||||
Delete::byFields('my_table', [Field::LT('value', 99, ':max'), Field::GE('value', 18, ':min')]),
|
||||
'DELETE statement not constructed correctly');
|
||||
@@ -37,7 +43,7 @@ class DeleteTest extends TestCase
|
||||
#[TestDox('By contains succeeds for PostgreSQL')]
|
||||
public function testByContainsSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('DELETE FROM somewhere WHERE data @> :criteria', Delete::byContains('somewhere'),
|
||||
'DELETE statement not constructed correctly');
|
||||
}
|
||||
@@ -52,7 +58,7 @@ class DeleteTest extends TestCase
|
||||
#[TestDox('By JSON Path succeeds for PostgreSQL')]
|
||||
public function testByJsonPathSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('DELETE FROM here WHERE jsonb_path_exists(data, :path::jsonpath)',
|
||||
Delete::byJsonPath('here'), 'DELETE statement not constructed correctly');
|
||||
}
|
||||
|
||||
@@ -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\Query;
|
||||
|
||||
@@ -15,12 +21,12 @@ class ExistsTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
|
||||
public function testQuerySucceeds(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM abc WHERE def)', Exists::query('abc', 'def'),
|
||||
'Existence query not generated correctly');
|
||||
}
|
||||
@@ -28,14 +34,14 @@ class ExistsTest extends TestCase
|
||||
#[TestDox('By ID succeeds')]
|
||||
public function testByIdSucceeds(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = :id)", Exists::byId('dox'),
|
||||
'Existence query not generated correctly');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceeds(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM box WHERE data->>'status' <> :status)",
|
||||
Exists::byFields('box', [Field::NE('status', 'occupied', ':status')]),
|
||||
'Existence query not generated correctly');
|
||||
@@ -44,7 +50,7 @@ class ExistsTest extends TestCase
|
||||
#[TestDox('By contains succeeds for PostgreSQL')]
|
||||
public function testByContainsSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM pocket WHERE data @> :criteria)',
|
||||
Exists::byContains('pocket'), 'Existence query not generated correctly');
|
||||
}
|
||||
@@ -59,7 +65,7 @@ class ExistsTest extends TestCase
|
||||
#[TestDox('By JSON Path succeeds for PostgreSQL')]
|
||||
public function testByJsonPathSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM lint WHERE jsonb_path_exists(data, :path::jsonpath))',
|
||||
Exists::byJsonPath('lint'), 'Existence query not generated correctly');
|
||||
}
|
||||
|
||||
@@ -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\Query;
|
||||
|
||||
@@ -15,20 +21,20 @@ class FindTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds')]
|
||||
public function testByIdSucceeds(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals("SELECT data FROM here WHERE data->>'id' = :id", Find::byId('here'),
|
||||
'SELECT query not generated correctly');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceeds(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock",
|
||||
Find::byFields('there', [Field::EQ('active', true, ':act'), Field::EQ('locked', true, ':lock')],
|
||||
FieldMatch::Any),
|
||||
@@ -38,7 +44,7 @@ class FindTest extends TestCase
|
||||
#[TestDox('By contains succeeds for PostgreSQL')]
|
||||
public function testByContainsSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('SELECT data FROM disc WHERE data @> :criteria', Find::byContains('disc'),
|
||||
'SELECT query not generated correctly');
|
||||
}
|
||||
@@ -53,7 +59,7 @@ class FindTest extends TestCase
|
||||
#[TestDox('By JSON Path succeeds for PostgreSQL')]
|
||||
public function testByJsonPathSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('SELECT data FROM light WHERE jsonb_path_exists(data, :path::jsonpath)',
|
||||
Find::byJsonPath('light'), 'SELECT query not generated correctly');
|
||||
}
|
||||
|
||||
@@ -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\Query;
|
||||
|
||||
@@ -15,13 +21,13 @@ class PatchTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
parent::tearDown();
|
||||
}
|
||||
#[TestDox('By ID succeeds for PostgreSQL')]
|
||||
public function testByIdSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals("UPDATE doc_table SET data = data || :data WHERE data->>'id' = :id",
|
||||
Patch::byId('doc_table'), 'Patch UPDATE statement is not correct');
|
||||
}
|
||||
@@ -29,7 +35,7 @@ class PatchTest extends TestCase
|
||||
#[TestDox('By ID succeeds for SQLite')]
|
||||
public function testByIdSucceedsForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals("UPDATE my_table SET data = json_patch(data, json(:data)) WHERE data->>'id' = :id",
|
||||
Patch::byId('my_table'), 'Patch UPDATE statement is not correct');
|
||||
}
|
||||
@@ -44,7 +50,7 @@ class PatchTest extends TestCase
|
||||
#[TestDox('By fields succeeds for PostgreSQL')]
|
||||
public function testByFieldsSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals("UPDATE that SET data = data || :data WHERE (data->>'something')::numeric < :some",
|
||||
Patch::byFields('that', [Field::LT('something', 17, ':some')]), 'Patch UPDATE statement is not correct');
|
||||
}
|
||||
@@ -52,7 +58,7 @@ class PatchTest extends TestCase
|
||||
#[TestDox('By fields succeeds for SQLite')]
|
||||
public function testByFieldsSucceedsForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals(
|
||||
"UPDATE a_table SET data = json_patch(data, json(:data)) WHERE data->>'something' > :it",
|
||||
Patch::byFields('a_table', [Field::GT('something', 17, ':it')]), 'Patch UPDATE statement is not correct');
|
||||
@@ -67,7 +73,7 @@ class PatchTest extends TestCase
|
||||
#[TestDox('By contains succeeds for PostgreSQL')]
|
||||
public function testByContainsSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('UPDATE this SET data = data || :data WHERE data @> :criteria', Patch::byContains('this'),
|
||||
'Patch UPDATE statement is not correct');
|
||||
}
|
||||
@@ -82,7 +88,7 @@ class PatchTest extends TestCase
|
||||
#[TestDox('By JSON Path succeeds for PostgreSQL')]
|
||||
public function testByJsonPathSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('UPDATE that SET data = data || :data WHERE jsonb_path_exists(data, :path::jsonpath)',
|
||||
Patch::byJsonPath('that'), 'Patch UPDATE statement is not correct');
|
||||
}
|
||||
|
||||
@@ -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\Query;
|
||||
|
||||
@@ -15,13 +21,13 @@ class RemoveFieldsTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Configuration::$mode = null;
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
|
||||
#[TestDox('Update succeeds for PostgreSQL')]
|
||||
public function testUpdateSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('UPDATE taco SET data = data - :names::text[] WHERE it = true',
|
||||
RemoveFields::update('taco', [':names' => "{one,two}"], 'it = true'), 'UPDATE statement not correct');
|
||||
}
|
||||
@@ -29,7 +35,7 @@ class RemoveFieldsTest extends TestCase
|
||||
#[TestDox('Update succeeds for SQLite')]
|
||||
public function testUpdateSucceedsForSQLite(): void
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals('UPDATE burrito SET data = json_remove(data, :name0, :name1, :name2) WHERE a = b',
|
||||
RemoveFields::update('burrito', Parameters::fieldNames(':name', ['one', 'two', 'ten']), 'a = b'),
|
||||
'UPDATE statement not correct');
|
||||
@@ -44,7 +50,7 @@ class RemoveFieldsTest extends TestCase
|
||||
#[TestDox('By ID succeeds for PostgreSQL')]
|
||||
public function testByIdSucceedsForPostgreSQL()
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals("UPDATE churro SET data = data - :bite::text[] WHERE data->>'id' = :id",
|
||||
RemoveFields::byId('churro', Parameters::fieldNames(':bite', ['byte'])), 'UPDATE statement not correct');
|
||||
}
|
||||
@@ -52,7 +58,7 @@ class RemoveFieldsTest extends TestCase
|
||||
#[TestDox('By ID succeeds for SQLite')]
|
||||
public function testByIdSucceedsForSQLite()
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals("UPDATE quesadilla SET data = json_remove(data, :bite0) WHERE data->>'id' = :id",
|
||||
RemoveFields::byId('quesadilla', Parameters::fieldNames(':bite', ['byte'])),
|
||||
'UPDATE statement not correct');
|
||||
@@ -68,7 +74,7 @@ class RemoveFieldsTest extends TestCase
|
||||
#[TestDox('By fields succeeds for PostgreSQL')]
|
||||
public function testByFieldsSucceedsForPostgreSQL()
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals("UPDATE enchilada SET data = data - :sauce::text[] WHERE data->>'cheese' = :queso",
|
||||
RemoveFields::byFields('enchilada', [Field::EQ('cheese', 'jack', ':queso')],
|
||||
Parameters::fieldNames(':sauce', ['white'])),
|
||||
@@ -78,7 +84,7 @@ class RemoveFieldsTest extends TestCase
|
||||
#[TestDox('By fields succeeds for SQLite')]
|
||||
public function testByFieldsSucceedsForSQLite()
|
||||
{
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertEquals(
|
||||
"UPDATE chimichanga SET data = json_remove(data, :filling0) WHERE data->>'side' = :rice",
|
||||
RemoveFields::byFields('chimichanga', [Field::EQ('side', 'beans', ':rice')],
|
||||
@@ -95,7 +101,7 @@ class RemoveFieldsTest extends TestCase
|
||||
#[TestDox('By contains succeeds for PostgreSQL')]
|
||||
public function testByContainsSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals('UPDATE food SET data = data - :drink::text[] WHERE data @> :criteria',
|
||||
RemoveFields::byContains('food', Parameters::fieldNames(':drink', ['a', 'b'])),
|
||||
'UPDATE statement not correct');
|
||||
@@ -111,7 +117,7 @@ class RemoveFieldsTest extends TestCase
|
||||
#[TestDox('By JSON Path succeeds for PostgreSQL')]
|
||||
public function testByJsonPathSucceedsForPostgreSQL(): void
|
||||
{
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertEquals(
|
||||
'UPDATE dessert SET data = data - :cake::text[] WHERE jsonb_path_exists(data, :path::jsonpath)',
|
||||
RemoveFields::byJsonPath('dessert', Parameters::fieldNames(':cake', ['b', 'c'])),
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user