pdo-document/tests/Unit/Query/FindTest.php
2024-11-17 21:30:53 +00:00

54 lines
2.0 KiB
PHP

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{Configuration, DocumentException, Field, FieldMatch, Mode};
use BitBadger\PDODocument\Query\Find;
pest()->group('unit');
afterEach(function () { Configuration::overrideMode(null); });
describe('::byId()', function () {
test('generates correct SQL', function () {
Configuration::overrideMode(Mode::SQLite);
expect(Find::byId('here'))->toBe("SELECT data FROM here WHERE data->>'id' = :id");
});
});
describe('::byFields()', function () {
test('generates correct SQL', function () {
Configuration::overrideMode(Mode::SQLite);
expect(Find::byFields('there', [Field::equal('active', true, ':act'), Field::equal('locked', true, ':lock')],
FieldMatch::Any))
->toBe("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock");
});
});
describe('::byContains()', function () {
test('generates correct SQL [PostgreSQL]', function () {
Configuration::overrideMode(Mode::PgSQL);
expect(Find::byContains('disc'))->toBe('SELECT data FROM disc WHERE data @> :criteria');
})->group('postgresql');
test('throws an exception [SQLite]', function () {
Configuration::overrideMode(Mode::SQLite);
expect(fn () => Find::byContains(''))->toThrow(DocumentException::class);
})->group('sqlite');
});
describe('::byJsonPath()', function () {
test('generates correct SQL [PostgreSQL]', function () {
Configuration::overrideMode(Mode::PgSQL);
expect(Find::byJsonPath('light'))
->toBe('SELECT data FROM light WHERE jsonb_path_exists(data, :path::jsonpath)');
})->group('postgresql');
test('throws an exception [SQLite]', function () {
Configuration::overrideMode(Mode::SQLite);
expect(fn () => Find::byJsonPath(''))->toThrow(DocumentException::class);
})->group('sqlite');
});