ldc-cocotte/tests/Integration/WithApp/App/Cqs/Queries/Timeslots/GetAvailableTimeslotsQueryTest.php

134 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Queries\Timeslots;
use App\Contracts\QueryInput;
use App\Cqs\Queries\Timeslots\GetAvailableTimeslotsQuery;
use App\Cqs\Queries\Timeslots\GetAvailableTimeslotsQueryInput;
use App\Models\PickupException;
use App\Models\Shop;
use Carbon\CarbonImmutable;
use Domain\Enums\RepeatFrequency;
use Domain\Models\ValueObjects\Entities\ShopId;
use Domain\Models\ValueObjects\PickupTimeSlot;
use Domain\Services\PickupSchedulingService;
use Illuminate\Support\Carbon;
use Infrastructure\Adapters\Repositories\EloquentPickupExceptionRepository;
use Infrastructure\Adapters\Repositories\EloquentShopRepository;
use Shared\Helpers\UuidHelper;
afterEach(function (): void {
CarbonImmutable::setTestNow();
});
describe('GetAvailableTimeslotsQuery', function (): void {
it('returns an empty timeslotList when the shop does not exist', function (): void {
// GIVEN
CarbonImmutable::setTestNow('2026-06-15 10:00:00');
$query = new GetAvailableTimeslotsQuery(
new PickupSchedulingService(new EloquentPickupExceptionRepository()),
new EloquentShopRepository(),
);
$input = new GetAvailableTimeslotsQueryInput(new ShopId(UuidHelper::v7()));
// WHEN
$result = $query($input);
// THEN
expect($result->timeslotList)->toBe([]);
});
it('returns available PickupTimeSlot instances up to 28 days for an active shop', function (): void {
// GIVEN
$now = CarbonImmutable::parse('2026-06-15 10:00:00');
CarbonImmutable::setTestNow($now);
$shop = new Shop();
$shop->name = 'Boutique Active';
$shop->address = '10 rue Principale';
$shop->postcode = '75001';
$shop->city = 'Paris';
$shop->default = true;
$shop->save();
$query = new GetAvailableTimeslotsQuery(
new PickupSchedulingService(new EloquentPickupExceptionRepository()),
new EloquentShopRepository(),
);
$input = new GetAvailableTimeslotsQueryInput(new ShopId(UuidHelper::fromString($shop->id)));
// WHEN
$result = $query($input);
// THEN
$maxDate = $now->addDays(28);
expect($result->timeslotList)->not->toBeEmpty();
foreach ($result->timeslotList as $slot) {
expect($slot->getAt()->getTimestamp())->toBeLessThanOrEqual($maxDate->getTimestamp());
}
});
it('respects pickup exceptions attached to the shop', function (): void {
// GIVEN
CarbonImmutable::setTestNow('2026-06-15 10:00:00');
$shop = new Shop();
$shop->name = 'Boutique Exception';
$shop->address = '10 rue Principale';
$shop->postcode = '75001';
$shop->city = 'Paris';
$shop->default = true;
$shop->save();
$exception = new PickupException();
$exception->date = Carbon::parse('2026-06-19');
$exception->label = 'Fermeture exceptionnelle';
$exception->repeatable = false;
$exception->repeat_frequency = RepeatFrequency::YEARLY;
$exception->auto_updated = false;
$exception->save();
$shop->pickupExceptions()->attach($exception->id, [
'replacement_date' => '2026-06-20',
'order_deadline_at' => '2026-06-18 12:00:00',
]);
$query = new GetAvailableTimeslotsQuery(
new PickupSchedulingService(new EloquentPickupExceptionRepository()),
new EloquentShopRepository(),
);
$input = new GetAvailableTimeslotsQueryInput(new ShopId(UuidHelper::fromString($shop->id)));
// WHEN
$result = $query($input);
// THEN
$dates = array_map(
static fn (PickupTimeSlot $slot): string => $slot->getAt()->format('Y-m-d'),
$result->timeslotList,
);
expect($dates)->not->toContain('2026-06-19')
->and($dates)->toContain('2026-06-20');
});
it('throws InvalidArgumentException when invalid input is passed', function (): void {
// GIVEN
$query = new GetAvailableTimeslotsQuery(
new PickupSchedulingService(new EloquentPickupExceptionRepository()),
new EloquentShopRepository(),
);
$invalidInput = new class () implements QueryInput
{
};
// WHEN & THEN
expect(
/** @phpstan-ignore argument.type */
fn () => $query($invalidInput)
)->toThrow(\InvalidArgumentException::class);
});
});