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;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\{Count, Custom, DocumentException, Query};
|
|
|
|
use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper};
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Test\Integration\TestDocument;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQLite Integration tests for the Custom class
|
|
|
|
*/
|
|
|
|
#[TestDox('Custom (SQLite integration)')]
|
|
|
|
class CustomTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var string Database name for throwaway database */
|
|
|
|
private string $dbName;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->dbName = ThrowawayDb::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown(): void
|
|
|
|
{
|
|
|
|
ThrowawayDb::destroy($this->dbName);
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('runQuery() succeeds with a valid query')]
|
|
|
|
public function testRunQuerySucceedsWithAValidQuery(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []);
|
|
|
|
try {
|
|
|
|
$this->assertNotNull($stmt, 'The statement should not have been null');
|
|
|
|
} finally {
|
|
|
|
$stmt = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('runQuery() fails with an invalid query')]
|
|
|
|
public function testRunQueryFailsWithAnInvalidQuery(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
$stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []);
|
|
|
|
try {
|
|
|
|
$this->assertTrue(false, 'This code should not be reached');
|
|
|
|
} finally {
|
|
|
|
$stmt = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('list() succeeds when data is found')]
|
|
|
|
public function testListSucceedsWhenDataIsFound(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'The document list should not be null');
|
|
|
|
$count = 0;
|
|
|
|
foreach ($list->items() as $ignored) $count++;
|
|
|
|
$this->assertEquals(5, $count, 'There should have been 5 documents in the list');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('list() succeeds when no data is found')]
|
|
|
|
public function testListSucceedsWhenNoDataIsFound(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' > :value",
|
|
|
|
[':value' => 100], new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($list, 'The document list should not be null');
|
|
|
|
$this->assertFalse($list->hasItems(), 'There should have been no documents in the list');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('array() succeeds when data is found')]
|
|
|
|
public function testArraySucceedsWhenDataIsFound(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [],
|
|
|
|
new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($array, 'The document array should not be null');
|
|
|
|
$this->assertCount(2, $array, 'There should have been 2 documents in the array');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('array() succeeds when no data is found')]
|
|
|
|
public function testArraySucceedsWhenNoDataIsFound(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value",
|
|
|
|
[':value' => 'not there'], new DocumentMapper(TestDocument::class));
|
|
|
|
$this->assertNotNull($array, 'The document array should not be null');
|
|
|
|
$this->assertCount(0, $array, 'There should have been no documents in the array');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('single() succeeds when a row is found')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testSingleSucceedsWhenARowIsFound(): void
|
|
|
|
{
|
|
|
|
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
|
|
|
|
new DocumentMapper(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('one', $doc->get()->id, 'The incorrect document was returned');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('single() succeeds when a row is not found')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testSingleSucceedsWhenARowIsNotFound(): void
|
|
|
|
{
|
|
|
|
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
|
|
|
|
[':id' => 'eighty'], new DocumentMapper(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-27 02:15:00 +00:00
|
|
|
#[TestDox('nonQuery() succeeds when operating on data')]
|
|
|
|
public function testNonQuerySucceedsWhenOperatingOnData(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
|
|
$remaining = Count::all(ThrowawayDb::TABLE);
|
|
|
|
$this->assertEquals(0, $remaining, 'There should be no documents remaining in the table');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('nonQuery() succeeds when no data matches WHERE clause')]
|
|
|
|
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE data->>'num_value' > :value", [':value' => 100]);
|
|
|
|
$remaining = Count::all(ThrowawayDb::TABLE);
|
|
|
|
$this->assertEquals(5, $remaining, 'There should be 5 documents remaining in the table');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('scalar() succeeds')]
|
|
|
|
public function testScalarSucceeds(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$value = Custom::scalar("SELECT 5 AS it", [], new CountMapper());
|
|
|
|
$this->assertEquals(5, $value, 'The scalar value was not returned correctly');
|
|
|
|
}
|
|
|
|
}
|