Skip to content

Commit 3947aa2

Browse files
committed
MSI-1542: Provide MSI support for Shipment Web API endpoint
1 parent c33478b commit 3947aa2

File tree

5 files changed

+115
-152
lines changed

5 files changed

+115
-152
lines changed

app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ShipmentDocumentFactory
4242
private $hydratorPool;
4343

4444
/**
45-
* @var ExtensionAttributesProcessor|null
45+
* @var ExtensionAttributesProcessor
4646
*/
4747
private $extensionAttributesProcessor;
4848

@@ -68,8 +68,6 @@ public function __construct(
6868
}
6969

7070
/**
71-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
72-
*
7371
* @param OrderInterface $order
7472
* @param ShipmentItemCreationInterface[] $items
7573
* @param ShipmentTrackCreationInterface[] $tracks

app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory/ExtensionAttributesProcessor.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace Magento\Sales\Model\Order\ShipmentDocumentFactory;
99

10-
use Magento\Framework\Api\SimpleDataObjectConverter;
1110
use Magento\Framework\Reflection\ExtensionAttributesProcessor as AttributesProcessor;
1211
use Magento\Sales\Api\Data\ShipmentCreationArgumentsExtensionInterface;
1312
use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface;
1413
use Magento\Sales\Api\Data\ShipmentExtensionFactory;
14+
use Magento\Sales\Api\Data\ShipmentExtensionInterface;
1515
use Magento\Sales\Api\Data\ShipmentInterface;
1616

1717
/**
@@ -53,24 +53,25 @@ public function execute(
5353
if (null === $arguments) {
5454
return;
5555
}
56-
57-
$shipmentExtensionAttributes = $shipment->getExtensionAttributes();
58-
if (null === $shipmentExtensionAttributes) {
59-
$shipmentExtensionAttributes = $this->shipmentExtensionFactory->create();
56+
$shipmentExtensionAttributes = [];
57+
if (null !== $shipment->getExtensionAttributes()) {
58+
$shipmentExtensionAttributes = $this->extensionAttributesProcessor->buildOutputDataArray(
59+
$shipment->getExtensionAttributes(),
60+
ShipmentExtensionInterface::class
61+
);
62+
}
63+
$argumentsExtensionAttributes = [];
64+
if (null !== $arguments->getExtensionAttributes()) {
65+
$argumentsExtensionAttributes = $this->extensionAttributesProcessor->buildOutputDataArray(
66+
$arguments->getExtensionAttributes(),
67+
ShipmentCreationArgumentsExtensionInterface::class
68+
);
6069
}
6170

62-
$attributes = $arguments->getExtensionAttributes();
63-
$extensionAttributes = $this->extensionAttributesProcessor->buildOutputDataArray(
64-
$attributes,
65-
ShipmentCreationArgumentsExtensionInterface::class
66-
);
67-
68-
foreach ($extensionAttributes as $code => $value) {
69-
$setMethod = 'set' . SimpleDataObjectConverter::snakeCaseToUpperCamelCase($code);
71+
$mergedExtensionAttributes = $this->shipmentExtensionFactory->create([
72+
'data' => array_merge($shipmentExtensionAttributes, $argumentsExtensionAttributes)
73+
]);
7074

71-
if (method_exists($shipmentExtensionAttributes, $setMethod)) {
72-
$shipmentExtensionAttributes->$setMethod($value);
73-
}
74-
}
75+
$shipment->setExtensionAttributes($mergedExtensionAttributes);
7576
}
7677
}

app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactory/ExtensionAttributesProcessorTest.php

Lines changed: 0 additions & 132 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Model\Order;
9+
10+
use Magento\Sales\Api\Data\ShipmentCreationArgumentsExtensionInterfaceFactory;
11+
use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface;
12+
use Magento\Sales\Model\Order;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Provide tests for shipment document factory.
18+
*/
19+
class ShipmentDocumentFactoryTest extends TestCase
20+
{
21+
/**
22+
* @var Order
23+
*/
24+
private $order;
25+
26+
/**
27+
* @var ShipmentDocumentFactory
28+
*/
29+
private $shipmentDocumentFactory;
30+
31+
/**
32+
* @var ShipmentCreationArgumentsInterface
33+
*/
34+
private $shipmentCreationArgumentsInterface;
35+
36+
/**
37+
* @var ShipmentCreationArgumentsExtensionInterfaceFactory
38+
*/
39+
private $shipmentCreationArgumentsExtensionInterfaceFactory;
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
protected function setUp(): void
45+
{
46+
$objectManager = Bootstrap::getObjectManager();
47+
48+
$this->order = $objectManager->create(Order::class);
49+
$this->shipmentDocumentFactory = $objectManager->create(ShipmentDocumentFactory::class);
50+
$this->shipmentCreationArgumentsInterface = $objectManager
51+
->create(ShipmentCreationArgumentsInterface::class);
52+
$this->shipmentCreationArgumentsExtensionInterfaceFactory = $objectManager
53+
->create(ShipmentCreationArgumentsExtensionInterfaceFactory::class);
54+
}
55+
56+
/**
57+
* Create shipment with shipment creation arguments.
58+
*
59+
* @magentoDataFixture Magento/Sales/_files/order.php
60+
*/
61+
public function testCreate(): void
62+
{
63+
$order = $this->order->loadByIncrementId('100000001');
64+
$argumentsExtensionAttributes = $this->shipmentCreationArgumentsExtensionInterfaceFactory->create([
65+
'data' => ['test_attribute_value' => 'test_value']
66+
]);
67+
$this->shipmentCreationArgumentsInterface->setExtensionAttributes($argumentsExtensionAttributes);
68+
$shipment = $this->shipmentDocumentFactory->create(
69+
$order,
70+
[],
71+
[],
72+
null,
73+
false,
74+
[],
75+
$this->shipmentCreationArgumentsInterface
76+
);
77+
$shipmentExtensionAttributes = $shipment->getExtensionAttributes();
78+
self::assertEquals('test_value', $shipmentExtensionAttributes->getTestAttributeValue());
79+
}
80+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
10+
<extension_attributes for="Magento\Sales\Api\Data\ShipmentInterface">
11+
<attribute code="test_attribute_value" type="string"/>
12+
</extension_attributes>
13+
<extension_attributes for="Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface">
14+
<attribute code="test_attribute_value" type="string"/>
15+
</extension_attributes>
16+
</config>

0 commit comments

Comments
 (0)