v2 RC1 Changes (#7)

- Changes `items` and `hasItems` on `DocumentList` to be properties
- Updates dependent option/result library, which contains similar changes

Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
2024-10-02 01:37:08 +00:00
parent df436c9ef4
commit 486028bd40
22 changed files with 266 additions and 247 deletions

View File

@@ -44,14 +44,14 @@ class DocumentListTest extends TestCase
$list = null;
}
#[TestDox('items() succeeds')]
#[TestDox('items succeeds')]
public function testItemsSucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'There should have been a document list created');
$count = 0;
foreach ($list->items() as $item) {
foreach ($list->items as $item) {
$this->assertContains($item->id, ['one', 'two', 'three', 'four', 'five'],
'An unexpected document ID was returned');
$count++;
@@ -59,39 +59,39 @@ class DocumentListTest extends TestCase
$this->assertEquals(5, $count, 'There should have been 5 documents returned');
}
#[TestDox('items() fails when already consumed')]
#[TestDox('items fails when already consumed')]
public function testItemsFailsWhenAlreadyConsumed(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'There should have been a document list created');
$this->assertTrue($list->hasItems(), 'There should be items in the list');
$ignored = iterator_to_array($list->items());
$this->assertFalse($list->hasItems(), 'The list should no longer have items');
$this->assertTrue($list->hasItems, 'There should be items in the list');
$ignored = iterator_to_array($list->items);
$this->assertFalse($list->hasItems, 'The list should no longer have items');
$this->expectException(DocumentException::class);
iterator_to_array($list->items());
iterator_to_array($list->items);
}
#[TestDox('hasItems() succeeds with empty results')]
#[TestDox('hasItems succeeds with empty results')]
public function testHasItemsSucceedsWithEmptyResults(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' < 0", [],
new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'There should have been a document list created');
$this->assertFalse($list->hasItems(), 'There should be no items in the list');
$this->assertFalse($list->hasItems, 'There should be no items in the list');
}
#[TestDox('hasItems() succeeds with non-empty results')]
#[TestDox('hasItems succeeds with non-empty results')]
public function testHasItemsSucceedsWithNonEmptyResults(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'There should have been a document list created');
$this->assertTrue($list->hasItems(), 'There should be items in the list');
foreach ($list->items() as $ignored) {
$this->assertTrue($list->hasItems(), 'There should be items remaining in the list');
$this->assertTrue($list->hasItems, 'There should be items in the list');
foreach ($list->items as $ignored) {
$this->assertTrue($list->hasItems, 'There should be items remaining in the list');
}
$this->assertFalse($list->hasItems(), 'There should be no remaining items in the list');
$this->assertFalse($list->hasItems, 'There should be no remaining items in the list');
}
#[TestDox('map() succeeds')]
@@ -100,7 +100,7 @@ class DocumentListTest extends TestCase
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'There should have been a document list created');
$this->assertTrue($list->hasItems(), 'There should be items in the list');
$this->assertTrue($list->hasItems, 'There should be items in the list');
foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) {
$this->assertContains($mapped, ['eno', 'owt', 'eerht', 'ruof', 'evif'],
'An unexpected mapped value was returned');
@@ -113,7 +113,7 @@ class DocumentListTest extends TestCase
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'There should have been a document list created');
$this->assertTrue($list->hasItems(), 'There should be items in the list');
$this->assertTrue($list->hasItems, 'There should be items in the list');
$splats = [];
$list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); });
$this->assertEquals('*** *** ***** **** ****', implode(' ', $splats),
@@ -126,7 +126,7 @@ class DocumentListTest extends TestCase
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'There should have been a document list created');
$this->assertTrue($list->hasItems(), 'There should be items in the list');
$this->assertTrue($list->hasItems, 'There should be items in the list');
$lookup = $list->mapToArray(fn($it) => $it->id, fn($it) => $it->value);
$expected = ['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple'];
$this->assertEquals($expected, $lookup, 'The array was not mapped correctly');