pdo-document/tests/unit/Query/FindTest.php
Daniel J. Summers d8330d828a Derive mode from DSN function
- Add headers in all files
- Minor field name changes
2024-07-20 21:47:21 -04:00

74 lines
2.4 KiB
PHP

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit\Query;
use BitBadger\PDODocument\{Configuration, DocumentException, Field, FieldMatch, Mode};
use BitBadger\PDODocument\Query\Find;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Find class
*/
#[TestDox('Find Queries (Unit tests)')]
class FindTest extends TestCase
{
protected function tearDown(): void
{
Configuration::overrideMode(null);
}
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("SELECT data FROM here WHERE data->>'id' = :id", Find::byId('here'),
'SELECT query not generated correctly');
}
public function testByFieldsSucceeds(): void
{
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock",
Find::byFields('there', [Field::EQ('active', true, ':act'), Field::EQ('locked', true, ':lock')],
FieldMatch::Any),
'SELECT query not generated correctly');
}
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::overrideMode(Mode::PgSQL);
$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
{
Configuration::overrideMode(Mode::PgSQL);
$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('');
}
}