2 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
21b8f37411 Step 01 — Published Language
- Extraction des DTOs publics dans contracts/sales/v1/ :
  OrderPlaced, OrderConfirmed, OrderCancelled, OrderView
- NaiveSalesEventPublisher traduit les événements domaine en contrats v1
- Tests de contrat (schéma, sérialisation JSON) pour stabilité du langage publié
2026-03-04 00:29:51 +01:00
17 changed files with 812 additions and 22 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
MiniShop\Sales\Application\Port\SalesEventPublisher:
alias: MiniShop\Sales\Infrastructure\Messaging\NaiveSalesEventPublisher
alias: MiniShop\Sales\Infrastructure\Messaging\MessengerSalesEventPublisher
# --- Invoicing ---
MiniShop\Invoicing\Application\:

View File

@@ -5,9 +5,10 @@
"license": "MIT",
"require": {
"php": ">=8.5",
"symfony/framework-bundle": "^7.2",
"symfony/console": "^7.2",
"symfony/dotenv": "^7.2",
"symfony/framework-bundle": "^7.2",
"symfony/messenger": "^7.2",
"symfony/yaml": "^7.2"
},
"require-dev": {
@@ -19,7 +20,7 @@
"MiniShop\\Invoicing\\": "src/Invoicing/",
"MiniShop\\LegacyFulfillment\\": "src/LegacyFulfillment/",
"MiniShop\\Shared\\": "src/Shared/",
"MiniShop\\Contracts\\": "contracts/",
"MiniShop\\Contracts\\Sales\\V1\\": "contracts/sales/v1/",
"App\\": "apps/symfony/src/"
}
},

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",
"This file is @generated automatically"
],
"content-hash": "cf876267ce49272867204aaa70702ed4",
"content-hash": "1ad6323891b4f76eb1b6f55ff6741b32",
"packages": [
{
"name": "psr/cache",
@@ -55,6 +55,54 @@
},
"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",
"version": "2.0.2",
@@ -384,6 +432,83 @@
],
"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",
"version": "v8.0.6",
@@ -1508,6 +1633,100 @@
],
"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",
"version": "v1.33.0",

View 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,
) {}
}

View File

@@ -0,0 +1,16 @@
<?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,
) {}
}

View File

@@ -0,0 +1,23 @@
<?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,
) {}
}

View File

@@ -0,0 +1,24 @@
<?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,
) {}
}

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

@@ -4,6 +4,8 @@ declare(strict_types=1);
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\IssueInvoiceForExternalOrderHandler;
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.
* 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
{
@@ -27,42 +29,78 @@ final readonly class NaiveSalesEventPublisher implements SalesEventPublisher
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
{
// Appel direct vers Invoicing — couplage naif
($this->issueInvoiceHandler)(new IssueInvoiceForExternalOrder(
externalOrderId: $event->orderId->toString(),
customerName: 'Customer ' . $event->customerId->toString(),
customerAddress: 'N/A',
lines: array_map(
$lines = array_map(
static fn (OrderLine $line): array => [
'description' => $line->productName,
'productName' => $line->productName,
'quantity' => $line->quantity,
'unitPriceInCents' => $line->unitPrice->amount,
'currency' => $line->unitPrice->currency,
],
$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(
externalOrderId: $event->orderId->toString(),
recipientName: 'Customer ' . $event->customerId->toString(),
externalOrderId: $message->orderId,
recipientName: 'Customer ' . $message->customerId,
street: '1 Rue du Commerce',
city: 'Paris',
postalCode: '75001',
country: 'FR',
totalWeightInGrams: 1000,
description: sprintf('Order %s', $event->orderId->toString()),
description: sprintf('Order %s', $message->orderId),
));
}
public function publishOrderCancelled(OrderCancelled $event): void
{
// Pas d'action downstream sur OrderCancelled dans le scenario naif.
// Pas d'action downstream sur OrderCancelled.
}
}

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,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);
}
}

View 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);
}
}

View 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);
}
}

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);
}
}