http tests
This commit is contained in:
parent
b827038df3
commit
30706c3521
8 changed files with 216 additions and 214 deletions
|
|
@ -3,6 +3,7 @@
|
|||
namespace Tests\DataObject;
|
||||
|
||||
use Icefox\DTO\Attributes\FromInput;
|
||||
use Icefox\DTO\Attributes\FromRouteParameter;
|
||||
use Icefox\DTO\Factories\DataObjectFactory;
|
||||
use Illuminate\Support\Collection;
|
||||
use Psr\Log\NullLogger;
|
||||
|
|
@ -125,3 +126,70 @@ test('annotated array', function () {
|
|||
|
||||
expect($mapped)->toBe(['items' => [['value' => 1], ['value' => 2]]]);
|
||||
});
|
||||
|
||||
test('route parameter priority over from input', function () {
|
||||
$dto = new class (123) {
|
||||
public function __construct(
|
||||
#[FromRouteParameter('user_id')]
|
||||
#[FromInput('user_id')]
|
||||
public int $id,
|
||||
) {}
|
||||
};
|
||||
|
||||
$result = DataObjectFactory::mapInput(
|
||||
$dto::class,
|
||||
['user_id' => 456],
|
||||
['user_id' => 123],
|
||||
new \Psr\Log\NullLogger(),
|
||||
);
|
||||
|
||||
expect($result['id'])->toBe(123);
|
||||
});
|
||||
|
||||
test('multiple route parameters', function () {
|
||||
$dto = new class (45, 89, 'A') {
|
||||
public function __construct(
|
||||
#[FromRouteParameter('course_id')]
|
||||
public int $courseId,
|
||||
#[FromRouteParameter('student_id')]
|
||||
public int $studentId,
|
||||
#[FromInput('grade')]
|
||||
public string $grade,
|
||||
) {}
|
||||
};
|
||||
|
||||
$result = DataObjectFactory::mapInput(
|
||||
$dto::class,
|
||||
['grade' => 'A'],
|
||||
['course_id' => 45, 'student_id' => 89],
|
||||
new \Psr\Log\NullLogger(),
|
||||
);
|
||||
|
||||
expect($result['courseId'])->toBe(45)
|
||||
->and($result['studentId'])->toBe(89)
|
||||
->and($result['grade'])->toBe('A');
|
||||
});
|
||||
|
||||
test('route parameter with nested object', function () {
|
||||
$addressDto = new class ('Main St') {
|
||||
public function __construct(public string $street) {}
|
||||
};
|
||||
|
||||
$dto = new class (123, $addressDto) {
|
||||
public function __construct(
|
||||
#[FromRouteParameter('user_id')]
|
||||
public int $userId,
|
||||
public $address,
|
||||
) {}
|
||||
};
|
||||
|
||||
$result = DataObjectFactory::mapInput(
|
||||
$dto::class,
|
||||
['user_id' => 456, 'address' => ['street' => 'Main St']],
|
||||
['user_id' => 123],
|
||||
new \Psr\Log\NullLogger(),
|
||||
);
|
||||
|
||||
expect($result['userId'])->toBe(123)
|
||||
->and($result['address']['street'])->toBe('Main St');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue