2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
namespace Test\Unit;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters};
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2024-06-29 15:46:16 +00:00
|
|
|
use stdClass;
|
|
|
|
use Test\{PjsonDocument, PjsonId};
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the Parameters class
|
|
|
|
*/
|
2024-06-21 13:46:41 +00:00
|
|
|
#[TestDox('Parameters (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class ParametersTest extends TestCase
|
|
|
|
{
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('id() succeeds with string')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testIdSucceedsWithString(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals([':id' => 'key'], Parameters::id('key'), 'ID parameter not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('id() succeeds with non string')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testIdSucceedsWithNonString(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals([':id' => '7'], Parameters::id(7), 'ID parameter not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('json() succeeds for array')]
|
2024-06-29 15:46:16 +00:00
|
|
|
public function testJsonSucceedsForArray(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals([':it' => '{"id":18,"url":"https://www.unittest.com"}'],
|
|
|
|
Parameters::json(':it', ['id' => 18, 'url' => 'https://www.unittest.com']),
|
|
|
|
'JSON parameter not constructed correctly');
|
2024-07-04 17:16:04 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('json() succeeds for array with empty array parameter')]
|
2024-07-04 17:16:04 +00:00
|
|
|
public function testJsonSucceedsForArrayWithEmptyArrayParameter(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals([':it' => '{"id":18,"urls":[]}'], Parameters::json(':it', ['id' => 18, 'urls' => []]),
|
|
|
|
'JSON parameter not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('json() succeeds for 1D array with empty array parameter')]
|
2024-07-04 17:16:04 +00:00
|
|
|
public function testJsonSucceedsFor1DArrayWithEmptyArrayParameter(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals([':it' => '{"urls":[]}'], Parameters::json(':it', ['urls' => []]),
|
|
|
|
'JSON parameter not constructed correctly');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('json() succeeds for stdClass')]
|
2024-06-29 15:46:16 +00:00
|
|
|
public function testJsonSucceedsForStdClass(): void
|
|
|
|
{
|
|
|
|
$obj = new stdClass();
|
|
|
|
$obj->id = 19;
|
|
|
|
$obj->url = 'https://testhere.info';
|
|
|
|
$this->assertEquals([':it' => '{"id":19,"url":"https://testhere.info"}'], Parameters::json(':it', $obj),
|
|
|
|
'JSON parameter not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('json() succeeds for Pjson class')]
|
2024-06-29 15:46:16 +00:00
|
|
|
public function testJsonSucceedsForPjsonClass(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals([':it' => '{"id":"999","name":"a test","num_value":98}'],
|
|
|
|
Parameters::json(':it', new PjsonDocument(new PjsonId('999'), 'a test', 98, 'nothing')),
|
|
|
|
'JSON parameter not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('json() succeeds for array of Pjson class')]
|
2024-07-04 16:05:29 +00:00
|
|
|
public function testJsonSucceedsForArrayOfPjsonClass(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals([':it' => '{"pjson":[{"id":"997","name":"another test","num_value":94}]}'],
|
|
|
|
Parameters::json(':it',
|
|
|
|
['pjson' => [new PjsonDocument(new PjsonId('997'), 'another test', 94, 'nothing')]]),
|
|
|
|
'JSON parameter not constructed correctly');
|
|
|
|
}
|
2024-06-29 15:46:16 +00:00
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('nameFields() succeeds')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testNameFieldsSucceeds(): void
|
|
|
|
{
|
2024-09-27 02:15:00 +00:00
|
|
|
$named = [Field::equal('it', 17), Field::equal('also', 22, ':also'), Field::equal('other', 24)];
|
|
|
|
Parameters::nameFields($named);
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertCount(3, $named, 'There should be 3 parameters in the array');
|
|
|
|
$this->assertEquals(':field0', $named[0]->paramName, 'Parameter 1 not named correctly');
|
|
|
|
$this->assertEquals(':also', $named[1]->paramName, 'Parameter 2 not named correctly');
|
|
|
|
$this->assertEquals(':field2', $named[2]->paramName, 'Parameter 3 not named correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('addFields() succeeds')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testAddFieldsSucceeds(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals([':a' => 1, ':b' => 'two', ':z' => 18],
|
2024-09-27 02:15:00 +00:00
|
|
|
Parameters::addFields([Field::equal('b', 'two', ':b'), Field::equal('z', 18, ':z')], [':a' => 1]),
|
2024-06-08 23:58:45 +00:00
|
|
|
'Field parameters not added correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('fieldNames() succeeds for PostgreSQL')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testFieldNamesSucceedsForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
try {
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals([':names' => "{one,two,seven}"],
|
2024-06-08 23:58:45 +00:00
|
|
|
Parameters::fieldNames(':names', ['one', 'two', 'seven']), 'Field name parameters not correct');
|
|
|
|
} finally {
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('fieldNames() succeeds for SQLite')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testFieldNamesSucceedsForSQLite(): void
|
|
|
|
{
|
|
|
|
try {
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertEquals([':it0' => '$.test', ':it1' => '$.unit', ':it2' => '$.wow'],
|
|
|
|
Parameters::fieldNames(':it', ['test', 'unit', 'wow']), 'Field name parameters not correct');
|
|
|
|
} finally {
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('fieldNames() fails when mode not set')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testFieldNamesFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-08 23:58:45 +00:00
|
|
|
Parameters::fieldNames('', []);
|
|
|
|
}
|
|
|
|
}
|