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\Query;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters};
|
|
|
|
use BitBadger\PDODocument\Query\RemoveFields;
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the RemoveFields class
|
|
|
|
*/
|
2024-06-21 13:46:41 +00:00
|
|
|
#[TestDox('Remove Fields Queries (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class RemoveFieldsTest extends TestCase
|
|
|
|
{
|
2024-06-21 13:46:41 +00:00
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-21 13:46:41 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('update() succeeds for PostgreSQL')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testUpdateSucceedsForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals('UPDATE taco SET data = data - :names::text[] WHERE it = true',
|
|
|
|
RemoveFields::update('taco', [':names' => "{one,two}"], 'it = true'), 'UPDATE statement not correct');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('update() succeeds for SQLite')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testUpdateSucceedsForSQLite(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-21 13:46:41 +00:00
|
|
|
$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');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('update() fails when mode not set')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testUpdateFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
RemoveFields::update('wow', [], '');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byId() succeeds for PostgreSQL')]
|
|
|
|
public function testByIdSucceedsForPostgreSQL(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals("UPDATE churro SET data = data - :bite::text[] WHERE data->>'id' = :id",
|
|
|
|
RemoveFields::byId('churro', Parameters::fieldNames(':bite', ['byte'])), 'UPDATE statement not correct');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byId() succeeds for SQLite')]
|
|
|
|
public function testByIdSucceedsForSQLite(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-21 13:46:41 +00:00
|
|
|
$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');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byId() fails when mode not set')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByIdFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
RemoveFields::byId('oof', []);
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byFields() succeeds for PostgreSQL')]
|
|
|
|
public function testByFieldsSucceedsForPostgreSQL(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals("UPDATE enchilada SET data = data - :sauce::text[] WHERE data->>'cheese' = :queso",
|
2024-09-27 02:15:00 +00:00
|
|
|
RemoveFields::byFields('enchilada', [Field::equal('cheese', 'jack', ':queso')],
|
2024-06-21 13:46:41 +00:00
|
|
|
Parameters::fieldNames(':sauce', ['white'])),
|
|
|
|
'UPDATE statement not correct');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byFields() succeeds for SQLite')]
|
|
|
|
public function testByFieldsSucceedsForSQLite(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
"UPDATE chimichanga SET data = json_remove(data, :filling0) WHERE data->>'side' = :rice",
|
2024-09-27 02:15:00 +00:00
|
|
|
RemoveFields::byFields('chimichanga', [Field::equal('side', 'beans', ':rice')],
|
2024-06-21 13:46:41 +00:00
|
|
|
Parameters::fieldNames(':filling', ['beef'])),
|
|
|
|
'UPDATE statement not correct');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byFields() fails when mode not set')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByFieldsFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
RemoveFields::byFields('boing', [], []);
|
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byContains() succeeds for PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsSucceedsForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals('UPDATE food SET data = data - :drink::text[] WHERE data @> :criteria',
|
|
|
|
RemoveFields::byContains('food', Parameters::fieldNames(':drink', ['a', 'b'])),
|
|
|
|
'UPDATE statement not correct');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byContains() fails for non PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
RemoveFields::byContains('', []);
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byJsonPath() succeeds for PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathSucceedsForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
'UPDATE dessert SET data = data - :cake::text[] WHERE jsonb_path_exists(data, :path::jsonpath)',
|
|
|
|
RemoveFields::byJsonPath('dessert', Parameters::fieldNames(':cake', ['b', 'c'])),
|
|
|
|
'UPDATE statement not correct');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byJsonPath() fails for non PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
RemoveFields::byJsonPath('', []);
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|