192 lines
8.7 KiB
PHP
192 lines
8.7 KiB
PHP
<?php
|
|
/**
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
* @license MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Test\Integration\PostgreSQL;
|
|
|
|
use BitBadger\PDODocument\{Custom, Delete, Document, Field, Find};
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Test\Integration\{NumDocument, TestDocument};
|
|
|
|
/**
|
|
* PostgreSQL integration tests for the Find class
|
|
*/
|
|
#[TestDox('Find (PostgreSQL integration)')]
|
|
class FindTest 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 testAllSucceedsWhenThereIsData(): void
|
|
{
|
|
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
$this->assertEquals(5, $count, 'There should have been 5 documents in the list');
|
|
}
|
|
|
|
public function testAllSucceedsWhenThereIsNoData(): void
|
|
{
|
|
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
$this->assertFalse($docs->hasItems(), 'There should have been no documents in the list');
|
|
}
|
|
|
|
#[TestDox('By ID succeeds when a document is found')]
|
|
public function testByIdSucceedsWhenADocumentIsFound(): void
|
|
{
|
|
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
|
$this->assertEquals('two', $doc->get()->id, 'An incorrect document was returned');
|
|
}
|
|
|
|
#[TestDox('By ID succeeds when a document is found with numeric ID')]
|
|
public function testByIdSucceedsWhenADocumentIsFoundWithNumericId(): void
|
|
{
|
|
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]);
|
|
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
|
|
$doc = Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class);
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
|
$this->assertEquals(18, $doc->get()->id, 'An incorrect document was returned');
|
|
}
|
|
|
|
#[TestDox('By ID succeeds when a document is not found')]
|
|
public function testByIdSucceedsWhenADocumentIsNotFound(): void
|
|
{
|
|
$doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class);
|
|
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
|
|
}
|
|
|
|
public function testByFieldsSucceedsWhenDocumentsAreFound(): void
|
|
{
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 15)], TestDocument::class);
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
$this->assertEquals(2, $count, 'There should have been 2 documents in the list');
|
|
}
|
|
|
|
public function testByFieldsSucceedsWhenNoDocumentsAreFound(): void
|
|
{
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class);
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
$this->assertFalse($docs->hasItems(), 'There should have been no documents in the list');
|
|
}
|
|
|
|
public function testByContainsSucceedsWhenDocumentsAreFound(): void
|
|
{
|
|
$docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
$this->assertEquals(2, $count, 'There should have been 2 documents in the list');
|
|
}
|
|
|
|
public function testByContainsSucceedsWhenNoDocumentsAreFound(): void
|
|
{
|
|
$docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class);
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
$this->assertFalse($docs->hasItems(), 'The document list should be empty');
|
|
}
|
|
|
|
#[TestDox('By JSON Path succeeds when documents are found')]
|
|
public function testByJsonPathSucceedsWhenDocumentsAreFound(): void
|
|
{
|
|
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
$count = 0;
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
$this->assertEquals(2, $count, 'There should have been 2 documents in the list');
|
|
}
|
|
|
|
#[TestDox('By JSON Path succeeds when no documents are found')]
|
|
public function testByJsonPathSucceedsWhenNoDocumentsAreFound(): void
|
|
{
|
|
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class);
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
$this->assertFalse($docs->hasItems(), 'The document list should be empty');
|
|
}
|
|
|
|
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
|
|
{
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
|
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned');
|
|
}
|
|
|
|
public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void
|
|
{
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
|
$this->assertContains($doc->get()->id, ['two', 'four'], 'An incorrect document was returned');
|
|
}
|
|
|
|
public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void
|
|
{
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class);
|
|
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
|
|
}
|
|
|
|
public function testFirstByContainsSucceedsWhenADocumentIsFound(): void
|
|
{
|
|
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class);
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
|
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned');
|
|
}
|
|
|
|
public function testFirstByContainsSucceedsWhenMultipleDocumentsAreFound(): void
|
|
{
|
|
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
|
$this->assertContains($doc->get()->id, ['four', 'five'], 'An incorrect document was returned');
|
|
}
|
|
|
|
public function testFirstByContainsSucceedsWhenADocumentIsNotFound(): void
|
|
{
|
|
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class);
|
|
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
|
|
}
|
|
|
|
#[TestDox('First by JSON Path succeeds when a document is found')]
|
|
public function testFirstByJsonPathSucceedsWhenADocumentIsFound(): void
|
|
{
|
|
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class);
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
|
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned');
|
|
}
|
|
|
|
#[TestDox('First by JSON Path succeeds when multiple documents are found')]
|
|
public function testFirstByJsonPathSucceedsWhenMultipleDocumentsAreFound(): void
|
|
{
|
|
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
|
$this->assertContains($doc->get()->id, ['four', 'five'], 'An incorrect document was returned');
|
|
}
|
|
|
|
#[TestDox('First by JSON Path succeeds when a document is not found')]
|
|
public function testFirstByJsonPathSucceedsWhenADocumentIsNotFound(): void
|
|
{
|
|
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class);
|
|
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
|
|
}
|
|
}
|