tests
This commit is contained in:
parent
6b1a385292
commit
bba10b455f
10 changed files with 296 additions and 514 deletions
|
|
@ -2,211 +2,43 @@
|
|||
|
||||
namespace Tests;
|
||||
|
||||
use Icefox\DTO\Log;
|
||||
use Icefox\DTO\RuleFactory;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tests\Classes\ArrayDataObject;
|
||||
use Tests\Classes\CollectionDataObject;
|
||||
use Tests\Classes\FromInputObject;
|
||||
use Tests\Classes\OptionalData;
|
||||
use Tests\Classes\OptionalNullableData;
|
||||
use Tests\Classes\PrimitiveData;
|
||||
use Tests\Classes\RecursiveDataObject;
|
||||
use Tests\Classes\WithMapperObject;
|
||||
use Icefox\DTO\Attributes\FromInput;
|
||||
use Icefox\DTO\DataObjectFactory;
|
||||
use Illuminate\Support\Collection;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
describe('primitive data test', function () {
|
||||
it('creates required rules', function () {
|
||||
$rules = (new RuleFactory(new Log()))->make(PrimitiveData::class);
|
||||
expect($rules)->toMatchArray([
|
||||
'string' => ['required'],
|
||||
'int' => ['required', 'numeric'],
|
||||
'float' => ['required', 'numeric'],
|
||||
'bool' => ['required', 'boolean'],
|
||||
]);
|
||||
});
|
||||
readonly class MappedCollectionItem
|
||||
{
|
||||
public function __construct(#[FromInput('id_item')] public int $idItem) {}
|
||||
}
|
||||
|
||||
it('creates object with all required properties', function () {
|
||||
$object = PrimitiveData::fromArray([
|
||||
'string' => 'abc',
|
||||
'int' => 0,
|
||||
'float' => 3.14,
|
||||
'bool' => true,
|
||||
]);
|
||||
expect($object)->toBeInstanceOf(PrimitiveData::class);
|
||||
expect($object->string)->toBe('abc');
|
||||
expect($object->int)->toBe(0);
|
||||
expect($object->float)->toEqualWithDelta(3.14, 0.0001);
|
||||
expect($object->bool)->toBeTrue();
|
||||
});
|
||||
});
|
||||
readonly class MappedCollectionRoot
|
||||
{
|
||||
/**
|
||||
* @param Collection<int, MappedCollectionItem> $items
|
||||
*/
|
||||
public function __construct(public string $text, #[FromInput('data')] public Collection $items) {}
|
||||
}
|
||||
|
||||
describe('optional data', function () {
|
||||
it('creates optional rules', function () {
|
||||
$rules = (new RuleFactory(new Log()))->make(OptionalData::class);
|
||||
expect($rules)->toMatchArray([
|
||||
'string' => ['sometimes'],
|
||||
'int' => ['sometimes', 'numeric'],
|
||||
'float' => ['sometimes', 'numeric'],
|
||||
'bool' => ['sometimes', 'boolean'],
|
||||
]);
|
||||
});
|
||||
test('using from input', function () {
|
||||
$mapped = DataObjectFactory::mapInput(MappedCollectionRoot::class, [
|
||||
'text' => 'abc',
|
||||
'data' => [
|
||||
[ 'id_item' => 1 ],
|
||||
[ 'id_item' => 2 ],
|
||||
[ 'id_item' => 4 ],
|
||||
[ 'id_item' => 8 ],
|
||||
],
|
||||
], [], new NullLogger());
|
||||
var_dump($mapped);
|
||||
|
||||
it('creates object with default values', function () {
|
||||
$object = OptionalData::fromArray([]);
|
||||
expect($object)->toBeInstanceOf(OptionalData::class);
|
||||
expect($object->string)->toBe('xyz');
|
||||
expect($object->int)->toBe(3);
|
||||
expect($object->float)->toEqualWithDelta(0.777, 0.0001);
|
||||
expect($object->bool)->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('nullable data', function () {
|
||||
it('creates nullable rules', function () {
|
||||
$rules = (new RuleFactory(new Log()))->make(OptionalNullableData::class);
|
||||
expect($rules)->toMatchArray([
|
||||
'string' => ['required'],
|
||||
'int' => ['nullable', 'numeric'],
|
||||
'float' => ['sometimes', 'numeric'],
|
||||
'bool' => ['sometimes', 'boolean'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('accepts explicit null', function () {
|
||||
$object = OptionalNullableData::fromArray([
|
||||
'string' => 'ijk',
|
||||
'int' => null,
|
||||
]);
|
||||
expect($object)->toBeInstanceOf(OptionalNullableData::class);
|
||||
expect($object->string)->toBe('ijk');
|
||||
expect($object->int)->toBeNull();
|
||||
});
|
||||
|
||||
it('accepts implicit null', function () {
|
||||
$object = OptionalNullableData::fromArray(['string' => 'dfg']);
|
||||
expect($object)->toBeInstanceOf(OptionalNullableData::class);
|
||||
expect($object->string)->toBe('dfg');
|
||||
expect($object->int)->toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('reference other DataObject', function () {
|
||||
|
||||
it('creates recursive rules', function () {
|
||||
$rules = (new RuleFactory(new Log()))->make(RecursiveDataObject::class);
|
||||
expect($rules)->toMatchArray([
|
||||
'string' => ['required'],
|
||||
'extra.string' => ['required'],
|
||||
'extra.int' => ['required', 'numeric'],
|
||||
'extra.float' => ['required', 'numeric'],
|
||||
'extra.bool' => ['required', 'boolean'],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('primitive array', function () {
|
||||
it('creates array rules', function () {
|
||||
$rules = (new RuleFactory(new Log()))->make(ArrayDataObject::class);
|
||||
expect($rules)->toMatchArray([
|
||||
'values' => ['required', 'array'],
|
||||
'values.*' => ['required', 'numeric'],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('object array', function () {
|
||||
it('creates array rules', function () {
|
||||
$rules = (new RuleFactory(new Log()))->make(CollectionDataObject::class);
|
||||
expect($rules)->toMatchArray([
|
||||
'values' => ['required', 'array'],
|
||||
'values.*' => ['required'],
|
||||
'values.*.string' => ['required'],
|
||||
'values.*.int' => ['nullable', 'numeric'],
|
||||
'values.*.float' => ['sometimes', 'numeric'],
|
||||
'values.*.bool' => ['sometimes', 'boolean'],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('can map input names', function () {
|
||||
|
||||
it('creates rules with property names', function () {
|
||||
|
||||
$rules = (new RuleFactory(new Log()))->make(FromInputObject::class);
|
||||
expect($rules)->toMatchArray([
|
||||
'text' => ['required' ],
|
||||
'standard' => ['required', 'numeric'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('maps input name', function () {
|
||||
$object = FromInputObject::fromArray([
|
||||
'other_name' => 'xyz',
|
||||
'standard' => 1,
|
||||
]);
|
||||
expect($object->text)->toBe('xyz');
|
||||
expect($object->standard)->toBe(1);
|
||||
});
|
||||
|
||||
it('prioritizes the mapped input', function () {
|
||||
$object = FromInputObject::fromArray([
|
||||
'other_name' => 'xyz',
|
||||
'text' => 'abc',
|
||||
'standard' => 1,
|
||||
]);
|
||||
expect($object->text)->toBe('xyz');
|
||||
expect($object->standard)->toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with mapper object', function () {
|
||||
it('uses mapper', function () {
|
||||
$object = WithMapperObject::fromArray([
|
||||
'period' => [
|
||||
'start' => '1980-01-01',
|
||||
'end' => '1990-01-01',
|
||||
],
|
||||
'standard' => 1,
|
||||
]);
|
||||
expect($object->period->startsAt('1980-01-01'))->toBeTrue();
|
||||
expect($object->period->endsAt('1990-01-01'))->toBeTrue();
|
||||
});
|
||||
|
||||
it('uses mapper as validator', function () {
|
||||
$object = WithMapperObject::fromArray([
|
||||
'period' => [
|
||||
'end' => '1990-01-01',
|
||||
],
|
||||
'standard' => 1,
|
||||
]);
|
||||
})->throws(ValidationException::class);
|
||||
});
|
||||
|
||||
test('failed validation throws ValidationException', function () {
|
||||
$object = PrimitiveData::fromArray([
|
||||
'int' => 0,
|
||||
'float' => 3.14,
|
||||
'bool' => true,
|
||||
]);
|
||||
})->throws(ValidationException::class);
|
||||
|
||||
|
||||
test('creates collection', function () {
|
||||
$object = CollectionDataObject::fromArray([
|
||||
'values' => [
|
||||
[
|
||||
'string' => 'x',
|
||||
'int' => 1,
|
||||
'float' => 3.3,
|
||||
],
|
||||
[
|
||||
'string' => 'y',
|
||||
'int' => null,
|
||||
],
|
||||
expect($mapped)->toBe([
|
||||
'text' => 'abc',
|
||||
'items' => [
|
||||
[ 'idItem' => 1 ],
|
||||
[ 'idItem' => 2 ],
|
||||
[ 'idItem' => 4 ],
|
||||
[ 'idItem' => 8 ],
|
||||
],
|
||||
]);
|
||||
expect($object->values->count())->toBe(2);
|
||||
expect($object->values[0]->string)->toBe('x');
|
||||
expect($object->values[1]->int)->toBeNull();
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue