v2 RC1 Changes #7

Merged
danieljsummers merged 3 commits from v2rc1 into main 2024-10-02 01:37:09 +00:00
11 changed files with 110 additions and 95 deletions
Showing only changes of commit bf04ddfd72 - Show all commits

View File

@ -89,7 +89,7 @@ class Custom
*/ */
public static function array(string $query, array $parameters, Mapper $mapper): array 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);
} }
/** /**

View File

@ -44,42 +44,36 @@ class DocumentList
} }
} }
/** /** @var bool True if there are items still to be retrieved from the list, false if not */
* Does this list have items remaining? public bool $hasItems {
* get => !is_null($this->result);
* @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);
} }
/** /**
* The items from the query result * @var Generator<TDoc> The items from the document list
*
* @return Generator<TDoc> The items from the document list
* @throws DocumentException If this is called once the generator has been consumed * @throws DocumentException If this is called once the generator has been consumed
*/ */
public function items(): Generator public Generator $items {
{ get {
if (!$this->result) { if (!$this->result) {
if ($this->isConsumed) { if ($this->isConsumed) {
throw new DocumentException('Cannot call items() multiple times'); 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; $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 public function map(callable $map): Generator
{ {
foreach ($this->items() as $item) { foreach ($this->items as $item) {
yield $map($item); yield $map($item);
} }
} }
@ -105,7 +99,7 @@ class DocumentList
*/ */
public function iter(callable $f): void public function iter(callable $f): void
{ {
foreach ($this->items() as $item) { foreach ($this->items as $item) {
$f($item); $f($item);
} }
} }
@ -122,7 +116,7 @@ class DocumentList
public function mapToArray(callable $keyFunc, callable $valueFunc): array public function mapToArray(callable $keyFunc, callable $valueFunc): array
{ {
$results = []; $results = [];
foreach ($this->items() as $item) { foreach ($this->items as $item) {
$results[$keyFunc($item)] = $valueFunc($item); $results[$keyFunc($item)] = $valueFunc($item);
} }
return $results; return $results;

View File

@ -63,7 +63,7 @@ class CustomTest extends TestCase
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class)); $list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'The document list should not be null'); $this->assertNotNull($list, 'The document list should not be null');
$count = 0; $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'); $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", Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric > :value",
[':value' => 100], new DocumentMapper(TestDocument::class)); [':value' => 100], new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'The document list should not be null'); $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')] #[TestDox('array() succeeds when data is found')]

View File

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

View File

@ -39,8 +39,9 @@ class FindTest extends TestCase
{ {
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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; $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'); $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')]); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $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'); $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')]); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $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'); $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, $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class,
[Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]); [Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $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'); $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, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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')] #[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')], $docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
TestDocument::class, FieldMatch::All); TestDocument::class, FieldMatch::All);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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; $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'); $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, $docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
FieldMatch::All, [Field::named('id')]); FieldMatch::All, [Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly'); $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); $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->assertNotNull($docs, 'There should have been a document list returned');
$this->assertTrue($docs->hasItems, 'There should have been documents in the list');
$count = 0; $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'); $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); $docs = Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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')] #[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'])], $docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])],
ArrayDocument::class); ArrayDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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; $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'); $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'])], $docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])],
ArrayDocument::class); ArrayDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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')] #[TestDox('byContains() succeeds when documents are found')]
@ -174,8 +182,9 @@ class FindTest extends TestCase
{ {
$docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class); $docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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; $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'); $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, $docs = Find::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], TestDocument::class,
[Field::named('value')]); [Field::named('value')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['two', 'four'], $ids, 'The documents were not ordered correctly'); $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); $docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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')] #[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); $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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; $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'); $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, $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
[Field::named('id')]); [Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly'); $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); $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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')] #[TestDox('firstByFields() succeeds when a document is found')]

View File

@ -96,8 +96,8 @@ class PatchTest extends TestCase
Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']); Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']);
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class); $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $this->assertNotNull($docs, 'There should have been a document list returned');
$this->assertTrue($docs->hasItems(), 'The document list should not be empty'); $this->assertTrue($docs->hasItems, 'The document list should not be empty');
foreach ($docs->items() as $item) { foreach ($docs->items as $item) {
$this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned'); $this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned');
$this->assertEquals('blue', $item->value, 'The document was not patched'); $this->assertEquals('blue', $item->value, 'The document was not patched');
} }

View File

@ -89,8 +89,8 @@ class RemoveFieldsTest extends TestCase
RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']); RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']);
$docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class); $docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $this->assertNotNull($docs, 'There should have been a document list returned');
$this->assertTrue($docs->hasItems(), 'The document list should not have been empty'); $this->assertTrue($docs->hasItems, 'The document list should not have been empty');
foreach ($docs->items() as $item) { foreach ($docs->items as $item) {
$this->assertContains($item->id, ['two', 'four'], 'An incorrect document was returned'); $this->assertContains($item->id, ['two', 'four'], 'An incorrect document was returned');
$this->assertEquals('', $item->value, 'The value field was not removed'); $this->assertEquals('', $item->value, 'The value field was not removed');
} }
@ -117,8 +117,8 @@ class RemoveFieldsTest extends TestCase
RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']); RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']);
$docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class); $docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $this->assertNotNull($docs, 'There should have been a document list returned');
$this->assertTrue($docs->hasItems(), 'The document list should not have been empty'); $this->assertTrue($docs->hasItems, 'The document list should not have been empty');
foreach ($docs->items() as $item) { foreach ($docs->items as $item) {
$this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned'); $this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned');
$this->assertNull($item->sub, 'The sub field was not removed'); $this->assertNull($item->sub, 'The sub field was not removed');
} }

View File

@ -62,8 +62,9 @@ class CustomTest extends TestCase
{ {
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class)); $list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'The document list should not be null'); $this->assertNotNull($list, 'The document list should not be null');
$this->assertTrue($list->hasItems, 'There should have been documents in the list');
$count = 0; $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'); $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", $list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' > :value",
[':value' => 100], new DocumentMapper(TestDocument::class)); [':value' => 100], new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'The document list should not be null'); $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')] #[TestDox('array() succeeds when data is found')]

View File

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

View File

@ -40,8 +40,9 @@ class FindTest extends TestCase
{ {
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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; $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'); $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')]); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $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'); $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')]); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $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'); $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, $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class,
[Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]); [Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $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'); $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, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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')] #[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')], $docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
TestDocument::class, FieldMatch::All); TestDocument::class, FieldMatch::All);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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; $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'); $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, $docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
FieldMatch::All, [Field::named('id')]); FieldMatch::All, [Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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); $ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly'); $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); $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->assertNotNull($docs, 'There should have been a document list returned');
$this->assertTrue($docs->hasItems, 'There should have been documents in the list');
$count = 0; $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'); $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); $docs = Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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')] #[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'])], $docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])],
ArrayDocument::class); ArrayDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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; $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'); $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'])], $docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])],
ArrayDocument::class); ArrayDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned'); $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')] #[TestDox('byContains() fails')]

View File

@ -25,7 +25,7 @@ class DefinitionTest extends TestCase
parent::tearDown(); parent::tearDown();
} }
#[TestDox('ensureTable() succeeds for PosgtreSQL')] #[TestDox('ensureTable() succeeds for PostgreSQL')]
public function testEnsureTableSucceedsForPostgreSQL(): void public function testEnsureTableSucceedsForPostgreSQL(): void
{ {
Configuration::overrideMode(Mode::PgSQL); Configuration::overrideMode(Mode::PgSQL);