* @license MIT */ declare(strict_types=1); use BitBadger\PDODocument\{DocumentException, DocumentList, Query}; use BitBadger\PDODocument\Mapper\DocumentMapper; use Test\Integration\SQLite\ThrowawayDb; use Test\Integration\TestDocument; pest()->group('integration', 'sqlite'); 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 }); }); describe('->items', function () { 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); }); }); describe('->hasItems', function () { test('returns true when items exist', 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(); }); test('returns false when no items exist', function () { expect(DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' < 0", [], new DocumentMapper(TestDocument::class))) ->not->toBeNull()->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']); }); });