Add pjson support

This commit is contained in:
2024-06-29 11:46:16 -04:00
parent 50854275a8
commit 478684621c
8 changed files with 146 additions and 12 deletions

View File

@@ -6,6 +6,7 @@ use BitBadger\PDODocument\{DocumentException, Field};
use BitBadger\PDODocument\Mapper\DocumentMapper;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Test\{PjsonDocument, PjsonId};
// ** Test class hierarchy for serialization **
@@ -48,10 +49,28 @@ class DocumentMapperTest extends TestCase
$this->assertEquals('tester', $doc->subDoc->name, 'Sub-document name not filled correctly');
}
#[TestDox('Map succeeds with valid JSON for pjson class')]
public function testMapSucceedsWithValidJSONForPjsonClass(): void
{
$doc = (new DocumentMapper(PjsonDocument::class))->map(['data' => '{"id":"seven","name":"bob","num_value":8}']);
$this->assertNotNull($doc, 'The document should not have been null');
$this->assertEquals(new PjsonId('seven'), $doc->id, 'ID not filled correctly');
$this->assertEquals('bob', $doc->name, 'Name not filled correctly');
$this->assertEquals(8, $doc->numValue, 'Numeric value not filled correctly');
$this->assertFalse(isset($doc->skipped), 'Non-JSON field has not been set');
}
#[TestDox('Map fails with invalid JSON')]
public function testMapFailsWithInvalidJSON(): void
{
$this->expectException(DocumentException::class);
(new DocumentMapper(TestDocument::class))->map(['data' => 'this is not valid']);
}
#[TestDox('Map fails with invalid JSON for pjson class')]
public function testMapFailsWithInvalidJSONForPjsonClass(): void
{
$this->expectException(DocumentException::class);
(new DocumentMapper(PjsonDocument::class))->map(['data' => 'not even close']);
}
}