Step 05 — Hardening

- CorrelationId VO pour le tracing inter-BC
- IdempotencyStore (interface + InMemory) pour garde d'idempotence
- correlationId ajouté aux contrats Published Language (retro-compatible par défaut)
- Consumers Invoicing et LegacyFulfillment idempotents avec logging
- MessengerSalesEventPublisher propage les correlationIds
- Tests unitaires idempotence + tests d'intégration consommateurs idempotents
This commit is contained in:
2026-03-04 00:35:20 +01:00
parent 129ea58dae
commit b356033f7b
15 changed files with 257 additions and 6 deletions

View File

@@ -10,6 +10,9 @@ services:
MiniShop\Shared\Technical\Clock: MiniShop\Shared\Technical\Clock:
alias: MiniShop\Shared\Technical\SystemClock alias: MiniShop\Shared\Technical\SystemClock
MiniShop\Shared\Technical\IdempotencyStore:
alias: MiniShop\Shared\Technical\InMemoryIdempotencyStore
# --- Sales --- # --- Sales ---
MiniShop\Sales\Application\: MiniShop\Sales\Application\:
resource: '%kernel.project_dir%/src/Sales/Application/' resource: '%kernel.project_dir%/src/Sales/Application/'

View File

@@ -12,5 +12,6 @@ final readonly class OrderCancelled
{ {
public function __construct( public function __construct(
public string $orderId, public string $orderId,
public string $correlationId = '',
) {} ) {}
} }

View File

@@ -19,5 +19,6 @@ final readonly class OrderConfirmed
public int $totalInCents, public int $totalInCents,
public string $currency, public string $currency,
public array $lines, public array $lines,
public string $correlationId = '',
) {} ) {}
} }

View File

@@ -20,5 +20,6 @@ final readonly class OrderPlaced
public string $currency, public string $currency,
public string $placedAt, public string $placedAt,
public array $lines, public array $lines,
public string $correlationId = '',
) {} ) {}
} }

View File

