2024-11-15 04:38:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\{DocumentException, DocumentList, Query};
|
|
|
|
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
|
|
|
use Test\Integration\PostgreSQL\ThrowawayDb;
|
|
|
|
use Test\Integration\TestDocument;
|
|
|
|
|
|
|
|
pest()->group('integration', 'postgresql');
|
|
|
|
|
|
|
|
describe('::create()', function () {
|
|
|
|
test('creates a document list', function () {
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
expect($list)->not->toBeNull();
|
|
|
|
$list = null; // free database result
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-11-15 15:13:11 +00:00
|
|
|
describe('->items()', function () {
|
2024-11-15 04:38:47 +00:00
|
|
|
test('enumerates items in the list', function () {
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
expect($list)->not->toBeNull();
|
|
|
|
$count = 0;
|
|
|
|
foreach ($list->items() as $item) {
|
|
|
|
expect(['one', 'two', 'three', 'four', 'five'])->toContain($item->id);
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
expect($count)->toBe(5);
|
|
|
|
});
|
|
|
|
test('fails when the list is exhausted', function () {
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
|
|
|
|
$ignored = iterator_to_array($list->items());
|
|
|
|
expect($list)->hasItems()->toBeFalse()
|
|
|
|
->and(fn () => iterator_to_array($list->items()))->toThrow(DocumentException::class);
|
|
|
|
});
|
|
|
|
});
|
2024-11-15 15:13:11 +00:00
|
|
|
|
|
|
|
describe('->hasItems()', function () {
|
|
|
|
test('returns false when no items are in the list', function () {
|
|
|
|
expect(DocumentList::create(
|
|
|
|
Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric < 0", [],
|
|
|
|
new DocumentMapper(TestDocument::class)))
|
|
|
|
->not->toBeNull()
|
|
|
|
->hasItems()->toBeFalse();
|
|
|
|
});
|
|
|
|
test('returns true when items are in the list', function () {
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
|
|
|
|
foreach ($list->items() as $ignored) {
|
|
|
|
expect($list)->hasItems()->toBeTrue();
|
|
|
|
}
|
|
|
|
expect($list)->hasItems()->toBeFalse();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('->map()', function () {
|
|
|
|
test('transforms the list', function () {
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
|
|
|
|
foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) {
|
|
|
|
expect(['eno', 'owt', 'eerht', 'ruof', 'evif'])->toContain($mapped);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('->iter()', function () {
|
|
|
|
test('walks the list', function () {
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
|
|
|
|
$splats = [];
|
|
|
|
$list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); });
|
|
|
|
expect(implode(' ', $splats))->toBe('*** *** ***** **** ****');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('->mapToArray()', function () {
|
|
|
|
test('creates an associative array', function () {
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
expect($list)->not->toBeNull()->hasItems()->toBeTrue()
|
|
|
|
->and($list->mapToArray(fn($it) => $it->id, fn($it) => $it->value))
|
|
|
|
->toBe(['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']);
|
|
|
|
});
|
|
|
|
});
|