ldc-cocotte/tests/Feature/Jobs/UpsertShopsPickupExceptionsUsingApiForYearTest.php

84 lines
2.5 KiB
PHP
Raw Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
use App\Jobs\UpsertShopsPickupExceptionsUsingApiForYear;
use App\Models\PickupException;
use App\Models\Shop;
use Domain\Enums\RepeatFrequency;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
uses(RefreshDatabase::class);
it('throws an exception if the year is before 2026', function (): void {
// GIVEN
$invalidYear = 2025;
// WHEN / THEN
expect(fn () => new UpsertShopsPickupExceptionsUsingApiForYear($invalidYear))
->toThrow(\InvalidArgumentException::class);
});
it('upserts holidays from the api and syncs them to all shops', function (): void {
// GIVEN
$year = 2026;
$shops = Shop::factory()->count(2)->create();
Http::fake([
'*' => Http::response([
'2026-01-01' => '1er janvier',
'2026-12-25' => 'Jour de Noël',
], 200),
]);
// WHEN
$job = new UpsertShopsPickupExceptionsUsingApiForYear($year);
$job->handle();
// 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);
expect($shops->first()->pickupExceptions()->count())->toBe(2)
->and($shops->last()->pickupExceptions()->count())->toBe(2);
});
it('updates existing pickup exceptions without creating duplicates', function (): void {
// GIVEN
$year = 2026;
$shop = Shop::factory()->create();
PickupException::factory()->create([
'label' => '1er janvier',
'date' => '2026-01-01',
'repeatable' => false,
'repeat_frequency' => 'Weekly',
]);
Http::fake([
'*' => Http::response([
'2026-01-01' => '1er janvier',
], 200),
]);
// WHEN
$job = new UpsertShopsPickupExceptionsUsingApiForYear($year);
$job->handle();
// THEN
expect(PickupException::query()->count())->toBe(1);
$updatedException = PickupException::query()->firstOrFail();
expect($updatedException->date->format('Y-m-d'))->toBe('2026-01-01')
->and($updatedException->repeatable)->toBeTrue()
->and($updatedException->repeat_frequency)->toBe(RepeatFrequency::YEARLY);
expect($shop->pickupExceptions()->count())->toBe(1);
});