217 lines
10 KiB
PHP
217 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
* @license MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
use BitBadger\PDODocument\{Custom, Delete, Document, Field, FieldMatch, Find};
|
|
use Test\Integration\{ArrayDocument, NumDocument, TestDocument};
|
|
use Test\Integration\PostgreSQL\ThrowawayDb;
|
|
|
|
pest()->group('integration', 'postgresql');
|
|
|
|
describe('::all()', function () {
|
|
test('retrieves data', function () {
|
|
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
|
|
expect($docs)->not->toBeNull();
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
expect($count)->toBe(5);
|
|
});
|
|
test('sorts data ascending', function () {
|
|
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]);
|
|
expect($docs)->not->toBeNull()
|
|
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
|
->toBe(['five', 'four', 'one', 'three', 'two']);
|
|
});
|
|
test('sorts data descending', function () {
|
|
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]);
|
|
expect($docs)->not->toBeNull()
|
|
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
|
->toBe(['two', 'three', 'one', 'four', 'five']);
|
|
});
|
|
test('sorts data numerically', function () {
|
|
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class,
|
|
[Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]);
|
|
expect($docs)->not->toBeNull()
|
|
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
|
->toBe(['two', 'four', 'one', 'three', 'five']);
|
|
});
|
|
test('retrieves empty results', function () {
|
|
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
expect(Find::all(ThrowawayDb::TABLE, TestDocument::class))
|
|
->not->toBeNull()
|
|
->hasItems()->toBeFalse();
|
|
});
|
|
});
|
|
|
|
describe('::byId()', function () {
|
|
test('retrieves a document via string ID', function () {
|
|
expect(Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class))
|
|
->isSome()->toBeTrue()
|
|
->get()->id->toBe('two');
|
|
});
|
|
test('retrieves a document via numeric ID', function () {
|
|
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]);
|
|
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
|
|
expect(Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class))
|
|
->isSome()->toBeTrue()
|
|
->get()->id->toBe(18);
|
|
});
|
|
test('returns None when a document is not found', function () {
|
|
expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone()->toBeTrue();
|
|
});
|
|
});
|
|
|
|
describe('::byFields()', function () {
|
|
test('retrieves matching documents', function () {
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
|
|
TestDocument::class, FieldMatch::All);
|
|
expect($docs)->not->toBeNull();
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
expect($count)->toBe(1);
|
|
});
|
|
test('retrieves ordered matching documents', function () {
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
|
|
FieldMatch::All, [Field::named('id')]);
|
|
expect($docs)->not->toBeNull()
|
|
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']);
|
|
});
|
|
test('retrieves documents matching a numeric IN clause', function () {
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class);
|
|
expect($docs)->not->toBeNull();
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
expect($count)->toBe(1);
|
|
});
|
|
test('returns an empty list when no matching documents are found', function () {
|
|
expect(Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class))
|
|
->not->toBeNull()
|
|
->hasItems()->toBeFalse();
|
|
});
|
|
test('retrieves documents matching an inArray condition', function () {
|
|
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
|
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])],
|
|
ArrayDocument::class);
|
|
expect($docs)->not->toBeNull();
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
expect($count)->toBe(2);
|
|
});
|
|
test('returns an empty list when no documents match an inArray condition', function () {
|
|
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
|
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
|
expect(Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])],
|
|
ArrayDocument::class))
|
|
->not->toBeNull()
|
|
->hasItems()->toBeFalse();
|
|
});
|
|
});
|
|
|
|
describe('::byContains()', function () {
|
|
test('retrieves matching documents', function () {
|
|
$docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
|
|
expect($docs)->not->toBeNull();
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
expect($count)->toBe(2);
|
|
});
|
|
test('retrieves ordered matching documents', function () {
|
|
$docs = Find::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], TestDocument::class,
|
|
[Field::named('value')]);
|
|
expect($docs)
|
|
->not->toBeNull()
|
|
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['two', 'four']);
|
|
});
|
|
test('returns an empty list when no documents match', function () {
|
|
expect(Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class))
|
|
->not->toBeNull()
|
|
->hasItems()->toBeFalse();
|
|
});
|
|
});
|
|
|
|
describe('::byJsonPath()', function () {
|
|
test('retrieves matching documents', function () {
|
|
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
|
expect($docs)->not->toBeNull();
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
expect($count)->toBe(2);
|
|
});
|
|
test('retrieves ordered matching documents', function () {
|
|
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
|
|
[Field::named('id')]);
|
|
expect($docs)->not->toBeNull()
|
|
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']);
|
|
});
|
|
test('returns an empty list when no documents match', function () {
|
|
expect(Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class))
|
|
->not->toBeNull()
|
|
->hasItems()->toBeFalse();
|
|
});
|
|
});
|
|
|
|
describe('::firstByFields()', function () {
|
|
test('retrieves a matching document', function () {
|
|
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class))
|
|
->isSome()->toBeTrue()->get()->id->toBe('two');
|
|
});
|
|
test('retrieves a document for multiple results', function () {
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
|
|
expect($doc)->isSome()->toBeTrue()->and(['two', 'four'])->toContain($doc->get()->id);
|
|
});
|
|
test('retrieves a document for multiple ordered results', function () {
|
|
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
|
|
orderBy: [Field::named('n:num_value DESC')]))
|
|
->isSome()->toBeTrue()->get()->id->toBe('four');
|
|
});
|
|
test('returns None when no documents match', function () {
|
|
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class))
|
|
->isNone()->toBeTrue();
|
|
});
|
|
});
|
|
|
|
describe('::firstByContains()', function () {
|
|
test('retrieves a matching document', function () {
|
|
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class))
|
|
->isSome()->toBeTrue()->get()->id->toBe('one');
|
|
});
|
|
test('retrieves a document for multiple results', function () {
|
|
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
|
|
expect($doc)->isSome()->toBeTrue()->and(['four', 'five'])->toContain($doc->get()->id);
|
|
});
|
|
test('retrieves a document for multiple ordered results', function () {
|
|
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class,
|
|
[Field::named('sub.bar NULLS FIRST')]))
|
|
->isSome()->toBeTrue()->get()->id->toBe('five');
|
|
});
|
|
test('returns None when no documents match', function () {
|
|
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class))
|
|
->isNone()->toBeTrue();
|
|
});
|
|
});
|
|
|
|
describe('::firstByJsonPath()', function () {
|
|
test('retrieves a matching document', function () {
|
|
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class))
|
|
->isSome()->toBeTrue()->get()->id->toBe('two');
|
|
});
|
|
test('retrieves a document for multiple results', function () {
|
|
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
|
expect($doc)->isSome()->toBeTrue()->and(['four', 'five'])->toContain($doc->get()->id);
|
|
});
|
|
test('retrieves a document for multiple ordered results', function () {
|
|
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
|
|
[Field::named('id DESC')]))
|
|
->isSome()->toBeTrue()->get()->id->toBe('four');
|
|
});
|
|
test('returns None when no documents match', function () {
|
|
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class))
|
|
->isNone()->toBeTrue();
|
|
});
|
|
});
|