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
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By ID succeeds')]
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-11 11:07:56 +00:00
|
|
|
Find::byFields('there', [Field::EQ('active', true, ':act'), Field::EQ('locked', true, ':lock')],
|
|
|
|
FieldMatch::Any),
|
2024-06-08 23:58:45 +00:00
|
|
|
'SELECT query not generated correctly');
|
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
|
|
|
#[TestDox('By contains succeeds for PostgreSQL')]
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By contains fails for non PostgreSQL')]
|
|
|
|
public function testByContainsFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::byContains('');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By JSON Path succeeds for PostgreSQL')]
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By JSON Path fails for non PostgreSQL')]
|
|
|
|
public function testByJsonPathFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::byJsonPath('');
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|