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

72 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Dto;
use App\Dto\ShopDto;
use App\Models\PickupException;
use App\Models\Shop;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
describe('ShopDto Integration', function () {
test('it maps from Shop Eloquent model correctly', function () {
// GIVEN
$shop = new Shop();
$shop->name = 'Boutique Centre';
$shop->address = '10 rue Centrale';
$shop->postcode = '69002';
$shop->city = 'Lyon';
$shop->phone = '+33472000000';
$shop->default = true;
$shop->save();
// WHEN
$dto = ShopDto::fromModel($shop);
// THEN
expect($dto->id)->toBe($shop->id)
->and($dto->name)->toBe('Boutique Centre')
->and($dto->address)->toBe('10 rue Centrale')
->and($dto->postcode)->toBe('69002')
->and($dto->city)->toBe('Lyon')
->and($dto->phone)->toBe('+33472000000')
->and($dto->default)->toBeTrue()
->and($dto->getRouteKey())->toBe($shop->id)
->and($dto->getRouteKeyName())->toBe('id')
->and($dto->toArray())->toBe([
'id' => $shop->id,
'name' => 'Boutique Centre',
'address' => '10 rue Centrale',
'postcode' => '69002',
'city' => 'Lyon',
'phone' => '+33472000000',
'latitude' => null,
'longitude' => null,
'default' => true,
]);
});
test('it maps loaded pickupExceptions relation correctly', function () {
// GIVEN
$shop = Shop::factory()->create();
$exception = PickupException::factory()->create();
$shop->pickupExceptions()->attach($exception->id, [
'replacement_date' => '2026-06-01',
'order_deadline_at' => '2026-05-31 12:00:00',
]);
$shop->load('pickupExceptions');
// WHEN
$dto = ShopDto::fromModel($shop);
// THEN
expect($dto->pickupExceptions)->not->toBeNull()
->and($dto->pickupExceptions)->toHaveCount(1)
->and($dto->pickupExceptions->first()->id)->toBe($exception->id)
->and($dto->pickupExceptions->first()->pivot)->not->toBeNull()
->and($dto->pickupExceptions->first()->pivot->replacement_date?->format('Y-m-d'))->toBe('2026-06-01');
});
});