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\Unit\Mapper;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\{DocumentException, Field};
|
|
|
|
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2024-06-29 15:46:16 +00:00
|
|
|
use Test\{PjsonDocument, PjsonId};
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
// ** Test class hierarchy for serialization **
|
|
|
|
|
|
|
|
class SubDocument
|
|
|
|
{
|
|
|
|
public function __construct(public int $id = 0, public string $name = '') { }
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestDocument
|
|
|
|
{
|
|
|
|
public function __construct(public int $id = 0, public SubDocument $subDoc = new SubDocument()) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the DocumentMapper class
|
|
|
|
*/
|
2024-06-21 13:46:41 +00:00
|
|
|
#[TestDox('Document Mapper (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class DocumentMapperTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testConstructorSucceedsWithDefaultField(): void
|
|
|
|
{
|
|
|
|
$mapper = new DocumentMapper(Field::class);
|
|
|
|
$this->assertEquals('data', $mapper->fieldName, 'Default field name should have been "data"');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructorSucceedsWithSpecifiedField(): void
|
|
|
|
{
|
|
|
|
$mapper = new DocumentMapper(Field::class, 'json');
|
|
|
|
$this->assertEquals('json', $mapper->fieldName, 'Field name not recorded correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('map() succeeds with valid JSON')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testMapSucceedsWithValidJSON(): void
|
|
|
|
{
|
2024-10-02 01:37:08 +00:00
|
|
|
$doc = new DocumentMapper(TestDocument::class)->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']);
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertNotNull($doc, 'The document should not have been null');
|
|
|
|
$this->assertEquals(7, $doc->id, 'ID not filled correctly');
|
|
|
|
$this->assertNotNull($doc->subDoc, 'The sub-document should not have been null');
|
|
|
|
$this->assertEquals(22, $doc->subDoc->id, 'Sub-document ID not filled correctly');
|
|
|
|
$this->assertEquals('tester', $doc->subDoc->name, 'Sub-document name not filled correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('map() succeeds with valid JSON for Pjson class')]
|
2024-06-29 15:46:16 +00:00
|
|
|
public function testMapSucceedsWithValidJSONForPjsonClass(): void
|
|
|
|
{
|
2024-10-02 01:37:08 +00:00
|
|
|
$doc = new DocumentMapper(PjsonDocument::class)->map(['data' => '{"id":"seven","name":"bob","num_value":8}']);
|
2024-06-29 15:46:16 +00:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('map() fails with invalid JSON')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testMapFailsWithInvalidJSON(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
2024-10-02 01:37:08 +00:00
|
|
|
new DocumentMapper(TestDocument::class)->map(['data' => 'this is not valid']);
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
2024-06-29 15:46:16 +00:00
|
|
|
|
2024-09-27 02:15:00 +00:00
|
|
|
#[TestDox('map() fails with invalid JSON for Pjson class')]
|
2024-06-29 15:46:16 +00:00
|
|
|
public function testMapFailsWithInvalidJSONForPjsonClass(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
2024-10-02 01:37:08 +00:00
|
|
|
new DocumentMapper(PjsonDocument::class)->map(['data' => 'not even close']);
|
2024-06-29 15:46:16 +00:00
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|