3 Commits

Author SHA1 Message Date
85b461e441 Remove RC limitation; update deps 2024-11-20 22:45:17 -05:00
d896e9a6c6 Migrate tests to Pest 2024-11-17 17:55:26 -05:00
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
21 changed files with 247 additions and 254 deletions

View File

@@ -4,10 +4,14 @@ This library allows SQLite and PostgreSQL to be treated as document databases. I
## 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`
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
### Connection Details

View File

@@ -18,8 +18,8 @@
"docs": "https://bitbadger.solutions/open-source/pdo-document/"
},
"require": {
"php": "8.2 - 8.3",
"bit-badger/inspired-by-fsharp": "^1",
"php": ">=8.4",
"bit-badger/inspired-by-fsharp": "^2",
"netresearch/jsonmapper": "^4",
"ext-pdo": "*"
},

10
composer.lock generated
View File

@@ -4,18 +4,18 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "e36b4a89b7c83383aca991e570a4b3be",
"content-hash": "e412dd4c1121633ca2234a2e4458b7ae",
"packages": [
{
"name": "bit-badger/inspired-by-fsharp",
"version": "v1.0.0",
"version": "v2.0.0",
"source": {
"type": "git",
"url": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp",
"reference": "3cc780956daa410c74667bc9c03fffa19de74e91"
"reference": "c61ff7a831f152f6f9d6cbd1d30604924fa5c83e"
},
"require": {
"php": "8.2 - 8.3"
"php": ">=8.4"
},
"require-dev": {
"pestphp/pest": "^3.5",
@@ -3954,7 +3954,7 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": "8.2 - 8.3",
"php": ">=8.4",
"ext-pdo": "*"
},
"platform-dev": [],

View File

@@ -89,7 +89,7 @@ class Custom
*/
public static function array(string $query, array $parameters, Mapper $mapper): array
{
return iterator_to_array(self::list($query, $parameters, $mapper)->items());
return iterator_to_array(self::list($query, $parameters, $mapper)->items);
}
/**

View File

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

View File

@@ -38,7 +38,7 @@ describe('::list()', function () {
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull();
$count = 0;
foreach ($list->items() as $ignored) $count++;
foreach ($list->items as $ignored) $count++;
expect($count)->toBe(5);
});
test('returns empty list when no data found', function () {
@@ -46,7 +46,7 @@ describe('::list()', function () {
Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric > :value",
[':value' => 100], new DocumentMapper(TestDocument::class)))
->not->toBeNull()
->hasItems()->toBeFalse();
->hasItems->toBeFalse();
});
});
@@ -67,15 +67,14 @@ describe('::array()', function () {
describe('::single()', function () {
test('returns a document when one is found', function () {
expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
new DocumentMapper(TestDocument::class)))
->isSome()->toBeTrue()
->get()->id->toBe('one');
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
new DocumentMapper(TestDocument::class));
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('one');
});
test('returns no document when one is not found', function () {
expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
[':id' => 'eighty'], new DocumentMapper(TestDocument::class)))
->isNone()->toBeTrue();
->isNone->toBeTrue();
});
});

View File

@@ -22,13 +22,13 @@ describe('::create()', function () {
});
});
describe('->items()', function () {
describe('->items', function () {
test('enumerates items in the list', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull();
$count = 0;
foreach ($list->items() as $item) {
foreach ($list->items as $item) {
expect(['one', 'two', 'three', 'four', 'five'])->toContain($item->id);
$count++;
}
@@ -37,29 +37,29 @@ describe('->items()', function () {
test('fails when the list is exhausted', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
$ignored = iterator_to_array($list->items());
expect($list)->hasItems()->toBeFalse()
->and(fn () => iterator_to_array($list->items()))->toThrow(DocumentException::class);
expect($list)->not->toBeNull()->hasItems->toBeTrue();
$ignored = iterator_to_array($list->items);
expect($list)->hasItems->toBeFalse()
->and(fn () => iterator_to_array($list->items))->toThrow(DocumentException::class);
});
});
describe('->hasItems()', function () {
describe('->hasItems', function () {
test('returns false when no items are in the list', function () {
expect(DocumentList::create(
Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric < 0", [],
new DocumentMapper(TestDocument::class)))
->not->toBeNull()
->hasItems()->toBeFalse();
->hasItems->toBeFalse();
});
test('returns true when items are in the list', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
foreach ($list->items() as $ignored) {
expect($list)->hasItems()->toBeTrue();
expect($list)->not->toBeNull()->hasItems->toBeTrue();
foreach ($list->items as $ignored) {
expect($list)->hasItems->toBeTrue();
}
expect($list)->hasItems()->toBeFalse();
expect($list)->hasItems->toBeFalse();
});
});
@@ -67,7 +67,7 @@ describe('->map()', function () {
test('transforms the list', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
expect($list)->not->toBeNull()->hasItems->toBeTrue();
foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) {
expect(['eno', 'owt', 'eerht', 'ruof', 'evif'])->toContain($mapped);
}
@@ -78,7 +78,7 @@ describe('->iter()', function () {
test('walks the list', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
expect($list)->not->toBeNull()->hasItems->toBeTrue();
$splats = [];
$list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); });
expect(implode(' ', $splats))->toBe('*** *** ***** **** ****');
@@ -89,7 +89,7 @@ describe('->mapToArray()', function () {
test('creates an associative array', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue()
expect($list)->not->toBeNull()->hasItems->toBeTrue()
->and($list->mapToArray(fn($it) => $it->id, fn($it) => $it->value))
->toBe(['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']);
});

View File

@@ -18,14 +18,14 @@ describe('::insert()', function () {
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
expect($tryDoc)
->isSome()->toBeTrue()
->and($tryDoc->get())
->isSome->toBeTrue()
->and($tryDoc->value)
->id->toBe('turkey')
->value->toBe('')
->num_value->toBe(0)
->sub->not->toBeNull()
->sub->foo->toBe('gobble')
->sub->bar->toBe('gobble');
->sub->bar->toBe('gobble')
->and($tryDoc->value->value)->toBe('');
});
test('inserts an array with auto-number ID, not provided', function () {
Configuration::$autoId = AutoId::Number;
@@ -34,15 +34,15 @@ describe('::insert()', function () {
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
expect($doc)->isSome()->toBeTrue()
->and(json_decode($doc->get()['data']))
expect($doc)->isSome->toBeTrue()
->and(json_decode($doc->value['data']))
->id->toBe(1);
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE " . Query::whereById(docId: 2),
[':id' => 2], new ArrayMapper());
expect($doc)->isSome()->toBeTrue()
->and(json_decode($doc->get()['data']))
expect($doc)->isSome->toBeTrue()
->and(json_decode($doc->value['data']))
->id->toBe(2);
} finally {
Configuration::$autoId = AutoId::None;
@@ -54,8 +54,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
expect($doc)->isSome()->toBeTrue()
->and(json_decode($doc->get()['data']))
expect($doc)->isSome->toBeTrue()
->and(json_decode($doc->value['data']))
->id->toBe(7);
} finally {
Configuration::$autoId = AutoId::None;
@@ -69,8 +69,8 @@ describe('::insert()', function () {
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class);
expect($doc)
->isSome()->toBeTrue()
->and($doc->get())->id->not->toBeEmpty();
->isSome->toBeTrue()
->and($doc->value)->id->not->toBeEmpty();
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -82,8 +82,8 @@ describe('::insert()', function () {
$uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->toBe($uuid);
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->toBe($uuid);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -95,8 +95,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->toHaveLength(6);
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->toHaveLength(6);
} finally {
Configuration::$autoId = AutoId::None;
Configuration::$idStringLength = 16;
@@ -108,8 +108,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->toBe('my-key');
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->toBe('my-key');
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -117,8 +117,8 @@ describe('::insert()', function () {
test('inserts an object with no automatic ID', function () {
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue();
$doc = $tryDoc->get();
expect($tryDoc)->isSome->toBeTrue();
$doc = $tryDoc->value;
expect($doc)
->id->toBe('turkey')
->num_value->toBe(0)
@@ -134,13 +134,13 @@ describe('::insert()', function () {
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->toBe(1);
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->toBe(1);
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->toBe(2);
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->toBe(2);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -151,8 +151,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->toBe(64);
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->toBe(64);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -163,8 +163,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->not->toBeEmpty();
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->not->toBeEmpty();
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -176,8 +176,8 @@ describe('::insert()', function () {
$uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->toBe($uuid);
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->toBe($uuid);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -189,8 +189,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->toHaveLength(40);
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->toHaveLength(40);
} finally {
Configuration::$autoId = AutoId::None;
Configuration::$idStringLength = 16;
@@ -202,8 +202,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
expect($doc)->isSome()->toBeTrue()
->and($doc->get())->id->toBe('my-key');
expect($doc)->isSome->toBeTrue()
->and($doc->value)->id->toBe('my-key');
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -217,13 +217,13 @@ describe('::insert()', function () {
describe('::save()', function () {
test('inserts a new document', function () {
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))->isSome()->toBeTrue();
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))->isSome->toBeTrue();
});
test('updates an existing document', function () {
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue()
->and($tryDoc->get())
expect($tryDoc)->isSome->toBeTrue()
->and($tryDoc->value)
->num_value->toBe(44)
->sub->toBeNull();
});
@@ -233,8 +233,8 @@ describe('::update()', function () {
test('replaces an existing document', function () {
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue();
$doc = $tryDoc->get();
expect($tryDoc)->isSome->toBeTrue();
$doc = $tryDoc->value;
expect($doc)
->num_value->toBe(8)
->sub->not->toBeNull()
@@ -244,6 +244,6 @@ describe('::update()', function () {
});
test('does nothing for a non-existent document', function () {
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
expect(Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class))->isNone()->toBeTrue();
expect(Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class))->isNone->toBeTrue();
});
});

View File

@@ -17,7 +17,7 @@ describe('::all()', function () {
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(5);
});
test('sorts data ascending', function () {
@@ -43,25 +43,23 @@ describe('::all()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
expect(Find::all(ThrowawayDb::TABLE, TestDocument::class))
->not->toBeNull()
->hasItems()->toBeFalse();
->hasItems->toBeFalse();
});
});
describe('::byId()', function () {
test('retrieves a document via string ID', function () {
expect(Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class))
->isSome()->toBeTrue()
->get()->id->toBe('two');
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
});
test('retrieves a document via numeric ID', function () {
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]);
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
expect(Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class))
->isSome()->toBeTrue()
->get()->id->toBe(18);
$doc = Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe(18);
});
test('returns None when a document is not found', function () {
expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone()->toBeTrue();
expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone->toBeTrue();
});
});
@@ -71,7 +69,7 @@ describe('::byFields()', function () {
TestDocument::class, FieldMatch::All);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(1);
});
test('retrieves ordered matching documents', function () {
@@ -84,13 +82,13 @@ describe('::byFields()', function () {
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(1);
});
test('returns an empty list when no matching documents are found', function () {
expect(Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class))
->not->toBeNull()
->hasItems()->toBeFalse();
->hasItems->toBeFalse();
});
test('retrieves documents matching an inArray condition', function () {
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
@@ -99,7 +97,7 @@ describe('::byFields()', function () {
ArrayDocument::class);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(2);
});
test('returns an empty list when no documents match an inArray condition', function () {
@@ -108,7 +106,7 @@ describe('::byFields()', function () {
expect(Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])],
ArrayDocument::class))
->not->toBeNull()
->hasItems()->toBeFalse();
->hasItems->toBeFalse();
});
});
@@ -117,7 +115,7 @@ describe('::byContains()', function () {
$docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(2);
});
test('retrieves ordered matching documents', function () {
@@ -130,7 +128,7 @@ describe('::byContains()', function () {
test('returns an empty list when no documents match', function () {
expect(Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class))
->not->toBeNull()
->hasItems()->toBeFalse();
->hasItems->toBeFalse();
});
});
@@ -139,7 +137,7 @@ describe('::byJsonPath()', function () {
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(2);
});
test('retrieves ordered matching documents', function () {
@@ -151,66 +149,66 @@ describe('::byJsonPath()', function () {
test('returns an empty list when no documents match', function () {
expect(Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class))
->not->toBeNull()
->hasItems()->toBeFalse();
->hasItems->toBeFalse();
});
});
describe('::firstByFields()', function () {
test('retrieves a matching document', function () {
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe('two');
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
});
test('retrieves a document for multiple results', function () {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
expect($doc)->isSome()->toBeTrue()->and(['two', 'four'])->toContain($doc->get()->id);
expect($doc)->isSome->toBeTrue()->and(['two', 'four'])->toContain($doc->value->id);
});
test('retrieves a document for multiple ordered results', function () {
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
orderBy: [Field::named('n:num_value DESC')]))
->isSome()->toBeTrue()->get()->id->toBe('four');
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
orderBy: [Field::named('n:num_value DESC')]);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('four');
});
test('returns None when no documents match', function () {
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class))
->isNone()->toBeTrue();
->isNone->toBeTrue();
});
});
describe('::firstByContains()', function () {
test('retrieves a matching document', function () {
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe('one');
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('one');
});
test('retrieves a document for multiple results', function () {
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
expect($doc)->isSome()->toBeTrue()->and(['four', 'five'])->toContain($doc->get()->id);
expect($doc)->isSome->toBeTrue()->and(['four', 'five'])->toContain($doc->value->id);
});
test('retrieves a document for multiple ordered results', function () {
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class,
[Field::named('sub.bar NULLS FIRST')]))
->isSome()->toBeTrue()->get()->id->toBe('five');
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class,
[Field::named('sub.bar NULLS FIRST')]);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('five');
});
test('returns None when no documents match', function () {
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class))
->isNone()->toBeTrue();
->isNone->toBeTrue();
});
});
describe('::firstByJsonPath()', function () {
test('retrieves a matching document', function () {
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe('two');
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
});
test('retrieves a document for multiple results', function () {
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
expect($doc)->isSome()->toBeTrue()->and(['four', 'five'])->toContain($doc->get()->id);
expect($doc)->isSome->toBeTrue()->and(['four', 'five'])->toContain($doc->value->id);
});
test('retrieves a document for multiple ordered results', function () {
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
[Field::named('id DESC')]))
->isSome()->toBeTrue()->get()->id->toBe('four');
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
[Field::named('id DESC')]);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('four');
});
test('returns None when no documents match', function () {
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class))
->isNone()->toBeTrue();
->isNone->toBeTrue();
});
});

View File

@@ -15,9 +15,8 @@ pest()->group('integration', 'postgresql');
describe('::byId()', function () {
test('updates an existing document', function () {
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))
->isSome()->toBeTrue()
->get()->num_value->toBe(44);
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->num_value->toBe(44);
});
test('does nothing when a document does not exist', function () {
$id = 'forty-seven';
@@ -42,8 +41,8 @@ describe('::byContains()', function () {
test('updates existing documents', function () {
Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]);
$tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue()
->and($tryDoc->get())
expect($tryDoc)->isSome->toBeTrue()
->and($tryDoc->value)
->id->toBe('two')
->num_value->toBe(12);
});
@@ -58,8 +57,8 @@ describe('::byJsonPath()', function () {
test('updates existing documents', function () {
Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']);
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
expect($docs)->not->toBeNull()->hasItems()->toBeTrue();
foreach ($docs->items() as $item) {
expect($docs)->not->toBeNull()->hasItems->toBeTrue();
foreach ($docs->items as $item) {
expect(['four', 'five'])->toContain($item->id)
->and($item->value)->toBe('blue');
}

View File

@@ -16,8 +16,8 @@ describe('::byId()', function () {
test('removes fields', function () {
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue();
$doc = $tryDoc->get();
expect($tryDoc)->isSome->toBeTrue();
$doc = $tryDoc->value;
expect($doc)->sub->toBeNull()
->and($doc->value)->toBeEmpty();
});
@@ -34,9 +34,8 @@ describe('::byId()', function () {
describe('::byFields()', function () {
test('removes fields from matching documents', function () {
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class))
->isSome()->toBeTrue()
->get()->sub->toBeNull();
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->sub->toBeNull();
});
test('does nothing when the field to remove does not exist', function () {
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('nada')]))->toBeFalse();
@@ -53,8 +52,8 @@ describe('::byContains()', function () {
$criteria = ['sub' => ['foo' => 'green']];
RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']);
$docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class);
expect($docs)->not->toBeNull()->hasItems()->toBeTrue();
foreach ($docs->items() as $item) {
expect($docs)->not->toBeNull()->hasItems->toBeTrue();
foreach ($docs->items as $item) {
expect(['two', 'four'])->toContain($item->id)
->and($item->value)->toBeEmpty();
}
@@ -74,8 +73,8 @@ describe('::byJsonPath()', function () {
$path = '$.value ? (@ == "purple")';
RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']);
$docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class);
expect($docs)->not->toBeNull()->hasItems()->toBeTrue();
foreach ($docs->items() as $item) {
expect($docs)->not->toBeNull()->hasItems->toBeTrue();
foreach ($docs->items as $item) {
expect(['four', 'five'])->toContain($item->id)
->and($item->sub)->toBeNull();
}

View File

@@ -38,14 +38,14 @@ describe('::list()', function () {
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull();
$count = 0;
foreach ($list->items() as $ignored) $count++;
foreach ($list->items as $ignored) $count++;
expect($count)->toBe(5);
});
test('returns empty list when not data is found', function () {
expect(Custom::list(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' > :value",
[':value' => 100], new DocumentMapper(TestDocument::class)))
->not->toBeNull()
->hasItems()->toBeFalse();
->hasItems->toBeFalse();
});
});
@@ -65,14 +65,14 @@ describe('::array()', function () {
describe('::single()', function () {
test('returns a document when one is found', function () {
expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
new DocumentMapper(TestDocument::class)))
->isSome()->toBeTrue()->get()->id->toBe('one');
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
new DocumentMapper(TestDocument::class));
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('one');
});
test('returns no document when none is found', function () {
expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
[':id' => 'eighty'], new DocumentMapper(TestDocument::class)))
->isNone()->toBeTrue();
->isNone->toBeTrue();
});
});

View File

@@ -22,13 +22,13 @@ describe('::create()', function () {
});
});
describe('->items()', function () {
describe('->items', function () {
test('enumerates items in the list', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull();
$count = 0;
foreach ($list->items() as $item) {
foreach ($list->items as $item) {
expect(['one', 'two', 'three', 'four', 'five'])->toContain($item->id);
$count++;
}
@@ -37,27 +37,27 @@ describe('->items()', function () {
test('fails when the list is exhausted', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
$ignored = iterator_to_array($list->items());
expect($list)->hasItems()->toBeFalse()
->and(fn () => iterator_to_array($list->items()))->toThrow(DocumentException::class);
expect($list)->not->toBeNull()->hasItems->toBeTrue();
$ignored = iterator_to_array($list->items);
expect($list)->hasItems->toBeFalse()
->and(fn () => iterator_to_array($list->items))->toThrow(DocumentException::class);
});
});
describe('->hasItems()', function () {
describe('->hasItems', function () {
test('returns true when items exist', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
foreach ($list->items() as $ignored) {
expect($list)->hasItems()->toBeTrue();
expect($list)->not->toBeNull()->hasItems->toBeTrue();
foreach ($list->items as $ignored) {
expect($list)->hasItems->toBeTrue();
}
expect($list)->hasItems()->toBeFalse();
expect($list)->hasItems->toBeFalse();
});
test('returns false when no items exist', function () {
expect(DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' < 0", [],
new DocumentMapper(TestDocument::class)))
->not->toBeNull()->hasItems()->toBeFalse();
->not->toBeNull()->hasItems->toBeFalse();
});
});
@@ -65,7 +65,7 @@ describe('->map()', function () {
test('transforms the list', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
expect($list)->not->toBeNull()->hasItems->toBeTrue();
foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) {
expect(['eno', 'owt', 'eerht', 'ruof', 'evif'])->toContain($mapped);
}
@@ -76,7 +76,7 @@ describe('->iter()', function () {
test('walks the list', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
expect($list)->not->toBeNull()->hasItems->toBeTrue();
$splats = [];
$list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); });
expect(implode(' ', $splats))->toBe('*** *** ***** **** ****');
@@ -87,7 +87,7 @@ describe('->mapToArray()', function () {
test('creates an associative array', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue()
expect($list)->not->toBeNull()->hasItems->toBeTrue()
->and($list->mapToArray(fn($it) => $it->id, fn($it) => $it->value))
->toBe(['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']);
});

View File

@@ -17,14 +17,14 @@ describe('::insert()', function () {
test('inserts an array with no automatic ID', function () {
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue()
->and($tryDoc->get())
expect($tryDoc)->isSome->toBeTrue()
->and($tryDoc->value)
->id->toBe('turkey')
->num_value->toBe(0)
->sub->not->toBeNull()
->sub->foo->toBe('gobble')
->sub->bar->toBe('gobble')
->and($tryDoc->get()->value)->toBeEmpty();
->and($tryDoc->value->value)->toBeEmpty();
});
test('inserts an array with auto-number ID, not provided', function () {
Configuration::$autoId = AutoId::Number;
@@ -33,14 +33,14 @@ describe('::insert()', function () {
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
expect($doc)->isSome()->toBeTrue()
->and(json_decode($doc->get()['data']))->id->toBe(1);
expect($doc)->isSome->toBeTrue()
->and(json_decode($doc->value['data']))->id->toBe(1);
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = 2", [],
new ArrayMapper());
expect($doc)->isSome()->toBeTrue()
->and(json_decode($doc->get()['data']))->id->toBe(2);
expect($doc)->isSome->toBeTrue()
->and(json_decode($doc->value['data']))->id->toBe(2);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -51,8 +51,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
expect($doc)->isSome()->toBeTrue()
->and(json_decode($doc->get()['data']))->id->toBe(7);
expect($doc)->isSome->toBeTrue()
->and(json_decode($doc->value['data']))->id->toBe(7);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -62,8 +62,8 @@ describe('::insert()', function () {
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class))
->isSome()->toBeTrue()->get()->id->not->toBeEmpty();
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->not->toBeEmpty();
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -74,8 +74,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe($uuid);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe($uuid);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -86,8 +86,8 @@ describe('::insert()', function () {
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class))
->isSome()->toBeTrue()->get()->id->toHaveLength(6);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toHaveLength(6);
} finally {
Configuration::$autoId = AutoId::None;
Configuration::$idStringLength = 16;
@@ -98,8 +98,8 @@ describe('::insert()', function () {
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe('my-key');
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('my-key');
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -107,14 +107,14 @@ describe('::insert()', function () {
test('inserts an object with no automatic ID', function () {
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue()
->and($tryDoc->get())
expect($tryDoc)->isSome->toBeTrue()
->and($tryDoc->value)
->id->toBe('turkey')
->num_value->toBe(0)
->sub->not->toBeNull()
->sub->foo->toBe('gobble')
->sub->bar->toBe('gobble')
->and($tryDoc->get()->value)->toBeEmpty();
->and($tryDoc->value->value)->toBeEmpty();
});
test('inserts an object with auto-number ID, not provided', function () {
Configuration::$autoId = AutoId::Number;
@@ -122,12 +122,12 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class))
->isSome()->toBeTrue()->get()->id->toBe(1);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe(1);
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class))
->isSome()->toBeTrue()->get()->id->toBe(2);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe(2);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -137,8 +137,8 @@ describe('::insert()', function () {
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class))
->isSome()->toBeTrue()->get()->id->toBe(64);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe(64);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -148,8 +148,8 @@ describe('::insert()', function () {
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class))
->isSome()->toBeTrue()->get()->id->not->toBeEmpty();
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->not->toBeEmpty();
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -160,8 +160,8 @@ describe('::insert()', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe($uuid);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe($uuid);
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -172,8 +172,8 @@ describe('::insert()', function () {
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class))
->isSome()->toBeTrue()->get()->id->toHaveLength(40);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toHaveLength(40);
} finally {
Configuration::$autoId = AutoId::None;
Configuration::$idStringLength = 16;
@@ -184,8 +184,8 @@ describe('::insert()', function () {
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe('my-key');
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('my-key');
} finally {
Configuration::$autoId = AutoId::None;
}
@@ -199,13 +199,13 @@ describe('::insert()', function () {
describe('::save()', function () {
test('inserts a new document', function () {
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))->isSome()->toBeTrue();
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))->isSome->toBeTrue();
});
test('updates an existing document', function () {
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue()
->and($tryDoc->get())
expect($tryDoc)->isSome->toBeTrue()
->and($tryDoc->value)
->num_value->toBe(44)
->sub->toBeNull();
});
@@ -215,17 +215,17 @@ describe('::update()', function () {
test('replaces an existing document', function () {
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue()
->and($tryDoc->get())
expect($tryDoc)->isSome->toBeTrue()
->and($tryDoc->value)
->num_value->toBe(8)
->sub->not->toBeNull()
->sub->foo->toBe('y')
->sub->bar->toBe('z')
->and($tryDoc->get()->value)->toBe('howdy');
->and($tryDoc->value->value)->toBe('howdy');
});
test('does nothing for a non-existent document', function () {
expect(Exists::byId(ThrowawayDb::TABLE, 'two-hundred'))->toBeFalse();
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
expect(Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class))->isNone()->toBeTrue();
expect(Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class))->isNone->toBeTrue();
});
});

View File

@@ -17,7 +17,7 @@ describe('::all()', function () {
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(5);
});
test('sorts data ascending', function () {
@@ -42,22 +42,22 @@ describe('::all()', function () {
test('returns an empty list when no data exists', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
expect(Find::all(ThrowawayDb::TABLE, TestDocument::class))
->not->toBeNull()->hasItems()->toBeFalse();
->not->toBeNull()->hasItems->toBeFalse();
});
});
describe('::byId()', function () {
test('returns a document when it exists', function () {
expect(Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe('two');
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
});
test('returns a document with a numeric ID', function () {
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
expect(Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe('18');
$doc = Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('18');
});
test('returns None when no document exists', function () {
expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone()->toBeTrue();
expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone->toBeTrue();
});
});
@@ -67,7 +67,7 @@ describe('::byFields()', function () {
TestDocument::class, FieldMatch::All);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(1);
});
test('returns ordered matching documents', function () {
@@ -80,12 +80,12 @@ describe('::byFields()', function () {
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(1);
});
test('returns empty list when no documents match', function () {
expect(Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class))
->not->toBeNull()->hasItems()->toBeFalse();
->not->toBeNull()->hasItems->toBeFalse();
});
test('returns matching documents for inArray comparison', function () {
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
@@ -94,7 +94,7 @@ describe('::byFields()', function () {
ArrayDocument::class);
expect($docs)->not->toBeNull();
$count = 0;
foreach ($docs->items() as $ignored) $count++;
foreach ($docs->items as $ignored) $count++;
expect($count)->toBe(2);
});
test('returns empty list when no documents match inArray comparison', function () {
@@ -102,7 +102,7 @@ describe('::byFields()', function () {
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
expect(Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])],
ArrayDocument::class))
->not->toBeNull()->hasItems()->toBeFalse();
->not->toBeNull()->hasItems->toBeFalse();
});
});
@@ -120,21 +120,21 @@ describe('::byJsonPath()', function () {
describe('::firstByFields()', function () {
test('returns a matching document', function () {
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe('two');
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
});
test('returns one of several matching documents', function () {
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
expect($doc)->isSome()->toBeTrue()->and(['two', 'four'])->toContain($doc->get()->id);
expect($doc)->isSome->toBeTrue()->and(['two', 'four'])->toContain($doc->value->id);
});
test('returns first of ordered matching documents', function () {
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
orderBy: [Field::named('n:num_value DESC')]))
->isSome()->toBeTrue()->get()->id->toBe('four');
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
orderBy: [Field::named('n:num_value DESC')]);
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('four');
});
test('returns None when no documents match', function () {
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class))
->isNone()->toBeTrue();
->isNone->toBeTrue();
});
});

View File

@@ -15,8 +15,8 @@ pest()->group('integration', 'sqlite');
describe('::byId()', function () {
test('updates an existing document', function () {
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))
->isSome()->toBeTrue()->get()->num_value->toBe(44);
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->num_value->toBe(44);
});
test('does nothing when no document exists', function () {
expect(Exists::byId(ThrowawayDb::TABLE, 'forty-seven'))->toBeFalse();

View File

@@ -16,9 +16,9 @@ describe('::byId()', function () {
test('updates an existing document', function () {
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
expect($tryDoc)->isSome()->toBeTrue()
->and($tryDoc->get())->sub->toBeNull()
->and($tryDoc->get()->value)->toBeEmpty();
expect($tryDoc)->isSome->toBeTrue()
->and($tryDoc->value)->sub->toBeNull()
->and($tryDoc->value->value)->toBeEmpty();
});
test('does nothing when the field to remove does not exist', function () {
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('a_field_that_does_not_exist')]))->toBeFalse();
@@ -33,8 +33,8 @@ describe('::byId()', function () {
describe('::byFields()', function () {
test('updates matching documents', function () {
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class))
->isSome()->toBeTrue()->get()->sub->toBeNull();
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class);
expect($doc)->isSome->toBeTrue()->and($doc->value)->sub->toBeNull();
});
test('does nothing when the field to remove does not exist', function () {
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('nada')]))->toBeFalse();

View File

@@ -13,6 +13,6 @@ pest()->group('unit');
describe('->map()', function () {
test('returns the given array', function () {
$result = ['one' => 2, 'three' => 4, 'eight' => 'five'];
expect((new ArrayMapper())->map($result))->toBe($result);
expect(new ArrayMapper()->map($result))->toBe($result);
});
});

View File

@@ -12,6 +12,6 @@ pest()->group('unit');
describe('->map()', function () {
test('returns item 0 in the given array', function () {
expect((new CountMapper())->map([5, 8, 10]))->toBe(5);
expect(new CountMapper()->map([5, 8, 10]))->toBe(5);
});
});

View File

@@ -35,7 +35,7 @@ describe('Constructor', function () {
describe('->map()', function () {
test('deserializes valid JSON', function () {
$doc = (new DocumentMapper(DocMapTestDoc::class))
$doc = new DocumentMapper(DocMapTestDoc::class)
->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']);
expect($doc)
->not->toBeNull()
@@ -46,7 +46,7 @@ describe('->map()', function () {
->name->toBe('tester');
});
test('deserializes valid JSON [Pjson]', function () {
$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}']);
expect($doc)
->not->toBeNull()
->id->toEqual(new PjsonId('seven'))
@@ -55,11 +55,11 @@ describe('->map()', function () {
->skipped->toBeEmpty();
});
test('throws for invalid JSON', function () {
expect(fn() => (new DocumentMapper(DocMapTestDoc::class))->map(['data' => 'this is not valid']))
expect(fn() => new DocumentMapper(DocMapTestDoc::class)->map(['data' => 'this is not valid']))
->toThrow(DocumentException::class);
});
test('throws for invalid JSON [Pjson]', function () {
expect(fn() => (new DocumentMapper(PjsonDocument::class))->map(['data' => 'not even close']))
expect(fn() => new DocumentMapper(PjsonDocument::class)->map(['data' => 'not even close']))
->toThrow(DocumentException::class);
});
});

View File

@@ -16,13 +16,13 @@ afterEach(function () { Configuration::overrideMode(null); });
describe('->map()', function () {
test('returns a boolean value from index 0 [PostgreSQL]', function () {
Configuration::overrideMode(Mode::PgSQL);
expect((new ExistsMapper())->map([false, 'nope']))->toBeFalse();
expect(new ExistsMapper()->map([false, 'nope']))->toBeFalse();
});
test('returns a number value as boolean from index 0 [SQLite]', function () {
Configuration::overrideMode(Mode::SQLite);
expect((new ExistsMapper())->map([1, 'yep']))->toBeTrue();
expect(new ExistsMapper()->map([1, 'yep']))->toBeTrue();
});
test('throws if mode is not set', function () {
expect(fn() => (new ExistsMapper())->map(['0']))->toThrow(DocumentException::class);
expect(fn() => new ExistsMapper()->map(['0']))->toThrow(DocumentException::class);
});
});