pdo-document/tests/Unit/ParametersTest.php

86 lines
3.6 KiB
PHP
Raw Normal View History

2024-10-05 19:43:41 +00:00
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters};
use Test\{PjsonDocument, PjsonId};
pest()->group('unit');
describe('::id()', function () {
test('creates string ID parameter', function () {
expect(Parameters::id('key'))->toEqual([':id' => 'key']);
});
test('creates string from numeric ID parameter', function () {
expect(Parameters::id(7))->toEqual([':id' => '7']);
});
});
describe('::json()', function () {
test('serializes an array', function () {
expect(Parameters::json(':it', ['id' => 18, 'url' => 'https://www.unittest.com']))
->toEqual([':it' => '{"id":18,"url":"https://www.unittest.com"}']);
});
test('serializes an array w/ an empty array value', function () {
expect(Parameters::json(':it', ['id' => 18, 'urls' => []]))->toEqual([':it' => '{"id":18,"urls":[]}']);
});
test('serializes a 1-D array w/ an empty array value', function () {
expect(Parameters::json(':it', ['urls' => []]))->toEqual([':it' => '{"urls":[]}']);
});
test('serializes a stdClass instance', function () {
$obj = new stdClass();
$obj->id = 19;
$obj->url = 'https://testhere.info';
expect(Parameters::json(':it', $obj))->toEqual([':it' => '{"id":19,"url":"https://testhere.info"}']);
});
test('serializes a Pjson class instance', function () {
expect(Parameters::json(':it', new PjsonDocument(new PjsonId('999'), 'a test', 98, 'nothing')))
->toEqual([':it' => '{"id":"999","name":"a test","num_value":98}']);
});
test('serializes an array of Pjson class instances', function () {
expect(Parameters::json(':it',
['pjson' => [new PjsonDocument(new PjsonId('997'), 'another test', 94, 'nothing')]]))
->toEqual([':it' => '{"pjson":[{"id":"997","name":"another test","num_value":94}]}']);
});
});
describe('::nameFields()', function () {
test('provides missing parameter names', function () {
$named = [Field::equal('it', 17), Field::equal('also', 22, ':also'), Field::equal('other', 24)];
Parameters::nameFields($named);
expect($named)
->toHaveLength(3)
->sequence(
fn($it) => $it->paramName->toBe(':field0'),
fn($it) => $it->paramName->toBe(':also'),
fn($it) => $it->paramName->toBe(':field2'));
});
});
describe('::addFields()', function () {
test('appends to an existing parameter array', function () {
expect(Parameters::addFields([Field::equal('b', 'two', ':b'), Field::equal('z', 18, ':z')], [':a' => 1]))
->toEqual([':a' => 1, ':b' => 'two', ':z' => 18]);
});
});
describe('::fieldNames()', function () {
afterEach(function () { Configuration::overrideMode(null); });
test('generates names [PostgreSQL]', function () {
Configuration::overrideMode(Mode::PgSQL);
expect(Parameters::fieldNames(':names', ['one', 'two', 'seven']))->toEqual([':names' => "{one,two,seven}"]);
})->group('postgresql');
test('generates names [SQLite]', function () {
Configuration::overrideMode(Mode::SQLite);
expect(Parameters::fieldNames(':it', ['test', 'unit', 'wow']))
->toEqual([':it0' => '$.test', ':it1' => '$.unit', ':it2' => '$.wow']);
})->group('sqlite');
test('throws when mode is not set', function () {
expect(fn() => Parameters::fieldNames('', []))->toThrow(DocumentException::class);
});
});