83 lines
2.5 KiB
PHP
83 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\App\Jobs;
|
|
|
|
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
|
|
UpsertShopsPickupExceptionsUsingApiForYear::dispatchSync($year);
|
|
|
|
// 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
|
|
UpsertShopsPickupExceptionsUsingApiForYear::dispatchSync($year);
|
|
|
|
// 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);
|
|
});
|