2026-11-15 19:49:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:26:35 +01:00
|
|
|
namespace Tests\Feature\App\Jobs;
|
|
|
|
|
|
2026-11-15 19:49:48 +01:00
|
|
|
use App\Jobs\SendMissingOrderReminderJob;
|
|
|
|
|
use App\Models\Customer;
|
|
|
|
|
use App\Models\Employee;
|
2026-11-15 21:39:33 +01:00
|
|
|
use App\Models\Order;
|
|
|
|
|
use App\Models\Shop;
|
2026-11-15 19:49:48 +01:00
|
|
|
use App\Notifications\MissingOrderReminderNotification;
|
|
|
|
|
use App\Services\IcsGeneratorService;
|
|
|
|
|
use Carbon\CarbonImmutable;
|
2026-11-15 21:39:33 +01:00
|
|
|
use Domain\Enums\OrderStatus;
|
|
|
|
|
use Domain\Enums\RepeatFrequency;
|
|
|
|
|
use Domain\Models\Entities\PickupExceptionOnDate;
|
2026-11-15 19:49:48 +01:00
|
|
|
use Domain\Models\ValueObjects\Code;
|
2026-11-15 21:39:33 +01:00
|
|
|
use Domain\Models\ValueObjects\DeliveryAddress;
|
2026-11-15 19:49:48 +01:00
|
|
|
use Domain\Models\ValueObjects\Email;
|
2026-11-15 21:39:33 +01:00
|
|
|
use Domain\Models\ValueObjects\Entities\PickupExceptionId;
|
2026-11-15 19:49:48 +01:00
|
|
|
use Domain\Models\ValueObjects\Matricule;
|
2026-11-15 21:39:33 +01:00
|
|
|
use Domain\Models\ValueObjects\OrderEmployee;
|
|
|
|
|
use Domain\Models\ValueObjects\OrderReference;
|
2026-11-15 19:49:48 +01:00
|
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperServiceCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperSiteCode;
|
2026-11-15 21:39:33 +01:00
|
|
|
use Domain\Models\ValueObjects\ShopPickupOverride;
|
2026-11-15 19:49:48 +01:00
|
|
|
use Domain\Ports\Repositories\PickupExceptionRepository;
|
|
|
|
|
use Domain\Services\PickupSchedulingService;
|
2026-11-15 21:39:33 +01:00
|
|
|
use Domain\Services\ShopperService;
|
|
|
|
|
use Illuminate\Cache\ArrayStore;
|
|
|
|
|
use Illuminate\Cache\Repository as CacheRepository;
|
2026-11-15 19:49:48 +01:00
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
|
use Illuminate\Support\Facades\Date;
|
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
|
use Illuminate\Support\Str;
|
2026-11-15 21:39:33 +01:00
|
|
|
use Money\Currency;
|
|
|
|
|
use Money\Money;
|
2026-11-17 12:15:39 +01:00
|
|
|
use Shared\Helpers\UuidHelper;
|
2026-11-15 19:49:48 +01:00
|
|
|
|
|
|
|
|
describe('SendMissingOrderReminderJob', function (): void {
|
2026-11-15 21:39:33 +01:00
|
|
|
it('dispatches MissingOrderReminderNotification once per week with precomputed cutoff and ICS content', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
2026-11-15 19:49:48 +01:00
|
|
|
// GIVEN
|
|
|
|
|
Notification::fake();
|
|
|
|
|
Config::set('app.url', 'https://cocotte.example.com');
|
|
|
|
|
Config::set('app.name', 'Cocotte Test');
|
2026-11-15 21:39:33 +01:00
|
|
|
Date::setTestNow('2026-11-16 08:00:00'); // Monday 8am -> Friday 2026-11-20
|
|
|
|
|
|
|
|
|
|
Shop::factory()->create(['default' => true]);
|
2026-11-15 19:49:48 +01:00
|
|
|
|
|
|
|
|
/** @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);
|
2026-11-15 21:39:33 +01:00
|
|
|
$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());
|
2026-11-15 19:49:48 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
2026-11-15 21:39:33 +01:00
|
|
|
$job = new SendMissingOrderReminderJob();
|
|
|
|
|
$job->handle($schedulingService, $shopperService, $icsGenerator, $cache);
|
|
|
|
|
// Second invocation within the same week must be idempotent.
|
|
|
|
|
$job->handle($schedulingService, $shopperService, $icsGenerator, $cache);
|
2026-11-15 19:49:48 +01:00
|
|
|
|
|
|
|
|
// THEN
|
2026-11-15 21:39:33 +01:00
|
|
|
Notification::assertSentToTimes($employee, MissingOrderReminderNotification::class, 1);
|
2026-11-15 19:49:48 +01:00
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-15 21:39:33 +01:00
|
|
|
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(
|
2026-11-17 12:15:39 +01:00
|
|
|
id: new PickupExceptionId(UuidHelper::v7()),
|
2026-11-15 21:39:33 +01:00
|
|
|
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(),
|
|
|
|
|
]);
|
|
|
|
|
|
2026-11-16 15:54:41 +01:00
|
|
|
Order::query()->forceCreate([
|
2026-11-17 12:15:39 +01:00
|
|
|
'id' => UuidHelper::v7()->toString(),
|
2026-11-15 21:39:33 +01:00
|
|
|
'shop_id' => $shop->id,
|
2026-11-17 12:15:39 +01:00
|
|
|
'reference' => new OrderReference(UuidHelper::v7()),
|
2026-11-15 21:39:33 +01:00
|
|
|
'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(
|
2026-11-17 12:15:39 +01:00
|
|
|
id: new PickupExceptionId(UuidHelper::v7()),
|
2026-11-15 21:39:33 +01:00
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-15 19:49:48 +01:00
|
|
|
Date::setTestNow();
|
|
|
|
|
});
|
|
|
|
|
});
|