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

74 lines
2.3 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();
}
#[TestDox('all() succeeds')]
public function testAllSucceeds(): void
{
$this->assertEquals('SELECT COUNT(*) FROM a_table', Count::all('a_table'),
'SELECT statement not generated correctly');
}
#[TestDox('byFields() succeeds')]
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('byContains() 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('byContains() fails for non PostgreSQL')]
public function testByContainsFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Count::byContains('');
}
#[TestDox('byJsonPath() 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('byJsonPath() fails for non PostgreSQL')]
public function testByJsonPathFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Count::byJsonPath('');
}
}