Compare commits

...

2 Commits

Author SHA1 Message Date
486028bd40 v2 RC1 Changes (#7)
- Changes `items` and `hasItems` on `DocumentList` to be properties
- Updates dependent option/result library, which contains similar changes

Reviewed-on: #7
2024-10-02 01:37:08 +00:00
df436c9ef4 Changes for RC1 (#6)
- `ORDER BY` clauses support qualified fields
- Restrict supported PHP versions; this will prevent inadvertent upgrades to the upcoming 8.4-compliant version

Reviewed-on: #6
2024-10-01 00:29:12 +00:00
24 changed files with 332 additions and 286 deletions

View File

@ -4,10 +4,14 @@ This library allows SQLite and PostgreSQL to be treated as document databases. I
## Add via Composer ## Add via Composer
[![Packagist Version](https://img.shields.io/packagist/v/bit-badger/pdo-document)](https://packagist.org/packages/bit-badger/pdo-document) [![Static Badge](https://img.shields.io/badge/v1.0.0--rc1-orange?label=php%208.2)
](https://packagist.org/packages/bit-badger/pdo-document#v1.0.0-rc1)     [![Packagist Version](https://img.shields.io/packagist/v/bit-badger/pdo-document?include_prereleases&label=php%208.4)
](https://packagist.org/packages/bit-badger/pdo-document)
`composer require bit-badger/pdo-document` `composer require bit-badger/pdo-document`
For the v1 series, the `DocumentList` type's members `hasItems` and `items` are functions; in the v2 series, they are properties. Additionally, the `Option` and `Result` types included in the project have a similar difference; see the [v1 README](https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp/src/branch/v1/README.md) for PHP 8.2 or 8.3 and the [v2 README](https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp/src/branch/main/README.md) for PHP 8.4. Both versions are supported; the v1 / v2 distinction helps composer make the right choice based on the target PHP version of your project.
## Configuration ## Configuration
### Connection Details ### Connection Details

View File

@ -17,10 +17,10 @@
"rss": "https://git.bitbadger.solutions/bit-badger/pdo-document.rss", "rss": "https://git.bitbadger.solutions/bit-badger/pdo-document.rss",
"docs": "https://bitbadger.solutions/open-source/pdo-document/" "docs": "https://bitbadger.solutions/open-source/pdo-document/"
}, },
"minimum-stability": "beta", "minimum-stability": "RC",
"require": { "require": {
"php": ">=8.2", "php": ">=8.4",
"bit-badger/inspired-by-fsharp": "^1", "bit-badger/inspired-by-fsharp": "^2",
"netresearch/jsonmapper": "^4", "netresearch/jsonmapper": "^4",
"ext-pdo": "*" "ext-pdo": "*"
}, },

26
composer.lock generated
View File

@ -4,18 +4,18 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "7f9c6e825e6f0968b2dedefec770f558", "content-hash": "20902a67aa621a28ad20d5faf0a30629",
"packages": [ "packages": [
{ {
"name": "bit-badger/inspired-by-fsharp", "name": "bit-badger/inspired-by-fsharp",
"version": "v1.0.0-beta2", "version": "v2.0.0-rc1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp", "url": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp",
"reference": "fad428a4e40b606987499b17bb2d5b7d4b04502d" "reference": "9327d8fa297a261f985870ebe6f70edada78a8ad"
}, },
"require": { "require": {
"php": "^8.2" "php": ">=8.4"
}, },
"require-dev": { "require-dev": {
"phpoption/phpoption": "^1", "phpoption/phpoption": "^1",
@ -49,7 +49,7 @@
"rss": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp.rss", "rss": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp.rss",
"source": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp" "source": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp"
}, },
"time": "2024-07-29T17:58:33+00:00" "time": "2024-10-01T03:06:57+00:00"
}, },
{ {
"name": "netresearch/jsonmapper", "name": "netresearch/jsonmapper",
@ -166,16 +166,16 @@
}, },
{ {
"name": "nikic/php-parser", "name": "nikic/php-parser",
"version": "v5.2.0", "version": "v5.3.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/nikic/PHP-Parser.git", "url": "https://github.com/nikic/PHP-Parser.git",
"reference": "23c79fbbfb725fb92af9bcf41065c8e9a0d49ddb" "reference": "3abf7425cd284141dc5d8d14a9ee444de3345d1a"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/23c79fbbfb725fb92af9bcf41065c8e9a0d49ddb", "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/3abf7425cd284141dc5d8d14a9ee444de3345d1a",
"reference": "23c79fbbfb725fb92af9bcf41065c8e9a0d49ddb", "reference": "3abf7425cd284141dc5d8d14a9ee444de3345d1a",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -218,9 +218,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/nikic/PHP-Parser/issues", "issues": "https://github.com/nikic/PHP-Parser/issues",
"source": "https://github.com/nikic/PHP-Parser/tree/v5.2.0" "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.0"
}, },
"time": "2024-09-15T16:40:33+00:00" "time": "2024-09-29T13:56:26+00:00"
}, },
{ {
"name": "phar-io/manifest", "name": "phar-io/manifest",
@ -1844,12 +1844,12 @@
} }
], ],
"aliases": [], "aliases": [],
"minimum-stability": "beta", "minimum-stability": "RC",
"stability-flags": [], "stability-flags": [],
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": ">=8.2", "php": ">=8.4",
"ext-pdo": "*" "ext-pdo": "*"
}, },
"platform-dev": [], "platform-dev": [],

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

