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

75 lines
2.9 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Jobs\SendMissingOrderReminderJob;
use App\Models\Customer;
use App\Models\Employee;
use App\Notifications\MissingOrderReminderNotification;
use App\Services\IcsGeneratorService;
use Carbon\CarbonImmutable;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Domain\Ports\Repositories\PickupExceptionRepository;
use Domain\Services\PickupSchedulingService;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
describe('SendMissingOrderReminderJob', function (): void {
it('dispatches MissingOrderReminderNotification with precomputed cutoff and ICS content', function (): void {
// 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
/** @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);
$schedulingService = new PickupSchedulingService($pickupExceptionRepo);
$icsGenerator = new IcsGeneratorService('Cocotte Test');
// WHEN
new SendMissingOrderReminderJob()->handle($schedulingService, $icsGenerator);
// THEN
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');
}
);
Date::setTestNow();
});
});