2024-06-08 23:58:45 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Test\Integration\SQLite;
|
|
|
|
|
2024-06-21 13:46:41 +00:00
|
|
|
use BitBadger\PDODocument\{Custom, Document, DocumentException, Field, Find};
|
2024-06-08 23:58:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Test\Integration\TestDocument;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQLite integration tests for the Find class
|
|
|
|
*/
|
|
|
|
#[TestDox('Find (SQLite 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->assertNotFalse($doc, 'There should have been a document returned');
|
|
|
|
$this->assertEquals('two', $doc->id, 'An incorrect document was returned');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By ID succeeds when a document is found with numeric ID')]
|
|
|
|
public function testByIdSucceedsWhenADocumentIsFoundWithNumericId(): void
|
|
|
|
{
|
|
|
|
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
|
|
|
|
$doc = Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class);
|
|
|
|
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
|
|
$this->assertEquals('18', $doc->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->assertFalse($doc, 'There should not have been a document returned');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByFieldsSucceedsWhenDocumentsAreFound(): void
|
|
|
|
{
|
|
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::GT('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::GT('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');
|
|
|
|
}
|
|
|
|
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::byContains('', [], TestDocument::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By JSON Path fails')]
|
|
|
|
public function testByJsonPathFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::byJsonPath('', '', TestDocument::class);
|
|
|
|
}
|
|
|
|
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
|
|
|
|
{
|
|
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'another')], TestDocument::class);
|
|
|
|
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
|
|
$this->assertEquals('two', $doc->id, 'The incorrect document was returned');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void
|
|
|
|
{
|
|
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('sub.foo', 'green')], TestDocument::class);
|
|
|
|
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
|
|
$this->assertContains($doc->id, ['two', 'four'], 'An incorrect document was returned');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void
|
|
|
|
{
|
|
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'absent')], TestDocument::class);
|
|
|
|
$this->assertFalse($doc, 'There should not have been a document returned');
|
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
|
|
|
public function testFirstByContainsFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::firstByContains('', [], TestDocument::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('First by JSON Path fails')]
|
|
|
|
public function testFirstByJsonPathFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::firstByJsonPath('', '', TestDocument::class);
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|