input mapping
This commit is contained in:
parent
bba10b455f
commit
fc46fe20ee
29 changed files with 193 additions and 887 deletions
127
tests/DataObject/DataObjectTest.php
Normal file
127
tests/DataObject/DataObjectTest.php
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\DataObject;
|
||||
|
||||
use Icefox\DTO\Attributes\FromInput;
|
||||
use Icefox\DTO\DataObjectFactory;
|
||||
use Illuminate\Support\Collection;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
readonly class Element
|
||||
{
|
||||
public function __construct(public int $value) {}
|
||||
}
|
||||
|
||||
readonly class Node
|
||||
{
|
||||
public function __construct(public Element $element) {}
|
||||
}
|
||||
|
||||
test('basic nested object', function () {
|
||||
$input = DataObjectFactory::mapInput(Node::class, ['element' => ['value' => 1 ] ], [], new NullLogger());
|
||||
expect($input)->toBe(['element' => ['value' => 1]]);
|
||||
});
|
||||
|
||||
readonly class MappedElement
|
||||
{
|
||||
public function __construct(#[FromInput('name')] public int $value) {}
|
||||
}
|
||||
|
||||
readonly class MappedNode
|
||||
{
|
||||
public function __construct(public MappedElement $element) {}
|
||||
}
|
||||
|
||||
test('basic nested input map', function () {
|
||||
$input = DataObjectFactory::mapInput(MappedNode::class, ['element' => ['name' => 1 ] ], [], new NullLogger());
|
||||
expect($input)->toBe(['element' => ['value' => 1]]);
|
||||
});
|
||||
|
||||
readonly class MappedCollectionItem
|
||||
{
|
||||
public function __construct(#[FromInput('id_item')] public int $idItem) {}
|
||||
}
|
||||
|
||||
readonly class MappedCollectionRoot
|
||||
{
|
||||
/**
|
||||
* @param Collection<int, MappedCollectionItem> $items
|
||||
*/
|
||||
public function __construct(public string $text, #[FromInput('data')] public Collection $items) {}
|
||||
}
|
||||
|
||||
test('using from input nested', function () {
|
||||
$mapped = DataObjectFactory::mapInput(MappedCollectionRoot::class, [
|
||||
'text' => 'abc',
|
||||
'data' => [
|
||||
[ 'id_item' => 1 ],
|
||||
[ 'id_item' => 2 ],
|
||||
[ 'id_item' => 4 ],
|
||||
[ 'id_item' => 8 ],
|
||||
],
|
||||
], [], new NullLogger());
|
||||
|
||||
expect($mapped)->toBe([
|
||||
'text' => 'abc',
|
||||
'items' => [
|
||||
[ 'idItem' => 1 ],
|
||||
[ 'idItem' => 2 ],
|
||||
[ 'idItem' => 4 ],
|
||||
[ 'idItem' => 8 ],
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
readonly class CollectionRoot
|
||||
{
|
||||
/**
|
||||
* @param Collection<int, MappedCollectionItem> $items
|
||||
*/
|
||||
public function __construct(public string $text, public Collection $items) {}
|
||||
}
|
||||
|
||||
test('using from input', function () {
|
||||
$mapped = DataObjectFactory::mapInput(MappedCollectionRoot::class, [
|
||||
'text' => 'abc',
|
||||
'items' => [
|
||||
[ 'id_item' => 1 ],
|
||||
[ 'id_item' => 2 ],
|
||||
[ 'id_item' => 4 ],
|
||||
[ 'id_item' => 8 ],
|
||||
],
|
||||
], [], new NullLogger());
|
||||
|
||||
expect($mapped)->toBe([
|
||||
'text' => 'abc',
|
||||
'items' => [
|
||||
[ 'idItem' => 1 ],
|
||||
[ 'idItem' => 2 ],
|
||||
[ 'idItem' => 4 ],
|
||||
[ 'idItem' => 8 ],
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
readonly class AnnotatedArrayItem
|
||||
{
|
||||
public function __construct(#[FromInput('name')] public float $value) {}
|
||||
}
|
||||
|
||||
readonly class AnnotatedArray
|
||||
{
|
||||
/**
|
||||
* @param array<int,AnnotatedArrayItem> $items
|
||||
*/
|
||||
public function __construct(public array $items) {}
|
||||
}
|
||||
|
||||
test('annotated array', function () {
|
||||
$mapped = DataObjectFactory::mapInput(
|
||||
AnnotatedArray::class,
|
||||
['items' => [['name' => 1], ['name' => 2]]],
|
||||
[],
|
||||
new NullLogger(),
|
||||
);
|
||||
|
||||
expect($mapped)->toBe(['items' => [['value' => 1], ['value' => 2]]]);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue