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\{Count, Exists, Field, Find, Patch};
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Test\Integration\TestDocument;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PostgreSQL integration tests for the Patch class
|
|
|
|
*/
|
|
|
|
#[TestDox('Patch (PostgreSQL integration)')]
|
|
|
|
class PatchTest 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 a document is updated')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByIdSucceedsWhenADocumentIsUpdated(): void
|
|
|
|
{
|
|
|
|
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
|
|
|
|
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($doc->isSome, 'There should have been a document returned');
|
|
|
|
$this->assertEquals(44, $doc->value->num_value, 'The updated document is not correct');
|
2024-06-21 13:46:41 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byId() succeeds when no document is updated')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByIdSucceedsWhenNoDocumentIsUpdated(): void
|
|
|
|
{
|
|
|
|
$id = 'forty-seven';
|
|
|
|
$this->assertFalse(Exists::byId(ThrowawayDb::TABLE, $id), 'The document should not exist');
|
|
|
|
Patch::byId(ThrowawayDb::TABLE, $id, ['foo' => 'green']);
|
|
|
|
$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 document is updated')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByFieldsSucceedsWhenADocumentIsUpdated(): void
|
|
|
|
{
|
2024-09-27 02:15:00 +00:00
|
|
|
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
|
|
|
|
$after = Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals(2, $after, 'There should have been 2 documents updated');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byFields() succeeds when no document is updated')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByFieldsSucceedsWhenNoDocumentIsUpdated(): void
|
|
|
|
{
|
2024-09-27 02:15:00 +00:00
|
|
|
$fields = [Field::equal('value', 'burgundy')];
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals(0, Count::byFields(ThrowawayDb::TABLE, $fields), 'There should be no matching documents');
|
|
|
|
Patch::byFields(ThrowawayDb::TABLE, $fields, ['foo' => 'green']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byContains() succeeds when documents are updated')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsSucceedsWhenDocumentsAreUpdated(): void
|
|
|
|
{
|
|
|
|
Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]);
|
2024-06-25 02:04:11 +00:00
|
|
|
$tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], 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('two', $doc->id, 'An incorrect document was returned');
|
|
|
|
$this->assertEquals(12, $doc->num_value, 'The document was not patched');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byContains() succeeds when no documents are updated')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsSucceedsWhenNoDocumentsAreUpdated(): void
|
|
|
|
{
|
|
|
|
$criteria = ['value' => 'updated'];
|
|
|
|
$this->assertEquals(0, Count::byContains(ThrowawayDb::TABLE, $criteria),
|
|
|
|
'There should be no matching documents');
|
|
|
|
Patch::byContains(ThrowawayDb::TABLE, $criteria, ['sub.foo' => 'green']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byJsonPath() succeeds when documents are updated')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathSucceedsWhenDocumentsAreUpdated(): void
|
|
|
|
{
|
|
|
|
Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']);
|
|
|
|
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', 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 be 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->assertEquals('blue', $item->value, 'The document was not patched');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byJsonPath() succeeds when documents are not updated')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathSucceedsWhenDocumentsAreNotUpdated(): void
|
|
|
|
{
|
|
|
|
$path = '$.num_value ? (@ > 100)';
|
|
|
|
$this->assertEquals(0, Count::byJsonPath(ThrowawayDb::TABLE, $path),
|
|
|
|
'There should be no documents matching this path');
|
|
|
|
Patch::byJsonPath(ThrowawayDb::TABLE, $path, ['value' => 'blue']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
}
|