pdo-document/tests/unit/Query/CountTest.php

72 lines
2.2 KiB
PHP
Raw Normal View History

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit\Query;
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
use BitBadger\PDODocument\Query\Count;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Count class
*/
#[TestDox('Count Queries (Unit tests)')]
class CountTest extends TestCase
{
public function tearDown(): void
{
Configuration::overrideMode(null);
parent::tearDown();
}
public function testAllSucceeds(): void
{
$this->assertEquals('SELECT COUNT(*) FROM a_table', Count::all('a_table'),
'SELECT statement not generated correctly');
}
public function testByFieldsSucceeds(): void
{
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors",
Count::byFields('somewhere', [Field::greater('errors', 10, ':errors')]),
'SELECT statement not generated correctly');
}
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::overrideMode(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::overrideMode(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('');
}
}