Migrate tests to Pest
This commit is contained in:
18
tests/Unit/Mapper/ArrayMapperTest.php
Normal file
18
tests/Unit/Mapper/ArrayMapperTest.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\Mapper\ArrayMapper;
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
describe('->map()', function () {
|
||||
test('returns the given array', function () {
|
||||
$result = ['one' => 2, 'three' => 4, 'eight' => 'five'];
|
||||
expect(new ArrayMapper()->map($result))->toBe($result);
|
||||
});
|
||||
});
|
||||
17
tests/Unit/Mapper/CountMapperTest.php
Normal file
17
tests/Unit/Mapper/CountMapperTest.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\Mapper\CountMapper;
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
describe('->map()', function () {
|
||||
test('returns item 0 in the given array', function () {
|
||||
expect(new CountMapper()->map([5, 8, 10]))->toBe(5);
|
||||
});
|
||||
});
|
||||
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);
|
||||
});
|
||||
});
|
||||
28
tests/Unit/Mapper/ExistsMapperTest.php
Normal file
28
tests/Unit/Mapper/ExistsMapperTest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
|
||||
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
afterEach(function () { Configuration::overrideMode(null); });
|
||||
|
||||
describe('->map()', function () {
|
||||
test('returns a boolean value from index 0 [PostgreSQL]', function () {
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
expect(new ExistsMapper()->map([false, 'nope']))->toBeFalse();
|
||||
});
|
||||
test('returns a number value as boolean from index 0 [SQLite]', function () {
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
expect(new ExistsMapper()->map([1, 'yep']))->toBeTrue();
|
||||
});
|
||||
test('throws if mode is not set', function () {
|
||||
expect(fn() => new ExistsMapper()->map(['0']))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
23
tests/Unit/Mapper/StringMapperTest.php
Normal file
23
tests/Unit/Mapper/StringMapperTest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\Mapper\StringMapper;
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
describe('->map()', function () {
|
||||
test('returns existing string column value', function () {
|
||||
expect(new StringMapper('test_field'))->map(['test_field' => 'test_value'])->toBe('test_value');
|
||||
});
|
||||
test('returns string value of non-string column', function () {
|
||||
expect(new StringMapper('a_number'))->map(['a_number' => 6.7])->toBe('6.7');
|
||||
});
|
||||
test('returns null for a missing column', function () {
|
||||
expect(new StringMapper('something_else'))->map([])->toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user