test(coverage): cover post-commit Listeners and AppSessionService (Lot 7B)

This commit is contained in:
Jérémy Munsch 2026-11-16 16:53:21 +01:00
parent 241ad87a2e
commit 3aaec0a6db
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
4 changed files with 287 additions and 0 deletions

View file

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Listeners;
use App\Events\CustomerPasswordUpdated;
use App\Listeners\CustomerPasswordUpdatedListener;
use App\Mail\CustomerPasswordHasReset;
use Domain\Models\ValueObjects\Email;
use Illuminate\Support\Facades\Mail;
it('queues CustomerPasswordHasReset mail to customer email when CustomerPasswordUpdated is handled', function (): void {
// GIVEN
Mail::fake();
$listener = new CustomerPasswordUpdatedListener();
$event = new CustomerPasswordUpdated(new Email('reset@example.com'));
// WHEN
$listener->handle($event);
// THEN
Mail::assertQueued(
CustomerPasswordHasReset::class,
static fn (CustomerPasswordHasReset $mail): bool => $mail->hasTo('reset@example.com'),
);
});

View file

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Listeners\Orders;
use App\Listeners\Orders\SendAdminOrderConfirmation;
use App\Mail\OrderConfirmAdminMail;
use Domain\Enums\Units\OrderUnit;
use Domain\Events\AnOrderHasBeenPlaced;
use Domain\Events\Dto\OrderPlacedLine;
use Domain\Events\Dto\OrderPlacedShop;
use Domain\Models\ValueObjects\Entities\OrderId;
use Domain\Models\ValueObjects\IncoId;
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\Config;
use Illuminate\Support\Facades\Mail;
use Money\Currency;
use Money\Money;
use Ramsey\Uuid\Uuid;
function makeOrderPlacedEvent(): AnOrderHasBeenPlaced
{
return new AnOrderHasBeenPlaced(
orderId: new OrderId(Uuid::uuid7()),
reference: new OrderReference(Uuid::uuid7()),
totalTtc: new Money(1500, new Currency('EUR')),
byMatricule: new Matricule('1234567'),
employee: new OrderEmployee(
matricule: new Matricule('1234567'),
firstname: 'Jean',
lastname: 'Dupont',
facility: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
),
shop: new OrderPlacedShop(
name: 'Boutique Test',
address: '1 rue de la Paix',
postcode: '75001',
city: 'Paris',
),
pickupDay: new \DateTimeImmutable('2026-11-20'),
lines: [
new OrderPlacedLine(
productName: 'Poulet Fermier',
productIncoId: new IncoId(12345),
productWeightIsVariable: false,
productPriceKgTtc: null,
productOrderUnit: OrderUnit::PCE,
quantity: 1,
totalTtc: new Money(1500, new Currency('EUR')),
),
],
);
}
it('throws UnexpectedValueException when mail.to.contact is missing', function (): void {
// GIVEN
Config::set('mail.to', []);
// WHEN / THEN
expect(static fn (): SendAdminOrderConfirmation => new SendAdminOrderConfirmation())
->toThrow(\UnexpectedValueException::class);
});
it('queues OrderConfirmAdminMail to configured contact email when AnOrderHasBeenPlaced is handled', function (): void {
// GIVEN
Mail::fake();
Config::set('mail.to.contact', 'contact@example.com');
$listener = new SendAdminOrderConfirmation();
$event = makeOrderPlacedEvent();
// WHEN
$listener->handle($event);
// THEN
Mail::assertQueued(
OrderConfirmAdminMail::class,
static fn (OrderConfirmAdminMail $mail): bool => $mail->hasTo('contact@example.com'),
);
});

View file

