Migrate tests to Pest (#8)
Reviewed-on: #8
This commit was merged in pull request #8.
This commit is contained in:
65
tests/Unit/Mapper/DocumentMapperTest.php
Normal file
65
tests/Unit/Mapper/DocumentMapperTest.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{DocumentException, Field};
|
||||
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
||||
use Test\{PjsonDocument, PjsonId};
|
||||
|
||||
// ** Test class hierarchy for serialization **
|
||||
|
||||
class DocMapSubDoc
|
||||
{
|
||||
public function __construct(public int $id = 0, public string $name = '') { }
|
||||
}
|
||||
|
||||
class DocMapTestDoc
|
||||
{
|
||||
public function __construct(public int $id = 0, public DocMapSubDoc $subDoc = new DocMapSubDoc()) { }
|
||||
}
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
describe('Constructor', function () {
|
||||
test('uses "data" as the default field name', function () {
|
||||
expect(new DocumentMapper(Field::class))->fieldName->toBe('data');
|
||||
});
|
||||
test('uses the provided field name', function () {
|
||||
expect(new DocumentMapper(Field::class, 'json'))->fieldName->toBe('json');
|
||||
});
|
||||
});
|
||||
|
||||
describe('->map()', function () {
|
||||
test('deserializes valid JSON', function () {
|
||||
$doc = (new DocumentMapper(DocMapTestDoc::class))
|
||||
->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']);
|
||||
expect($doc)
|
||||
->not->toBeNull()
|
||||
->id->toBe(7)
|
||||
->and($doc->subDoc)
|
||||
->not->toBeNull()
|
||||
->id->toBe(22)
|
||||
->name->toBe('tester');
|
||||
});
|
||||
test('deserializes valid JSON [Pjson]', function () {
|
||||
$doc = (new DocumentMapper(PjsonDocument::class))->map(['data' => '{"id":"seven","name":"bob","num_value":8}']);
|
||||
expect($doc)
|
||||
->not->toBeNull()
|
||||
->id->toEqual(new PjsonId('seven'))
|
||||
->name->toBe('bob')
|
||||
->numValue->toBe(8)
|
||||
->skipped->toBeEmpty();
|
||||
});
|
||||
test('throws for invalid JSON', function () {
|
||||
expect(fn() => (new DocumentMapper(DocMapTestDoc::class))->map(['data' => 'this is not valid']))
|
||||
->toThrow(DocumentException::class);
|
||||
});
|
||||
test('throws for invalid JSON [Pjson]', function () {
|
||||
expect(fn() => (new DocumentMapper(PjsonDocument::class))->map(['data' => 'not even close']))
|
||||
->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user