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:
97
tests/Integration/IdempotentConsumerTest.php
Normal file
97
tests/Integration/IdempotentConsumerTest.php
Normal 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',
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user