Compare commits
5 Commits
step/00-na
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b356033f7b | |||
| 129ea58dae | |||
| f8be8166b7 | |||
| dcc81ec9bb | |||
| 21b8f37411 |
11
apps/symfony/config/packages/messenger.yaml
Normal file
11
apps/symfony/config/packages/messenger.yaml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
framework:
|
||||||
|
messenger:
|
||||||
|
default_bus: messenger.bus.default
|
||||||
|
buses:
|
||||||
|
messenger.bus.default: ~
|
||||||
|
transports:
|
||||||
|
sync: 'sync://'
|
||||||
|
routing:
|
||||||
|
'MiniShop\Contracts\Sales\V1\Event\OrderConfirmed': sync
|
||||||
|
'MiniShop\Contracts\Sales\V1\Event\OrderPlaced': sync
|
||||||
|
'MiniShop\Contracts\Sales\V1\Event\OrderCancelled': sync
|
||||||
@@ -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/'
|
||||||
@@ -25,7 +28,7 @@ services:
|
|||||||
alias: MiniShop\Sales\Infrastructure\Persistence\InMemoryOrderRepository
|
alias: MiniShop\Sales\Infrastructure\Persistence\InMemoryOrderRepository
|
||||||
|
|
||||||
MiniShop\Sales\Application\Port\SalesEventPublisher:
|
MiniShop\Sales\Application\Port\SalesEventPublisher:
|
||||||
alias: MiniShop\Sales\Infrastructure\Messaging\NaiveSalesEventPublisher
|
alias: MiniShop\Sales\Infrastructure\Messaging\MessengerSalesEventPublisher
|
||||||
|
|
||||||
# --- Invoicing ---
|
# --- Invoicing ---
|
||||||
MiniShop\Invoicing\Application\:
|
MiniShop\Invoicing\Application\:
|
||||||
|
|||||||
@@ -5,9 +5,10 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.5",
|
"php": ">=8.5",
|
||||||
"symfony/framework-bundle": "^7.2",
|
|
||||||
"symfony/console": "^7.2",
|
"symfony/console": "^7.2",
|
||||||
"symfony/dotenv": "^7.2",
|
"symfony/dotenv": "^7.2",
|
||||||
|
"symfony/framework-bundle": "^7.2",
|
||||||
|
"symfony/messenger": "^7.2",
|
||||||
"symfony/yaml": "^7.2"
|
"symfony/yaml": "^7.2"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
@@ -19,7 +20,7 @@
|
|||||||
"MiniShop\\Invoicing\\": "src/Invoicing/",
|
"MiniShop\\Invoicing\\": "src/Invoicing/",
|
||||||
"MiniShop\\LegacyFulfillment\\": "src/LegacyFulfillment/",
|
"MiniShop\\LegacyFulfillment\\": "src/LegacyFulfillment/",
|
||||||
"MiniShop\\Shared\\": "src/Shared/",
|
"MiniShop\\Shared\\": "src/Shared/",
|
||||||
"MiniShop\\Contracts\\": "contracts/",
|
"MiniShop\\Contracts\\Sales\\V1\\": "contracts/sales/v1/",
|
||||||
"App\\": "apps/symfony/src/"
|
"App\\": "apps/symfony/src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
221
composer.lock
generated
221
composer.lock
generated
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "cf876267ce49272867204aaa70702ed4",
|
"content-hash": "1ad6323891b4f76eb1b6f55ff6741b32",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "psr/cache",
|
"name": "psr/cache",
|
||||||
@@ -55,6 +55,54 @@
|
|||||||
},
|
},
|
||||||
"time": "2021-02-03T23:26:27+00:00"
|
"time": "2021-02-03T23:26:27+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "psr/clock",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/php-fig/clock.git",
|
||||||
|
"reference": "e41a24703d4560fd0acb709162f73b8adfc3aa0d"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/php-fig/clock/zipball/e41a24703d4560fd0acb709162f73b8adfc3aa0d",
|
||||||
|
"reference": "e41a24703d4560fd0acb709162f73b8adfc3aa0d",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": "^7.0 || ^8.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Psr\\Clock\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "PHP-FIG",
|
||||||
|
"homepage": "https://www.php-fig.org/"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Common interface for reading the clock.",
|
||||||
|
"homepage": "https://github.com/php-fig/clock",
|
||||||
|
"keywords": [
|
||||||
|
"clock",
|
||||||
|
"now",
|
||||||
|
"psr",
|
||||||
|
"psr-20",
|
||||||
|
"time"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/php-fig/clock/issues",
|
||||||
|
"source": "https://github.com/php-fig/clock/tree/1.0.0"
|
||||||
|
},
|
||||||
|
"time": "2022-11-25T14:36:26+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "psr/container",
|
"name": "psr/container",
|
||||||
"version": "2.0.2",
|
"version": "2.0.2",
|
||||||
@@ -384,6 +432,83 @@
|
|||||||
],
|
],
|
||||||
"time": "2025-03-13T15:25:07+00:00"
|
"time": "2025-03-13T15:25:07+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "symfony/clock",
|
||||||
|
"version": "v8.0.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/symfony/clock.git",
|
||||||
|
"reference": "832119f9b8dbc6c8e6f65f30c5969eca1e88764f"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/symfony/clock/zipball/832119f9b8dbc6c8e6f65f30c5969eca1e88764f",
|
||||||
|
"reference": "832119f9b8dbc6c8e6f65f30c5969eca1e88764f",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.4",
|
||||||
|
"psr/clock": "^1.0"
|
||||||
|
},
|
||||||
|
"provide": {
|
||||||
|
"psr/clock-implementation": "1.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"files": [
|
||||||
|
"Resources/now.php"
|
||||||
|
],
|
||||||
|
"psr-4": {
|
||||||
|
"Symfony\\Component\\Clock\\": ""
|
||||||
|
},
|
||||||
|
"exclude-from-classmap": [
|
||||||
|
"/Tests/"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Nicolas Grekas",
|
||||||
|
"email": "p@tchwork.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Decouples applications from the system clock",
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"keywords": [
|
||||||
|
"clock",
|
||||||
|
"psr20",
|
||||||
|
"time"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"source": "https://github.com/symfony/clock/tree/v8.0.0"
|
||||||
|
},
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"url": "https://symfony.com/sponsor",
|
||||||
|
"type": "custom"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://github.com/fabpot",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://github.com/nicolas-grekas",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||||
|
"type": "tidelift"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"time": "2025-11-12T15:46:48+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "symfony/config",
|
"name": "symfony/config",
|
||||||
"version": "v8.0.6",
|
"version": "v8.0.6",
|
||||||
@@ -1508,6 +1633,100 @@
|
|||||||
],
|
],
|
||||||
"time": "2026-02-26T08:36:42+00:00"
|
"time": "2026-02-26T08:36:42+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "symfony/messenger",
|
||||||
|
"version": "v7.4.6",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/symfony/messenger.git",
|
||||||
|
"reference": "9da6166eb98937d903ed3685b317b426c82d3496"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/symfony/messenger/zipball/9da6166eb98937d903ed3685b317b426c82d3496",
|
||||||
|
"reference": "9da6166eb98937d903ed3685b317b426c82d3496",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.2",
|
||||||
|
"psr/log": "^1|^2|^3",
|
||||||
|
"symfony/clock": "^6.4|^7.0|^8.0",
|
||||||
|
"symfony/deprecation-contracts": "^2.5|^3"
|
||||||
|
},
|
||||||
|
"conflict": {
|
||||||
|
"symfony/console": "<7.2",
|
||||||
|
"symfony/event-dispatcher": "<6.4",
|
||||||
|
"symfony/event-dispatcher-contracts": "<2.5",
|
||||||
|
"symfony/framework-bundle": "<6.4",
|
||||||
|
"symfony/http-kernel": "<7.3",
|
||||||
|
"symfony/lock": "<7.4",
|
||||||
|
"symfony/serializer": "<6.4.32|>=7.3,<7.3.10|>=7.4,<7.4.4|>=8.0,<8.0.4"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"psr/cache": "^1.0|^2.0|^3.0",
|
||||||
|
"symfony/console": "^7.2|^8.0",
|
||||||
|
"symfony/dependency-injection": "^6.4|^7.0|^8.0",
|
||||||
|
"symfony/event-dispatcher": "^6.4|^7.0|^8.0",
|
||||||
|
"symfony/http-kernel": "^7.3|^8.0",
|
||||||
|
"symfony/lock": "^7.4|^8.0",
|
||||||
|
"symfony/process": "^6.4|^7.0|^8.0",
|
||||||
|
"symfony/property-access": "^6.4|^7.0|^8.0",
|
||||||
|
"symfony/rate-limiter": "^6.4|^7.0|^8.0",
|
||||||
|
"symfony/routing": "^6.4|^7.0|^8.0",
|
||||||
|
"symfony/serializer": "^6.4.32|~7.3.10|^7.4.4|^8.0.4",
|
||||||
|
"symfony/service-contracts": "^2.5|^3",
|
||||||
|
"symfony/stopwatch": "^6.4|^7.0|^8.0",
|
||||||
|
"symfony/validator": "^6.4|^7.0|^8.0",
|
||||||
|
"symfony/var-dumper": "^6.4|^7.0|^8.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Symfony\\Component\\Messenger\\": ""
|
||||||
|
},
|
||||||
|
"exclude-from-classmap": [
|
||||||
|
"/Tests/"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Samuel Roze",
|
||||||
|
"email": "samuel.roze@gmail.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Helps applications send and receive messages to/from other applications or via message queues",
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"support": {
|
||||||
|
"source": "https://github.com/symfony/messenger/tree/v7.4.6"
|
||||||
|
},
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"url": "https://symfony.com/sponsor",
|
||||||
|
"type": "custom"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://github.com/fabpot",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://github.com/nicolas-grekas",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||||
|
"type": "tidelift"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"time": "2026-02-25T16:50:00+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "symfony/polyfill-ctype",
|
"name": "symfony/polyfill-ctype",
|
||||||
"version": "v1.33.0",
|
"version": "v1.33.0",
|
||||||
|
|||||||
24
contracts/sales/v1/Api/OrderView.php
Normal file
24
contracts/sales/v1/Api/OrderView.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Contracts\Sales\V1\Api;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Published Language — sales.v1.OrderView
|
||||||
|
* DTO de consultation. Contrat immutable et versionne.
|
||||||
|
*/
|
||||||
|
final readonly class OrderView
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param list<array{productName: string, quantity: int, unitPriceInCents: int, currency: string, lineTotalInCents: int}> $lines
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public string $orderId,
|
||||||
|
public string $customerId,
|
||||||
|
public string $status,
|
||||||
|
public int $totalInCents,
|
||||||
|
public string $currency,
|
||||||
|
public array $lines,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
17
contracts/sales/v1/Event/OrderCancelled.php
Normal file
17
contracts/sales/v1/Event/OrderCancelled.php
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Contracts\Sales\V1\Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Published Language — sales.v1.OrderCancelled
|
||||||
|
* Contrat immutable et versionne. Ne jamais modifier les champs existants.
|
||||||
|
*/
|
||||||
|
final readonly class OrderCancelled
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $orderId,
|
||||||
|
public string $correlationId = '',
|
||||||
|
) {}
|
||||||
|
}
|
||||||
24
contracts/sales/v1/Event/OrderConfirmed.php
Normal file
24
contracts/sales/v1/Event/OrderConfirmed.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Contracts\Sales\V1\Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Published Language — sales.v1.OrderConfirmed
|
||||||
|
* Contrat immutable et versionne. Ne jamais modifier les champs existants.
|
||||||
|
*/
|
||||||
|
final readonly class OrderConfirmed
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param list<array{productName: string, quantity: int, unitPriceInCents: int, currency: string}> $lines
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public string $orderId,
|
||||||
|
public string $customerId,
|
||||||
|
public int $totalInCents,
|
||||||
|
public string $currency,
|
||||||
|
public array $lines,
|
||||||
|
public string $correlationId = '',
|
||||||
|
) {}
|
||||||
|
}
|
||||||
25
contracts/sales/v1/Event/OrderPlaced.php
Normal file
25
contracts/sales/v1/Event/OrderPlaced.php
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Contracts\Sales\V1\Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Published Language — sales.v1.OrderPlaced
|
||||||
|
* Contrat immutable et versionne. Ne jamais modifier les champs existants.
|
||||||
|
*/
|
||||||
|
final readonly class OrderPlaced
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param list<array{productName: string, quantity: int, unitPriceInCents: int, currency: string}> $lines
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public string $orderId,
|
||||||
|
public string $customerId,
|
||||||
|
public int $totalInCents,
|
||||||
|
public string $currency,
|
||||||
|
public string $placedAt,
|
||||||
|
public array $lines,
|
||||||
|
public string $correlationId = '',
|
||||||
|
) {}
|
||||||
|
}
|
||||||
61
src/Invoicing/Interfaces/Messaging/WhenOrderConfirmed.php
Normal file
61
src/Invoicing/Interfaces/Messaging/WhenOrderConfirmed.php
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
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 + 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,
|
||||||
|
customerAddress: 'N/A',
|
||||||
|
lines: array_map(
|
||||||
|
static fn (array $line): array => [
|
||||||
|
'description' => $line['productName'],
|
||||||
|
'quantity' => $line['quantity'],
|
||||||
|
'unitPriceInCents' => $line['unitPriceInCents'],
|
||||||
|
'currency' => $line['currency'],
|
||||||
|
],
|
||||||
|
$message->lines,
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\LegacyFulfillment\Infrastructure\AntiCorruption;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modele legacy : champs abreges et codifications imposees par le systeme d'expedition historique.
|
||||||
|
* Ce format ne doit jamais fuiter en dehors de l'ACL.
|
||||||
|
*/
|
||||||
|
final readonly class LegacyCreateShipmentCommand
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $ord_ref, // reference commande (format legacy: prefixe LEG-)
|
||||||
|
public string $rcpt_nm, // nom destinataire (tronque a 35 chars)
|
||||||
|
public string $rcpt_addr, // adresse sur une ligne
|
||||||
|
public string $rcpt_zip, // code postal
|
||||||
|
public string $rcpt_cty, // code pays ISO 2
|
||||||
|
public int $wgt_g, // poids en grammes
|
||||||
|
public string $desc, // description colis
|
||||||
|
public string $req_dt, // date demande format legacy YYYYMMDD
|
||||||
|
public string $sts, // statut: NEW, DIS (dispatched)
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\LegacyFulfillment\Infrastructure\AntiCorruption;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Anti-Corruption Layer : traduit le Published Language sales.v1 vers le modele
|
||||||
|
* legacy d'expedition. Isole toutes les bizarreries du format legacy.
|
||||||
|
*
|
||||||
|
* @see §13.2 du boilerplate spec
|
||||||
|
*/
|
||||||
|
final class LegacyShipmentAcl
|
||||||
|
{
|
||||||
|
private const int MAX_RECIPIENT_LENGTH = 35;
|
||||||
|
|
||||||
|
public function fromSalesOrderConfirmed(OrderConfirmed $message): LegacyCreateShipmentCommand
|
||||||
|
{
|
||||||
|
$recipientName = mb_substr('Customer ' . $message->customerId, 0, self::MAX_RECIPIENT_LENGTH);
|
||||||
|
|
||||||
|
$totalWeight = max(100, count($message->lines) * 500);
|
||||||
|
|
||||||
|
$description = sprintf(
|
||||||
|
'CMD-%s/%d articles',
|
||||||
|
mb_substr($message->orderId, 0, 8),
|
||||||
|
count($message->lines),
|
||||||
|
);
|
||||||
|
|
||||||
|
return new LegacyCreateShipmentCommand(
|
||||||
|
ord_ref: 'LEG-' . $message->orderId,
|
||||||
|
rcpt_nm: $recipientName,
|
||||||
|
rcpt_addr: 'N/A',
|
||||||
|
rcpt_zip: '00000',
|
||||||
|
rcpt_cty: 'FR',
|
||||||
|
wgt_g: $totalWeight,
|
||||||
|
desc: $description,
|
||||||
|
req_dt: date('Ymd'),
|
||||||
|
sts: 'NEW',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\LegacyFulfillment\Interfaces\Messaging;
|
||||||
|
|
||||||
|
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 + Idempotent : passe par l'Anti-Corruption Layer avec
|
||||||
|
* garde d'idempotence pour eviter les doublons.
|
||||||
|
*/
|
||||||
|
#[AsMessageHandler]
|
||||||
|
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(
|
||||||
|
externalOrderId: $message->orderId,
|
||||||
|
recipientName: $legacyCommand->rcpt_nm,
|
||||||
|
street: $legacyCommand->rcpt_addr,
|
||||||
|
city: 'N/A',
|
||||||
|
postalCode: $legacyCommand->rcpt_zip,
|
||||||
|
country: $legacyCommand->rcpt_cty,
|
||||||
|
totalWeightInGrams: $legacyCommand->wgt_g,
|
||||||
|
description: $legacyCommand->desc,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Sales\Infrastructure\Messaging;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderCancelled as OrderCancelledContract;
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed as OrderConfirmedContract;
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderPlaced as OrderPlacedContract;
|
||||||
|
use MiniShop\Sales\Application\Port\SalesEventPublisher;
|
||||||
|
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. Propage un correlationId pour le tracing.
|
||||||
|
*/
|
||||||
|
final readonly class MessengerSalesEventPublisher implements SalesEventPublisher
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private MessageBusInterface $messageBus,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function publishOrderPlaced(OrderPlaced $event): void
|
||||||
|
{
|
||||||
|
$this->messageBus->dispatch(new OrderPlacedContract(
|
||||||
|
orderId: $event->orderId->toString(),
|
||||||
|
customerId: $event->customerId->toString(),
|
||||||
|
totalInCents: $event->total->amount,
|
||||||
|
currency: $event->total->currency,
|
||||||
|
placedAt: $event->placedAt->format(\DateTimeInterface::ATOM),
|
||||||
|
lines: [],
|
||||||
|
correlationId: CorrelationId::generate()->toString(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function publishOrderConfirmed(OrderConfirmed $event): void
|
||||||
|
{
|
||||||
|
$this->messageBus->dispatch(new OrderConfirmedContract(
|
||||||
|
orderId: $event->orderId->toString(),
|
||||||
|
customerId: $event->customerId->toString(),
|
||||||
|
totalInCents: $event->total->amount,
|
||||||
|
currency: $event->total->currency,
|
||||||
|
lines: array_map(
|
||||||
|
static fn (OrderLine $line): array => [
|
||||||
|
'productName' => $line->productName,
|
||||||
|
'quantity' => $line->quantity,
|
||||||
|
'unitPriceInCents' => $line->unitPrice->amount,
|
||||||
|
'currency' => $line->unitPrice->currency,
|
||||||
|
],
|
||||||
|
$event->lines,
|
||||||
|
),
|
||||||
|
correlationId: CorrelationId::generate()->toString(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function publishOrderCancelled(OrderCancelled $event): void
|
||||||
|
{
|
||||||
|
$this->messageBus->dispatch(new OrderCancelledContract(
|
||||||
|
orderId: $event->orderId->toString(),
|
||||||
|
correlationId: CorrelationId::generate()->toString(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace MiniShop\Sales\Infrastructure\Messaging;
|
namespace MiniShop\Sales\Infrastructure\Messaging;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed as OrderConfirmedContract;
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderPlaced as OrderPlacedContract;
|
||||||
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\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrder;
|
use MiniShop\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrder;
|
||||||
@@ -16,7 +18,7 @@ use MiniShop\Sales\Domain\Model\OrderLine;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Couplage naif : Sales appelle directement les handlers des autres BC.
|
* Couplage naif : Sales appelle directement les handlers des autres BC.
|
||||||
* Ce couplage sera supprime dans les episodes suivants (Published Language, Conformist, ACL).
|
* Depuis step/01, la traduction passe par le Published Language (contracts/sales/v1).
|
||||||
*/
|
*/
|
||||||
final readonly class NaiveSalesEventPublisher implements SalesEventPublisher
|
final readonly class NaiveSalesEventPublisher implements SalesEventPublisher
|
||||||
{
|
{
|
||||||
@@ -27,42 +29,78 @@ final readonly class NaiveSalesEventPublisher implements SalesEventPublisher
|
|||||||
|
|
||||||
public function publishOrderPlaced(OrderPlaced $event): void
|
public function publishOrderPlaced(OrderPlaced $event): void
|
||||||
{
|
{
|
||||||
// Pas d'action downstream sur OrderPlaced dans le scenario naif.
|
// Traduction domaine → Published Language (pour reference)
|
||||||
|
new OrderPlacedContract(
|
||||||
|
orderId: $event->orderId->toString(),
|
||||||
|
customerId: $event->customerId->toString(),
|
||||||
|
totalInCents: $event->total->amount,
|
||||||
|
currency: $event->total->currency,
|
||||||
|
placedAt: $event->placedAt->format(\DateTimeInterface::ATOM),
|
||||||
|
lines: array_map(
|
||||||
|
static fn (OrderLine $line): array => [
|
||||||
|
'productName' => $line->productName,
|
||||||
|
'quantity' => $line->quantity,
|
||||||
|
'unitPriceInCents' => $line->unitPrice->amount,
|
||||||
|
'currency' => $line->unitPrice->currency,
|
||||||
|
],
|
||||||
|
[], // OrderPlaced doesn't carry lines in this version
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// Pas d'action downstream sur OrderPlaced.
|
||||||
}
|
}
|
||||||
|
|
||||||
public function publishOrderConfirmed(OrderConfirmed $event): void
|
public function publishOrderConfirmed(OrderConfirmed $event): void
|
||||||
{
|
{
|
||||||
// Appel direct vers Invoicing — couplage naif
|
$lines = array_map(
|
||||||
($this->issueInvoiceHandler)(new IssueInvoiceForExternalOrder(
|
|
||||||
externalOrderId: $event->orderId->toString(),
|
|
||||||
customerName: 'Customer ' . $event->customerId->toString(),
|
|
||||||
customerAddress: 'N/A',
|
|
||||||
lines: array_map(
|
|
||||||
static fn (OrderLine $line): array => [
|
static fn (OrderLine $line): array => [
|
||||||
'description' => $line->productName,
|
'productName' => $line->productName,
|
||||||
'quantity' => $line->quantity,
|
'quantity' => $line->quantity,
|
||||||
'unitPriceInCents' => $line->unitPrice->amount,
|
'unitPriceInCents' => $line->unitPrice->amount,
|
||||||
'currency' => $line->unitPrice->currency,
|
'currency' => $line->unitPrice->currency,
|
||||||
],
|
],
|
||||||
$event->lines,
|
$event->lines,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Step 01 : traduction domaine → Published Language
|
||||||
|
$message = new OrderConfirmedContract(
|
||||||
|
orderId: $event->orderId->toString(),
|
||||||
|
customerId: $event->customerId->toString(),
|
||||||
|
totalInCents: $event->total->amount,
|
||||||
|
currency: $event->total->currency,
|
||||||
|
lines: $lines,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Appel direct vers Invoicing — toujours naif mais via Published Language
|
||||||
|
($this->issueInvoiceHandler)(new IssueInvoiceForExternalOrder(
|
||||||
|
externalOrderId: $message->orderId,
|
||||||
|
customerName: 'Customer ' . $message->customerId,
|
||||||
|
customerAddress: 'N/A',
|
||||||
|
lines: array_map(
|
||||||
|
static fn (array $line): array => [
|
||||||
|
'description' => $line['productName'],
|
||||||
|
'quantity' => $line['quantity'],
|
||||||
|
'unitPriceInCents' => $line['unitPriceInCents'],
|
||||||
|
'currency' => $line['currency'],
|
||||||
|
],
|
||||||
|
$message->lines,
|
||||||
),
|
),
|
||||||
));
|
));
|
||||||
|
|
||||||
// Appel direct vers LegacyFulfillment — couplage naif
|
// Appel direct vers LegacyFulfillment — toujours naif mais via Published Language
|
||||||
($this->requestShipmentHandler)(new RequestShipmentFromSalesOrder(
|
($this->requestShipmentHandler)(new RequestShipmentFromSalesOrder(
|
||||||
externalOrderId: $event->orderId->toString(),
|
externalOrderId: $message->orderId,
|
||||||
recipientName: 'Customer ' . $event->customerId->toString(),
|
recipientName: 'Customer ' . $message->customerId,
|
||||||
street: '1 Rue du Commerce',
|
street: '1 Rue du Commerce',
|
||||||
city: 'Paris',
|
city: 'Paris',
|
||||||
postalCode: '75001',
|
postalCode: '75001',
|
||||||
country: 'FR',
|
country: 'FR',
|
||||||
totalWeightInGrams: 1000,
|
totalWeightInGrams: 1000,
|
||||||
description: sprintf('Order %s', $event->orderId->toString()),
|
description: sprintf('Order %s', $message->orderId),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function publishOrderCancelled(OrderCancelled $event): void
|
public function publishOrderCancelled(OrderCancelled $event): void
|
||||||
{
|
{
|
||||||
// Pas d'action downstream sur OrderCancelled dans le scenario naif.
|
// Pas d'action downstream sur OrderCancelled.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
69
tests/Contract/Sales/v1/ConformistCompatibilityTest.php
Normal file
69
tests/Contract/Sales/v1/ConformistCompatibilityTest.php
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Tests\Contract\Sales\v1;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
|
||||||
|
use MiniShop\Invoicing\Application\Command\IssueInvoiceForExternalOrder;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test de compatibilite Conformist : Invoicing consomme sales.v1.OrderConfirmed
|
||||||
|
* tel quel, sans traduction. Toute evolution du schema upstream doit rester
|
||||||
|
* compatible avec ce consommateur.
|
||||||
|
*/
|
||||||
|
final class ConformistCompatibilityTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_invoicing_can_consume_order_confirmed_v1(): void
|
||||||
|
{
|
||||||
|
$invoiceRepo = new InMemoryInvoiceRepository();
|
||||||
|
$handler = new IssueInvoiceForExternalOrderHandler(
|
||||||
|
$invoiceRepo,
|
||||||
|
new SequentialInvoiceNumberGenerator(),
|
||||||
|
new SystemClock(),
|
||||||
|
);
|
||||||
|
$consumer = new WhenOrderConfirmed($handler, new InMemoryIdempotencyStore());
|
||||||
|
|
||||||
|
$message = new OrderConfirmed(
|
||||||
|
orderId: 'order-conformist-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
totalInCents: 3000,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
$consumer($message);
|
||||||
|
|
||||||
|
$invoice = $invoiceRepo->findByExternalOrderId('order-conformist-001');
|
||||||
|
self::assertNotNull($invoice, 'Invoicing (Conformist) must be able to consume sales.v1.OrderConfirmed.');
|
||||||
|
self::assertSame('order-conformist-001', $invoice->externalOrderId);
|
||||||
|
self::assertCount(1, $invoice->lines());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_schema_backward_compatible_with_extra_fields(): void
|
||||||
|
{
|
||||||
|
// Simule un schema upstream avec un champ supplementaire (retro-compatible)
|
||||||
|
$json = '{"orderId":"order-002","customerId":"cust-002","totalInCents":5000,"currency":"EUR","lines":[{"productName":"Gadget","quantity":1,"unitPriceInCents":5000,"currency":"EUR"}],"newField":"ignored"}';
|
||||||
|
$decoded = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
$message = new OrderConfirmed(
|
||||||
|
orderId: $decoded['orderId'],
|
||||||
|
customerId: $decoded['customerId'],
|
||||||
|
totalInCents: $decoded['totalInCents'],
|
||||||
|
currency: $decoded['currency'],
|
||||||
|
lines: $decoded['lines'],
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame('order-002', $message->orderId);
|
||||||
|
self::assertSame(5000, $message->totalInCents);
|
||||||
|
}
|
||||||
|
}
|
||||||
78
tests/Contract/Sales/v1/OrderConfirmedSchemaTest.php
Normal file
78
tests/Contract/Sales/v1/OrderConfirmedSchemaTest.php
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Tests\Contract\Sales\v1;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests de stabilite du contrat sales.v1.OrderConfirmed.
|
||||||
|
*/
|
||||||
|
final class OrderConfirmedSchemaTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_contract_has_required_fields(): void
|
||||||
|
{
|
||||||
|
$event = new OrderConfirmed(
|
||||||
|
orderId: 'order-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
totalInCents: 5500,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||||
|
['productName' => 'Gadget', 'quantity' => 1, 'unitPriceInCents' => 2500, 'currency' => 'EUR'],
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame('order-001', $event->orderId);
|
||||||
|
self::assertSame('cust-001', $event->customerId);
|
||||||
|
self::assertSame(5500, $event->totalInCents);
|
||||||
|
self::assertSame('EUR', $event->currency);
|
||||||
|
self::assertCount(2, $event->lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_contract_serializes_to_stable_json(): void
|
||||||
|
{
|
||||||
|
$event = new OrderConfirmed(
|
||||||
|
orderId: 'order-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
totalInCents: 5500,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
$json = json_encode($event, JSON_THROW_ON_ERROR);
|
||||||
|
$decoded = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
self::assertArrayHasKey('orderId', $decoded);
|
||||||
|
self::assertArrayHasKey('customerId', $decoded);
|
||||||
|
self::assertArrayHasKey('totalInCents', $decoded);
|
||||||
|
self::assertArrayHasKey('currency', $decoded);
|
||||||
|
self::assertArrayHasKey('lines', $decoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_lines_contain_required_fields(): void
|
||||||
|
{
|
||||||
|
$event = new OrderConfirmed(
|
||||||
|
orderId: 'order-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
totalInCents: 3000,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
$json = json_encode($event, JSON_THROW_ON_ERROR);
|
||||||
|
$decoded = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
|
||||||
|
$line = $decoded['lines'][0];
|
||||||
|
|
||||||
|
self::assertArrayHasKey('productName', $line);
|
||||||
|
self::assertArrayHasKey('quantity', $line);
|
||||||
|
self::assertArrayHasKey('unitPriceInCents', $line);
|
||||||
|
self::assertArrayHasKey('currency', $line);
|
||||||
|
}
|
||||||
|
}
|
||||||
79
tests/Contract/Sales/v1/OrderPlacedSchemaTest.php
Normal file
79
tests/Contract/Sales/v1/OrderPlacedSchemaTest.php
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Tests\Contract\Sales\v1;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderPlaced;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests de stabilite du contrat sales.v1.OrderPlaced.
|
||||||
|
* Garantit que le schema est stable et ne casse pas les consommateurs downstream.
|
||||||
|
*/
|
||||||
|
final class OrderPlacedSchemaTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_contract_has_required_fields(): void
|
||||||
|
{
|
||||||
|
$event = new OrderPlaced(
|
||||||
|
orderId: 'order-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
totalInCents: 3000,
|
||||||
|
currency: 'EUR',
|
||||||
|
placedAt: '2026-01-15T10:30:00+00:00',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame('order-001', $event->orderId);
|
||||||
|
self::assertSame('cust-001', $event->customerId);
|
||||||
|
self::assertSame(3000, $event->totalInCents);
|
||||||
|
self::assertSame('EUR', $event->currency);
|
||||||
|
self::assertSame('2026-01-15T10:30:00+00:00', $event->placedAt);
|
||||||
|
self::assertCount(1, $event->lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_contract_serializes_to_stable_json(): void
|
||||||
|
{
|
||||||
|
$event = new OrderPlaced(
|
||||||
|
orderId: 'order-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
totalInCents: 3000,
|
||||||
|
currency: 'EUR',
|
||||||
|
placedAt: '2026-01-15T10:30:00+00:00',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
$json = json_encode($event, JSON_THROW_ON_ERROR);
|
||||||
|
$decoded = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
self::assertArrayHasKey('orderId', $decoded);
|
||||||
|
self::assertArrayHasKey('customerId', $decoded);
|
||||||
|
self::assertArrayHasKey('totalInCents', $decoded);
|
||||||
|
self::assertArrayHasKey('currency', $decoded);
|
||||||
|
self::assertArrayHasKey('placedAt', $decoded);
|
||||||
|
self::assertArrayHasKey('lines', $decoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_contract_deserializes_from_json(): void
|
||||||
|
{
|
||||||
|
$json = '{"orderId":"order-001","customerId":"cust-001","totalInCents":3000,"currency":"EUR","placedAt":"2026-01-15T10:30:00+00:00","lines":[{"productName":"Widget","quantity":2,"unitPriceInCents":1500,"currency":"EUR"}]}';
|
||||||
|
|
||||||
|
$decoded = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
$event = new OrderPlaced(
|
||||||
|
orderId: $decoded['orderId'],
|
||||||
|
customerId: $decoded['customerId'],
|
||||||
|
totalInCents: $decoded['totalInCents'],
|
||||||
|
currency: $decoded['currency'],
|
||||||
|
placedAt: $decoded['placedAt'],
|
||||||
|
lines: $decoded['lines'],
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame('order-001', $event->orderId);
|
||||||
|
self::assertSame(3000, $event->totalInCents);
|
||||||
|
}
|
||||||
|
}
|
||||||
57
tests/Contract/Sales/v1/OrderViewSchemaTest.php
Normal file
57
tests/Contract/Sales/v1/OrderViewSchemaTest.php
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Tests\Contract\Sales\v1;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Api\OrderView;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests de stabilite du contrat sales.v1.OrderView.
|
||||||
|
*/
|
||||||
|
final class OrderViewSchemaTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_contract_has_required_fields(): void
|
||||||
|
{
|
||||||
|
$view = new OrderView(
|
||||||
|
orderId: 'order-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
status: 'confirmed',
|
||||||
|
totalInCents: 5500,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR', 'lineTotalInCents' => 3000],
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame('order-001', $view->orderId);
|
||||||
|
self::assertSame('confirmed', $view->status);
|
||||||
|
self::assertSame(5500, $view->totalInCents);
|
||||||
|
self::assertCount(1, $view->lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_contract_serializes_to_stable_json(): void
|
||||||
|
{
|
||||||
|
$view = new OrderView(
|
||||||
|
orderId: 'order-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
status: 'placed',
|
||||||
|
totalInCents: 3000,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR', 'lineTotalInCents' => 3000],
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
$json = json_encode($view, JSON_THROW_ON_ERROR);
|
||||||
|
$decoded = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
self::assertArrayHasKey('orderId', $decoded);
|
||||||
|
self::assertArrayHasKey('customerId', $decoded);
|
||||||
|
self::assertArrayHasKey('status', $decoded);
|
||||||
|
self::assertArrayHasKey('totalInCents', $decoded);
|
||||||
|
self::assertArrayHasKey('currency', $decoded);
|
||||||
|
self::assertArrayHasKey('lines', $decoded);
|
||||||
|
}
|
||||||
|
}
|
||||||
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',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
50
tests/Integration/InvoicingConformistTest.php
Normal file
50
tests/Integration/InvoicingConformistTest.php
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Tests\Integration;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test d'integration : le consumer Conformist d'Invoicing recoit un message
|
||||||
|
* sales.v1.OrderConfirmed et cree une facture sans traduction.
|
||||||
|
*/
|
||||||
|
final class InvoicingConformistTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_invoicing_creates_invoice_on_order_confirmed(): void
|
||||||
|
{
|
||||||
|
$invoiceRepo = new InMemoryInvoiceRepository();
|
||||||
|
$consumer = new WhenOrderConfirmed(
|
||||||
|
new IssueInvoiceForExternalOrderHandler(
|
||||||
|
$invoiceRepo,
|
||||||
|
new SequentialInvoiceNumberGenerator(),
|
||||||
|
new SystemClock(),
|
||||||
|
),
|
||||||
|
new InMemoryIdempotencyStore(),
|
||||||
|
);
|
||||||
|
|
||||||
|
$consumer(new OrderConfirmed(
|
||||||
|
orderId: 'order-int-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
totalInCents: 4500,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||||
|
['productName' => 'Gadget', 'quantity' => 1, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||||
|
],
|
||||||
|
));
|
||||||
|
|
||||||
|
$invoice = $invoiceRepo->findByExternalOrderId('order-int-001');
|
||||||
|
self::assertNotNull($invoice);
|
||||||
|
self::assertCount(2, $invoice->lines());
|
||||||
|
self::assertSame('INV-000001', $invoice->invoiceNumber);
|
||||||
|
}
|
||||||
|
}
|
||||||
49
tests/Integration/LegacyFulfillmentAclTest.php
Normal file
49
tests/Integration/LegacyFulfillmentAclTest.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Tests\Integration;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
|
||||||
|
use MiniShop\LegacyFulfillment\Application\Command\RequestShipmentFromSalesOrderHandler;
|
||||||
|
use MiniShop\LegacyFulfillment\Domain\Model\LegacyOrderRef;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test d'integration : le consumer ACL de LegacyFulfillment recoit un message
|
||||||
|
* sales.v1.OrderConfirmed, le traduit via l'ACL, et cree une demande d'expedition.
|
||||||
|
*/
|
||||||
|
final class LegacyFulfillmentAclTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_acl_consumer_creates_shipment_from_contract(): void
|
||||||
|
{
|
||||||
|
$shipmentRepo = new InMemoryShipmentRequestRepository();
|
||||||
|
$gateway = new FakeLegacyFulfillmentGateway();
|
||||||
|
|
||||||
|
$consumer = new WhenOrderConfirmed(
|
||||||
|
new LegacyShipmentAcl(),
|
||||||
|
new RequestShipmentFromSalesOrderHandler($shipmentRepo, $gateway, new SystemClock()),
|
||||||
|
new InMemoryIdempotencyStore(),
|
||||||
|
);
|
||||||
|
|
||||||
|
$consumer(new OrderConfirmed(
|
||||||
|
orderId: 'order-acl-int-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
totalInCents: 3000,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: [
|
||||||
|
['productName' => 'Widget', 'quantity' => 2, 'unitPriceInCents' => 1500, 'currency' => 'EUR'],
|
||||||
|
],
|
||||||
|
));
|
||||||
|
|
||||||
|
$shipment = $shipmentRepo->findByOrderRef(LegacyOrderRef::fromExternalId('order-acl-int-001'));
|
||||||
|
self::assertNotNull($shipment, 'ACL consumer must create a shipment request.');
|
||||||
|
self::assertCount(1, $gateway->sentRequests());
|
||||||
|
}
|
||||||
|
}
|
||||||
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MiniShop\Tests\Unit\LegacyFulfillment\Infrastructure\AntiCorruption;
|
||||||
|
|
||||||
|
use MiniShop\Contracts\Sales\V1\Event\OrderConfirmed;
|
||||||
|
use MiniShop\LegacyFulfillment\Infrastructure\AntiCorruption\LegacyShipmentAcl;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests du mapping ACL : sales.v1.OrderConfirmed → LegacyCreateShipmentCommand.
|
||||||
|
*/
|
||||||
|
final class LegacyShipmentAclTest extends TestCase
|
||||||
|
{
|
||||||
|
private LegacyShipmentAcl $acl;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->acl = new LegacyShipmentAcl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_maps_order_ref_with_legacy_prefix(): void
|
||||||
|
{
|
||||||
|
$command = $this->acl->fromSalesOrderConfirmed($this->createMessage());
|
||||||
|
|
||||||
|
self::assertStringStartsWith('LEG-', $command->ord_ref);
|
||||||
|
self::assertSame('LEG-order-acl-001', $command->ord_ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_truncates_recipient_name(): void
|
||||||
|
{
|
||||||
|
$message = new OrderConfirmed(
|
||||||
|
orderId: 'order-001',
|
||||||
|
customerId: 'very-long-customer-id-that-exceeds-thirty-five-characters-limit',
|
||||||
|
totalInCents: 1000,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: [['productName' => 'X', 'quantity' => 1, 'unitPriceInCents' => 1000, 'currency' => 'EUR']],
|
||||||
|
);
|
||||||
|
|
||||||
|
$command = $this->acl->fromSalesOrderConfirmed($message);
|
||||||
|
|
||||||
|
self::assertLessThanOrEqual(35, mb_strlen($command->rcpt_nm));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_calculates_weight_from_line_count(): void
|
||||||
|
{
|
||||||
|
$command = $this->acl->fromSalesOrderConfirmed($this->createMessage(lineCount: 3));
|
||||||
|
|
||||||
|
self::assertSame(1500, $command->wgt_g); // 3 * 500g
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_minimum_weight_is_100g(): void
|
||||||
|
{
|
||||||
|
$command = $this->acl->fromSalesOrderConfirmed($this->createMessage(lineCount: 0));
|
||||||
|
|
||||||
|
self::assertGreaterThanOrEqual(100, $command->wgt_g);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_status_is_new(): void
|
||||||
|
{
|
||||||
|
$command = $this->acl->fromSalesOrderConfirmed($this->createMessage());
|
||||||
|
|
||||||
|
self::assertSame('NEW', $command->sts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_date_format_is_legacy_yyyymmdd(): void
|
||||||
|
{
|
||||||
|
$command = $this->acl->fromSalesOrderConfirmed($this->createMessage());
|
||||||
|
|
||||||
|
self::assertMatchesRegularExpression('/^\d{8}$/', $command->req_dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_description_contains_order_ref_and_article_count(): void
|
||||||
|
{
|
||||||
|
$command = $this->acl->fromSalesOrderConfirmed($this->createMessage(lineCount: 2));
|
||||||
|
|
||||||
|
self::assertStringContainsString('CMD-', $command->desc);
|
||||||
|
self::assertStringContainsString('2 articles', $command->desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createMessage(int $lineCount = 1): OrderConfirmed
|
||||||
|
{
|
||||||
|
$lines = array_fill(0, $lineCount, [
|
||||||
|
'productName' => 'Widget',
|
||||||
|
'quantity' => 1,
|
||||||
|
'unitPriceInCents' => 1000,
|
||||||
|
'currency' => 'EUR',
|
||||||
|
]);
|
||||||
|
|
||||||
|
return new OrderConfirmed(
|
||||||
|
orderId: 'order-acl-001',
|
||||||
|
customerId: 'cust-001',
|
||||||
|
totalInCents: $lineCount * 1000,
|
||||||
|
currency: 'EUR',
|
||||||
|
lines: $lines,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
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