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\Query;
|
|
|
|
|
2024-06-21 13:46:41 +00:00
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, FieldMatch, Mode};
|
2024-06-08 23:58:45 +00:00
|
|
|
use BitBadger\PDODocument\Query\Find;
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the Find class
|
|
|
|
*/
|
2024-06-21 13:46:41 +00:00
|
|
|
#[TestDox('Find Queries (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class FindTest extends TestCase
|
|
|
|
{
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
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('byId() succeeds')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByIdSucceeds(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertEquals("SELECT data FROM here WHERE data->>'id' = :id", Find::byId('here'),
|
|
|
|
'SELECT query not generated correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byFields() succeeds')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByFieldsSucceeds(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertEquals("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock",
|
2024-09-27 02:15:00 +00:00
|
|
|
Find::byFields('there', [Field::equal('active', true, ':act'), Field::equal('locked', true, ':lock')],
|
2024-06-11 11:07:56 +00:00
|
|
|
FieldMatch::Any),
|
2024-06-08 23:58:45 +00:00
|
|
|
'SELECT query not generated correctly');
|
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byContains() succeeds for PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsSucceedsForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals('SELECT data FROM disc WHERE data @> :criteria', Find::byContains('disc'),
|
|
|
|
'SELECT query not generated correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byContains() fails for non PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::byContains('');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byJsonPath() succeeds for PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathSucceedsForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals('SELECT data FROM light WHERE jsonb_path_exists(data, :path::jsonpath)',
|
|
|
|
Find::byJsonPath('light'), 'SELECT query not generated correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byJsonPath() fails for non PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::byJsonPath('');
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|