feat(pickup-exceptions): implement upsert and query for public holidays

Added functionality to upsert pickup exceptions for public holidays
and query for auto-updated exceptions. This includes new classes
for handling input and service integration with the public holidays API.
This commit is contained in:
Jérémy Munsch 2026-11-15 20:04:29 +01:00
parent 43d63bce2e
commit faef51091e
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
14 changed files with 481 additions and 313 deletions

View file

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace App\Cqs\Actions\PickupExceptions;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Dto\PublicHolidayDto;
use App\Models\PickupException;
use App\Models\Shop;
use Domain\Enums\RepeatFrequency;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<
* \App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYearInput,
* void
* >
*/
final class UpsertShopsPickupExceptionsForYear implements Action
{
/**
* @param \App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYearInput $input
*/
#[\Override]
public function __invoke(ActionInput $input): void
{
Assert::isInstanceOf($input, UpsertShopsPickupExceptionsForYearInput::class);
if ($input->holidays === []) {
return;
}
foreach ($input->holidays as $holiday) {
$exception = PickupException::query()
->firstOrNew([
'date' => $holiday->date->format('Y-m-d'),
'label' => $holiday->label,
]);
$exception->fill([
'date' => $holiday->date->format('Y-m-d'),
'repeatable' => true,
'repeat_frequency' => RepeatFrequency::YEARLY,
]);
$exception->setAttribute('auto_updated', true);
$exception->saveOrFail();
}
$repeatables = PickupException::query()
->whereYear('date', $input->year)
->whereIn('label', array_map(
fn (PublicHolidayDto $holiday): string => $holiday->label,
$input->holidays
))
->get();
Shop::query()->get()
->each(fn (Shop $shop) => $shop->pickupExceptions()
->syncWithoutDetaching($repeatables));
}
}

View file

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Cqs\Actions\PickupExceptions;
use App\Contracts\ActionInput;
use App\Dto\PublicHolidayDto;
use Webmozart\Assert\Assert;
final readonly class UpsertShopsPickupExceptionsForYearInput implements ActionInput
{
/**
* @param list<PublicHolidayDto> $holidays
*/
public function __construct(
public int $year,
public array $holidays,
) {
Assert::greaterThanEq($this->year, 2026);
Assert::allIsInstanceOf($this->holidays, PublicHolidayDto::class);
}
}

View file

@ -1,130 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Cqs\Actions\PickupExceptions;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Models\PickupException;
use App\Models\Shop;
use Domain\Enums\RepeatFrequency;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<
* \App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYearInput,
* void
* >
*/
final class UpsertShopsPickupExceptionsUsingApiForYear implements Action
{
private const string CALENDAR_API_URL = 'https://calendrier.api.gouv.fr/jours-feries/metropole/:year.json';
private const int API_CALL_TIMEOUT = 120;
/**
* @param \App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYearInput $input
*/
#[\Override]
public function __invoke(ActionInput $input): void
{
Assert::isInstanceOf($input, UpsertShopsPickupExceptionsUsingApiForYearInput::class);
if (
$input->onlyIfMissing
&& PickupException::query()
->whereYear('date', $input->year)
->where('auto_updated', true)
->exists()
) {
return;
}
$holidayList = $this->getHolidaysForYear($input->year);
foreach ($holidayList as $holiday) {
$exception = PickupException::query()
->firstOrNew([
'date' => $holiday->date->format('Y-m-d'),
'label' => $holiday->label,
]);
$exception->fill([
'date' => $holiday->date->format('Y-m-d'),
'repeatable' => true,
'repeat_frequency' => RepeatFrequency::YEARLY,
]);
$exception->setAttribute('auto_updated', true);
$exception->saveOrFail();
}
$repeatablesFromApi = PickupException::query()
->whereYear('date', $input->year)
->whereIn('label', array_map(
fn ($v) => $v->label,
$holidayList
))
->get();
Shop::query()->get()
->each(fn ($shop) => $shop->pickupExceptions()
->syncWithoutDetaching($repeatablesFromApi));
}
/** @return object{date:\DateTimeInterface,label:string}[] */
private function getHolidaysForYear(int $year): array
{
$url = $this->getCalendarApiUrlFor($year);
$response = Http::timeout(self::API_CALL_TIMEOUT)->get($url);
throw_unless(
$response->successful(),
\UnexpectedValueException::class,
'Failed to fetch calendar from api'
);
$outputList = $response
->collect()
->map(fn ($label, $date) => [
'date' => $date,
'label' => $label,
])->all();
$rules = [
'*' => 'array',
'*.date' => ['required', 'date'],
'*.label' => ['required', 'string'],
];
$validator = Validator::make($outputList, $rules);
if ($validator->fails()) {
$errors = $validator->errors();
throw new \UnexpectedValueException(
'Invalid response: ' .
json_encode($errors->messages())
);
}
return array_map(fn (array $entry) => (object) [
'date' => Date::parse(
$entry['date'],
new \DateTimeZone('Europe/Paris')
),
'label' => Arr::string($entry, 'label'),
], $outputList);
}
private function getCalendarApiUrlFor(int $year): string
{
return str_replace(
':year',
strval($year),
self::CALENDAR_API_URL
);
}
}

