alpha2 #2

Merged
danieljsummers merged 5 commits from alpha2 into main 2024-06-11 11:07:57 +00:00
2 changed files with 232 additions and 3 deletions
Showing only changes of commit 2f1db190d7 - Show all commits

View File

@ -0,0 +1,11 @@
<?php declare(strict_types=1);
namespace Test\Integration;
/**
* A test document with a numeric ID
*/
class NumDocument
{
public function __construct(public int $id = 0, public string $value = '') { }
}

View File

@ -2,10 +2,11 @@
namespace Test\Integration\SQLite; namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Document, DocumentException, Find}; use BitBadger\PDODocument\{AutoId, Configuration, Custom, Document, DocumentException, Field, Find};
use BitBadger\PDODocument\Mapper\ArrayMapper;
use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Test\Integration\{SubDocument, TestDocument}; use Test\Integration\{NumDocument, SubDocument, TestDocument};
/** /**
* SQLite integration tests for the Document class * SQLite integration tests for the Document class
@ -28,7 +29,125 @@ class DocumentTest extends TestCase
parent::tearDown(); parent::tearDown();
} }
public function testInsertSucceeds(): void #[TestDox('Insert succeeds for array no auto ID')]
public function testInsertSucceedsForArrayNoAutoId(): void
{
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => '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');
}
#[TestDox('Insert succeeds for array with auto number ID not provided')]
public function testInsertSucceedsForArrayWithAutoNumberIdNotProvided(): void
{
Configuration::$autoId = AutoId::Number;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
$this->assertNotFalse($doc, 'There should have been a document returned');
$obj = json_decode($doc['data']);
$this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated');
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = 2", [],
new ArrayMapper());
$this->assertNotFalse($doc, 'There should have been a document returned');
$obj = json_decode($doc['data']);
$this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated');
} finally {
Configuration::$autoId = AutoId::None;
}
}
#[TestDox('Insert succeeds for array with auto number ID with ID provided')]
public function testInsertSucceedsForArrayWithAutoNumberIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::Number;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]);
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
$this->assertNotFalse($doc, 'There should have been a document returned');
$obj = json_decode($doc['data']);
$this->assertEquals(7, $obj->id, 'The ID 7 should have been stored');
} finally {
Configuration::$autoId = AutoId::None;
}
}
#[TestDox('Insert succeeds for array with auto UUID ID not provided')]
public function testInsertSucceedsForArrayWithAutoUuidIdNotProvided(): void
{
Configuration::$autoId = AutoId::UUID;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 5)], TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertNotEmpty($doc->id, 'The ID should have been auto-generated');
} finally {
Configuration::$autoId = AutoId::None;
}
}
#[TestDox('Insert succeeds for array with auto UUID ID with ID provided')]
public function testInsertSucceedsForArrayWithAutoUuidIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::UUID;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 12)], TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals($uuid, $doc->id, 'The ID should not have been changed');
} finally {
Configuration::$autoId = AutoId::None;
}
}
#[TestDox('Insert succeeds for array with auto string ID not provided')]
public function testInsertSucceedsForArrayWithAutoStringIdNotProvided(): void
{
Configuration::$autoId = AutoId::RandomString;
Configuration::$idStringLength = 6;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 8)], TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals(6, strlen($doc->id), 'The ID should have been auto-generated and had 6 characters');
} finally {
Configuration::$autoId = AutoId::None;
Configuration::$idStringLength = 16;
}
}
#[TestDox('Insert succeeds for array with auto string ID with ID provided')]
public function testInsertSucceedsForArrayWithAutoStringIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::RandomString;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 3)], TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals('my-key', $doc->id, 'The ID should not have been changed');
} finally {
Configuration::$autoId = AutoId::None;
}
}
#[TestDox('Insert succeeds for object no auto ID')]
public function testInsertSucceedsForObjectNoAutoId(): void
{ {
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble'))); Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
$doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); $doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
@ -41,6 +160,105 @@ class DocumentTest extends TestCase
$this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect'); $this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect');
} }
#[TestDox('Insert succeeds for object with auto number ID not provided')]
public function testInsertSucceedsForObjectWithAutoNumberIdNotProvided(): void
{
Configuration::$autoId = AutoId::Number;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'taco')], NumDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals(1, $doc->id, 'The ID 1 should have been auto-generated');
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'burrito')], NumDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals(2, $doc->id, 'The ID 2 should have been auto-generated');
} finally {
Configuration::$autoId = AutoId::None;
}
}
#[TestDox('Insert succeeds for object with auto number ID with ID provided')]
public function testInsertSucceedsForObjectWithAutoNumberIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::Number;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'large')], NumDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals(64, $doc->id, 'The ID 64 should have been stored');
} finally {
Configuration::$autoId = AutoId::None;
}
}
#[TestDox('Insert succeeds for object with auto UUID ID not provided')]
public function testInsertSucceedsForObjectWithAutoUuidIdNotProvided(): void
{
Configuration::$autoId = AutoId::UUID;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EX('value')], TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertNotEmpty($doc->id, 'The ID should have been auto-generated');
} finally {
Configuration::$autoId = AutoId::None;
}
}
#[TestDox('Insert succeeds for object with auto UUID ID with ID provided')]
public function testInsertSucceedsForObjectWithAutoUuidIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::UUID;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$uuid = AutoId::generateUUID();
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 14)], TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals($uuid, $doc->id, 'The ID should not have been changed');
} finally {
Configuration::$autoId = AutoId::None;
}
}
#[TestDox('Insert succeeds for object with auto string ID not provided')]
public function testInsertSucceedsForObjectWithAutoStringIdNotProvided(): void
{
Configuration::$autoId = AutoId::RandomString;
Configuration::$idStringLength = 40;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 55)], TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals(40, strlen($doc->id), 'The ID should have been auto-generated and had 40 characters');
} finally {
Configuration::$autoId = AutoId::None;
Configuration::$idStringLength = 16;
}
}
#[TestDox('Insert succeeds for object with auto string ID with ID provided')]
public function testInsertSucceedsForObjectWithAutoStringIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::RandomString;
try {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 3)], TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals('my-key', $doc->id, 'The ID should not have been changed');
} finally {
Configuration::$autoId = AutoId::None;
}
}
public function testInsertFailsForDuplicateKey(): void public function testInsertFailsForDuplicateKey(): void
{ {
$this->expectException(DocumentException::class); $this->expectException(DocumentException::class);