@ -143,6 +143,44 @@ class Query
return "UPDATE $tableName SET data = :data WHERE " . self::whereById(); return "UPDATE $tableName SET data = :data WHERE " . self::whereById();
} }
/**
* Transform a field to an ORDER BY clause segment
*
* @param Field $field The field by which ordering should be implemented
* @return string The ORDER BY fragment for the given field
* @throws Exception If the database mode has not been set
*/
private static function mapToOrderBy(Field $field): string
{
$mode = Configuration::mode('render ORDER BY clause');
if (str_contains($field->fieldName, ' ')) {
$parts = explode(' ', $field->fieldName);
$field->fieldName = array_shift($parts);
$direction = ' ' . implode(' ', $parts);
} else {
$direction = '';
}
if (str_starts_with($field->fieldName, 'n:')) {
$field->fieldName = substr($field->fieldName, 2);
$value = match ($mode) {
Mode::PgSQL => '(' . $field->path() . ')::numeric',
Mode::SQLite => $field->path()
};
} elseif (str_starts_with($field->fieldName, 'i:')) {
$field->fieldName = substr($field->fieldName, 2);
$value = match ($mode) {
Mode::PgSQL => 'LOWER(' . $field->path() . ')',
Mode::SQLite => $field->path() . ' COLLATE NOCASE'
};
} else {
$value = $field->path();
}
return (empty($field->qualifier) ? '' : "$field->qualifier.") . $value . $direction;
}
/** /**
* Create an `ORDER BY` clause ('n:' treats field as number, 'i:' does case-insensitive ordering) * Create an `ORDER BY` clause ('n:' treats field as number, 'i:' does case-insensitive ordering)
* *
@ -152,36 +190,6 @@ class Query
*/ */
public static function orderBy(array $fields): string public static function orderBy(array $fields): string
{ {
if (empty($fields)) return ""; return empty($fields) ? "" : ' ORDER BY ' . implode(', ', array_map(self::mapToOrderBy(...), $fields));
$mode = Configuration::mode('render ORDER BY clause');
$sqlFields = array_map(function (Field $it) use ($mode) {
if (str_contains($it->fieldName, ' ')) {
$parts = explode(' ', $it->fieldName);
$it->fieldName = array_shift($parts);
$direction = ' ' . implode(' ', $parts);
} else {
$direction = '';
}
if (str_starts_with($it->fieldName, 'n:')) {
$it->fieldName = substr($it->fieldName, 2);
$value = match ($mode) {
Mode::PgSQL => '(' . $it->path() . ')::numeric',
Mode::SQLite => $it->path()
};
} elseif (str_starts_with($it->fieldName, 'i:')) {
$it->fieldName = substr($it->fieldName, 2);
$value = match ($mode) {
Mode::PgSQL => 'LOWER(' . $it->path() . ')',
Mode::SQLite => $it->path() . ' COLLATE NOCASE'
};
} else {
$value = $it->path();
}
return $value . $direction;
}, $fields);
return ' ORDER BY ' . implode(', ', $sqlFields);
} }
} }

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')]
@ -100,8 +100,8 @@ class CustomTest extends TestCase
{ {
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'], $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
new DocumentMapper(TestDocument::class)); new DocumentMapper(TestDocument::class));
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('one', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('single() succeeds when a row is not found')] #[TestDox('single() succeeds when a row is not found')]
@ -109,7 +109,7 @@ class CustomTest extends TestCase
{ {
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
[':id' => 'eighty'], new DocumentMapper(TestDocument::class)); [':id' => 'eighty'], new DocumentMapper(TestDocument::class));
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
#[TestDox('nonQuery() succeeds when operating on data')] #[TestDox('nonQuery() succeeds when operating on data')]

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

