Add classes and tests from common project
This commit is contained in:
19
tests/unit/Mapper/ArrayMapperTest.php
Normal file
19
tests/unit/Mapper/ArrayMapperTest.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Mapper;
|
||||
|
||||
use BitBadger\PDODocument\Mapper\ArrayMapper;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the ArrayMapper class
|
||||
*/
|
||||
class ArrayMapperTest extends TestCase
|
||||
{
|
||||
public function testMapSucceeds(): void
|
||||
{
|
||||
$result = ['one' => 2, 'three' => 4, 'eight' => 'five'];
|
||||
$mapped = (new ArrayMapper())->map($result);
|
||||
$this->assertSame($result, $mapped, 'The array mapper should return the parameter given to it');
|
||||
}
|
||||
}
|
||||
57
tests/unit/Mapper/JsonMapperTest.php
Normal file
57
tests/unit/Mapper/JsonMapperTest.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Mapper;
|
||||
|
||||
use BitBadger\PDODocument\DocumentException;
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Mapper\JsonMapper;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
// ** 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 JsonMapper class
|
||||
*/
|
||||
class JsonMapperTest extends TestCase
|
||||
{
|
||||
public function testConstructorSucceedsWithDefaultField(): void
|
||||
{
|
||||
$mapper = new JsonMapper(Field::class);
|
||||
$this->assertEquals('data', $mapper->fieldName, 'Default field name should have been "data"');
|
||||
}
|
||||
|
||||
public function testConstructorSucceedsWithSpecifiedField(): void
|
||||
{
|
||||
$mapper = new JsonMapper(Field::class, 'json');
|
||||
$this->assertEquals('json', $mapper->fieldName, 'Field name not recorded correctly');
|
||||
}
|
||||
|
||||
#[TestDox('Map succeeds with valid JSON')]
|
||||
public function testMapSucceedsWithValidJSON(): void
|
||||
{
|
||||
$doc = (new JsonMapper(TestDocument::class))->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']);
|
||||
$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');
|
||||
}
|
||||
|
||||
#[TestDox('Map fails with invalid JSON')]
|
||||
public function testMapFailsWithInvalidJSON(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
(new JsonMapper(TestDocument::class))->map(['data' => 'this is not valid']);
|
||||
}
|
||||
}
|
||||
29
tests/unit/Mapper/StringMapperTest.php
Normal file
29
tests/unit/Mapper/StringMapperTest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Mapper;
|
||||
|
||||
use BitBadger\PDODocument\Mapper\StringMapper;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StringMapperTest extends TestCase
|
||||
{
|
||||
public function testMapSucceedsWhenFieldIsPresentAndString()
|
||||
{
|
||||
$result = ['test_field' => 'test_value'];
|
||||
$mapper = new StringMapper('test_field');
|
||||
$this->assertEquals('test_value', $mapper->map($result), 'String value not returned correctly');
|
||||
}
|
||||
|
||||
public function testMapSucceedsWhenFieldIsPresentAndNotString()
|
||||
{
|
||||
$result = ['a_number' => 6.7];
|
||||
$mapper = new StringMapper('a_number');
|
||||
$this->assertEquals('6.7', $mapper->map($result), 'Number value not returned correctly');
|
||||
}
|
||||
|
||||
public function testMapSucceedsWhenFieldIsNotPresent()
|
||||
{
|
||||
$mapper = new StringMapper('something_else');
|
||||
$this->assertNull($mapper->map([]), 'Missing value not returned correctly');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user