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, 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
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-21 13:46:41 +00:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('all() succeeds')]
|
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-09-27 02:15:00 +00:00
|
|
|
#[TestDox('byFields() succeeds')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByFieldsSucceeds(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors",
|
2024-09-27 02:15:00 +00:00
|
|
|
Count::byFields('somewhere', [Field::greater('errors', 10, ':errors')]),
|
2024-06-21 13:46:41 +00:00
|
|
|
'SELECT statement not generated correctly');
|
|
|
|
}
|
|
|
|
|
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 COUNT(*) FROM the_table WHERE data @> :criteria', Count::byContains('the_table'),
|
|
|
|
'SELECT statement 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);
|
|
|
|
Count::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 COUNT(*) FROM a_table WHERE jsonb_path_exists(data, :path::jsonpath)',
|
|
|
|
Count::byJsonPath('a_table'), 'SELECT statement 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);
|
|
|
|
Count::byJsonPath('');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
}
|