@ -40,8 +40,8 @@ class DocumentTest extends TestCase
{ {
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]); Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
$this->assertTrue($tryDoc->isSome(), 'There should have been a document inserted'); $this->assertTrue($tryDoc->isSome, 'There should have been a document inserted');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect'); $this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
$this->assertEquals('', $doc->value, 'The value was incorrect'); $this->assertEquals('', $doc->value, 'The value was incorrect');
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect'); $this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
@ -59,15 +59,15 @@ class DocumentTest extends TestCase
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]); Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$obj = json_decode($doc->get()['data']); $obj = json_decode($doc->value['data']);
$this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated'); $this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated');
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]); Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE " . Query::whereById(docId: 2), $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE " . Query::whereById(docId: 2),
[':id' => 2], new ArrayMapper()); [':id' => 2], new ArrayMapper());
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$obj = json_decode($doc->get()['data']); $obj = json_decode($doc->value['data']);
$this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated'); $this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
@ -82,8 +82,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]); Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$obj = json_decode($doc->get()['data']); $obj = json_decode($doc->value['data']);
$this->assertEquals(7, $obj->id, 'The ID 7 should have been stored'); $this->assertEquals(7, $obj->id, 'The ID 7 should have been stored');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
@ -98,8 +98,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]); Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated'); $this->assertNotEmpty($doc->value->id, 'The ID should have been auto-generated');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -114,8 +114,8 @@ class DocumentTest extends TestCase
$uuid = AutoId::generateUUID(); $uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]); Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed'); $this->assertEquals($uuid, $doc->value->id, 'The ID should not have been changed');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -130,8 +130,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]); Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(6, strlen($doc->get()->id), $this->assertEquals(6, strlen($doc->value->id),
'The ID should have been auto-generated and had 6 characters'); 'The ID should have been auto-generated and had 6 characters');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
@ -147,8 +147,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]); Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed'); $this->assertEquals('my-key', $doc->value->id, 'The ID should not have been changed');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -159,8 +159,8 @@ class DocumentTest extends TestCase
{ {
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble'))); Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
$this->assertTrue($tryDoc->isSome(), 'There should have been a document inserted'); $this->assertTrue($tryDoc->isSome, 'There should have been a document inserted');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect'); $this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
$this->assertEquals('', $doc->value, 'The value was incorrect'); $this->assertEquals('', $doc->value, 'The value was incorrect');
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect'); $this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
@ -178,13 +178,13 @@ class DocumentTest extends TestCase
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco')); Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(1, $doc->get()->id, 'The ID 1 should have been auto-generated'); $this->assertEquals(1, $doc->value->id, 'The ID 1 should have been auto-generated');
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito')); Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(2, $doc->get()->id, 'The ID 2 should have been auto-generated'); $this->assertEquals(2, $doc->value->id, 'The ID 2 should have been auto-generated');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -198,8 +198,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large')); Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(64, $doc->get()->id, 'The ID 64 should have been stored'); $this->assertEquals(64, $doc->value->id, 'The ID 64 should have been stored');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -213,8 +213,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9)); Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated'); $this->assertNotEmpty($doc->value->id, 'The ID should have been auto-generated');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -229,8 +229,8 @@ class DocumentTest extends TestCase
$uuid = AutoId::generateUUID(); $uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14)); Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed'); $this->assertEquals($uuid, $doc->value->id, 'The ID should not have been changed');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -245,8 +245,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55)); Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(40, strlen($doc->get()->id), $this->assertEquals(40, strlen($doc->value->id),
'The ID should have been auto-generated and had 40 characters'); 'The ID should have been auto-generated and had 40 characters');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
@ -262,8 +262,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3)); Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed'); $this->assertEquals('my-key', $doc->value->id, 'The ID should not have been changed');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -281,7 +281,7 @@ class DocumentTest extends TestCase
{ {
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b'))); Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
} }
#[TestDox('save() succeeds when a document is updated')] #[TestDox('save() succeeds when a document is updated')]
@ -289,8 +289,8 @@ class DocumentTest extends TestCase
{ {
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44)); Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
$this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); $this->assertTrue($tryDoc->isSome, 'There should have been a document returned');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals(44, $doc->num_value, 'The numeric value was not updated'); $this->assertEquals(44, $doc->num_value, 'The numeric value was not updated');
$this->assertNull($doc->sub, 'The sub-document should have been null'); $this->assertNull($doc->sub, 'The sub-document should have been null');
} }
@ -300,8 +300,8 @@ class DocumentTest extends TestCase
{ {
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z'))); Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertNotFalse($tryDoc->isSome(), 'There should have been a document returned'); $this->assertNotFalse($tryDoc->isSome, 'There should have been a document returned');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals('howdy', $doc->value, 'The value was incorrect'); $this->assertEquals('howdy', $doc->value, 'The value was incorrect');
$this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect'); $this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect');
$this->assertNotNull($doc->sub, 'The sub-document should not have been null'); $this->assertNotNull($doc->sub, 'The sub-document should not have been null');
@ -314,6 +314,6 @@ class DocumentTest extends TestCase
{ {
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200')); Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
$doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
} }

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,15 +82,15 @@ 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')]
public function testByIdSucceedsWhenADocumentIsFound(): void public function testByIdSucceedsWhenADocumentIsFound(): void
{ {
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('two', $doc->get()->id, 'An incorrect document was returned'); $this->assertEquals('two', $doc->value->id, 'An incorrect document was returned');
} }
#[TestDox('byId() succeeds when a document is found with numeric ID')] #[TestDox('byId() succeeds when a document is found with numeric ID')]
@ -95,15 +99,15 @@ class FindTest extends TestCase
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]); Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]);
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']); Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
$doc = Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(18, $doc->get()->id, 'An incorrect document was returned'); $this->assertEquals(18, $doc->value->id, 'An incorrect document was returned');
} }
#[TestDox('byId() succeeds when a document is not found')] #[TestDox('byId() succeeds when a document is not found')]
public function testByIdSucceedsWhenADocumentIsNotFound(): void public function testByIdSucceedsWhenADocumentIsNotFound(): void
{ {
$doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
#[TestDox('byFields() succeeds when documents are found')] #[TestDox('byFields() succeeds when documents are 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,23 +234,23 @@ 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')]
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
{ {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('two', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('firstByFields() succeeds when multiple documents are found')] #[TestDox('firstByFields() succeeds when multiple documents are found')]
public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void
{ {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertContains($doc->get()->id, ['two', 'four'], 'An incorrect document was returned'); $this->assertContains($doc->value->id, ['two', 'four'], 'An incorrect document was returned');
} }
#[TestDox('firstByFields() succeeds when multiple ordered documents are found')] #[TestDox('firstByFields() succeeds when multiple ordered documents are found')]
@ -246,31 +258,31 @@ class FindTest extends TestCase
{ {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class, $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
orderBy: [Field::named('n:num_value DESC')]); orderBy: [Field::named('n:num_value DESC')]);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('four', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('four', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('firstByFields() succeeds when a document is not found')] #[TestDox('firstByFields() succeeds when a document is not found')]
public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void
{ {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
#[TestDox('firstByContains() succeeds when a document is found')] #[TestDox('firstByContains() succeeds when a document is found')]
public function testFirstByContainsSucceedsWhenADocumentIsFound(): void public function testFirstByContainsSucceedsWhenADocumentIsFound(): void
{ {
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class); $doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('one', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('firstByContains() succeeds when multiple documents are found')] #[TestDox('firstByContains() succeeds when multiple documents are found')]
public function testFirstByContainsSucceedsWhenMultipleDocumentsAreFound(): void public function testFirstByContainsSucceedsWhenMultipleDocumentsAreFound(): void
{ {
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class); $doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertContains($doc->get()->id, ['four', 'five'], 'An incorrect document was returned'); $this->assertContains($doc->value->id, ['four', 'five'], 'An incorrect document was returned');
} }
#[TestDox('firstByContains() succeeds when multiple ordered documents are found')] #[TestDox('firstByContains() succeeds when multiple ordered documents are found')]
@ -278,31 +290,31 @@ class FindTest extends TestCase
{ {
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class, $doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class,
[Field::named('sub.bar NULLS FIRST')]); [Field::named('sub.bar NULLS FIRST')]);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('five', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('five', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('firstByContains() succeeds when a document is not found')] #[TestDox('firstByContains() succeeds when a document is not found')]
public function testFirstByContainsSucceedsWhenADocumentIsNotFound(): void public function testFirstByContainsSucceedsWhenADocumentIsNotFound(): void
{ {
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class); $doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
#[TestDox('firstByJsonPath() succeeds when a document is found')] #[TestDox('firstByJsonPath() succeeds when a document is found')]
public function testFirstByJsonPathSucceedsWhenADocumentIsFound(): void public function testFirstByJsonPathSucceedsWhenADocumentIsFound(): void
{ {
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class); $doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('two', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('firstByJsonPath() succeeds when multiple documents are found')] #[TestDox('firstByJsonPath() succeeds when multiple documents are found')]
public function testFirstByJsonPathSucceedsWhenMultipleDocumentsAreFound(): void public function testFirstByJsonPathSucceedsWhenMultipleDocumentsAreFound(): void
{ {
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class); $doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertContains($doc->get()->id, ['four', 'five'], 'An incorrect document was returned'); $this->assertContains($doc->value->id, ['four', 'five'], 'An incorrect document was returned');
} }
#[TestDox('firstByJsonPath() succeeds when multiple ordered documents are found')] #[TestDox('firstByJsonPath() succeeds when multiple ordered documents are found')]
@ -310,14 +322,14 @@ class FindTest extends TestCase
{ {
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class, $doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
[Field::named('id DESC')]); [Field::named('id DESC')]);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('four', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('four', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('firstByJsonPath() succeeds when a document is not found')] #[TestDox('firstByJsonPath() succeeds when a document is not found')]
public function testFirstByJsonPathSucceedsWhenADocumentIsNotFound(): void public function testFirstByJsonPathSucceedsWhenADocumentIsNotFound(): void
{ {
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class); $doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
} }

View File

@ -39,8 +39,8 @@ class PatchTest extends TestCase
{ {
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]); Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(44, $doc->get()->num_value, 'The updated document is not correct'); $this->assertEquals(44, $doc->value->num_value, 'The updated document is not correct');
} }
#[TestDox('byId() succeeds when no document is updated')] #[TestDox('byId() succeeds when no document is updated')]
@ -74,8 +74,8 @@ class PatchTest extends TestCase
{ {
Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]); Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]);
$tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class); $tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class);
$this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); $this->assertTrue($tryDoc->isSome, 'There should have been a document returned');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals('two', $doc->id, 'An incorrect document was returned'); $this->assertEquals('two', $doc->id, 'An incorrect document was returned');
$this->assertEquals(12, $doc->num_value, 'The document was not patched'); $this->assertEquals(12, $doc->num_value, 'The document was not patched');
} }
@ -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

