From bf04ddfd729c2d18615ffb5417cbefce248ddf9c Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 1 Oct 2024 18:39:57 -0400 Subject: [PATCH] Change doc list hasItems and items to properties - Update associated tests --- src/Custom.php | 2 +- src/DocumentList.php | 56 +++++++++---------- tests/integration/postgresql/CustomTest.php | 4 +- .../postgresql/DocumentListTest.php | 34 +++++------ tests/integration/postgresql/FindTest.php | 34 +++++++---- tests/integration/postgresql/PatchTest.php | 4 +- .../postgresql/RemoveFieldsTest.php | 8 +-- tests/integration/sqlite/CustomTest.php | 5 +- tests/integration/sqlite/DocumentListTest.php | 34 +++++------ tests/integration/sqlite/FindTest.php | 22 +++++--- tests/unit/Query/DefinitionTest.php | 2 +- 11 files changed, 110 insertions(+), 95 deletions(-) diff --git a/src/Custom.php b/src/Custom.php index d7f4fac..c5b4352 100644 --- a/src/Custom.php +++ b/src/Custom.php @@ -89,7 +89,7 @@ class Custom */ public static function array(string $query, array $parameters, Mapper $mapper): array { - return iterator_to_array(self::list($query, $parameters, $mapper)->items()); + return iterator_to_array(self::list($query, $parameters, $mapper)->items); } /** diff --git a/src/DocumentList.php b/src/DocumentList.php index d351e48..bb5e616 100644 --- a/src/DocumentList.php +++ b/src/DocumentList.php @@ -44,42 +44,36 @@ class DocumentList } } - /** - * Does this list have items remaining? - * - * @return bool True if there are items still to be retrieved from the list, false if not - */ - public function hasItems(): bool - { - return !is_null($this->result); + /** @var bool True if there are items still to be retrieved from the list, false if not */ + public bool $hasItems { + get => !is_null($this->result); } /** - * The items from the query result - * - * @return Generator The items from the document list + * @var Generator The items from the document list * @throws DocumentException If this is called once the generator has been consumed */ - public function items(): Generator - { - if (!$this->result) { - if ($this->isConsumed) { - throw new DocumentException('Cannot call items() multiple times'); + public Generator $items { + get { + if (!$this->result) { + if ($this->isConsumed) { + throw new DocumentException('Cannot call items() multiple times'); + } + $this->isConsumed = true; + return; + } + if (!$this->first) { + $this->isConsumed = true; + $this->result = null; + return; + } + yield $this->first; + while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) { + yield $this->mapper->map($row); } $this->isConsumed = true; - return; + $this->result = null; } - if (!$this->first) { - $this->isConsumed = true; - $this->result = null; - return; - } - yield $this->first; - while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) { - yield $this->mapper->map($row); - } - $this->isConsumed = true; - $this->result = null; } /** @@ -92,7 +86,7 @@ class DocumentList */ public function map(callable $map): Generator { - foreach ($this->items() as $item) { + foreach ($this->items as $item) { yield $map($item); } } @@ -105,7 +99,7 @@ class DocumentList */ public function iter(callable $f): void { - foreach ($this->items() as $item) { + foreach ($this->items as $item) { $f($item); } } @@ -122,7 +116,7 @@ class DocumentList public function mapToArray(callable $keyFunc, callable $valueFunc): array { $results = []; - foreach ($this->items() as $item) { + foreach ($this->items as $item) { $results[$keyFunc($item)] = $valueFunc($item); } return $results; diff --git a/tests/integration/postgresql/CustomTest.php b/tests/integration/postgresql/CustomTest.php index ba0b5a9..f889a55 100644 --- a/tests/integration/postgresql/CustomTest.php +++ b/tests/integration/postgresql/CustomTest.php @@ -63,7 +63,7 @@ class CustomTest extends TestCase $list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class)); $this->assertNotNull($list, 'The document list should not be null'); $count = 0; - foreach ($list->items() as $ignored) $count++; + foreach ($list->items as $ignored) $count++; $this->assertEquals(5, $count, 'There should have been 5 documents in the list'); } @@ -74,7 +74,7 @@ class CustomTest extends TestCase Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric > :value", [':value' => 100], new DocumentMapper(TestDocument::class)); $this->assertNotNull($list, 'The document list should not be null'); - $this->assertFalse($list->hasItems(), 'There should have been no documents in the list'); + $this->assertFalse($list->hasItems, 'There should have been no documents in the list'); } #[TestDox('array() succeeds when data is found')] diff --git a/tests/integration/postgresql/DocumentListTest.php b/tests/integration/postgresql/DocumentListTest.php index 827247b..0167a40 100644 --- a/tests/integration/postgresql/DocumentListTest.php +++ b/tests/integration/postgresql/DocumentListTest.php @@ -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,40 +59,40 @@ 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')::numeric < 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')] @@ -101,7 +101,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'); @@ -114,7 +114,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), @@ -127,7 +127,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'); diff --git a/tests/integration/postgresql/FindTest.php b/tests/integration/postgresql/FindTest.php index 182ce2a..2e47466 100644 --- a/tests/integration/postgresql/FindTest.php +++ b/tests/integration/postgresql/FindTest.php @@ -39,8 +39,9 @@ class FindTest extends TestCase { $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(5, $count, 'There should have been 5 documents in the list'); } @@ -49,6 +50,7 @@ class FindTest extends TestCase { $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['five', 'four', 'one', 'three', 'two'], $ids, 'The documents were not ordered correctly'); } @@ -58,6 +60,7 @@ class FindTest extends TestCase { $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['two', 'three', 'one', 'four', 'five'], $ids, 'The documents were not ordered correctly'); } @@ -68,6 +71,7 @@ class FindTest extends TestCase $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['two', 'four', 'one', 'three', 'five'], $ids, 'The documents were not ordered correctly'); } @@ -78,7 +82,7 @@ class FindTest extends TestCase Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertFalse($docs->hasItems(), 'There should have been no documents in the list'); + $this->assertFalse($docs->hasItems, 'There should have been no documents in the list'); } #[TestDox('byId() succeeds when a document is found')] @@ -112,8 +116,9 @@ class FindTest extends TestCase $docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')], TestDocument::class, FieldMatch::All); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(1, $count, 'There should have been 1 document in the list'); } @@ -123,6 +128,7 @@ class FindTest extends TestCase $docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class, FieldMatch::All, [Field::named('id')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly'); } @@ -132,8 +138,9 @@ class FindTest extends TestCase { $docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(1, $count, 'There should have been 1 document in the list'); } @@ -142,7 +149,7 @@ class FindTest extends TestCase { $docs = Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertFalse($docs->hasItems(), 'There should have been no documents in the list'); + $this->assertFalse($docs->hasItems, 'There should have been no documents in the list'); } #[TestDox('byFields() succeeds for inArray when matching documents exist')] @@ -153,8 +160,9 @@ class FindTest extends TestCase $docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])], ArrayDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(2, $count, 'There should have been 2 documents in the list'); } @@ -166,7 +174,7 @@ class FindTest extends TestCase $docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])], ArrayDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertFalse($docs->hasItems(), 'There should have been no documents in the list'); + $this->assertFalse($docs->hasItems, 'There should have been no documents in the list'); } #[TestDox('byContains() succeeds when documents are found')] @@ -174,8 +182,9 @@ class FindTest extends TestCase { $docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(2, $count, 'There should have been 2 documents in the list'); } @@ -185,6 +194,7 @@ class FindTest extends TestCase $docs = Find::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], TestDocument::class, [Field::named('value')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['two', 'four'], $ids, 'The documents were not ordered correctly'); } @@ -194,7 +204,7 @@ class FindTest extends TestCase { $docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertFalse($docs->hasItems(), 'The document list should be empty'); + $this->assertFalse($docs->hasItems, 'The document list should be empty'); } #[TestDox('byJsonPath() succeeds when documents are found')] @@ -202,8 +212,9 @@ class FindTest extends TestCase { $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(2, $count, 'There should have been 2 documents in the list'); } @@ -213,6 +224,7 @@ class FindTest extends TestCase $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class, [Field::named('id')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly'); } @@ -222,7 +234,7 @@ class FindTest extends TestCase { $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertFalse($docs->hasItems(), 'The document list should be empty'); + $this->assertFalse($docs->hasItems, 'The document list should be empty'); } #[TestDox('firstByFields() succeeds when a document is found')] diff --git a/tests/integration/postgresql/PatchTest.php b/tests/integration/postgresql/PatchTest.php index 7b15586..9d2b0cb 100644 --- a/tests/integration/postgresql/PatchTest.php +++ b/tests/integration/postgresql/PatchTest.php @@ -96,8 +96,8 @@ class PatchTest extends TestCase Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']); $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertTrue($docs->hasItems(), 'The document list should not be empty'); - foreach ($docs->items() as $item) { + $this->assertTrue($docs->hasItems, 'The document list should not be empty'); + foreach ($docs->items as $item) { $this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned'); $this->assertEquals('blue', $item->value, 'The document was not patched'); } diff --git a/tests/integration/postgresql/RemoveFieldsTest.php b/tests/integration/postgresql/RemoveFieldsTest.php index ce61251..0d2d260 100644 --- a/tests/integration/postgresql/RemoveFieldsTest.php +++ b/tests/integration/postgresql/RemoveFieldsTest.php @@ -89,8 +89,8 @@ class RemoveFieldsTest extends TestCase RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']); $docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertTrue($docs->hasItems(), 'The document list should not have been empty'); - foreach ($docs->items() as $item) { + $this->assertTrue($docs->hasItems, 'The document list should not have been empty'); + foreach ($docs->items as $item) { $this->assertContains($item->id, ['two', 'four'], 'An incorrect document was returned'); $this->assertEquals('', $item->value, 'The value field was not removed'); } @@ -117,8 +117,8 @@ class RemoveFieldsTest extends TestCase RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']); $docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertTrue($docs->hasItems(), 'The document list should not have been empty'); - foreach ($docs->items() as $item) { + $this->assertTrue($docs->hasItems, 'The document list should not have been empty'); + foreach ($docs->items as $item) { $this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned'); $this->assertNull($item->sub, 'The sub field was not removed'); } diff --git a/tests/integration/sqlite/CustomTest.php b/tests/integration/sqlite/CustomTest.php index 5bb4c4c..3108c9e 100644 --- a/tests/integration/sqlite/CustomTest.php +++ b/tests/integration/sqlite/CustomTest.php @@ -62,8 +62,9 @@ class CustomTest extends TestCase { $list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class)); $this->assertNotNull($list, 'The document list should not be null'); + $this->assertTrue($list->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($list->items() as $ignored) $count++; + foreach ($list->items as $ignored) $count++; $this->assertEquals(5, $count, 'There should have been 5 documents in the list'); } @@ -73,7 +74,7 @@ class CustomTest extends TestCase $list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' > :value", [':value' => 100], new DocumentMapper(TestDocument::class)); $this->assertNotNull($list, 'The document list should not be null'); - $this->assertFalse($list->hasItems(), 'There should have been no documents in the list'); + $this->assertFalse($list->hasItems, 'There should have been no documents in the list'); } #[TestDox('array() succeeds when data is found')] diff --git a/tests/integration/sqlite/DocumentListTest.php b/tests/integration/sqlite/DocumentListTest.php index 8ea2647..2a5e9ee 100644 --- a/tests/integration/sqlite/DocumentListTest.php +++ b/tests/integration/sqlite/DocumentListTest.php @@ -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'); diff --git a/tests/integration/sqlite/FindTest.php b/tests/integration/sqlite/FindTest.php index 85e0721..cbb7f89 100644 --- a/tests/integration/sqlite/FindTest.php +++ b/tests/integration/sqlite/FindTest.php @@ -40,8 +40,9 @@ class FindTest extends TestCase { $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(5, $count, 'There should have been 5 documents in the list'); } @@ -50,6 +51,7 @@ class FindTest extends TestCase { $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['five', 'four', 'one', 'three', 'two'], $ids, 'The documents were not ordered correctly'); } @@ -59,6 +61,7 @@ class FindTest extends TestCase { $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['two', 'three', 'one', 'four', 'five'], $ids, 'The documents were not ordered correctly'); } @@ -69,6 +72,7 @@ class FindTest extends TestCase $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['two', 'four', 'one', 'three', 'five'], $ids, 'The documents were not ordered correctly'); } @@ -79,7 +83,7 @@ class FindTest extends TestCase Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertFalse($docs->hasItems(), 'There should have been no documents in the list'); + $this->assertFalse($docs->hasItems, 'There should have been no documents in the list'); } #[TestDox('byId() succeeds when a document is found')] @@ -112,8 +116,9 @@ class FindTest extends TestCase $docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')], TestDocument::class, FieldMatch::All); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(1, $count, 'There should have been 1 document in the list'); } @@ -123,6 +128,7 @@ class FindTest extends TestCase $docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class, FieldMatch::All, [Field::named('id')]); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false); $this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly'); } @@ -132,8 +138,9 @@ class FindTest extends TestCase { $docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(1, $count, 'There should have been 1 document in the list'); } @@ -142,7 +149,7 @@ class FindTest extends TestCase { $docs = Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertFalse($docs->hasItems(), 'There should have been no documents in the list'); + $this->assertFalse($docs->hasItems, 'There should have been no documents in the list'); } #[TestDox('byFields() succeeds for inArray when matching documents exist')] @@ -153,8 +160,9 @@ class FindTest extends TestCase $docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])], ArrayDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); + $this->assertTrue($docs->hasItems, 'There should have been documents in the list'); $count = 0; - foreach ($docs->items() as $ignored) $count++; + foreach ($docs->items as $ignored) $count++; $this->assertEquals(2, $count, 'There should have been 2 documents in the list'); } @@ -166,7 +174,7 @@ class FindTest extends TestCase $docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])], ArrayDocument::class); $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertFalse($docs->hasItems(), 'There should have been no documents in the list'); + $this->assertFalse($docs->hasItems, 'There should have been no documents in the list'); } #[TestDox('byContains() fails')] diff --git a/tests/unit/Query/DefinitionTest.php b/tests/unit/Query/DefinitionTest.php index 3af4c42..936ac7c 100644 --- a/tests/unit/Query/DefinitionTest.php +++ b/tests/unit/Query/DefinitionTest.php @@ -25,7 +25,7 @@ class DefinitionTest extends TestCase parent::tearDown(); } - #[TestDox('ensureTable() succeeds for PosgtreSQL')] + #[TestDox('ensureTable() succeeds for PostgreSQL')] public function testEnsureTableSucceedsForPostgreSQL(): void { Configuration::overrideMode(Mode::PgSQL);