2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-21 13:46:41 +00:00
|
|
|
|
|
|
|
namespace Test\Integration\PostgreSQL;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\{Field, Find, RemoveFields};
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Test\Integration\TestDocument;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PostgreSQL integration tests for the RemoveFields class
|
|
|
|
*/
|
|
|
|
#[TestDox('Remove Fields (PostgreSQL integration)')]
|
|
|
|
class RemoveFieldsTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var string Database name for throwaway database */
|
|
|
|
private string $dbName;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->dbName = ThrowawayDb::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
ThrowawayDb::destroy($this->dbName);
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byId() succeeds when fields are removed')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByIdSucceedsWhenFieldsAreRemoved(): void
|
|
|
|
{
|
|
|
|
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
|
2024-06-25 02:04:11 +00:00
|
|
|
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($tryDoc->isSome, 'There should have been a document returned');
|
|
|
|
$doc = $tryDoc->value;
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals('', $doc->value, 'Value should have been blank (its default value)');
|
|
|
|
$this->assertNull($doc->sub, 'Sub-document should have been null');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byId() succeeds when a field is not removed')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByIdSucceedsWhenAFieldIsNotRemoved(): void
|
|
|
|
{
|
|
|
|
RemoveFields::byId(ThrowawayDb::TABLE, 'one', ['a_field_that_does_not_exist']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byId() succeeds when no document is matched')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByIdSucceedsWhenNoDocumentIsMatched(): void
|
|
|
|
{
|
|
|
|
RemoveFields::byId(ThrowawayDb::TABLE, 'fifty', ['sub']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byFields() succeeds when a field is removed')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByFieldsSucceedsWhenAFieldIsRemoved(): void
|
|
|
|
{
|
2024-09-27 02:15:00 +00:00
|
|
|
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
|
|
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class);
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($doc->isSome, 'There should have been a document returned');
|
|
|
|
$this->assertNull($doc->value->sub, 'Sub-document should have been null');
|
2024-06-21 13:46:41 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byFields() succeeds when a field is not removed')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByFieldsSucceedsWhenAFieldIsNotRemoved(): void
|
|
|
|
{
|
2024-09-27 02:15:00 +00:00
|
|
|
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['nada']);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byFields() succeeds when no document is matched')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByFieldsSucceedsWhenNoDocumentIsMatched(): void
|
|
|
|
{
|
2024-09-27 02:15:00 +00:00
|
|
|
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')], ['value']);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byContains() succeeds when a field is removed')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsSucceedsWhenAFieldIsRemoved(): void
|
|
|
|
{
|
|
|
|
$criteria = ['sub' => ['foo' => 'green']];
|
|
|
|
RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']);
|
|
|
|
$docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class);
|
|
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($docs->hasItems, 'The document list should not have been empty');
|
|
|
|
foreach ($docs->items as $item) {
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertContains($item->id, ['two', 'four'], 'An incorrect document was returned');
|
|
|
|
$this->assertEquals('', $item->value, 'The value field was not removed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byContains() succeeds when a field is not removed')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsSucceedsWhenAFieldIsNotRemoved(): void
|
|
|
|
{
|
|
|
|
RemoveFields::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], ['invalid_field']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byContains() succeeds when no document is matched')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsSucceedsWhenNoDocumentIsMatched(): void
|
|
|
|
{
|
|
|
|
RemoveFields::byContains(ThrowawayDb::TABLE, ['value' => 'substantial'], ['num_value']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byJsonPath() succeeds when a field is removed')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathSucceedsWhenAFieldIsRemoved(): void
|
|
|
|
{
|
|
|
|
$path = '$.value ? (@ == "purple")';
|
|
|
|
RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']);
|
|
|
|
$docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class);
|
|
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($docs->hasItems, 'The document list should not have been empty');
|
|
|
|
foreach ($docs->items as $item) {
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned');
|
|
|
|
$this->assertNull($item->sub, 'The sub field was not removed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byJsonPath() succeeds when a field is not removed')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathSucceedsWhenAFieldIsNotRemoved(): void
|
|
|
|
{
|
|
|
|
RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "purple")', ['submarine']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byJsonPath() succeeds when no document is matched')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathSucceedsWhenNoDocumentIsMatched(): void
|
|
|
|
{
|
|
|
|
RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")', ['value']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
}
|