diff --git a/src/LegacyFulfillment/Infrastructure/AntiCorruption/LegacyCreateShipmentCommand.php b/src/LegacyFulfillment/Infrastructure/AntiCorruption/LegacyCreateShipmentCommand.php new file mode 100644 index 0000000..dd01366 --- /dev/null +++ b/src/LegacyFulfillment/Infrastructure/AntiCorruption/LegacyCreateShipmentCommand.php @@ -0,0 +1,24 @@ +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', + ); + } +} diff --git a/src/LegacyFulfillment/Interfaces/Messaging/WhenOrderConfirmed.php b/src/LegacyFulfillment/Interfaces/Messaging/WhenOrderConfirmed.php new file mode 100644 index 0000000..5ecaf0a --- /dev/null +++ b/src/LegacyFulfillment/Interfaces/Messaging/WhenOrderConfirmed.php @@ -0,0 +1,40 @@ +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, + )); + } +} diff --git a/tests/Integration/LegacyFulfillmentAclTest.php b/tests/Integration/LegacyFulfillmentAclTest.php new file mode 100644 index 0000000..015892f --- /dev/null +++ b/tests/Integration/LegacyFulfillmentAclTest.php @@ -0,0 +1,47 @@ + '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()); + } +} diff --git a/tests/Unit/LegacyFulfillment/Infrastructure/AntiCorruption/LegacyShipmentAclTest.php b/tests/Unit/LegacyFulfillment/Infrastructure/AntiCorruption/LegacyShipmentAclTest.php new file mode 100644 index 0000000..975ab56 --- /dev/null +++ b/tests/Unit/LegacyFulfillment/Infrastructure/AntiCorruption/LegacyShipmentAclTest.php @@ -0,0 +1,99 @@ +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, + ); + } +}