Change doc list hasItems and items to properties
- Update associated tests
This commit is contained in:
parent
4b29345dc7
commit
bf04ddfd72
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<TDoc> The items from the document list
|
||||
* @var Generator<TDoc> 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;
|
||||
|
@ -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')]
|
||||
|
@ -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');
|
||||
|
@ -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')]
|
||||
|
@ -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');
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
||||
|
@ -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')]
|
||||
|
@ -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');
|
||||
|
@ -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')]
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user