@ -39,8 +39,8 @@ class RemoveFieldsTest extends TestCase
{ {
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']); RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
$this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); $this->assertTrue($tryDoc->isSome, 'There should have been a document returned');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals('', $doc->value, 'Value should have been blank (its default value)'); $this->assertEquals('', $doc->value, 'Value should have been blank (its default value)');
$this->assertNull($doc->sub, 'Sub-document should have been null'); $this->assertNull($doc->sub, 'Sub-document should have been null');
} }
@ -64,8 +64,8 @@ class RemoveFieldsTest extends TestCase
{ {
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']); RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertNull($doc->get()->sub, 'Sub-document should have been null'); $this->assertNull($doc->value->sub, 'Sub-document should have been null');
} }
#[TestDox('byFields() succeeds when a field is not removed')] #[TestDox('byFields() succeeds when a field is not removed')]
@ -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')]
@ -99,8 +100,8 @@ class CustomTest extends TestCase
{ {
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'], $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
new DocumentMapper(TestDocument::class)); new DocumentMapper(TestDocument::class));
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('one', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('single() succeeds when a row is not found')] #[TestDox('single() succeeds when a row is not found')]
@ -108,7 +109,7 @@ class CustomTest extends TestCase
{ {
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
[':id' => 'eighty'], new DocumentMapper(TestDocument::class)); [':id' => 'eighty'], new DocumentMapper(TestDocument::class));
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
#[TestDox('nonQuery() succeeds when operating on data')] #[TestDox('nonQuery() succeeds when operating on data')]

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,8 @@ class DocumentTest extends TestCase
{ {
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]); Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
$this->assertTrue($tryDoc->isSome(), 'There should have been a document inserted'); $this->assertTrue($tryDoc->isSome, 'There should have been a document inserted');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect'); $this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
$this->assertEquals('', $doc->value, 'The value was incorrect'); $this->assertEquals('', $doc->value, 'The value was incorrect');
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect'); $this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
@ -59,15 +59,15 @@ class DocumentTest extends TestCase
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]); Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$obj = json_decode($doc->get()['data']); $obj = json_decode($doc->value['data']);
$this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated'); $this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated');
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]); Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = 2", [], $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = 2", [],
new ArrayMapper()); new ArrayMapper());
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$obj = json_decode($doc->get()['data']); $obj = json_decode($doc->value['data']);
$this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated'); $this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
@ -82,8 +82,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]); Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$obj = json_decode($doc->get()['data']); $obj = json_decode($doc->value['data']);
$this->assertEquals(7, $obj->id, 'The ID 7 should have been stored'); $this->assertEquals(7, $obj->id, 'The ID 7 should have been stored');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
@ -98,8 +98,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]); Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated'); $this->assertNotEmpty($doc->value->id, 'The ID should have been auto-generated');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -114,8 +114,8 @@ class DocumentTest extends TestCase
$uuid = AutoId::generateUUID(); $uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]); Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed'); $this->assertEquals($uuid, $doc->value->id, 'The ID should not have been changed');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -130,8 +130,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]); Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(6, strlen($doc->get()->id), $this->assertEquals(6, strlen($doc->value->id),
'The ID should have been auto-generated and had 6 characters'); 'The ID should have been auto-generated and had 6 characters');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
@ -147,8 +147,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]); Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed'); $this->assertEquals('my-key', $doc->value->id, 'The ID should not have been changed');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -159,8 +159,8 @@ class DocumentTest extends TestCase
{ {
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble'))); Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
$this->assertNotFalse($tryDoc->isSome(), 'There should have been a document inserted'); $this->assertNotFalse($tryDoc->isSome, 'There should have been a document inserted');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect'); $this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
$this->assertEquals('', $doc->value, 'The value was incorrect'); $this->assertEquals('', $doc->value, 'The value was incorrect');
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect'); $this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
@ -178,13 +178,13 @@ class DocumentTest extends TestCase
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco')); Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(1, $doc->get()->id, 'The ID 1 should have been auto-generated'); $this->assertEquals(1, $doc->value->id, 'The ID 1 should have been auto-generated');
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito')); Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(2, $doc->get()->id, 'The ID 2 should have been auto-generated'); $this->assertEquals(2, $doc->value->id, 'The ID 2 should have been auto-generated');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -198,8 +198,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large')); Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(64, $doc->get()->id, 'The ID 64 should have been stored'); $this->assertEquals(64, $doc->value->id, 'The ID 64 should have been stored');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -213,8 +213,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9)); Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated'); $this->assertNotEmpty($doc->value->id, 'The ID should have been auto-generated');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -229,8 +229,8 @@ class DocumentTest extends TestCase
$uuid = AutoId::generateUUID(); $uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14)); Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed'); $this->assertEquals($uuid, $doc->value->id, 'The ID should not have been changed');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -245,8 +245,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55)); Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(40, strlen($doc->get()->id), $this->assertEquals(40, strlen($doc->value->id),
'The ID should have been auto-generated and had 40 characters'); 'The ID should have been auto-generated and had 40 characters');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
@ -262,8 +262,8 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3)); Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed'); $this->assertEquals('my-key', $doc->value->id, 'The ID should not have been changed');
} finally { } finally {
Configuration::$autoId = AutoId::None; Configuration::$autoId = AutoId::None;
} }
@ -281,7 +281,7 @@ class DocumentTest extends TestCase
{ {
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b'))); Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
} }
#[TestDox('save() succeeds when a document is updated')] #[TestDox('save() succeeds when a document is updated')]
@ -289,8 +289,8 @@ class DocumentTest extends TestCase
{ {
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44)); Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
$this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); $this->assertTrue($tryDoc->isSome, 'There should have been a document returned');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals(44, $doc->num_value, 'The numeric value was not updated'); $this->assertEquals(44, $doc->num_value, 'The numeric value was not updated');
$this->assertNull($doc->sub, 'The sub-document should have been null'); $this->assertNull($doc->sub, 'The sub-document should have been null');
} }
@ -300,8 +300,8 @@ class DocumentTest extends TestCase
{ {
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z'))); Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); $this->assertTrue($tryDoc->isSome, 'There should have been a document returned');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals('howdy', $doc->value, 'The value was incorrect'); $this->assertEquals('howdy', $doc->value, 'The value was incorrect');
$this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect'); $this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect');
$this->assertNotNull($doc->sub, 'The sub-document should not have been null'); $this->assertNotNull($doc->sub, 'The sub-document should not have been null');
@ -314,6 +314,6 @@ class DocumentTest extends TestCase
{ {
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200')); Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
$doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
} }

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,15 +83,15 @@ 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')]
public function testByIdSucceedsWhenADocumentIsFound(): void public function testByIdSucceedsWhenADocumentIsFound(): void
{ {
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('two', $doc->get()->id, 'An incorrect document was returned'); $this->assertEquals('two', $doc->value->id, 'An incorrect document was returned');
} }
#[TestDox('byId() succeeds when a document is found with numeric ID')] #[TestDox('byId() succeeds when a document is found with numeric ID')]
@ -95,15 +99,15 @@ class FindTest extends TestCase
{ {
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']); Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
$doc = Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('18', $doc->get()->id, 'An incorrect document was returned'); $this->assertEquals('18', $doc->value->id, 'An incorrect document was returned');
} }
#[TestDox('byId() succeeds when a document is not found')] #[TestDox('byId() succeeds when a document is not found')]
public function testByIdSucceedsWhenADocumentIsNotFound(): void public function testByIdSucceedsWhenADocumentIsNotFound(): void
{ {
$doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
#[TestDox('byFields() succeeds when documents are found')] #[TestDox('byFields() succeeds when documents are 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')]
@ -187,16 +195,16 @@ class FindTest extends TestCase
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
{ {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('two', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('firstByFields() succeeds when multiple documents are found')] #[TestDox('firstByFields() succeeds when multiple documents are found')]
public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void
{ {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertContains($doc->get()->id, ['two', 'four'], 'An incorrect document was returned'); $this->assertContains($doc->value->id, ['two', 'four'], 'An incorrect document was returned');
} }
#[TestDox('firstByFields() succeeds when multiple ordered documents are found')] #[TestDox('firstByFields() succeeds when multiple ordered documents are found')]
@ -204,15 +212,15 @@ class FindTest extends TestCase
{ {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class, $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
orderBy: [Field::named('n:num_value DESC')]); orderBy: [Field::named('n:num_value DESC')]);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals('four', $doc->get()->id, 'The incorrect document was returned'); $this->assertEquals('four', $doc->value->id, 'The incorrect document was returned');
} }
#[TestDox('firstByFields() succeeds when a document is not found')] #[TestDox('firstByFields() succeeds when a document is not found')]
public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void
{ {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned'); $this->assertTrue($doc->isNone, 'There should not have been a document returned');
} }
#[TestDox('firstByContains() fails')] #[TestDox('firstByContains() fails')]

View File

@ -39,8 +39,8 @@ class PatchTest extends TestCase
{ {
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]); Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertEquals(44, $doc->get()->num_value, 'The updated document is not correct'); $this->assertEquals(44, $doc->value->num_value, 'The updated document is not correct');
} }
#[TestDox('byId() succeeds when no document is updated')] #[TestDox('byId() succeeds when no document is updated')]

View File

@ -39,8 +39,8 @@ class RemoveFieldsTest extends TestCase
{ {
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']); RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
$this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); $this->assertTrue($tryDoc->isSome, 'There should have been a document returned');
$doc = $tryDoc->get(); $doc = $tryDoc->value;
$this->assertEquals('', $doc->value, 'Value should have been blank (its default value)'); $this->assertEquals('', $doc->value, 'Value should have been blank (its default value)');
$this->assertNull($doc->sub, 'Sub-document should have been null'); $this->assertNull($doc->sub, 'Sub-document should have been null');
} }
@ -64,8 +64,8 @@ class RemoveFieldsTest extends TestCase
{ {
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']); RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class); $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned'); $this->assertTrue($doc->isSome, 'There should have been a document returned');
$this->assertNull($doc->get()->sub, 'Sub-document should have been null'); $this->assertNull($doc->value->sub, 'Sub-document should have been null');
} }
#[TestDox('byFields() succeeds when a field is not removed')] #[TestDox('byFields() succeeds when a field is not removed')]

