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-07-25 00:57:23 +00:00
|
|
|
use BitBadger\PDODocument\{DocumentException, DocumentList, Query};
|
2024-06-08 23:58:45 +00:00
|
|
|
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Test\Integration\TestDocument;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQLite integration tests for the DocumentList class
|
|
|
|
*/
|
|
|
|
#[TestDox('DocumentList (SQLite integration)')]
|
|
|
|
class DocumentListTest 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-27 02:15:00 +00:00
|
|
|
#[TestDox('create() succeeds')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testCreateSucceeds(): void
|
|
|
|
{
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'There should have been a document list created');
|
|
|
|
$list = null;
|
|
|
|
}
|
|
|
|
|
2024-10-02 01:37:08 +00:00
|
|
|
#[TestDox('items succeeds')]
|
2024-07-25 00:57:23 +00:00
|
|
|
public function testItemsSucceeds(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'There should have been a document list created');
|
|
|
|
$count = 0;
|
2024-10-02 01:37:08 +00:00
|
|
|
foreach ($list->items as $item) {
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertContains($item->id, ['one', 'two', 'three', 'four', 'five'],
|
|
|
|
'An unexpected document ID was returned');
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
$this->assertEquals(5, $count, 'There should have been 5 documents returned');
|
|
|
|
}
|
|
|
|
|
2024-10-02 01:37:08 +00:00
|
|
|
#[TestDox('items fails when already consumed')]
|
2024-07-25 00:57:23 +00:00
|
|
|
public function testItemsFailsWhenAlreadyConsumed(): void
|
|
|
|
{
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'There should have been a document list created');
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($list->hasItems, 'There should be items in the list');
|
|
|
|
$ignored = iterator_to_array($list->items);
|
|
|
|
$this->assertFalse($list->hasItems, 'The list should no longer have items');
|
2024-07-25 00:57:23 +00:00
|
|
|
$this->expectException(DocumentException::class);
|
2024-10-02 01:37:08 +00:00
|
|
|
iterator_to_array($list->items);
|
2024-07-25 00:57:23 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 01:37:08 +00:00
|
|
|
#[TestDox('hasItems succeeds with empty results')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testHasItemsSucceedsWithEmptyResults(): void
|
|
|
|
{
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' < 0", [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'There should have been a document list created');
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertFalse($list->hasItems, 'There should be no items in the list');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 01:37:08 +00:00
|
|
|
#[TestDox('hasItems succeeds with non-empty results')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testHasItemsSucceedsWithNonEmptyResults(): void
|
|
|
|
{
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'There should have been a document list created');
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($list->hasItems, 'There should be items in the list');
|
|
|
|
foreach ($list->items as $ignored) {
|
|
|
|
$this->assertTrue($list->hasItems, 'There should be items remaining in the list');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertFalse($list->hasItems, 'There should be no remaining items in the list');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
2024-09-27 02:15:00 +00:00
|
|
|
|
|
|
|
#[TestDox('map() succeeds')]
|
2024-07-25 00:57:23 +00:00
|
|
|
public function testMapSucceeds(): void
|
|
|
|
{
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'There should have been a document list created');
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($list->hasItems, 'There should be items in the list');
|
2024-07-25 00:57:23 +00:00
|
|
|
foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) {
|
|
|
|
$this->assertContains($mapped, ['eno', 'owt', 'eerht', 'ruof', 'evif'],
|
|
|
|
'An unexpected mapped value was returned');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('iter() succeeds')]
|
2024-07-25 00:57:23 +00:00
|
|
|
public function testIterSucceeds(): void
|
|
|
|
{
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'There should have been a document list created');
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($list->hasItems, 'There should be items in the list');
|
2024-07-25 00:57:23 +00:00
|
|
|
$splats = [];
|
|
|
|
$list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); });
|
|
|
|
$this->assertEquals('*** *** ***** **** ****', implode(' ', $splats),
|
|
|
|
'Iteration did not have the expected result');
|
|
|
|
}
|
2024-07-29 00:01:55 +00:00
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('mapToArray() succeeds')]
|
2024-07-29 00:01:55 +00:00
|
|
|
public function testMapToArraySucceeds(): void
|
|
|
|
{
|
|
|
|
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'There should have been a document list created');
|
2024-10-02 01:37:08 +00:00
|
|
|
$this->assertTrue($list->hasItems, 'There should be items in the list');
|
2024-07-29 00:01:55 +00:00
|
|
|
$lookup = $list->mapToArray(fn($it) => $it->id, fn($it) => $it->value);
|
|
|
|
$expected = ['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple'];
|
|
|
|
$this->assertEquals($expected, $lookup, 'The array was not mapped correctly');
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|