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

91 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Contracts\Executor;
use App\Cqs\Actions\Orders\ClaimTodayOrderReminder;
use App\Jobs\SendReminderMail;
use App\Models\Customer;
use App\Models\Employee;
use App\Models\Order;
use App\Models\Shop;
use App\Notifications\OrderTodayReminderNotification;
use Domain\Enums\OrderStatus;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\Email;
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 Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Money\Currency;
use Money\Money;
use Money\MoneyFormatter;
use Ramsey\Uuid\Uuid;
describe('SendReminderMail job', function (): void {
it('sends today reminder once and marks orders as reminded idempotently', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
Notification::fake();
Date::setTestNow('2026-11-20 10:00:00');
$shop = Shop::factory()->create();
$customer = Customer::query()->forceCreate([
'email' => new Email('today-reminder@example.com'),
'hashcode' => (string) Str::uuid(),
]);
$employee = Employee::query()->forceCreate([
'customer_id' => $customer->id,
'matricule' => new Matricule('5234567'),
'nom' => 'Bernard',
'prenom' => 'Alice',
'code' => new Code('MNQ2345'),
'etablissement' => new ShopperEstablishmentCode('01'),
'site' => new ShopperSiteCode('SITE'),
'service' => new ShopperServiceCode('01'),
]);
$order = Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(3000, new Currency('EUR')),
'employee' => new OrderEmployee(
new Matricule('5234567'),
'Alice',
'Bernard',
new ShopperEstablishmentCode('01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('01'),
),
'delivery_address' => new DeliveryAddress('Shop', '1 rue Test', '75000', 'Paris'),
'pickup_day' => '2026-11-20',
]);
$formatter = \Mockery::mock(MoneyFormatter::class);
$formatter->shouldReceive('format')->once()->andReturn('30,00 €');
$executor = $this->app->make(Executor::class);
$claimAction = new ClaimTodayOrderReminder();
$job = new SendReminderMail($formatter);
// WHEN
$job->handle($executor, $claimAction);
// Re-running on same day (e.g. retry or duplicate run) must not notify twice.
$job->handle($executor, $claimAction);
// THEN
Notification::assertSentToTimes($employee, OrderTodayReminderNotification::class, 1);
expect($order->fresh()?->reminded_at)->not->toBeNull()
->and($job->uniqueId())->toBe('2026-11-20');
Date::setTestNow();
});
});