Daniel J. Summers
d067f8983f
- Add In/InArray support - Add ORDER BY support for `Find` functions - Update dependencies - Implement fixes identified via static analysis Reviewed-on: #5
105 lines
3.6 KiB
PHP
105 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
* @license MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Test\Unit\Query;
|
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
|
|
use BitBadger\PDODocument\Query\Patch;
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Unit tests for the Patch class
|
|
*/
|
|
#[TestDox('Patch Queries (Unit tests)')]
|
|
class PatchTest extends TestCase
|
|
{
|
|
protected function tearDown(): void
|
|
{
|
|
Configuration::overrideMode(null);
|
|
parent::tearDown();
|
|
}
|
|
#[TestDox('byId() succeeds for PostgreSQL')]
|
|
public function testByIdSucceedsForPostgreSQL(): void
|
|
{
|
|
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');
|
|
}
|
|
|
|
#[TestDox('byId() succeeds for SQLite')]
|
|
public function testByIdSucceedsForSQLite(): void
|
|
{
|
|
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');
|
|
}
|
|
|
|
#[TestDox('byId() fails when mode not set')]
|
|
public function testByIdFailsWhenModeNotSet(): void
|
|
{
|
|
$this->expectException(DocumentException::class);
|
|
Patch::byId('oof');
|
|
}
|
|
|
|
#[TestDox('byFields() succeeds for PostgreSQL')]
|
|
public function testByFieldsSucceedsForPostgreSQL(): void
|
|
{
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
$this->assertEquals("UPDATE that SET data = data || :data WHERE (data->>'something')::numeric < :some",
|
|
Patch::byFields('that', [Field::less('something', 17, ':some')]), 'Patch UPDATE statement is not correct');
|
|
}
|
|
|
|
#[TestDox('byFields() succeeds for SQLite')]
|
|
public function testByFieldsSucceedsForSQLite(): void
|
|
{
|
|
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::greater('something', 17, ':it')]),
|
|
'Patch UPDATE statement is not correct');
|
|
}
|
|
|
|
#[TestDox('byFields() fails when mode not set')]
|
|
public function testByFieldsFailsWhenModeNotSet(): void
|
|
{
|
|
$this->expectException(DocumentException::class);
|
|
Patch::byFields('oops', []);
|
|
}
|
|
|
|
#[TestDox('byContains() succeeds for PostgreSQL')]
|
|
public function testByContainsSucceedsForPostgreSQL(): void
|
|
{
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
$this->assertEquals('UPDATE this SET data = data || :data WHERE data @> :criteria', Patch::byContains('this'),
|
|
'Patch UPDATE statement is not correct');
|
|
}
|
|
|
|
#[TestDox('byContains() fails for non PostgreSQL')]
|
|
public function testByContainsFailsForNonPostgreSQL(): void
|
|
{
|
|
$this->expectException(DocumentException::class);
|
|
Patch::byContains('');
|
|
}
|
|
|
|
#[TestDox('byJsonPath() succeeds for PostgreSQL')]
|
|
public function testByJsonPathSucceedsForPostgreSQL(): void
|
|
{
|
|
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');
|
|
}
|
|
|
|
#[TestDox('byJsonPath() fails for non PostgreSQL')]
|
|
public function testByJsonPathFailsForNonPostgreSQL(): void
|
|
{
|
|
$this->expectException(DocumentException::class);
|
|
Patch::byJsonPath('');
|
|
}
|
|
}
|