Compare commits
2 Commits
step/03-ac
...
step/05-ha
| Author | SHA1 | Date | |
|---|---|---|---|
| b356033f7b | |||
| 129ea58dae |
@@ -10,6 +10,9 @@ services:
|
||||
MiniShop\Shared\Technical\Clock:
|
||||
alias: MiniShop\Shared\Technical\SystemClock
|
||||
|
||||
MiniShop\Shared\Technical\IdempotencyStore:
|
||||
alias: MiniShop\Shared\Technical\InMemoryIdempotencyStore
|
||||
|
||||
# --- Sales ---
|
||||
MiniShop\Sales\Application\:
|
||||
resource: '%kernel.project_dir%/src/Sales/Application/'
|
||||
|
||||
@@ -12,5 +12,6 @@ final readonly class OrderCancelled
|
||||
{
|
||||
public function __construct(
|
||||
public string $orderId,
|
||||
public string $correlationId = '',
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -19,5 +19,6 @@ final readonly class OrderConfirmed
|
||||
public int $totalInCents,
|
||||
public string $currency,
|
||||
public array $lines,
|
||||
public string $correlationId = '',
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -20,5 +20,6 @@ final readonly class OrderPlaced
|
||||
public string $currency,
|
||||
public string $placedAt,
|
||||
public array $lines,
|
||||
public string $correlationId = '',
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -7,21 +7,42 @@ namespace MiniShop\Invoicing\Interfaces\Messaging;
|
||||
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
|
||||
use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrder;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Conformist : Invoicing consomme le contrat sales.v1.OrderConfirmed tel quel,
|
||||
* sans traduction. La dependance upstream est explicite.
|
||||
* Conformist + Idempotent : consomme sales.v1.OrderConfirmed tel quel.
|
||||
* Garde d'idempotence pour eviter les doublons en cas de re-delivery.
|
||||
*/
|
||||
#[AsMessageHandler]
|
||||
final readonly class WhenOrderConfirmed
|
||||
{
|
||||
public function __construct(
|
||||
private IssueInvoiceForExternalOrderHandler $handler,
|
||||
private IdempotencyStore $idempotencyStore,
|
||||
private LoggerInterface $logger = new NullLogger(),
|
||||
) {}
|
||||
|
||||
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(
|
||||
externalOrderId: $message->orderId,
|
||||
customerName: 'Customer ' . $message->customerId,
|
||||
|
||||
@@ -8,11 +8,14 @@ use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
|
||||
use MiniShop\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrder;
|
||||
use MiniShop\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrderHandler;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Consumer ACL : recoit sales.v1.OrderConfirmed, passe par l'Anti-Corruption Layer
|
||||
* pour traduire vers le modele legacy, puis delegue au handler applicatif.
|
||||
* Consumer ACL + Idempotent : passe par l'Anti-Corruption Layer avec
|
||||
* garde d'idempotence pour eviter les doublons.
|
||||
*/
|
||||
#[AsMessageHandler]
|
||||
final readonly class WhenOrderConfirmed
|
||||
@@ -20,10 +23,28 @@ final readonly class WhenOrderConfirmed
|
||||
public function __construct(
|
||||
private LegacyShipmentAcl $acl,
|
||||
private RequestShipmentFromSalesOrderHandler $handler,
|
||||
private IdempotencyStore $idempotencyStore,
|
||||
private LoggerInterface $logger = new NullLogger(),
|
||||
) {}
|
||||
|
||||
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);
|
||||
|
||||
($this->handler)(new RequestShipmentFromSalesOrder(
|
||||
|
||||
@@ -12,11 +12,12 @@ use MiniShop\Sales\Domain\Event\OrderCancelled;
|
||||
use MiniShop\Sales\Domain\Event\OrderConfirmed;
|
||||
use MiniShop\Sales\Domain\Event\OrderPlaced;
|
||||
use MiniShop\Sales\Domain\Model\OrderLine;
|
||||
use MiniShop\Shared\Technical\CorrelationId;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
@@ -33,6 +34,7 @@ final readonly class MessengerSalesEventPublisher implements SalesEventPublisher
|
||||
currency: $event->total->currency,
|
||||
placedAt: $event->placedAt->format(\DateTimeInterface::ATOM),
|
||||
lines: [],
|
||||
correlationId: CorrelationId::generate()->toString(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -52,6 +54,7 @@ final readonly class MessengerSalesEventPublisher implements SalesEventPublisher
|
||||
],
|
||||
$event->lines,
|
||||
),
|
||||
correlationId: CorrelationId::generate()->toString(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -59,6 +62,7 @@ final readonly class MessengerSalesEventPublisher implements SalesEventPublisher
|
||||
{
|
||||
$this->messageBus->dispatch(new OrderCancelledContract(
|
||||
orderId: $event->orderId->toString(),
|
||||
correlationId: CorrelationId::generate()->toString(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
84
src/Sales/Interfaces/Http/Api/V1/OrderController.php
Normal file
84
src/Sales/Interfaces/Http/Api/V1/OrderController.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MiniShop\Sales\Interfaces\Http\Api\V1;
|
||||
|
||||
use MiniShop\Sales\Application\Command\ConfirmOrder;
|
||||
use MiniShop\Sales\Application\Command\ConfirmOrderHandler;
|
||||
use MiniShop\Sales\Application\Command\PlaceOrder;
|
||||
use MiniShop\Sales\Application\Command\PlaceOrderHandler;
|
||||
use MiniShop\Sales\Application\Query\GetOrderById;
|
||||
use MiniShop\Sales\Application\Query\GetOrderByIdHandler;
|
||||
use MiniShop\Sales\Application\Query\ListOrdersByCustomer;
|
||||
use MiniShop\Sales\Application\Query\ListOrdersByCustomerHandler;
|
||||
use MiniShop\Shared\Technical\UuidGenerator;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
/**
|
||||
* Open Host Service — sales.v1
|
||||
* API stable et versionnee. Les modeles internes ne sont jamais exposes.
|
||||
* Tous les DTOs de reponse passent par OrderViewAssembler → OrderView (Published Language).
|
||||
*/
|
||||
#[Route('/api/sales/v1')]
|
||||
final readonly class OrderController
|
||||
{
|
||||
public function __construct(
|
||||
private PlaceOrderHandler $placeOrderHandler,
|
||||
private ConfirmOrderHandler $confirmOrderHandler,
|
||||
private GetOrderByIdHandler $getOrderByIdHandler,
|
||||
private ListOrdersByCustomerHandler $listOrdersByCustomerHandler,
|
||||
private OrderViewAssembler $assembler,
|
||||
) {}
|
||||
|
||||
#[Route('/orders', name: 'ohs_sales_place_order', methods: ['POST'])]
|
||||
public function placeOrder(Request $request): JsonResponse
|
||||
{
|
||||
/** @var array{customerId: string, lines: list<array{productName: string, quantity: int, unitPriceInCents: int, currency: string}>} $data */
|
||||
$data = json_decode($request->getContent(), true, flags: JSON_THROW_ON_ERROR);
|
||||
|
||||
$orderId = UuidGenerator::generate();
|
||||
|
||||
($this->placeOrderHandler)(new PlaceOrder(
|
||||
orderId: $orderId,
|
||||
customerId: $data['customerId'],
|
||||
lines: $data['lines'],
|
||||
));
|
||||
|
||||
$order = ($this->getOrderByIdHandler)(new GetOrderById($orderId));
|
||||
|
||||
return new JsonResponse($this->assembler->toView($order), Response::HTTP_CREATED);
|
||||
}
|
||||
|
||||
#[Route('/orders/{orderId}', name: 'ohs_sales_get_order', methods: ['GET'])]
|
||||
public function getOrder(string $orderId): JsonResponse
|
||||
{
|
||||
$order = ($this->getOrderByIdHandler)(new GetOrderById($orderId));
|
||||
|
||||
return new JsonResponse($this->assembler->toView($order));
|
||||
}
|
||||
|
||||
#[Route('/orders/{orderId}/confirm', name: 'ohs_sales_confirm_order', methods: ['POST'])]
|
||||
public function confirmOrder(string $orderId): JsonResponse
|
||||
{
|
||||
($this->confirmOrderHandler)(new ConfirmOrder(orderId: $orderId));
|
||||
|
||||
$order = ($this->getOrderByIdHandler)(new GetOrderById($orderId));
|
||||
|
||||
return new JsonResponse($this->assembler->toView($order));
|
||||
}
|
||||
|
||||
#[Route('/customers/{customerId}/orders', name: 'ohs_sales_list_orders', methods: ['GET'])]
|
||||
public function listOrders(string $customerId): JsonResponse
|
||||
{
|
||||
$orders = ($this->listOrdersByCustomerHandler)(new ListOrdersByCustomer($customerId));
|
||||
|
||||
return new JsonResponse(array_map(
|
||||
fn ($order) => $this->assembler->toView($order),
|
||||
$orders,
|
||||
));
|
||||
}
|
||||
}
|
||||
37
src/Sales/Interfaces/Http/Api/V1/OrderViewAssembler.php
Normal file
37
src/Sales/Interfaces/Http/Api/V1/OrderViewAssembler.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MiniShop\Sales\Interfaces\Http\Api\V1;
|
||||
|
||||
use MiniShop\Contracts\Sales\V1\Api\OrderView;
|
||||
use MiniShop\Sales\Domain\Model\Order;
|
||||
use MiniShop\Sales\Domain\Model\OrderLine;
|
||||
|
||||
/**
|
||||
* Assemble le modele interne Order vers le DTO public OrderView (sales.v1).
|
||||
* Les modeles internes de Sales ne sont jamais exposes directement.
|
||||
*/
|
||||
final class OrderViewAssembler
|
||||
{
|
||||
public function toView(Order $order): OrderView
|
||||
{
|
||||
return new OrderView(
|
||||
orderId: $order->id->toString(),
|
||||
customerId: $order->customerId->toString(),
|
||||
status: $order->status()->value,
|
||||
totalInCents: $order->total()->amount,
|
||||
currency: $order->total()->currency,
|
||||
lines: array_map(
|
||||
static fn (OrderLine $line): array => [
|
||||
'productName' => $line->productName,
|
||||
'quantity' => $line->quantity,
|
||||
'unitPriceInCents' => $line->unitPrice->amount,
|
||||
'currency' => $line->unitPrice->currency,
|
||||
'lineTotalInCents' => $line->lineTotal()->amount,
|
||||
],
|
||||
$order->lines(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
25
src/Shared/Technical/CorrelationId.php
Normal file
25
src/Shared/Technical/CorrelationId.php
Normal 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;
|
||||
}
|
||||
}
|
||||
14
src/Shared/Technical/IdempotencyStore.php
Normal file
14
src/Shared/Technical/IdempotencyStore.php
Normal 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;
|
||||
}
|
||||
22
src/Shared/Technical/InMemoryIdempotencyStore.php
Normal file
22
src/Shared/Technical/InMemoryIdempotencyStore.php
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrderHandler;
|
||||
use MiniShop\Invoicing\Infrastructure\Persistence\InMemoryInvoiceRepository;
|
||||
use MiniShop\Invoicing\Infrastructure\SequentialInvoiceNumberGenerator;
|
||||
use MiniShop\Invoicing\Interfaces\Messaging\WhenOrderConfirmed;
|
||||
use MiniShop\Shared\Technical\InMemoryIdempotencyStore;
|
||||
use MiniShop\Shared\Technical\SystemClock;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -28,7 +29,7 @@ final class ConformistCompatibilityTest extends TestCase
|
||||
new SequentialInvoiceNumberGenerator(),
|
||||
new SystemClock(),
|
||||
);
|
||||
$consumer = new WhenOrderConfirmed($handler);
|
||||
$consumer = new WhenOrderConfirmed($handler, new InMemoryIdempotencyStore());
|
||||
|
||||
$message = new OrderConfirmed(
|
||||
orderId: 'order-conformist-001',
|
||||
|
||||
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',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use MiniShop\Invoicing\Infrastructure\Persistence\InMemoryInvoiceRepository;
|
||||
use MiniShop\Invoicing\Infrastructure\SequentialInvoiceNumberGenerator;
|
||||
use MiniShop\Invoicing\Interfaces\Messaging\WhenOrderConfirmed;
|
||||
use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrderHandler;
|
||||
use MiniShop\Shared\Technical\InMemoryIdempotencyStore;
|
||||
use MiniShop\Shared\Technical\SystemClock;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -27,6 +28,7 @@ final class InvoicingConformistTest extends TestCase
|
||||
new SequentialInvoiceNumberGenerator(),
|
||||
new SystemClock(),
|
||||
),
|
||||
new InMemoryIdempotencyStore(),
|
||||
);
|
||||
|
||||
$consumer(new OrderConfirmed(
|
||||
|
||||
@@ -11,6 +11,7 @@ use MiniShop\LegacyFulfillment\Infrastructure\AntiCorruption\LegacyShipmentAcl;
|
||||
use MiniShop\LegacyFulfillment\Infrastructure\Gateway\FakeLegacyFulfillmentGateway;
|
||||
use MiniShop\LegacyFulfillment\Infrastructure\Persistence\InMemoryShipmentRequestRepository;
|
||||
use MiniShop\LegacyFulfillment\Interfaces\Messaging\WhenOrderConfirmed;
|
||||
use MiniShop\Shared\Technical\InMemoryIdempotencyStore;
|
||||
use MiniShop\Shared\Technical\SystemClock;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -28,6 +29,7 @@ final class LegacyFulfillmentAclTest extends TestCase
|
||||
$consumer = new WhenOrderConfirmed(
|
||||
new LegacyShipmentAcl(),
|
||||
new RequestShipmentFromSalesOrderHandler($shipmentRepo, $gateway, new SystemClock()),
|
||||
new InMemoryIdempotencyStore(),
|
||||
);
|
||||
|
||||
$consumer(new OrderConfirmed(
|
||||
|
||||
94
tests/Integration/Sales/OhsApiTest.php
Normal file
94
tests/Integration/Sales/OhsApiTest.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MiniShop\Tests\Integration\Sales;
|
||||
|
||||
use MiniShop\Contracts\Sales\V1\Api\OrderView;
|
||||
use MiniShop\Sales\Application\Command\PlaceOrder;
|
||||
use MiniShop\Sales\Application\Command\PlaceOrderHandler;
|
||||
use MiniShop\Sales\Application\Query\GetOrderById;
|
||||
use MiniShop\Sales\Application\Query\GetOrderByIdHandler;
|
||||
use MiniShop\Sales\Domain\Model\OrderLine;
|
||||
use MiniShop\Sales\Interfaces\Http\Api\V1\OrderViewAssembler;
|
||||
use MiniShop\Sales\Infrastructure\Persistence\InMemoryOrderRepository;
|
||||
use MiniShop\Shared\Technical\SystemClock;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test de l'Open Host Service : verifie que l'API OHS expose uniquement
|
||||
* des DTOs du Published Language (OrderView) et jamais les modeles internes.
|
||||
*/
|
||||
final class OhsApiTest extends TestCase
|
||||
{
|
||||
public function test_assembler_returns_order_view_contract(): void
|
||||
{
|
||||
$orderRepo = new InMemoryOrderRepository();
|
||||
$clock = new SystemClock();
|
||||
$assembler = new OrderViewAssembler();
|
||||
|
||||
// Create a stub publisher that does nothing
|
||||
$publisher = new class implements \MiniShop\Sales\Application\Port\SalesEventPublisher {
|
||||
public function publishOrderPlaced(\MiniShop\Sales\Domain\Event\OrderPlaced $event): void {}
|
||||
public function publishOrderConfirmed(\MiniShop\Sales\Domain\Event\OrderConfirmed $event): void {}
|
||||
public function publishOrderCancelled(\MiniShop\Sales\Domain\Event\OrderCancelled $event): void {}
|
||||
};
|
||||
|
||||
$placeHandler = new PlaceOrderHandler($orderRepo, $publisher, $clock);
|
||||
$getHandler = new GetOrderByIdHandler($orderRepo);
|
||||
|
||||
$orderId = 'ohs-test-001';
|
||||
($placeHandler)(new PlaceOrder(
|
||||
orderId: $orderId,
|
||||
customerId: 'cust-001',
|
||||
lines: [
|
||||
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||
],
|
||||
));
|
||||
|
||||
$order = ($getHandler)(new GetOrderById($orderId));
|
||||
$view = $assembler->toView($order);
|
||||
|
||||
// Verifie que le retour est un DTO du Published Language
|
||||
self::assertInstanceOf(OrderView::class, $view);
|
||||
self::assertSame($orderId, $view->orderId);
|
||||
self::assertSame('placed', $view->status);
|
||||
self::assertSame(3000, $view->totalInCents);
|
||||
self::assertSame('EUR', $view->currency);
|
||||
self::assertCount(1, $view->lines);
|
||||
}
|
||||
|
||||
public function test_order_view_does_not_expose_internal_model(): void
|
||||
{
|
||||
$assembler = new OrderViewAssembler();
|
||||
$orderRepo = new InMemoryOrderRepository();
|
||||
$clock = new SystemClock();
|
||||
$publisher = new class implements \MiniShop\Sales\Application\Port\SalesEventPublisher {
|
||||
public function publishOrderPlaced(\MiniShop\Sales\Domain\Event\OrderPlaced $event): void {}
|
||||
public function publishOrderConfirmed(\MiniShop\Sales\Domain\Event\OrderConfirmed $event): void {}
|
||||
public function publishOrderCancelled(\MiniShop\Sales\Domain\Event\OrderCancelled $event): void {}
|
||||
};
|
||||
|
||||
($placeHandler = new PlaceOrderHandler($orderRepo, $publisher, $clock))(new PlaceOrder(
|
||||
orderId: 'ohs-test-002',
|
||||
customerId: 'cust-001',
|
||||
lines: [['productName' => 'Widget', 'quantity' => 1, 'unitPriceInCents' => 1000, 'currency' => 'EUR']],
|
||||
));
|
||||
|
||||
$order = (new GetOrderByIdHandler($orderRepo))(new GetOrderById('ohs-test-002'));
|
||||
$view = $assembler->toView($order);
|
||||
|
||||
$json = json_encode($view, JSON_THROW_ON_ERROR);
|
||||
$decoded = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
|
||||
|
||||
// Le JSON ne doit contenir que les champs du Published Language
|
||||
$allowedKeys = ['orderId', 'customerId', 'status', 'totalInCents', 'currency', 'lines'];
|
||||
self::assertSame($allowedKeys, array_keys($decoded));
|
||||
|
||||
// Les lignes ne doivent pas contenir de types internes (Money, OrderLine)
|
||||
$lineKeys = array_keys($decoded['lines'][0]);
|
||||
self::assertContains('productName', $lineKeys);
|
||||
self::assertContains('lineTotalInCents', $lineKeys);
|
||||
self::assertNotContains('unitPrice', $lineKeys); // pas d'objet Money
|
||||
}
|
||||
}
|
||||
36
tests/Unit/Shared/IdempotencyTest.php
Normal file
36
tests/Unit/Shared/IdempotencyTest.php
Normal 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'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user