Compare commits
1 Commits
step/02-co
...
step/03-ac
| Author | SHA1 | Date | |
|---|---|---|---|
| f8be8166b7 |
@@ -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,40 @@
|
||||
<?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 Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
/**
|
||||
* Consumer ACL : recoit sales.v1.OrderConfirmed, passe par l'Anti-Corruption Layer
|
||||
* pour traduire vers le modele legacy, puis delegue au handler applicatif.
|
||||
*/
|
||||
#[AsMessageHandler]
|
||||
final readonly class WhenOrderConfirmed
|
||||
{
|
||||
public function __construct(
|
||||
private LegacyShipmentAcl $acl,
|
||||
private RequestShipmentFromSalesOrderHandler $handler,
|
||||
) {}
|
||||
|
||||
public function __invoke(OrderConfirmed $message): void
|
||||
{
|
||||
$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,
|
||||
));
|
||||
}
|
||||
}
|
||||
47
tests/Integration/LegacyFulfillmentAclTest.php
Normal file
47
tests/Integration/LegacyFulfillmentAclTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?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\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()),
|
||||
);
|
||||
|
||||
$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());
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user