pdo-document/tests/Integration/PostgreSQL/PatchTest.php

72 lines
3.0 KiB
PHP

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{Count, Exists, Field, Find, Patch};
use Test\Integration\PostgreSQL\ThrowawayDb;
use Test\Integration\TestDocument;
pest()->group('integration', 'postgresql');
describe('::byId()', function () {
test('updates an existing document', function () {
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->num_value->toBe(44);
});
test('does nothing when a document does not exist', function () {
$id = 'forty-seven';
expect(Exists::byId(ThrowawayDb::TABLE, $id))->toBeFalse();
Patch::byId(ThrowawayDb::TABLE, $id, ['foo' => 'green']); // no exception = pass
});
});
describe('::byFields()', function () {
test('updates existing documents', function () {
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
expect(Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]))->toBe(2);
});
test('does nothing when no matching documents exist', function () {
$fields = [Field::equal('value', 'burgundy')];
expect(Count::byFields(ThrowawayDb::TABLE, $fields))->toBe(0);
Patch::byFields(ThrowawayDb::TABLE, $fields, ['foo' => 'green']); // no exception = pass
});
});
describe('::byContains()', function () {
test('updates existing documents', function () {
Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]);
$tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class);
expect($tryDoc)->isSome->toBeTrue()
->and($tryDoc->value)
->id->toBe('two')
->num_value->toBe(12);
});
test('does nothing when no matching documents exist', function () {
$criteria = ['value' => 'updated'];
expect(Count::byContains(ThrowawayDb::TABLE, $criteria))->toBe(0);
Patch::byContains(ThrowawayDb::TABLE, $criteria, ['sub.foo' => 'green']); // no exception = pass
});
});
describe('::byJsonPath()', function () {
test('updates existing documents', function () {
Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']);
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
expect($docs)->not->toBeNull()->hasItems->toBeTrue();
foreach ($docs->items as $item) {
expect(['four', 'five'])->toContain($item->id)
->and($item->value)->toBe('blue');
}
});
test('does nothing when no matching documents exist', function () {
$path = '$.num_value ? (@ > 100)';
expect(Count::byJsonPath(ThrowawayDb::TABLE, $path))->toBe(0);
Patch::byJsonPath(ThrowawayDb::TABLE, $path, ['value' => 'blue']); // no exception = pass
});
});