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

87 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Jobs;
2026-06-14 21:32:07 +02:00
use App\Jobs\UpsertShopsPickupExceptionsUsingApiForYear;
use App\Models\PickupException;
use App\Models\Shop;
use Domain\Enums\RepeatFrequency;
use Illuminate\Support\Facades\Http;
use Webmozart\Assert\Assert;
2026-06-14 21:32:07 +02:00
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
UpsertShopsPickupExceptionsUsingApiForYear::dispatchSync($year);
2026-06-14 21:32:07 +02:00
// THEN
expect(PickupException::query()->count())->toBe(2);
$newYear = PickupException::query()->where('label', '1er janvier')->first();
Assert::notNull($newYear);
2026-06-14 21:32:07 +02:00
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);
$firstShop = $shops->first();
$lastShop = $shops->last();
Assert::notNull($firstShop);
Assert::notNull($lastShop);
expect($firstShop->pickupExceptions()->count())->toBe(2)
->and($lastShop->pickupExceptions()->count())->toBe(2);
2026-06-14 21:32:07 +02:00
});
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
UpsertShopsPickupExceptionsUsingApiForYear::dispatchSync($year);
2026-06-14 21:32:07 +02:00
// 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);
});