2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-21 13:46:41 +00:00
|
|
|
|
|
|
|
namespace Test\Integration\PostgreSQL;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\{Count, Field};
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PostgreSQL integration tests for the Count class
|
|
|
|
*/
|
|
|
|
#[TestDox('Count (PostgreSQL integration)')]
|
|
|
|
class CountTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var string Database name for throwaway database */
|
|
|
|
private string $dbName;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->dbName = ThrowawayDb::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
ThrowawayDb::destroy($this->dbName);
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllSucceeds(): void
|
|
|
|
{
|
|
|
|
$count = Count::all(ThrowawayDb::TABLE);
|
|
|
|
$this->assertEquals(5, $count, 'There should have been 5 matching documents');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByFieldsSucceedsForANumericRange(): void
|
|
|
|
{
|
2024-09-21 00:29:47 +00:00
|
|
|
$count = Count::byFields(ThrowawayDb::TABLE, [Field::between('num_value', 10, 20)]);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals(3, $count, 'There should have been 3 matching documents');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByFieldsSucceedsForANonNumericRange(): void
|
|
|
|
{
|
2024-09-21 00:29:47 +00:00
|
|
|
$count = Count::byFields(ThrowawayDb::TABLE, [Field::between('value', 'aardvark', 'apple')]);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals(1, $count, 'There should have been 1 matching document');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByContainsSucceedsWhenDocumentsMatch(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(2, Count::byContains(ThrowawayDb::TABLE, ['value' => 'purple']),
|
|
|
|
'There should have been 2 matching documents');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByContainsSucceedsWhenNoDocumentsMatch(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(0, Count::byContains(ThrowawayDb::TABLE, ['value' => 'magenta']),
|
|
|
|
'There should have been no matching documents');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By JSON Path succeeds when documents match')]
|
|
|
|
public function testByJsonPathSucceedsWhenDocumentsMatch(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(2, Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ < 5)'),
|
|
|
|
'There should have been 2 matching documents');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By JSON Path succeeds when no documents match')]
|
|
|
|
public function testByJsonPathSucceedsWhenNoDocumentsMatch(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(0, Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)'),
|
|
|
|
'There should have been no matching documents');
|
|
|
|
}
|
|
|
|
}
|