Initial SQLite development #1

Merged
danieljsummers merged 25 commits from develop into main 2024-06-08 23:58:45 +00:00
3 changed files with 130 additions and 1 deletions
Showing only changes of commit a96fdf9d14 - Show all commits

View File

@ -43,7 +43,12 @@ class Custom
$stmt->bindValue($key, $value, $dataType); $stmt->bindValue($key, $value, $dataType);
} }
if ($debug) echo '<pre>SQL: ' . $stmt->queryString . '</pre>'; if ($debug) echo '<pre>SQL: ' . $stmt->queryString . '</pre>';
try {
if ($stmt->execute()) return $stmt; if ($stmt->execute()) return $stmt;
} catch (PDOException $ex) {
$keyword = explode(' ', $query, 2)[0];
throw new DocumentException("Error executing $keyword statement: " . $stmt->errorCode(), previous: $ex);
}
$keyword = explode(' ', $query, 2)[0]; $keyword = explode(' ', $query, 2)[0];
throw new DocumentException("Error executing $keyword statement: " . $stmt->errorCode()); throw new DocumentException("Error executing $keyword statement: " . $stmt->errorCode());
} }

View File

@ -0,0 +1,42 @@
<?php declare(strict_types=1);
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Count, Field};
use PHPUnit\Framework\TestCase;
class CountTest 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 testAllSucceeds(): void
{
$count = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(5, $count, 'There should have been 5 matching documents');
}
public function testByFieldsSucceedsForANumericRange(): void
{
$count = Count::byFields(ThrowawayDb::TABLE, [Field::BT('num_value', 10, 20)]);
$this->assertEquals(3, $count, 'There should have been 3 matching documents');
}
public function testByFieldsSucceedsForANonNumericRange(): void
{
$count = Count::byFields(ThrowawayDb::TABLE, [Field::BT('value', 'aardvark', 'apple')]);
$this->assertEquals(1, $count, 'There should have been 1 matching document');
}
}

View File

@ -0,0 +1,82 @@
<?php declare(strict_types=1);
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Document, DocumentException, Find};
use PHPUnit\Framework\TestCase;
use Test\Integration\{SubDocument, TestDocument};
/**
* SQLite integration tests for the Document class
*/
class DocumentTest 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 testInsertSucceeds(): void
{
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
$doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document inserted');
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
$this->assertEquals('', $doc->value, 'The value was incorrect');
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
$this->assertNotNull($doc->sub, 'The sub-document should not have been null');
$this->assertEquals('gobble', $doc->sub->foo, 'The sub-document foo property was incorrect');
$this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect');
}
public function testInsertFailsForDuplicateKey(): void
{
$this->expectException(DocumentException::class);
Document::insert(ThrowawayDb::TABLE, new TestDocument('one'));
}
public function testSaveSucceedsWhenADocumentIsInserted(): void
{
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
}
public function testSaveSucceedsWhenADocumentIsUpdated(): void
{
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals(44, $doc->num_value, 'The numeric value was not updated');
$this->assertNull($doc->sub, 'The sub-document should have been null');
}
public function testUpdateSucceedsWhenReplacingADocument(): void
{
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals('howdy', $doc->value, 'The value was incorrect');
$this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect');
$this->assertNotNull($doc->sub, 'The sub-document should not have been null');
$this->assertEquals('y', $doc->sub->foo, 'The sub-document foo property was incorrect');
$this->assertEquals('z', $doc->sub->bar, 'The sub-document bar property was incorrect');
}
public function testUpdateSucceedsWhenNoDocumentIsReplaced(): void
{
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
$doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class);
$this->assertFalse($doc, 'There should not have been a document returned');
}
}