View File

@ -22,7 +22,7 @@ class ArrayMapperTest extends TestCase
public function testMapSucceeds(): void public function testMapSucceeds(): void
{ {
$result = ['one' => 2, 'three' => 4, 'eight' => 'five']; $result = ['one' => 2, 'three' => 4, 'eight' => 'five'];
$mapped = (new ArrayMapper())->map($result); $mapped = new ArrayMapper()->map($result);
$this->assertSame($result, $mapped, 'The array mapper should return the parameter given to it'); $this->assertSame($result, $mapped, 'The array mapper should return the parameter given to it');
} }
} }

View File

@ -21,6 +21,6 @@ class CountMapperTest extends TestCase
#[TestDox('map() succeeds')] #[TestDox('map() succeeds')]
public function testMapSucceeds(): void public function testMapSucceeds(): void
{ {
$this->assertEquals(5, (new CountMapper())->map([5, 8, 10]), 'Count not correct'); $this->assertEquals(5, new CountMapper()->map([5, 8, 10]), 'Count not correct');
} }
} }

View File

@ -47,7 +47,7 @@ class DocumentMapperTest extends TestCase
#[TestDox('map() succeeds with valid JSON')] #[TestDox('map() succeeds with valid JSON')]
public function testMapSucceedsWithValidJSON(): void public function testMapSucceedsWithValidJSON(): void
{ {
$doc = (new DocumentMapper(TestDocument::class))->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']); $doc = new DocumentMapper(TestDocument::class)->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']);
$this->assertNotNull($doc, 'The document should not have been null'); $this->assertNotNull($doc, 'The document should not have been null');
$this->assertEquals(7, $doc->id, 'ID not filled correctly'); $this->assertEquals(7, $doc->id, 'ID not filled correctly');
$this->assertNotNull($doc->subDoc, 'The sub-document should not have been null'); $this->assertNotNull($doc->subDoc, 'The sub-document should not have been null');
@ -58,7 +58,7 @@ class DocumentMapperTest extends TestCase
#[TestDox('map() succeeds with valid JSON for Pjson class')] #[TestDox('map() succeeds with valid JSON for Pjson class')]
public function testMapSucceedsWithValidJSONForPjsonClass(): void public function testMapSucceedsWithValidJSONForPjsonClass(): void
{ {
$doc = (new DocumentMapper(PjsonDocument::class))->map(['data' => '{"id":"seven","name":"bob","num_value":8}']); $doc = new DocumentMapper(PjsonDocument::class)->map(['data' => '{"id":"seven","name":"bob","num_value":8}']);
$this->assertNotNull($doc, 'The document should not have been null'); $this->assertNotNull($doc, 'The document should not have been null');
$this->assertEquals(new PjsonId('seven'), $doc->id, 'ID not filled correctly'); $this->assertEquals(new PjsonId('seven'), $doc->id, 'ID not filled correctly');
$this->assertEquals('bob', $doc->name, 'Name not filled correctly'); $this->assertEquals('bob', $doc->name, 'Name not filled correctly');
@ -70,13 +70,13 @@ class DocumentMapperTest extends TestCase
public function testMapFailsWithInvalidJSON(): void public function testMapFailsWithInvalidJSON(): void
{ {
$this->expectException(DocumentException::class); $this->expectException(DocumentException::class);
(new DocumentMapper(TestDocument::class))->map(['data' => 'this is not valid']); new DocumentMapper(TestDocument::class)->map(['data' => 'this is not valid']);
} }
#[TestDox('map() fails with invalid JSON for Pjson class')] #[TestDox('map() fails with invalid JSON for Pjson class')]
public function testMapFailsWithInvalidJSONForPjsonClass(): void public function testMapFailsWithInvalidJSONForPjsonClass(): void
{ {
$this->expectException(DocumentException::class); $this->expectException(DocumentException::class);
(new DocumentMapper(PjsonDocument::class))->map(['data' => 'not even close']); new DocumentMapper(PjsonDocument::class)->map(['data' => 'not even close']);
} }
} }

