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

276 lines
12 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Jobs;
use App\Jobs\SendMissingOrderReminderJob;
use App\Models\Customer;
use App\Models\Employee;
use App\Models\Order;
use App\Models\Shop;
use App\Notifications\MissingOrderReminderNotification;
use App\Services\IcsGeneratorService;
use Carbon\CarbonImmutable;
use Domain\Enums\OrderStatus;
use Domain\Enums\RepeatFrequency;
use Domain\Models\Entities\PickupExceptionOnDate;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\PickupExceptionId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\OrderEmployee;
use Domain\Models\ValueObjects\OrderReference;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Domain\Models\ValueObjects\ShopPickupOverride;
use Domain\Ports\Repositories\PickupExceptionRepository;
use Domain\Services\PickupSchedulingService;
use Domain\Services\ShopperService;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Repository as CacheRepository;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Money\Currency;
use Money\Money;
use Shared\Helpers\UuidHelper;
describe('SendMissingOrderReminderJob', function (): void {
it('dispatches MissingOrderReminderNotification once per week with precomputed cutoff and ICS content', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
Notification::fake();
Config::set('app.url', 'https://cocotte.example.com');
Config::set('app.name', 'Cocotte Test');
Date::setTestNow('2026-11-16 08:00:00'); // Monday 8am -> Friday 2026-11-20
Shop::factory()->create(['default' => true]);
/** @var Customer $customer */
$customer = Customer::query()->forceCreate([
'email' => new Email('reminder@example.com'),
'hashcode' => (string) Str::uuid(),
]);
/** @var Employee $employee */
$employee = Employee::query()->forceCreate([
'customer_id' => $customer->id,
'matricule' => new Matricule('1234567'),
'nom' => 'Dupont',
'prenom' => 'Jean',
'code' => new Code('ABC1234'),
'etablissement' => new ShopperEstablishmentCode('01'),
'site' => new ShopperSiteCode('SITE'),
'service' => new ShopperServiceCode('01'),
'start_date' => CarbonImmutable::now()->subMonth(),
'end_date' => CarbonImmutable::now()->addMonth(),
]);
$pickupExceptionRepo = \Mockery::mock(PickupExceptionRepository::class);
$pickupExceptionRepo->shouldReceive('getExceptionsForShop')->once()->andReturn([]);
$schedulingService = new PickupSchedulingService($pickupExceptionRepo);
$shopperService = $this->app->make(ShopperService::class);
$icsGenerator = new IcsGeneratorService('Cocotte Test');
$cache = new CacheRepository(new ArrayStore());
// WHEN
$job = new SendMissingOrderReminderJob();
$job->handle($schedulingService, $shopperService, $icsGenerator, $cache);
// Second invocation within the same week must be idempotent.
$job->handle($schedulingService, $shopperService, $icsGenerator, $cache);
// THEN
Notification::assertSentToTimes($employee, MissingOrderReminderNotification::class, 1);
Notification::assertSentTo(
$employee,
MissingOrderReminderNotification::class,
function (MissingOrderReminderNotification $notification) use ($employee): bool {
$mail = $notification->toMail($employee);
return count($mail->rawAttachments) === 1
&& $mail->rawAttachments[0]['name'] === 'rappel-commande.ics'
&& str_contains((string) $mail->rawAttachments[0]['data'], 'BEGIN:VCALENDAR');
}
);
expect($cache->has('missing-order-reminder:2026-W47:' . $employee->id))->toBeTrue();
Date::setTestNow();
});
it('skips employees when their shop is closed on pickup day without replacement', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
Notification::fake();
Config::set('app.url', 'https://cocotte.example.com');
Config::set('app.name', 'Cocotte Test');
Date::setTestNow('2026-11-16 08:00:00'); // Monday 8am -> Friday 2026-11-20
Shop::factory()->create(['default' => true]);
/** @var Customer $customer */
$customer = Customer::query()->forceCreate([
'email' => new Email('closed-shop@example.com'),
'hashcode' => (string) Str::uuid(),
]);
/** @var Employee $employee */
$employee = Employee::query()->forceCreate([
'customer_id' => $customer->id,
'matricule' => new Matricule('2234567'),
'nom' => 'Martin',
'prenom' => 'Paul',
'code' => new Code('DEF1234'),
'etablissement' => new ShopperEstablishmentCode('01'),
'site' => new ShopperSiteCode('SITE'),
'service' => new ShopperServiceCode('01'),
'start_date' => CarbonImmutable::now()->subMonth(),
'end_date' => CarbonImmutable::now()->addMonth(),
]);
$exception = new PickupExceptionOnDate(
id: new PickupExceptionId(UuidHelper::v7()),
date: CarbonImmutable::parse('2026-11-20', 'Europe/Paris'),
label: 'Fermeture exceptionnelle',
repeatable: false,
repeatFrequency: RepeatFrequency::YEARLY,
autoUpdated: false,
);
$override = new ShopPickupOverride($exception, replacementDate: null, orderDeadlineAt: null);
$pickupExceptionRepo = \Mockery::mock(PickupExceptionRepository::class);
$pickupExceptionRepo->shouldReceive('getExceptionsForShop')->once()->andReturn([$override]);
$schedulingService = new PickupSchedulingService($pickupExceptionRepo);
$shopperService = $this->app->make(ShopperService::class);
$icsGenerator = new IcsGeneratorService('Cocotte Test');
$cache = new CacheRepository(new ArrayStore());
// WHEN
new SendMissingOrderReminderJob()->handle(
$schedulingService,
$shopperService,
$icsGenerator,
$cache,
);
// THEN
Notification::assertNothingSent();
expect($cache->has('missing-order-reminder:2026-W47:' . $employee->id))->toBeFalse();
Date::setTestNow();
});
it('uses replacement pickup day and excludes employees who already ordered for replacement date', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
Notification::fake();
Config::set('app.url', 'https://cocotte.example.com');
Config::set('app.name', 'Cocotte Test');
Date::setTestNow('2026-11-16 08:00:00'); // Monday 8am -> Friday 2026-11-20 replaced by Thursday 2026-11-19
$shop = Shop::factory()->create(['default' => true]);
$orderedCustomer = Customer::query()->forceCreate([
'email' => new Email('ordered-repl@example.com'),
'hashcode' => (string) Str::uuid(),
]);
$orderedEmployee = Employee::query()->forceCreate([
'customer_id' => $orderedCustomer->id,
'matricule' => new Matricule('3234567'),
'nom' => 'Ordered',
'prenom' => 'Claire',
'code' => new Code('GHJ2345'),
'etablissement' => new ShopperEstablishmentCode('01'),
'site' => new ShopperSiteCode('SITE'),
'service' => new ShopperServiceCode('01'),
'start_date' => CarbonImmutable::now()->subMonth(),
'end_date' => CarbonImmutable::now()->addMonth(),
]);
Order::query()->forceCreate([
'id' => UuidHelper::v7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference(UuidHelper::v7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(2500, new Currency('EUR')),
'employee' => new OrderEmployee(
new Matricule('3234567'),
'Claire',
'Ordered',
new ShopperEstablishmentCode('01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('01'),
),
'delivery_address' => new DeliveryAddress('Shop', '1 rue Test', '75000', 'Paris'),
'pickup_day' => '2026-11-19',
]);
$missingCustomer = Customer::query()->forceCreate([
'email' => new Email('missing-repl@example.com'),
'hashcode' => (string) Str::uuid(),
]);
$missingEmployee = Employee::query()->forceCreate([
'customer_id' => $missingCustomer->id,
'matricule' => new Matricule('4234567'),
'nom' => 'Missing',
'prenom' => 'Luc',
'code' => new Code('KMN2345'),
'etablissement' => new ShopperEstablishmentCode('01'),
'site' => new ShopperSiteCode('SITE'),
'service' => new ShopperServiceCode('01'),
'start_date' => CarbonImmutable::now()->subMonth(),
'end_date' => CarbonImmutable::now()->addMonth(),
]);
$exception = new PickupExceptionOnDate(
id: new PickupExceptionId(UuidHelper::v7()),
date: CarbonImmutable::parse('2026-11-20', 'Europe/Paris'),
label: 'Décalage jeudi',
repeatable: false,
repeatFrequency: RepeatFrequency::YEARLY,
autoUpdated: false,
);
$override = new ShopPickupOverride(
$exception,
replacementDate: CarbonImmutable::parse('2026-11-19', 'Europe/Paris'),
orderDeadlineAt: CarbonImmutable::parse('2026-11-16 16:00:00', 'Europe/Paris'),
);
$pickupExceptionRepo = \Mockery::mock(PickupExceptionRepository::class);
$pickupExceptionRepo->shouldReceive('getExceptionsForShop')->once()->andReturn([$override]);
$schedulingService = new PickupSchedulingService($pickupExceptionRepo);
$shopperService = $this->app->make(ShopperService::class);
$icsGenerator = new IcsGeneratorService('Cocotte Test');
$cache = new CacheRepository(new ArrayStore());
// WHEN
new SendMissingOrderReminderJob()->handle(
$schedulingService,
$shopperService,
$icsGenerator,
$cache,
);
// THEN
Notification::assertNotSentTo($orderedEmployee, MissingOrderReminderNotification::class);
Notification::assertSentTo(
$missingEmployee,
MissingOrderReminderNotification::class,
function (MissingOrderReminderNotification $notification) use ($missingEmployee): bool {
$rendered = $notification->toMail($missingEmployee)->render();
return str_contains((string) $rendered, '19/11/2026')
&& str_contains((string) $rendered, '16h');
}
);
Date::setTestNow();
});
});