85 lines
3.7 KiB
PHP
85 lines
3.7 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace Test\Integration\SQLite;
|
|
|
|
use BitBadger\PDODocument\{Document, DocumentException, Find};
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Test\Integration\{SubDocument, TestDocument};
|
|
|
|
/**
|
|
* SQLite integration tests for the Document class
|
|
*/
|
|
#[TestDox('Document (SQLite integration)')]
|
|
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');
|
|
}
|
|
}
|