View File

@ -24,7 +24,7 @@ class ExistsMapperTest extends TestCase
{ {
try { try {
Configuration::overrideMode(Mode::PgSQL); Configuration::overrideMode(Mode::PgSQL);
$this->assertFalse((new ExistsMapper())->map([false, 'nope']), 'Result should have been false'); $this->assertFalse(new ExistsMapper()->map([false, 'nope']), 'Result should have been false');
} finally { } finally {
Configuration::overrideMode(null); Configuration::overrideMode(null);
} }
@ -35,7 +35,7 @@ class ExistsMapperTest extends TestCase
{ {
try { try {
Configuration::overrideMode(Mode::SQLite); Configuration::overrideMode(Mode::SQLite);
$this->assertTrue((new ExistsMapper())->map([1, 'yep']), 'Result should have been true'); $this->assertTrue(new ExistsMapper()->map([1, 'yep']), 'Result should have been true');
} finally { } finally {
Configuration::overrideMode(null); Configuration::overrideMode(null);
} }
@ -46,6 +46,6 @@ class ExistsMapperTest extends TestCase
{ {
$this->expectException(DocumentException::class); $this->expectException(DocumentException::class);
Configuration::overrideMode(null); Configuration::overrideMode(null);
(new ExistsMapper())->map(['0']); new ExistsMapper()->map(['0']);
} }
} }

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);

View File

@ -253,6 +253,25 @@ class QueryTest extends TestCase
'ORDER BY not constructed correctly'); 'ORDER BY not constructed correctly');
} }
#[TestDox('orderBy() succeeds with one qualified field for PostgreSQL')]
public function testOrderBySucceedsWithOneQualifiedFieldForPostgreSQL(): void
{
Configuration::overrideMode(Mode::PgSQL);
$field = Field::named('TestField');
$field->qualifier = 'qual';
$this->assertEquals(" ORDER BY qual.data->>'TestField'", Query::orderBy([$field]),
'ORDER BY not constructed correctly');
}
#[TestDox('orderBy() succeeds with one qualified field for SQLite')]
public function testOrderBySucceedsWithOneQualifiedFieldForSQLite(): void
{
$field = Field::named('TestField');
$field->qualifier = 'qual';
$this->assertEquals(" ORDER BY qual.data->>'TestField'", Query::orderBy([$field]),
'ORDER BY not constructed correctly');
}
#[TestDox('orderBy() succeeds with multiple fields and direction for PostgreSQL')] #[TestDox('orderBy() succeeds with multiple fields and direction for PostgreSQL')]
public function testOrderBySucceedsWithMultipleFieldsAndDirectionForPostgreSQL(): void public function testOrderBySucceedsWithMultipleFieldsAndDirectionForPostgreSQL(): void
{ {