1 Commits

Author SHA1 Message Date
dcc81ec9bb Step 02 — Conformist (Invoicing)
- Ajout Symfony Messenger comme bus d'intégration
- MessengerSalesEventPublisher remplace le NaivePublisher pour dispatch via Messenger
- WhenOrderConfirmed (Invoicing) : consumer Conformist qui consomme sales.v1.OrderConfirmed tel quel
- Tests de compatibilité Conformist et intégration
- Configuration Messenger avec transport sync
2026-03-04 00:31:08 +01:00
8 changed files with 454 additions and 3 deletions

View 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

View File

@@ -25,7 +25,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\:

View File

@@ -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": {

221
composer.lock generated
View File

@@ -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",

View File

@@ -0,0 +1,40 @@
<?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 Symfony\Component\Messenger\Attribute\AsMessageHandler;
/**
* Conformist : Invoicing consomme le contrat sales.v1.OrderConfirmed tel quel,
* sans traduction. La dependance upstream est explicite.
*/
#[AsMessageHandler]
final readonly class WhenOrderConfirmed
{
public function __construct(
private IssueInvoiceForExternalOrderHandler $handler,
) {}
public function __invoke(OrderConfirmed $message): void
{
($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,
),
));
}
}

View File

@@ -0,0 +1,64 @@
<?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 Symfony\Component\Messenger\MessageBusInterface;
/**
* Publie les evenements de domaine Sales sous forme de contrats Published Language
* via Symfony Messenger. Remplace le NaiveSalesEventPublisher.
*/
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: [],
));
}
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,
),
));
}
public function publishOrderCancelled(OrderCancelled $event): void
{
$this->messageBus->dispatch(new OrderCancelledContract(
orderId: $event->orderId->toString(),
));
}
}

View File

@@ -0,0 +1,68 @@
<?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\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);
$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);
}
}

View File

@@ -0,0 +1,48 @@
<?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\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(),
),
);
$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);
}
}