220 lines
10 KiB
PHP

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{Custom, Delete, Document, Field, FieldMatch, Json};
use Test\Integration\{ArrayDocument, NumDocument, TestDocument};
use Test\Integration\PostgreSQL\ThrowawayDb;
pest()->group('integration', 'postgresql');
/**
* Expect document ordering by verifying the index of IDs against others
*
* @param string $json The JSON string to be searched
* @param array $ids The IDs to be verified
*/
function expect_doc_order(string $json, array $ids): void
{
for ($idx = 0; $idx < sizeof($ids) - 1; $idx++) {
expect(strpos($json, '"' . $ids[$idx] . '",'))
->toBeLessThan(strpos($json, '"' . $ids[$idx + 1] . '",'),
"ID $ids[$idx] should have occurred before ID {$ids[$idx + 1]} in JSON $json");
}
}
describe('::all()', function () {
test('retrieves data', function () {
expect(Json::all(ThrowawayDb::TABLE))
->toContain('{"id": "one",')
->toContain('{"id": "two",')
->toContain('{"id": "three",')
->toContain('{"id": "four",')
->toContain('{"id": "five",');
});
test('sorts data ascending', function () {
expect_doc_order(Json::all(ThrowawayDb::TABLE, [Field::named('id')]), ['five', 'four', 'one', 'three', 'two']);
});
test('sorts data descending', function () {
expect_doc_order(Json::all(ThrowawayDb::TABLE, [Field::named('id DESC')]),
['two', 'three', 'one', 'four', 'five']);
});
test('sorts data numerically', function () {
expect_doc_order(
Json::all(ThrowawayDb::TABLE, [Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]),
['two', 'four', 'one', 'three', 'five']);
});
test('retrieves empty results', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
expect(Json::all(ThrowawayDb::TABLE))->toBe('[]');
});
});
describe('::byId()', function () {
test('retrieves a document via string ID', function () {
expect(Json::byId(ThrowawayDb::TABLE, 'two'))->toStartWith('{')->toContain('"id": "two",')->toEndWith('}');
});
test('retrieves a document via numeric ID', function () {
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]);
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
expect(Json::byId(ThrowawayDb::TABLE, 18))->toStartWith('{')->toContain('"id": 18,')->toEndWith('}');
});
test('returns "{}" when a document is not found', function () {
expect(Json::byId(ThrowawayDb::TABLE, 'seventy-five'))->toBe('{}');
});
});
//describe('::byFields()', function () {
// test('retrieves matching documents', function () {
// $docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
// TestDocument::class, FieldMatch::All);
// expect($docs)->not->toBeNull();
// $count = 0;
// foreach ($docs->items as $ignored) $count++;
// expect($count)->toBe(1);
// });
// test('retrieves ordered matching documents', function () {
// $docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
// FieldMatch::All, [Field::named('id')]);
// expect($docs)->not->toBeNull()
// ->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']);
// });
// test('retrieves documents matching a numeric IN clause', 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++;
// 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();
// });
// test('retrieves documents matching an inArray condition', function () {
// Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
// foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
// $docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])],
// ArrayDocument::class);
// expect($docs)->not->toBeNull();
// $count = 0;
// foreach ($docs->items as $ignored) $count++;
// expect($count)->toBe(2);
// });
// test('returns an empty list when no documents match an inArray condition', function () {
// Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
// 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();
// });
//});
//
//describe('::byContains()', function () {
// test('retrieves matching documents', function () {
// $docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
// expect($docs)->not->toBeNull();
// $count = 0;
// foreach ($docs->items as $ignored) $count++;
// expect($count)->toBe(2);
// });
// test('retrieves ordered matching documents', function () {
// $docs = Find::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], TestDocument::class,
// [Field::named('value')]);
// expect($docs)
// ->not->toBeNull()
// ->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['two', 'four']);
// });
// test('returns an empty list when no documents match', function () {
// expect(Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class))
// ->not->toBeNull()
// ->hasItems->toBeFalse();
// });
//});
//
//describe('::byJsonPath()', function () {
// test('retrieves matching documents', function () {
// $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
// expect($docs)->not->toBeNull();
// $count = 0;
// foreach ($docs->items as $ignored) $count++;
// expect($count)->toBe(2);
// });
// test('retrieves ordered matching documents', function () {
// $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
// [Field::named('id')]);
// expect($docs)->not->toBeNull()
// ->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']);
// });
// test('returns an empty list when no documents match', function () {
// expect(Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class))
// ->not->toBeNull()
// ->hasItems->toBeFalse();
// });
//});
//
//describe('::firstByFields()', function () {
// test('retrieves a matching document', function () {
// $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->value->id);
// });
// test('retrieves a document for multiple ordered results', function () {
// $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();
// });
//});
//
//describe('::firstByContains()', function () {
// test('retrieves a matching document', function () {
// $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->value->id);
// });
// test('retrieves a document for multiple ordered results', function () {
// $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();
// });
//});
//
//describe('::firstByJsonPath()', function () {
// test('retrieves a matching document', function () {
// $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->value->id);
// });
// test('retrieves a document for multiple ordered results', function () {
// $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();
// });
//});