laravel-data/tests/DataObject/DataObjectTest.php
2026-02-25 17:47:17 -03:00

127 lines
3.1 KiB
PHP

<?php
namespace Tests\DataObject;
use Icefox\DTO\Attributes\FromInput;
use Icefox\DTO\Factories\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]]]);
});