2024-06-08 23:58:45 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Test\Unit\Query;
|
|
|
|
|
2024-06-21 13:46:41 +00:00
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
|
2024-06-08 23:58:45 +00:00
|
|
|
use BitBadger\PDODocument\Query\Count;
|
2024-06-21 13:46:41 +00:00
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
2024-06-08 23:58:45 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the Count class
|
|
|
|
*/
|
2024-06-21 13:46:41 +00:00
|
|
|
#[TestDox('Count Queries (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class CountTest extends TestCase
|
|
|
|
{
|
2024-06-21 13:46:41 +00:00
|
|
|
public function tearDown(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = null;
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testAllSucceeds(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals('SELECT COUNT(*) FROM a_table', Count::all('a_table'),
|
|
|
|
'SELECT statement not generated correctly');
|
|
|
|
}
|
|
|
|
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByFieldsSucceeds(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors",
|
|
|
|
Count::byFields('somewhere', [Field::GT('errors', 10, ':errors')]),
|
|
|
|
'SELECT statement not generated correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By contains succeeds for PostgreSQL')]
|
|
|
|
public function testByContainsSucceedsForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
$this->assertEquals('SELECT COUNT(*) FROM the_table WHERE data @> :criteria', Count::byContains('the_table'),
|
|
|
|
'SELECT statement not generated correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By contains fails for non PostgreSQL')]
|
|
|
|
public function testByContainsFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Count::byContains('');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By JSON Path succeeds for PostgreSQL')]
|
|
|
|
public function testByJsonPathSucceedsForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
$this->assertEquals('SELECT COUNT(*) FROM a_table WHERE jsonb_path_exists(data, :path::jsonpath)',
|
|
|
|
Count::byJsonPath('a_table'), 'SELECT statement not generated correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By JSON Path fails for non PostgreSQL')]
|
|
|
|
public function testByJsonPathFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Count::byJsonPath('');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
}
|