ldc-cocotte/tests/Integration/WithApp/App/Dto/PickupExceptionDtoTest.php

57 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Dto;
use App\Dto\PickupExceptionDto;
use App\Models\PickupException;
use App\Models\Shop;
describe('PickupExceptionDto Integration', function () {
test('fromModel works without pivot loaded', function () {
// GIVEN
$exception = PickupException::factory()->create();
// WHEN
$dto = PickupExceptionDto::fromModel($exception);
// THEN
$array = $dto->toArray();
expect($dto->id)->toBe($exception->id)
->and($dto->label)->toBe($exception->label)
->and($dto->pivot)->toBeNull()
->and($dto->getRouteKey())->toBe($exception->id)
->and($dto->getRouteKeyName())->toBe('id')
->and($dto->resolveRouteBinding('some-id'))->toBeNull()
->and($dto->resolveChildRouteBinding('child', 'some-id', 'field'))->toBeNull()
->and($array)->toHaveKey('id', $exception->id)
->and($array)->toHaveKey('label', $exception->label)
->and($array)->toHaveKey('repeat_frequency', $exception->repeat_frequency->value);
});
test('fromModel works with pivot loaded from shop relationship', function () {
// GIVEN
$shop = Shop::factory()->create();
$exception = PickupException::factory()->create();
$shop->pickupExceptions()->attach($exception->id, [
'replacement_date' => '2026-05-02',
'order_deadline_at' => '2026-05-01 18:00:00',
]);
$loadedShop = Shop::query()->with('pickupExceptions')->find($shop->id);
$loadedException = $loadedShop?->pickupExceptions->first();
assert($loadedException !== null);
// WHEN
$dto = PickupExceptionDto::fromModel($loadedException);
// THEN
expect($dto->id)->toBe($exception->id)
->and($dto->pivot)->not->toBeNull()
->and($dto->pivot?->replacement_date?->format('Y-m-d'))->toBe('2026-05-02')
->and($dto->pivot?->order_deadline_at?->format('Y-m-d H:i:s'))->toBe('2026-05-01 18:00:00');
});
});