View file

@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Cqs\Actions\PickupExceptions;
use App\Contracts\ActionInput;
use Webmozart\Assert\Assert;
final readonly class UpsertShopsPickupExceptionsUsingApiForYearInput implements ActionInput
{
public function __construct(
public int $year,
public bool $onlyIfMissing = false,
) {
Assert::greaterThanEq($this->year, 2026);
}
}

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App\Cqs\Queries\PickupExceptions;
use App\Contracts\Query;
use App\Contracts\QueryInput;
use App\Models\PickupException;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Query<
* \App\Cqs\Queries\PickupExceptions\HasAutoUpdatedPickupExceptionsForYearQueryInput,
* bool
* >
*/
final readonly class HasAutoUpdatedPickupExceptionsForYearQuery implements Query
{
/**
* @param \App\Cqs\Queries\PickupExceptions\HasAutoUpdatedPickupExceptionsForYearQueryInput $input
*/
#[\Override]
public function __invoke(QueryInput $input): bool
{
Assert::isInstanceOf($input, HasAutoUpdatedPickupExceptionsForYearQueryInput::class);
return PickupException::query()
->whereYear('date', $input->year)
->where('auto_updated', true)
->exists();
}
}

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Cqs\Queries\PickupExceptions;
use App\Contracts\QueryInput;
use Webmozart\Assert\Assert;
final readonly class HasAutoUpdatedPickupExceptionsForYearQueryInput implements QueryInput
{
public function __construct(
public int $year,
) {
Assert::greaterThanEq($this->year, 2026);
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Dto;
use Carbon\CarbonImmutable;
final readonly class PublicHolidayDto
{
public function __construct(
public CarbonImmutable $date,
public string $label,
) {
}
}

View file

@ -5,8 +5,11 @@ declare(strict_types=1);
namespace App\Jobs;
use App\Contracts\Executor;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYear as UpsertShopsPickupExceptionsAction;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYearInput;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYear;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYearInput;
use App\Cqs\Queries\PickupExceptions\HasAutoUpdatedPickupExceptionsForYearQuery;
use App\Cqs\Queries\PickupExceptions\HasAutoUpdatedPickupExceptionsForYearQueryInput;
use App\Services\PublicHolidaysApiService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Date;
@ -15,24 +18,63 @@ class EnsurePublicHolidaysSyncedJob implements ShouldQueue
{
use Queueable;
public int $tries = 3;
/**
* @var list<int>
*/
public array $backoff = [10, 30];
public function handle(
Executor $executor,
UpsertShopsPickupExceptionsAction $action,
HasAutoUpdatedPickupExceptionsForYearQuery $hasHolidaysQuery,
PublicHolidaysApiService $apiService,
UpsertShopsPickupExceptionsForYear $action,
): void {
$now = Date::now();
if ($now->year >= 2026) {
$executor->execute(
$this->syncYearIfMissing(
$now->year,
$executor,
$hasHolidaysQuery,
$apiService,
$action,
new UpsertShopsPickupExceptionsUsingApiForYearInput($now->year, onlyIfMissing: true),
);
}
if ($now->month >= 11) {
$executor->execute(
$this->syncYearIfMissing(
$now->year + 1,
$executor,
$hasHolidaysQuery,
$apiService,
$action,
new UpsertShopsPickupExceptionsUsingApiForYearInput($now->year + 1, onlyIfMissing: true),
);
}
}
private function syncYearIfMissing(
int $year,
Executor $executor,
HasAutoUpdatedPickupExceptionsForYearQuery $hasHolidaysQuery,
PublicHolidaysApiService $apiService,
UpsertShopsPickupExceptionsForYear $action,
): void {
$alreadySynced = $executor->execute(
$hasHolidaysQuery,
new HasAutoUpdatedPickupExceptionsForYearQueryInput($year),
);
if ($alreadySynced) {
return;
}
$holidays = $apiService->fetchForYear($year);
$executor->execute(
$action,
new UpsertShopsPickupExceptionsForYearInput($year, $holidays),
);
}
}

View file

@ -5,8 +5,9 @@ declare(strict_types=1);
namespace App\Jobs;
use App\Contracts\Executor;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYear as UpsertShopsPickupExceptionsAction;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYearInput;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYear;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYearInput;
use App\Services\PublicHolidaysApiService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Date;
@ -15,6 +16,13 @@ class UpsertShopsPickupExceptionsUsingApiForYear implements ShouldQueue
{
use Queueable;
public int $tries = 3;
/**
* @var list<int>
*/
public array $backoff = [10, 30];
protected int $year;
public function __construct(?int $year = null)
@ -29,11 +37,14 @@ class UpsertShopsPickupExceptionsUsingApiForYear implements ShouldQueue
public function handle(
Executor $executor,
UpsertShopsPickupExceptionsAction $action,
PublicHolidaysApiService $apiService,
UpsertShopsPickupExceptionsForYear $action,
): void {
$holidays = $apiService->fetchForYear($this->year);
$executor->execute(
$action,
new UpsertShopsPickupExceptionsUsingApiForYearInput($this->year),
new UpsertShopsPickupExceptionsForYearInput($this->year, $holidays),
);
}
}

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Dto\PublicHolidayDto;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Webmozart\Assert\Assert;
class PublicHolidaysApiService
{
private const string CALENDAR_API_URL = 'https://calendrier.api.gouv.fr/jours-feries/metropole/:year.json';
private const int API_CALL_TIMEOUT_SECONDS = 15;
/**
* @return list<PublicHolidayDto>
*/
public function fetchForYear(int $year): array
{
Assert::greaterThanEq($year, 2026);
$url = str_replace(':year', strval($year), self::CALENDAR_API_URL);
$response = Http::timeout(self::API_CALL_TIMEOUT_SECONDS)->get($url);
throw_unless(
$response->successful(),
\UnexpectedValueException::class,
'Failed to fetch calendar from api'
);
$outputList = $response
->collect()
->map(fn ($label, $date): array => [
'date' => $date,
'label' => $label,
])->values()->all();
$validator = Validator::make($outputList, [
'*' => 'array',
'*.date' => ['required', 'date'],
'*.label' => ['required', 'string'],
]);
if ($validator->fails()) {
throw new \UnexpectedValueException(
'Invalid response: ' . json_encode($validator->errors()->messages())
);
}
$timezone = new \DateTimeZone('Europe/Paris');
return array_values(array_map(
fn (array $entry): PublicHolidayDto => new PublicHolidayDto(
date: CarbonImmutable::parse(Arr::string($entry, 'date'), $timezone),
label: Arr::string($entry, 'label'),
),
$outputList
));
}
}

View file

@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
use App\Contracts\ActionInput;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYear;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYearInput;
use App\Dto\PublicHolidayDto;
use App\Models\PickupException;
use App\Models\Shop;
use Carbon\CarbonImmutable;
use Domain\Enums\RepeatFrequency;
describe('UpsertShopsPickupExceptionsForYear action', function (): void {
it('upserts provided holidays and syncs them to all shops without HTTP calls', function (): void {
// GIVEN
$shops = Shop::factory()->count(2)->create();
$holidays = [
new PublicHolidayDto(CarbonImmutable::parse('2026-01-01', 'Europe/Paris'), '1er janvier'),
new PublicHolidayDto(CarbonImmutable::parse('2026-12-25', 'Europe/Paris'), 'Jour de Noël'),
];
$action = new UpsertShopsPickupExceptionsForYear();
$input = new UpsertShopsPickupExceptionsForYearInput(2026, $holidays);
// WHEN
$action($input);
// THEN
expect(PickupException::query()->count())->toBe(2);
$newYear = PickupException::query()->where('label', '1er janvier')->first();
expect($newYear)->not->toBeNull()
->and($newYear?->date?->format('Y-m-d'))->toBe('2026-01-01')
->and($newYear?->repeatable)->toBeTrue()
->and($newYear?->repeat_frequency)->toBe(RepeatFrequency::YEARLY)
->and($newYear?->auto_updated)->toBeTrue()
->and($shops->first()?->pickupExceptions()->count())->toBe(2)
->and($shops->last()?->pickupExceptions()->count())->toBe(2);
});
it('does not overwrite previous year holidays when importing a new year and preserves shop associations', function (): void {
// GIVEN
$shop = Shop::factory()->create();
$armistice2026 = PickupException::factory()->create([
'date' => '2026-11-11',
'label' => 'Armistice',
'repeatable' => true,
'repeat_frequency' => RepeatFrequency::YEARLY,
'auto_updated' => true,
]);
$christmas2026 = PickupException::factory()->create([
'date' => '2026-12-25',
'label' => 'Jour de Noël',
'repeatable' => true,
'repeat_frequency' => RepeatFrequency::YEARLY,
'auto_updated' => true,
]);
$shop->pickupExceptions()->attach([$armistice2026->id, $christmas2026->id]);
$holidays = [
new PublicHolidayDto(CarbonImmutable::parse('2027-11-11', 'Europe/Paris'), 'Armistice'),
new PublicHolidayDto(CarbonImmutable::parse('2027-12-25', 'Europe/Paris'), 'Jour de Noël'),
];
$action = new UpsertShopsPickupExceptionsForYear();
$input = new UpsertShopsPickupExceptionsForYearInput(2027, $holidays);
// WHEN
$action($input);
// THEN
expect(PickupException::query()->count())->toBe(4)
->and(PickupException::query()->whereYear('date', 2026)->count())->toBe(2)
->and(PickupException::query()->whereYear('date', 2027)->count())->toBe(2)
->and($armistice2026->fresh()?->date?->format('Y-m-d'))->toBe('2026-11-11')
->and($christmas2026->fresh()?->date?->format('Y-m-d'))->toBe('2026-12-25')
->and($shop->pickupExceptions()->count())->toBe(4);
});
it('rejects years before 2026 in input', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new UpsertShopsPickupExceptionsForYearInput(2025, []))
->toThrow(\InvalidArgumentException::class);
});
it('throws when an invalid input is passed', function (): void {
// GIVEN
$action = new UpsertShopsPickupExceptionsForYear();
$invalidInput = new class () implements ActionInput
{
};
// WHEN
expect(
/** @phpstan-ignore argument.type */
fn () => $action($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});

View file

@ -1,154 +0,0 @@
<?php
declare(strict_types=1);
use App\Contracts\ActionInput;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYear;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYearInput;
use App\Models\PickupException;
use App\Models\Shop;
use Domain\Enums\RepeatFrequency;
use Illuminate\Support\Facades\Http;
describe('UpsertShopsPickupExceptionsUsingApiForYear action', function (): void {
it('upserts holidays from api and syncs them to all shops', function (): void {
// GIVEN
$shops = Shop::factory()->count(2)->create();
Http::fake([
'*' => Http::response([
'2026-01-01' => '1er janvier',
'2026-12-25' => 'Jour de Noël',
], 200),
]);
$action = new UpsertShopsPickupExceptionsUsingApiForYear();
$input = new UpsertShopsPickupExceptionsUsingApiForYearInput(2026);
// WHEN
$action($input);
// THEN
expect(PickupException::query()->count())->toBe(2);
$newYear = PickupException::query()->where('label', '1er janvier')->first();
expect($newYear)->not->toBeNull()
->and($newYear?->date?->format('Y-m-d'))->toBe('2026-01-01')
->and($newYear?->repeatable)->toBeTrue()
->and($newYear?->repeat_frequency)->toBe(RepeatFrequency::YEARLY)
->and($shops->first()?->pickupExceptions()->count())->toBe(2)
->and($shops->last()?->pickupExceptions()->count())->toBe(2);
});
it('does not overwrite previous year holidays when importing a new year and preserves shop associations', function (): void {
// GIVEN
$shop = Shop::factory()->create();
$armistice2026 = PickupException::factory()->create([
'date' => '2026-11-11',
'label' => 'Armistice',
'repeatable' => true,
'repeat_frequency' => RepeatFrequency::YEARLY,
'auto_updated' => true,
]);
$christmas2026 = PickupException::factory()->create([
'date' => '2026-12-25',
'label' => 'Jour de Noël',
'repeatable' => true,
'repeat_frequency' => RepeatFrequency::YEARLY,
'auto_updated' => true,
]);
$shop->pickupExceptions()->attach([$armistice2026->id, $christmas2026->id]);
Http::fake([
'https://calendrier.api.gouv.fr/jours-feries/metropole/2027.json' => Http::response([
'2027-11-11' => 'Armistice',
'2027-12-25' => 'Jour de Noël',
], 200),
]);
$action = new UpsertShopsPickupExceptionsUsingApiForYear();
$input = new UpsertShopsPickupExceptionsUsingApiForYearInput(2027);
// WHEN
$action($input);
// THEN
expect(PickupException::query()->count())->toBe(4)
->and(PickupException::query()->whereYear('date', 2026)->count())->toBe(2)
->and(PickupException::query()->whereYear('date', 2027)->count())->toBe(2)
->and($armistice2026->fresh()?->date?->format('Y-m-d'))->toBe('2026-11-11')
->and($christmas2026->fresh()?->date?->format('Y-m-d'))->toBe('2026-12-25')
->and($shop->pickupExceptions()->count())->toBe(4);
});
it('skips the api call when onlyIfMissing is true and auto-updated holidays exist for the year', function (): void {
// GIVEN
PickupException::factory()->create([
'date' => '2026-01-01',
'label' => '1er janvier',
'repeatable' => true,
'repeat_frequency' => RepeatFrequency::YEARLY,
'auto_updated' => true,
]);
Http::fake();
$action = new UpsertShopsPickupExceptionsUsingApiForYear();
$input = new UpsertShopsPickupExceptionsUsingApiForYearInput(2026, onlyIfMissing: true);
// WHEN
$action($input);
// THEN
Http::assertNothingSent();
expect(PickupException::query()->count())->toBe(1);
});
it('calls the api when onlyIfMissing is true and no auto-updated holidays exist for the year', function (): void {
// GIVEN
PickupException::factory()->create([
'date' => '2026-05-01',
'label' => 'Manual exception',
'repeatable' => false,
'repeat_frequency' => RepeatFrequency::YEARLY,
'auto_updated' => false,
]);
Http::fake([
'https://calendrier.api.gouv.fr/jours-feries/metropole/2026.json' => Http::response([
'2026-01-01' => '1er janvier',
], 200),
]);
$action = new UpsertShopsPickupExceptionsUsingApiForYear();
$input = new UpsertShopsPickupExceptionsUsingApiForYearInput(2026, onlyIfMissing: true);
// WHEN
$action($input);
// THEN
Http::assertSentCount(1);
expect(PickupException::query()->whereYear('date', 2026)->where('auto_updated', true)->count())->toBe(1);
});
it('rejects years before 2026 in input', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new UpsertShopsPickupExceptionsUsingApiForYearInput(2025))
->toThrow(\InvalidArgumentException::class);
});
it('throws when an invalid input is passed', function (): void {
// GIVEN
$action = new UpsertShopsPickupExceptionsUsingApiForYear();
$invalidInput = new class () implements ActionInput
{
};
// WHEN
expect(
/** @phpstan-ignore argument.type */
fn () => $action($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});

View file

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
use App\Cqs\Queries\PickupExceptions\HasAutoUpdatedPickupExceptionsForYearQuery;
use App\Cqs\Queries\PickupExceptions\HasAutoUpdatedPickupExceptionsForYearQueryInput;
use App\Models\PickupException;
use Domain\Enums\RepeatFrequency;
describe('HasAutoUpdatedPickupExceptionsForYearQuery', function (): void {
it('returns true only when auto-updated pickup exceptions exist for the given year', function (): void {
// GIVEN
PickupException::factory()->create([
'date' => '2026-05-01',
'label' => 'Manual exception',
'repeatable' => false,
'repeat_frequency' => RepeatFrequency::YEARLY,
'auto_updated' => false,
]);
$query = new HasAutoUpdatedPickupExceptionsForYearQuery();
// WHEN
$beforeAuto = $query(new HasAutoUpdatedPickupExceptionsForYearQueryInput(2026));
PickupException::factory()->create([
'date' => '2026-01-01',
'label' => '1er janvier',
'repeatable' => true,
'repeat_frequency' => RepeatFrequency::YEARLY,
'auto_updated' => true,
]);
$afterAuto = $query(new HasAutoUpdatedPickupExceptionsForYearQueryInput(2026));
// THEN
expect($beforeAuto)->toBeFalse()
->and($afterAuto)->toBeTrue();
});
});

View file

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
use App\Dto\PublicHolidayDto;
use App\Services\PublicHolidaysApiService;
use Carbon\CarbonImmutable;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
describe('PublicHolidaysApiService', function (): void {
it('fetches and parses public holidays with a 15-second timeout', function (): void {
// GIVEN
Http::fake([
'https://calendrier.api.gouv.fr/jours-feries/metropole/2026.json' => Http::response([
'2026-01-01' => '1er janvier',
'2026-12-25' => 'Jour de Noël',
], 200),
]);
$service = new PublicHolidaysApiService();
// WHEN
$holidays = $service->fetchForYear(2026);
// THEN
expect($holidays)->toHaveCount(2)
->and($holidays[0])->toBeInstanceOf(PublicHolidayDto::class)
->and($holidays[0]->date)->toBeInstanceOf(CarbonImmutable::class)
->and($holidays[0]->date->format('Y-m-d'))->toBe('2026-01-01')
->and($holidays[0]->label)->toBe('1er janvier')
->and($holidays[1]->date->format('Y-m-d'))->toBe('2026-12-25')
->and($holidays[1]->label)->toBe('Jour de Noël');
Http::assertSent(fn (Request $request): bool => $request->url() === 'https://calendrier.api.gouv.fr/jours-feries/metropole/2026.json');
});
it('throws when the API returns a non-successful status', function (): void {
// GIVEN
Http::fake([
'*' => Http::response([], 503),
]);
$service = new PublicHolidaysApiService();
// WHEN / THEN
expect(fn () => $service->fetchForYear(2026))
->toThrow(\UnexpectedValueException::class, 'Failed to fetch calendar from api');
});
it('throws when year is before 2026', function (): void {
// GIVEN
$service = new PublicHolidaysApiService();
// WHEN / THEN
expect(fn () => $service->fetchForYear(2025))
->toThrow(\InvalidArgumentException::class);
});
});