@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Listeners\Orders;
use App\Listeners\Orders\SendCustomerOrderConfirmation;
use App\Models\Employee;
use App\Notifications\OrderConfirmed;
use Domain\Enums\Units\OrderUnit;
use Domain\Events\AnOrderHasBeenPlaced;
use Domain\Events\Dto\OrderPlacedLine;
use Domain\Events\Dto\OrderPlacedShop;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\Entities\OrderId;
use Domain\Models\ValueObjects\IncoId;
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\Notification;
use Money\Currency;
use Money\Money;
use Ramsey\Uuid\Uuid;
function makeCustomerOrderPlacedEvent(string $matricule): AnOrderHasBeenPlaced
{
return new AnOrderHasBeenPlaced(
orderId: new OrderId(Uuid::uuid7()),
reference: new OrderReference(Uuid::uuid7()),
totalTtc: new Money(1500, new Currency('EUR')),
byMatricule: new Matricule($matricule),
employee: new OrderEmployee(
matricule: new Matricule($matricule),
firstname: 'Jean',
lastname: 'Dupont',
facility: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
),
shop: new OrderPlacedShop(
name: 'Boutique Test',
address: '1 rue de la Paix',
postcode: '75001',
city: 'Paris',
),
pickupDay: new \DateTimeImmutable('2026-11-20'),
lines: [
new OrderPlacedLine(
productName: 'Poulet Fermier',
productIncoId: new IncoId(12345),
productWeightIsVariable: false,
productPriceKgTtc: null,
productOrderUnit: OrderUnit::PCE,
quantity: 1,
totalTtc: new Money(1500, new Currency('EUR')),
),
],
);
}
it('sends OrderConfirmed notification when matching Employee exists', function (): void {
// GIVEN
Notification::fake();
$employee = new Employee();
$employee->matricule = new Matricule('1234567');
$employee->nom = 'Dupont';
$employee->prenom = 'Jean';
$employee->etablissement = new ShopperEstablishmentCode('FAC01');
$employee->site = new ShopperSiteCode('SITE');
$employee->service = new ShopperServiceCode('101');
$employee->code = new Code('ABCD2345');
$employee->saveOrFail();
$listener = new SendCustomerOrderConfirmation();
$event = makeCustomerOrderPlacedEvent('1234567');
// WHEN
$listener->handle($event);
// THEN
Notification::assertSentTo($employee, OrderConfirmed::class);
});
it('sends nothing when no matching Employee exists', function (): void {
// GIVEN
Notification::fake();
$listener = new SendCustomerOrderConfirmation();
$event = makeCustomerOrderPlacedEvent('9999999');
// WHEN
$listener->handle($event);
// THEN
Notification::assertNothingSent();
});

View file

@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\Infra\Adapters\Services;
use Illuminate\Support\Facades\Session;
use Infrastructure\Adapters\Services\AppSessionService;
use Ramsey\Uuid\Uuid;
use Shared\Helpers\UuidHelper;
describe('AppSessionService', function (): void {
it('generates a valid UUIDv7 SessionId and persists its string value when session key is absent', function (): void {
// GIVEN
$sessionKey = 'test_session_id';
Session::forget($sessionKey);
$service = new AppSessionService($sessionKey);
// WHEN
$sessionId = $service->get();
// THEN
expect(UuidHelper::isV7($sessionId->value))->toBeTrue()
->and(Session::has($sessionKey))->toBeTrue()
->and(Session::get($sessionKey))->toBe($sessionId->value->toString());
});
it('returns the exact same SessionId on subsequent get calls without overwriting the session value', function (): void {
// GIVEN
$sessionKey = 'test_session_id';
Session::forget($sessionKey);
$service = new AppSessionService($sessionKey);
// WHEN
$first = $service->get();
$second = $service->get();
// THEN
expect($second->value->toString())->toBe($first->value->toString())
->and(Session::get($sessionKey))->toBe($first->value->toString());
});
it('hydrates an existing UUID string already stored in Session under the configured key', function (): void {
// GIVEN
$sessionKey = 'test_session_id';
$existingUuid = Uuid::uuid7()->toString();
Session::put($sessionKey, $existingUuid);
$service = new AppSessionService($sessionKey);
// WHEN
$sessionId = $service->get();
// THEN
expect($sessionId->value->toString())->toBe($existingUuid)
->and(Session::get($sessionKey))->toBe($existingUuid);
});
it('keeps separate AppSessionService instances with distinct session keys isolated', function (): void {
// GIVEN
$firstKey = 'first_session_id';
$secondKey = 'second_session_id';
Session::forget([$firstKey, $secondKey]);
$firstService = new AppSessionService($firstKey);
$secondService = new AppSessionService($secondKey);
// WHEN
$firstId = $firstService->get();
$secondId = $secondService->get();
// THEN
expect($firstId->value->toString())->not->toBe($secondId->value->toString())
->and(Session::get($firstKey))->toBe($firstId->value->toString())
->and(Session::get($secondKey))->toBe($secondId->value->toString());
});
});