pdo-document/tests/integration/postgresql/PatchTest.php

116 lines
4.9 KiB
PHP
Raw Normal View History

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
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();
}
#[TestDox('byId() succeeds when a document is updated')]
public function testByIdSucceedsWhenADocumentIsUpdated(): void
{
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
2024-07-28 23:45:29 +00:00
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
2024-06-25 02:04:11 +00:00
$this->assertEquals(44, $doc->get()->num_value, 'The updated document is not correct');
}
#[TestDox('byId() succeeds when no document is updated')]
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');
}
#[TestDox('byFields() succeeds when a document is updated')]
public function testByFieldsSucceedsWhenADocumentIsUpdated(): void
{
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
$after = Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]);
$this->assertEquals(2, $after, 'There should have been 2 documents updated');
}
#[TestDox('byFields() succeeds when no document is updated')]
public function testByFieldsSucceedsWhenNoDocumentIsUpdated(): void
{
$fields = [Field::equal('value', 'burgundy')];
$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');
}
#[TestDox('byContains() succeeds when documents are updated')]
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-07-28 23:45:29 +00:00
$this->assertTrue($tryDoc->isSome(), 'There should have been a document returned');
2024-06-25 02:04:11 +00:00
$doc = $tryDoc->get();
$this->assertEquals('two', $doc->id, 'An incorrect document was returned');
$this->assertEquals(12, $doc->num_value, 'The document was not patched');
}
#[TestDox('byContains() succeeds when no documents are updated')]
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');
}
#[TestDox('byJsonPath() succeeds when documents are updated')]
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');
$this->assertTrue($docs->hasItems(), 'The document list should not be empty');
foreach ($docs->items() as $item) {
$this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned');
$this->assertEquals('blue', $item->value, 'The document was not patched');
}
}
#[TestDox('byJsonPath() succeeds when documents are not updated')]
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');
}
}