laravel-data/tests/DataObjectTest.php
2026-02-25 12:29:47 -03:00

44 lines
1 KiB
PHP

<?php
namespace Tests;
use Icefox\DTO\Attributes\FromInput;
use Icefox\DTO\DataObjectFactory;
use Illuminate\Support\Collection;
use Psr\Log\NullLogger;
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', 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);
expect($mapped)->toBe([
'text' => 'abc',
'items' => [
[ 'idItem' => 1 ],
[ 'idItem' => 2 ],
[ 'idItem' => 4 ],
[ 'idItem' => 8 ],
],
]);
});