pdo-document/tests/unit/Query/RemoveFieldsTest.php

118 lines
4.1 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
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
*/
class RemoveFieldsTest extends TestCase
{
#[TestDox('Update succeeds for PostgreSQL')]
public function testUpdateSucceedsForPostgreSQL(): void
{
try {
Configuration::$mode = 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');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('Update succeeds for SQLite')]
public function testUpdateSucceedsForSQLite(): void
{
try {
Configuration::$mode = 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');
} finally {
Configuration::$mode = null;
}
}
public function testUpdateFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
RemoveFields::update('wow', [], '');
}
#[TestDox('By ID succeeds for PostgreSQL')]
public function testByIdSucceedsForPostgreSQL()
{
try {
Configuration::$mode = 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');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('By ID succeeds for SQLite')]
public function testByIdSucceedsForSQLite()
{
try {
Configuration::$mode = 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');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('By ID fails when mode not set')]
public function testByIdFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
RemoveFields::byId('oof', []);
}
#[TestDox('By fields succeeds for PostgreSQL')]
public function testByFieldsSucceedsForPostgreSQL()
{
try {
Configuration::$mode = 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'])),
'UPDATE statement not correct');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('By fields succeeds for SQLite')]
public function testByFieldsSucceedsForSQLite()
{
try {
Configuration::$mode = Mode::SQLite;
$this->assertEquals(
"UPDATE chimichanga SET data = json_remove(data, :filling0) WHERE data->>'side' = :rice",
RemoveFields::byFields('chimichanga', [Field::EQ('side', 'beans', ':rice')],
Parameters::fieldNames(':filling', ['beef'])),
'UPDATE statement not correct');
} finally {
Configuration::$mode = null;
}
}
public function testByFieldsFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
RemoveFields::byFields('boing', [], []);
}
}