pdo-document/tests/Integration/SQLite/CustomTest.php
2024-11-17 21:30:53 +00:00

95 lines
3.6 KiB
PHP

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{Count, Custom, DocumentException, Query};
use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper};
use Test\Integration\SQLite\ThrowawayDb;
use Test\Integration\TestDocument;
pest()->group('integration', 'sqlite');
describe('::runQuery()', function () {
test('runs a valid query successfully', function () {
$stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []);
try {
expect($stmt)->not->toBeNull();
} finally {
$stmt = null;
}
});
test('fails with an invalid query', function () {
$stmt = null;
try {
expect(function () use (&$stmt) { $stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []); })
->toThrow(DocumentException::class);
} finally {
$stmt = null;
}
});
});
describe('::list()', function () {
test('returns non-empty list when data is found', function () {
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull();
$count = 0;
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();
});
});
describe('::array()', function () {
test('returns non-empty array when data is found', function () {
expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [],
new DocumentMapper(TestDocument::class)))
->not->toBeNull()->toHaveCount(2);
});
test('returns empty array when data is not found', function () {
expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value",
[':value' => 'not there'], new DocumentMapper(TestDocument::class)))
->not->toBeNull()->toBeEmpty();
});
});
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');
});
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();
});
});
describe('::nonQuery()', function () {
test('works when documents match the WHERE clause', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
expect(Count::all(ThrowawayDb::TABLE))->toBe(0);
});
test('works when no documents match the WHERE clause', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE data->>'num_value' > :value", [':value' => 100]);
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
});
});
describe('::scalar()', function () {
test('returns a scalar value', function () {
expect(Custom::scalar("SELECT 5 AS it", [], new CountMapper()))->toBe(5);
});
});