* @license MIT */ declare(strict_types=1); use BitBadger\PDODocument\{Exists, Field, Find, RemoveFields}; use Test\Integration\PostgreSQL\ThrowawayDb; use Test\Integration\TestDocument; pest()->group('integration', 'postgresql'); describe('::byId()', function () { test('removes fields', function () { RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); expect($tryDoc)->isSome()->toBeTrue(); $doc = $tryDoc->get(); expect($doc)->sub->toBeNull() ->and($doc->value)->toBeEmpty(); }); test('does nothing when the field to remove does not exist', function () { expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('a_field_that_does_not_exist')]))->toBeFalse(); RemoveFields::byId(ThrowawayDb::TABLE, 'one', ['a_field_that_does_not_exist']); // no exception = pass }); test('does nothing when the document does not exist', function () { expect(Exists::byId(ThrowawayDb::TABLE, 'fifty'))->toBeFalse(); RemoveFields::byId(ThrowawayDb::TABLE, 'fifty', ['sub']); // no exception = pass }); }); describe('::byFields()', function () { test('removes fields from matching documents', function () { RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']); expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class)) ->isSome()->toBeTrue() ->get()->sub->toBeNull(); }); test('does nothing when the field to remove does not exist', function () { expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('nada')]))->toBeFalse(); RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['nada']); // no exception = pass }); test('does nothing when no documents match', function () { expect(Exists::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')]))->toBeFalse(); RemoveFields::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')], ['value']); // no exn = pass }); }); describe('::byContains()', function () { test('removes fields from matching documents', function () { $criteria = ['sub' => ['foo' => 'green']]; RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']); $docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class); expect($docs)->not->toBeNull()->hasItems()->toBeTrue(); foreach ($docs->items() as $item) { expect(['two', 'four'])->toContain($item->id) ->and($item->value)->toBeEmpty(); } }); test('does nothing when the field to remove does not exist', function () { expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('invalid_field')]))->toBeFalse(); RemoveFields::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], ['invalid_field']); // no exn = pass }); test('does nothing when no documents match', function () { expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'substantial']))->toBeFalse(); RemoveFields::byContains(ThrowawayDb::TABLE, ['value' => 'substantial'], ['num_value']); // no exception = pass }); }); describe('::byJsonPath()', function () { test('removes fields from matching documents', function () { $path = '$.value ? (@ == "purple")'; RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']); $docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class); expect($docs)->not->toBeNull()->hasItems()->toBeTrue(); foreach ($docs->items() as $item) { expect(['four', 'five'])->toContain($item->id) ->and($item->sub)->toBeNull(); } }); test('does nothing when the field to remove does not exist', function () { expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('submarine')]))->toBeFalse(); RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "purple")', ['submarine']); // no exception = pass }); test('does nothing when no documents match', function () { expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")'))->toBeFalse(); RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")', ['value']); // no exception = pass }); });