@@ -7,21 +7,42 @@ namespace MiniShop\Invoicing\Interfaces\Messaging;
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed; use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrder; use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrder;
use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrderHandler; use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrderHandler;
use MiniShop\Shared\Technical\IdempotencyStore;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Attribute\AsMessageHandler;
/** /**
* Conformist : Invoicing consomme le contrat sales.v1.OrderConfirmed tel quel, * Conformist + Idempotent : consomme sales.v1.OrderConfirmed tel quel.
* sans traduction. La dependance upstream est explicite. * Garde d'idempotence pour eviter les doublons en cas de re-delivery.
*/ */
#[AsMessageHandler] #[AsMessageHandler]
final readonly class WhenOrderConfirmed final readonly class WhenOrderConfirmed
{ {
public function __construct( public function __construct(
private IssueInvoiceForExternalOrderHandler $handler, private IssueInvoiceForExternalOrderHandler $handler,
private IdempotencyStore $idempotencyStore,
private LoggerInterface $logger = new NullLogger(),
) {} ) {}
public function __invoke(OrderConfirmed $message): void public function __invoke(OrderConfirmed $message): void
{ {
$idempotencyKey = 'invoicing:order-confirmed:' . $message->orderId;
if ($this->idempotencyStore->isDuplicate($idempotencyKey)) {
$this->logger->info('Duplicate OrderConfirmed ignored.', [
'orderId' => $message->orderId,
'correlationId' => $message->correlationId,
]);
return;
}
$this->logger->info('Processing OrderConfirmed.', [
'orderId' => $message->orderId,
'correlationId' => $message->correlationId,
]);
($this->handler)(new IssueInvoiceForExternalOrder( ($this->handler)(new IssueInvoiceForExternalOrder(
externalOrderId: $message->orderId, externalOrderId: $message->orderId,
customerName: 'Customer ' . $message->customerId, customerName: 'Customer ' . $message->customerId,

View File

@@ -8,11 +8,14 @@ use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
use MiniShop\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrder; use MiniShop\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrder;
use MiniShop\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrderHandler; use MiniShop\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrderHandler;
use MiniShop\LegacyFulfillment\Infrastructure\AntiCorruption\LegacyShipmentAcl; use MiniShop\LegacyFulfillment\Infrastructure\AntiCorruption\LegacyShipmentAcl;
use MiniShop\Shared\Technical\IdempotencyStore;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Attribute\AsMessageHandler;
/** /**
* Consumer ACL : recoit sales.v1.OrderConfirmed, passe par l'Anti-Corruption Layer * Consumer ACL + Idempotent : passe par l'Anti-Corruption Layer avec
* pour traduire vers le modele legacy, puis delegue au handler applicatif. * garde d'idempotence pour eviter les doublons.
*/ */
#[AsMessageHandler] #[AsMessageHandler]
final readonly class WhenOrderConfirmed final readonly class WhenOrderConfirmed
@@ -20,10 +23,28 @@ final readonly class WhenOrderConfirmed
public function __construct( public function __construct(
private LegacyShipmentAcl $acl, private LegacyShipmentAcl $acl,
private RequestShipmentFromSalesOrderHandler $handler, private RequestShipmentFromSalesOrderHandler $handler,
private IdempotencyStore $idempotencyStore,
private LoggerInterface $logger = new NullLogger(),
) {} ) {}
public function __invoke(OrderConfirmed $message): void public function __invoke(OrderConfirmed $message): void
{ {
$idempotencyKey = 'fulfillment:order-confirmed:' . $message->orderId;
if ($this->idempotencyStore->isDuplicate($idempotencyKey)) {
$this->logger->info('Duplicate OrderConfirmed ignored.', [
'orderId' => $message->orderId,
'correlationId' => $message->correlationId,
]);
return;
}
$this->logger->info('Processing OrderConfirmed via ACL.', [
'orderId' => $message->orderId,
'correlationId' => $message->correlationId,
]);
$legacyCommand = $this->acl->fromSalesOrderConfirmed($message); $legacyCommand = $this->acl->fromSalesOrderConfirmed($message);
($this->handler)(new RequestShipmentFromSalesOrder( ($this->handler)(new RequestShipmentFromSalesOrder(

View File

@@ -12,11 +12,12 @@ use MiniShop\Sales\Domain\Event\OrderCancelled;
use MiniShop\Sales\Domain\Event\OrderConfirmed; use MiniShop\Sales\Domain\Event\OrderConfirmed;
use MiniShop\Sales\Domain\Event\OrderPlaced; use MiniShop\Sales\Domain\Event\OrderPlaced;
use MiniShop\Sales\Domain\Model\OrderLine; use MiniShop\Sales\Domain\Model\OrderLine;
use MiniShop\Shared\Technical\CorrelationId;
use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\MessageBusInterface;
/** /**
* Publie les evenements de domaine Sales sous forme de contrats Published Language * Publie les evenements de domaine Sales sous forme de contrats Published Language
* via Symfony Messenger. Remplace le NaiveSalesEventPublisher. * via Symfony Messenger. Propage un correlationId pour le tracing.
*/ */
final readonly class MessengerSalesEventPublisher implements SalesEventPublisher final readonly class MessengerSalesEventPublisher implements SalesEventPublisher
{ {
@@ -33,6 +34,7 @@ final readonly class MessengerSalesEventPublisher implements SalesEventPublisher
currency: $event->total->currency, currency: $event->total->currency,
placedAt: $event->placedAt->format(\DateTimeInterface::ATOM), placedAt: $event->placedAt->format(\DateTimeInterface::ATOM),
lines: [], lines: [],
correlationId: CorrelationId::generate()->toString(),
)); ));
} }
@@ -52,6 +54,7 @@ final readonly class MessengerSalesEventPublisher implements SalesEventPublisher
], ],
$event->lines, $event->lines,
), ),
correlationId: CorrelationId::generate()->toString(),
)); ));
} }
@@ -59,6 +62,7 @@ final readonly class MessengerSalesEventPublisher implements SalesEventPublisher
{ {
$this->messageBus->dispatch(new OrderCancelledContract( $this->messageBus->dispatch(new OrderCancelledContract(
orderId: $event->orderId->toString(), orderId: $event->orderId->toString(),
correlationId: CorrelationId::generate()->toString(),
)); ));
} }
} }

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace MiniShop\Shared\Technical;
final readonly class CorrelationId
{
private function __construct(public string $value) {}
public static function generate(): self
{
return new self(UuidGenerator::generate());
}
public static function fromString(string $value): self
{
return new self($value);
}
public function toString(): string
{
return $this->value;
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace MiniShop\Shared\Technical;
interface IdempotencyStore
{
/**
* Returns true if the key was already processed (duplicate).
* Returns false and marks the key as processed (first time).
*/
public function isDuplicate(string $key): bool;
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace MiniShop\Shared\Technical;
final class InMemoryIdempotencyStore implements IdempotencyStore
{
/** @var array<string, true> */
private array $processed = [];
public function isDuplicate(string $key): bool
{
if (isset($this->processed[$key])) {
return true;
}
$this->processed[$key] = true;
return false;
}
}

View File

@@ -10,6 +10,7 @@ use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrderHandler;
use MiniShop\Invoicing\Infrastructure\Persistence\InMemoryInvoiceRepository; use MiniShop\Invoicing\Infrastructure\Persistence\InMemoryInvoiceRepository;
use MiniShop\Invoicing\Infrastructure\SequentialInvoiceNumberGenerator; use MiniShop\Invoicing\Infrastructure\SequentialInvoiceNumberGenerator;
use MiniShop\Invoicing\Interfaces\Messaging\WhenOrderConfirmed; use MiniShop\Invoicing\Interfaces\Messaging\WhenOrderConfirmed;
use MiniShop\Shared\Technical\InMemoryIdempotencyStore;
use MiniShop\Shared\Technical\SystemClock; use MiniShop\Shared\Technical\SystemClock;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@@ -28,7 +29,7 @@ final class ConformistCompatibilityTest extends TestCase
new SequentialInvoiceNumberGenerator(), new SequentialInvoiceNumberGenerator(),
new SystemClock(), new SystemClock(),
); );
$consumer = new WhenOrderConfirmed($handler); $consumer = new WhenOrderConfirmed($handler, new InMemoryIdempotencyStore());
$message = new OrderConfirmed( $message = new OrderConfirmed(
orderId: 'order-conformist-001', orderId: 'order-conformist-001',

View File

@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace MiniShop\Tests\Integration;
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrderHandler;
use MiniShop\Invoicing\Infrastructure\Persistence\InMemoryInvoiceRepository;
use MiniShop\Invoicing\Infrastructure\SequentialInvoiceNumberGenerator;
use MiniShop\Invoicing\Interfaces\Messaging\WhenOrderConfirmed as InvoicingConsumer;
use MiniShop\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrderHandler;
use MiniShop\LegacyFulfillment\Infrastructure\AntiCorruption\LegacyShipmentAcl;
use MiniShop\LegacyFulfillment\Infrastructure\Gateway\FakeLegacyFulfillmentGateway;
use MiniShop\LegacyFulfillment\Infrastructure\Persistence\InMemoryShipmentRequestRepository;
use MiniShop\LegacyFulfillment\Interfaces\Messaging\WhenOrderConfirmed as FulfillmentConsumer;
use MiniShop\Shared\Technical\InMemoryIdempotencyStore;
use MiniShop\Shared\Technical\SystemClock;
use PHPUnit\Framework\TestCase;
/**
* Test d'idempotence : un message recu deux fois ne doit pas creer de doublon.
*/
final class IdempotentConsumerTest extends TestCase
{
public function test_invoicing_consumer_ignores_duplicate(): void
{
$invoiceRepo = new InMemoryInvoiceRepository();
$idempotencyStore = new InMemoryIdempotencyStore();
$consumer = new InvoicingConsumer(
new IssueInvoiceForExternalOrderHandler(
$invoiceRepo,
new SequentialInvoiceNumberGenerator(),
new SystemClock(),
),
$idempotencyStore,
);
$message = $this->createMessage('idem-001');
$consumer($message);
$consumer($message); // duplicate
// Only one invoice should exist
$invoice = $invoiceRepo->findByExternalOrderId('idem-001');
self::assertNotNull($invoice);
self::assertSame('INV-000001', $invoice->invoiceNumber);
}
public function test_fulfillment_consumer_ignores_duplicate(): void
{
$shipmentRepo = new InMemoryShipmentRequestRepository();
$gateway = new FakeLegacyFulfillmentGateway();
$idempotencyStore = new InMemoryIdempotencyStore();
$consumer = new FulfillmentConsumer(
new LegacyShipmentAcl(),
new RequestShipmentFromSalesOrderHandler($shipmentRepo, $gateway, new SystemClock()),
$idempotencyStore,
);
$message = $this->createMessage('idem-002');
$consumer($message);
$consumer($message); // duplicate
// Only one shipment should exist, only one gateway call
self::assertCount(1, $gateway->sentRequests());
}
public function test_correlation_id_is_propagated(): void
{
$message = new OrderConfirmed(
orderId: 'corr-001',
customerId: 'cust-001',
totalInCents: 1000,
currency: 'EUR',
lines: [['productName' => 'X', 'quantity' => 1, 'unitPriceInCents' => 1000, 'currency' => 'EUR']],
correlationId: 'corr-id-abc-123',
);
self::assertSame('corr-id-abc-123', $message->correlationId);
}
private function createMessage(string $orderId): OrderConfirmed
{
return new OrderConfirmed(
orderId: $orderId,
customerId: 'cust-001',
totalInCents: 1500,
currency: 'EUR',
lines: [['productName' => 'Widget', 'quantity' => 1, 'unitPriceInCents' => 1500, 'currency' => 'EUR']],
correlationId: 'test-correlation-id',
);
}
}

View File

@@ -9,6 +9,7 @@ use MiniShop\Invoicing\Infrastructure\Persistence\InMemoryInvoiceRepository;
use MiniShop\Invoicing\Infrastructure\SequentialInvoiceNumberGenerator; use MiniShop\Invoicing\Infrastructure\SequentialInvoiceNumberGenerator;
use MiniShop\Invoicing\Interfaces\Messaging\WhenOrderConfirmed; use MiniShop\Invoicing\Interfaces\Messaging\WhenOrderConfirmed;
use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrderHandler; use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrderHandler;
use MiniShop\Shared\Technical\InMemoryIdempotencyStore;
use MiniShop\Shared\Technical\SystemClock; use MiniShop\Shared\Technical\SystemClock;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@@ -27,6 +28,7 @@ final class InvoicingConformistTest extends TestCase
new SequentialInvoiceNumberGenerator(), new SequentialInvoiceNumberGenerator(),
new SystemClock(), new SystemClock(),
), ),
new InMemoryIdempotencyStore(),
); );
$consumer(new OrderConfirmed( $consumer(new OrderConfirmed(

View File

@@ -11,6 +11,7 @@ use MiniShop\LegacyFulfillment\Infrastructure\AntiCorruption\LegacyShipmentAcl;
use MiniShop\LegacyFulfillment\Infrastructure\Gateway\FakeLegacyFulfillmentGateway; use MiniShop\LegacyFulfillment\Infrastructure\Gateway\FakeLegacyFulfillmentGateway;
use MiniShop\LegacyFulfillment\Infrastructure\Persistence\InMemoryShipmentRequestRepository; use MiniShop\LegacyFulfillment\Infrastructure\Persistence\InMemoryShipmentRequestRepository;
use MiniShop\LegacyFulfillment\Interfaces\Messaging\WhenOrderConfirmed; use MiniShop\LegacyFulfillment\Interfaces\Messaging\WhenOrderConfirmed;
use MiniShop\Shared\Technical\InMemoryIdempotencyStore;
use MiniShop\Shared\Technical\SystemClock; use MiniShop\Shared\Technical\SystemClock;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@@ -28,6 +29,7 @@ final class LegacyFulfillmentAclTest extends TestCase
$consumer = new WhenOrderConfirmed( $consumer = new WhenOrderConfirmed(
new LegacyShipmentAcl(), new LegacyShipmentAcl(),
new RequestShipmentFromSalesOrderHandler($shipmentRepo, $gateway, new SystemClock()), new RequestShipmentFromSalesOrderHandler($shipmentRepo, $gateway, new SystemClock()),
new InMemoryIdempotencyStore(),
); );
$consumer(new OrderConfirmed( $consumer(new OrderConfirmed(

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace MiniShop\Tests\Unit\Shared;
use MiniShop\Shared\Technical\InMemoryIdempotencyStore;
use PHPUnit\Framework\TestCase;
final class IdempotencyTest extends TestCase
{
public function test_first_call_is_not_duplicate(): void
{
$store = new InMemoryIdempotencyStore();
self::assertFalse($store->isDuplicate('key-1'));
}
public function test_second_call_is_duplicate(): void
{
$store = new InMemoryIdempotencyStore();
$store->isDuplicate('key-1');
self::assertTrue($store->isDuplicate('key-1'));
}
public function test_different_keys_are_independent(): void
{
$store = new InMemoryIdempotencyStore();
$store->isDuplicate('key-1');
self::assertFalse($store->isDuplicate('key-2'));
}
}