2024-06-04 01:09:03 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Test\Unit\Query;
|
|
|
|
|
2024-06-08 03:06:31 +00:00
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
|
2024-06-04 01:09:03 +00:00
|
|
|
use BitBadger\PDODocument\Query\Patch;
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the Patch class
|
|
|
|
*/
|
|
|
|
class PatchTest extends TestCase
|
|
|
|
{
|
|
|
|
#[TestDox('By ID succeeds for PostgreSQL')]
|
|
|
|
public function testByIdSucceedsForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
2024-06-05 00:59:24 +00:00
|
|
|
$this->assertEquals("UPDATE doc_table SET data = data || :data WHERE data->>'id' = :id",
|
2024-06-04 01:09:03 +00:00
|
|
|
Patch::byId('doc_table'), 'Patch UPDATE statement is not correct');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By ID succeeds for SQLite')]
|
|
|
|
public function testByIdSucceedsForSQLite(): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
2024-06-05 00:59:24 +00:00
|
|
|
$this->assertEquals("UPDATE my_table SET data = json_patch(data, json(:data)) WHERE data->>'id' = :id",
|
2024-06-04 01:09:03 +00:00
|
|
|
Patch::byId('my_table'), 'Patch UPDATE statement is not correct');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By ID fails when mode not set')]
|
|
|
|
public function testByIdFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Configuration::$mode = null;
|
|
|
|
Patch::byId('oof');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By fields succeeds for PostgreSQL')]
|
|
|
|
public function testByFieldsSucceedsForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
2024-06-05 00:59:24 +00:00
|
|
|
$this->assertEquals("UPDATE that SET data = data || :data WHERE data->>'something' < :some",
|
|
|
|
Patch::byFields('that', [Field::LT('something', 17, ':some')]),
|
2024-06-04 01:09:03 +00:00
|
|
|
'Patch UPDATE statement is not correct');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By fields succeeds for SQLite')]
|
|
|
|
public function testByFieldsSucceedsForSQLite(): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
|
|
|
$this->assertEquals(
|
2024-06-05 00:59:24 +00:00
|
|
|
"UPDATE a_table SET data = json_patch(data, json(:data)) WHERE data->>'something' > :it",
|
|
|
|
Patch::byFields('a_table', [Field::GT('something', 17, ':it')]),
|
2024-06-04 01:09:03 +00:00
|
|
|
'Patch UPDATE statement is not correct');
|
|
|
|
} finally {
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByFieldsFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Configuration::$mode = null;
|
|
|
|
Patch::byFields('oops', []);
|
|
|
|
}
|
|
|
|
}
|