2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
namespace Test\Integration\SQLite;
|
|
|
|
|
2024-09-26 00:03:17 +00:00
|
|
|
use BitBadger\PDODocument\{Custom, Delete, Document, DocumentException, Field, FieldMatch, Find};
|
2024-06-08 23:58:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2024-09-26 00:03:17 +00:00
|
|
|
use Test\Integration\ArrayDocument;
|
2024-06-08 23:58:45 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('all() succeeds when there is data')]
|
2024-06-08 23:58:45 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2024-09-27 01:52:06 +00:00
|
|
|
#[TestDox('all() succeeds when ordering data ascending')]
|
|
|
|
public function testAllSucceedsWhenOrderingDataAscending(): void
|
|
|
|
{
|
|
|
|
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]);
|
|
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
|
|
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
|
|
|
|
$this->assertEquals(['five', 'four', 'one', 'three', 'two'], $ids, 'The documents were not ordered correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('all() succeeds when ordering data descending')]
|
|
|
|
public function testAllSucceedsWhenOrderingDataDescending(): void
|
|
|
|
{
|
|
|
|
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]);
|
|
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
|
|
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
|
|
|
|
$this->assertEquals(['two', 'three', 'one', 'four', 'five'], $ids, 'The documents were not ordered correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('all() succeeds when ordering data numerically')]
|
|
|
|
public function testAllSucceedsWhenOrderingDataNumerically(): void
|
|
|
|
{
|
|
|
|
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class,
|
|
|
|
[Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]);
|
|
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
|
|
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
|
|
|
|
$this->assertEquals(['two', 'four', 'one', 'three', 'five'], $ids, 'The documents were not ordered correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('all() succeeds when there is no data')]
|
2024-06-08 23:58:45 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byId() succeeds when a document is found')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByIdSucceedsWhenADocumentIsFound(): void
|
|
|
|
{
|
|
|
|
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
2024-07-28 23:45:29 +00:00
|
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
2024-06-25 02:04:11 +00:00
|
|
|
$this->assertEquals('two', $doc->get()->id, 'An incorrect document was returned');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byId() succeeds when a document is found with numeric ID')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByIdSucceedsWhenADocumentIsFoundWithNumericId(): void
|
|
|
|
{
|
|
|
|
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
|
|
|
|
$doc = Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class);
|
2024-07-28 23:45:29 +00:00
|
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
2024-06-25 02:04:11 +00:00
|
|
|
$this->assertEquals('18', $doc->get()->id, 'An incorrect document was returned');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byId() succeeds when a document is not found')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByIdSucceedsWhenADocumentIsNotFound(): void
|
|
|
|
{
|
|
|
|
$doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class);
|
2024-07-28 23:45:29 +00:00
|
|
|
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byFields() succeeds when documents are found')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByFieldsSucceedsWhenDocumentsAreFound(): void
|
|
|
|
{
|
2024-09-26 00:03:17 +00:00
|
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
|
|
|
|
TestDocument::class, FieldMatch::All);
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
|
|
$count = 0;
|
|
|
|
foreach ($docs->items() as $ignored) $count++;
|
2024-09-26 00:03:17 +00:00
|
|
|
$this->assertEquals(1, $count, 'There should have been 1 document in the list');
|
|
|
|
}
|
|
|
|
|
2024-09-27 01:52:06 +00:00
|
|
|
#[TestDox('byFields() succeeds when documents are found and ordered')]
|
|
|
|
public function testByFieldsSucceedsWhenDocumentsAreFoundAndOrdered(): void
|
|
|
|
{
|
|
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
|
|
|
|
FieldMatch::All, [Field::named('id')]);
|
|
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
|
|
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
|
|
|
|
$this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-26 00:03:17 +00:00
|
|
|
#[TestDox('byFields() succeeds when documents are found using IN with numeric field')]
|
2024-09-27 01:52:06 +00:00
|
|
|
public function testByFieldsSucceedsWhenDocumentsAreFoundUsingInWithNumericField(): void
|
2024-09-26 00:03:17 +00:00
|
|
|
{
|
|
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class);
|
|
|
|
$this->assertNotNull($docs, 'There should have been a document list returned');
|
|
|
|
$count = 0;
|
|
|
|
foreach ($docs->items() as $ignored) $count++;
|
|
|
|
$this->assertEquals(1, $count, 'There should have been 1 document in the list');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byFields() succeeds when no documents are found')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByFieldsSucceedsWhenNoDocumentsAreFound(): void
|
|
|
|
{
|
2024-09-21 00:29:47 +00:00
|
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class);
|
2024-06-08 23:58:45 +00:00
|
|
|
$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-09-26 00:03:17 +00:00
|
|
|
#[TestDox('byFields() succeeds for inArray when matching documents exist')]
|
|
|
|
public function testByFieldsSucceedsForInArrayWhenMatchingDocumentsExist(): void
|
|
|
|
{
|
|
|
|
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
|
|
|
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
|
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])],
|
|
|
|
ArrayDocument::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('byFields() succeeds for inArray when no matching documents exist')]
|
|
|
|
public function testByFieldsSucceedsForInArrayWhenNoMatchingDocumentsExist(): void
|
|
|
|
{
|
|
|
|
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
|
|
|
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
|
|
|
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])],
|
|
|
|
ArrayDocument::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-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byContains() fails')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::byContains('', [], TestDocument::class);
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byJsonPath() fails')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::byJsonPath('', '', TestDocument::class);
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('firstByFields() succeeds when a document is found')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
|
|
|
|
{
|
2024-09-21 00:29:47 +00:00
|
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
|
2024-07-28 23:45:29 +00:00
|
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
2024-06-25 02:04:11 +00:00
|
|
|
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('firstByFields() succeeds when multiple documents are found')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void
|
|
|
|
{
|
2024-09-21 00:29:47 +00:00
|
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
|
2024-07-28 23:45:29 +00:00
|
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
2024-06-25 02:04:11 +00:00
|
|
|
$this->assertContains($doc->get()->id, ['two', 'four'], 'An incorrect document was returned');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 01:52:06 +00:00
|
|
|
#[TestDox('firstByFields() succeeds when multiple ordered documents are found')]
|
|
|
|
public function testFirstByFieldsSucceedsWhenMultipleOrderedDocumentsAreFound(): void
|
|
|
|
{
|
|
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
|
|
|
|
orderBy: [Field::named('n:num_value DESC')]);
|
|
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
|
|
|
$this->assertEquals('four', $doc->get()->id, 'The incorrect document was returned');
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('firstByFields() succeeds when a document is not found')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void
|
|
|
|
{
|
2024-09-21 00:29:47 +00:00
|
|
|
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class);
|
2024-07-28 23:45:29 +00:00
|
|
|
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('firstByContains() fails')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testFirstByContainsFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::firstByContains('', [], TestDocument::class);
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('firstByJsonPath() fails')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testFirstByJsonPathFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Find::firstByJsonPath('', '', TestDocument::class);
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|