use Data namespace

This commit is contained in:
icefox 2026-03-07 11:27:12 -03:00
parent 71d49def6b
commit a5b80681c9
No known key found for this signature in database
21 changed files with 283 additions and 68 deletions

View file

@ -2,9 +2,9 @@
namespace Tests\DataObject;
use Icefox\DTO\Attributes\FromInput;
use Icefox\DTO\Attributes\FromRouteParameter;
use Icefox\DTO\Factories\DataObjectFactory;
use Icefox\Data\Attributes\FromInput;
use Icefox\Data\Attributes\FromRouteParameter;
use Icefox\Data\Factories\DataObjectFactory;
use Illuminate\Support\Collection;
use Psr\Log\NullLogger;
@ -193,3 +193,208 @@ test('route parameter with nested object', function () {
expect($result['userId'])->toBe(123)
->and($result['address']['street'])->toBe('Main St');
});
readonly class SimpleWithDefaults
{
public function __construct(
public string $name,
public int $age,
public string $city = 'Unknown',
) {}
/**
* @return array<string,mixed>
*/
public static function defaults(): array
{
return [
'city' => 'New York',
'age' => 25,
];
}
}
test('defaults with fromArray - basic usage', function () {
$object = DataObjectFactory::fromArray(
SimpleWithDefaults::class,
['name' => 'John'],
[],
);
expect($object->name)->toBe('John')
->and($object->age)->toBe(25)
->and($object->city)->toBe('New York');
});
readonly class NestedWithDefaults
{
public function __construct(
public string $title,
public SimpleWithDefaults $user,
) {}
/**
* @return array<string,array<string,mixed>>
*/
public static function defaults(): array
{
return [
'user' => [
'name' => 'Default User',
'age' => 30,
],
];
}
}
test('defaults with nested objects', function () {
$object = DataObjectFactory::fromArray(
NestedWithDefaults::class,
['title' => 'Admin Dashboard'],
[],
);
expect($object->title)->toBe('Admin Dashboard')
->and($object->user->name)->toBe('Default User')
->and($object->user->age)->toBe(30)
->and($object->user->city)->toBe('Unknown');
});
test('defaults merged with input - input overrides defaults', function () {
$object = DataObjectFactory::fromArray(
SimpleWithDefaults::class,
['name' => 'Alice', 'age' => 40, 'city' => 'Los Angeles'],
[],
);
expect($object->name)->toBe('Alice')
->and($object->age)->toBe(40)
->and($object->city)->toBe('Los Angeles');
});
test('defaults with simple nested structures', function () {
$object = DataObjectFactory::fromArray(
NestedWithDefaults::class,
['title' => 'Simple Project'],
[],
);
expect($object->title)->toBe('Simple Project')
->and($object->user->name)->toBe('Default User')
->and($object->user->age)->toBe(30);
});
readonly class ArrayWithDefaults
{
/**
* @param array<string,mixed> $settings
*/
public function __construct(
public string $projectName,
public array $settings,
) {}
/**
* @return array<string,array<string,mixed>>
*/
public static function defaults(): array
{
return [
'settings' => [
'theme' => 'dark',
'notifications' => true,
'language' => 'en',
],
];
}
}
test('defaults with array structures', function () {
$object = DataObjectFactory::fromArray(
ArrayWithDefaults::class,
['projectName' => 'New Project'],
[],
);
expect($object->projectName)->toBe('New Project')
->and($object->settings)->toBe([
'theme' => 'dark',
'notifications' => true,
'language' => 'en',
]);
});
test('defaults partially overridden by input', function () {
$object = DataObjectFactory::fromArray(
ArrayWithDefaults::class,
[
'projectName' => 'Custom Project',
'settings' => [
'theme' => 'light',
'language' => 'fr',
],
],
[],
);
expect($object->projectName)->toBe('Custom Project')
->and($object->settings)->toBe([
'theme' => 'light',
'notifications' => true, // From defaults
'language' => 'fr',
]);
});
readonly class WithInputMappingAndDefaults
{
public function __construct(
#[FromInput('full_name')]
public string $name,
#[FromInput('user_age')]
public int $age,
public string $role = 'user',
) {}
/**
* @return array<string,mixed>
*/
public static function defaults(): array
{
return [
'name' => 'Default Name',
'role' => 'admin',
'age' => 18,
];
}
}
test('defaults work with FromInput attribute mapping', function () {
$object = DataObjectFactory::fromArray(
WithInputMappingAndDefaults::class,
['full_name' => 'John Doe'],
[],
);
expect($object->name)->toBe('John Doe') // From input mapping
->and($object->age)->toBe(18) // From defaults
->and($object->role)->toBe('admin'); // From defaults
});
test('array_merge_recursive behavior with nested arrays', function () {
$object = DataObjectFactory::fromArray(
ArrayWithDefaults::class,
[
'projectName' => 'Test Project',
'settings' => [
'new_setting' => 'custom_value',
],
],
[],
);
expect($object->settings)->toBe([
'theme' => 'dark', // From defaults
'notifications' => true, // From defaults
'language' => 'en', // From defaults
'new_setting' => 'custom_value', // From input
]);
});