Skip to content

Commit 256b60e

Browse files
committed
MAGETWO-70498: [Magento Cloud] - /rest/default/V1/order/<order id>/ship and configurable products - for 2.1
1 parent 0ab6392 commit 256b60e

File tree

2 files changed

+128
-56
lines changed

2 files changed

+128
-56
lines changed

app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77

88
use Magento\Framework\DataObject;
99
use Magento\Framework\Exception\LocalizedException;
10-
use Magento\Framework\Message\ManagerInterface;
11-
use Magento\Framework\Registry;
1210
use Magento\Sales\Api\Data\ShipmentTrackCreationInterface;
1311
use Magento\Sales\Api\Data\ShipmentTrackCreationInterfaceFactory;
1412
use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory;
15-
use Magento\Sales\Api\ShipmentRepositoryInterface;
16-
use Magento\Sales\Api\OrderRepositoryInterface;
1713
use Magento\Sales\Model\Order\ShipmentDocumentFactory;
1814
use Magento\Sales\Api\Data\ShipmentItemCreationInterface;
1915

@@ -33,22 +29,22 @@
3329
class ShipmentLoader extends DataObject
3430
{
3531
/**
36-
* @var ManagerInterface
32+
* @var \Magento\Framework\Message\ManagerInterface
3733
*/
3834
protected $messageManager;
3935

4036
/**
41-
* @var Registry
37+
* @var \Magento\Framework\Registry
4238
*/
4339
protected $registry;
4440

4541
/**
46-
* @var ShipmentRepositoryInterface
42+
* @var \Magento\Sales\Api\ShipmentRepositoryInterface
4743
*/
4844
protected $shipmentRepository;
4945

5046
/**
51-
* @var OrderRepositoryInterface
47+
* @var \Magento\Sales\Api\OrderRepositoryInterface
5248
*/
5349
protected $orderRepository;
5450

@@ -57,46 +53,80 @@ class ShipmentLoader extends DataObject
5753
*/
5854
protected $documentFactory;
5955

56+
/**
57+
* @var \Magento\Sales\Model\Order\ShipmentFactory
58+
* @deprecated
59+
*/
60+
protected $shipmentFactory;
61+
6062
/**
6163
* @var ShipmentTrackCreationInterfaceFactory
6264
*/
65+
protected $shipmentTrackCreationFactory;
66+
67+
/**
68+
* @var \Magento\Sales\Model\Order\Shipment\TrackFactory
69+
* @deprecated
70+
*/
6371
protected $trackFactory;
6472

6573
/**
6674
* @var ShipmentItemCreationInterfaceFactory
6775
*/
68-
private $itemFactory;
76+
private $shipmentItemCreationFactory;
6977

7078
/**
71-
* @param ManagerInterface $messageManager
72-
* @param Registry $registry
73-
* @param ShipmentRepositoryInterface $shipmentRepository
74-
* @param OrderRepositoryInterface $orderRepository
75-
* @param ShipmentDocumentFactory $documentFactory
76-
* @param ShipmentTrackCreationInterfaceFactory $trackFactory
77-
* @param ShipmentItemCreationInterfaceFactory $itemFactory
79+
* @param \Magento\Framework\Message\ManagerInterface $messageManager
80+
* @param \Magento\Framework\Registry $registry
81+
* @param \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository
82+
* @param \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory
83+
* @param \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory
84+
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
7885
* @param array $data
86+
* @param ShipmentDocumentFactory|null $documentFactory
87+
* @param ShipmentTrackCreationInterfaceFactory|null $trackFactory
88+
* @param ShipmentItemCreationInterfaceFactory|null $shipmentItemCreationFactory
7989
*/
8090
public function __construct(
81-
ManagerInterface $messageManager,
82-
Registry $registry,
83-
ShipmentRepositoryInterface $shipmentRepository,
84-
OrderRepositoryInterface $orderRepository,
85-
ShipmentDocumentFactory $documentFactory,
86-
ShipmentTrackCreationInterfaceFactory $trackFactory,
87-
ShipmentItemCreationInterfaceFactory $itemFactory,
88-
array $data = []
91+
\Magento\Framework\Message\ManagerInterface $messageManager,
92+
\Magento\Framework\Registry $registry,
93+
\Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository,
94+
\Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory,
95+
\Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory,
96+
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
97+
array $data = [],
98+
ShipmentDocumentFactory $documentFactory = null,
99+
ShipmentTrackCreationInterfaceFactory $shipmentTrackCreationFactory = null,
100+
ShipmentItemCreationInterfaceFactory $shipmentItemCreationFactory = null
89101
) {
90102
$this->messageManager = $messageManager;
91103
$this->registry = $registry;
92104
$this->shipmentRepository = $shipmentRepository;
93-
$this->orderRepository = $orderRepository;
94-
$this->documentFactory = $documentFactory;
105+
$this->shipmentFactory = $shipmentFactory;
95106
$this->trackFactory = $trackFactory;
96-
$this->itemFactory = $itemFactory;
107+
$this->orderRepository = $orderRepository;
108+
$this->documentFactory = $documentFactory ?: ObjectManager::getInstance()->get(ShipmentDocumentFactory::class);
109+
$this->shipmentTrackCreationFactory = $shipmentTrackCreationFactory
110+
?: ObjectManager::getInstance()->get(ShipmentTrackCreationInterfaceFactory::class);
111+
$this->shipmentItemCreationFactory = $shipmentItemCreationFactory
112+
?: ObjectManager::getInstance()->get(ShipmentItemCreationInterfaceFactory::class);
113+
97114
parent::__construct($data);
98115
}
99116

117+
/**
118+
* Initialize shipment items QTY
119+
*
120+
* @return array
121+
* @deprecated
122+
*/
123+
protected function getItemQtys()
124+
{
125+
$data = $this->getShipment();
126+
127+
return isset($data['items']) ? $data['items'] : [];
128+
}
129+
100130
/**
101131
* Initialize shipment model instance
102132
*
@@ -165,7 +195,7 @@ private function getTrackingArray()
165195
);
166196
}
167197
/** @var ShipmentTrackCreationInterface $trackCreation */
168-
$trackCreation = $this->trackFactory->create();
198+
$trackCreation = $this->shipmentTrackCreationFactory->create();
169199
$trackCreation->setTrackNumber($track['number']);
170200
$trackCreation->setTitle($track['title']);
171201
$trackCreation->setCarrierCode($track['carrier_code']);
@@ -187,7 +217,7 @@ private function getShipmentItems(array $shipmentData)
187217
$itemQty = isset($shipmentData['items']) ? $shipmentData['items'] : [];
188218
foreach ($itemQty as $itemId => $quantity) {
189219
/** @var ShipmentItemCreationInterface $item */
190-
$item = $this->itemFactory->create();
220+
$item = $this->shipmentItemCreationFactory->create();
191221
$item->setOrderItemId($itemId);
192222
$item->setQty($quantity);
193223
$shipmentItems[] = $item;

app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/ShipmentLoaderTest.php

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,64 +38,90 @@ class ShipmentLoaderTest extends \PHPUnit_Framework_TestCase
3838
*/
3939
protected $shipmentRepositoryMock;
4040

41+
/**
42+
* @var \Magento\Sales\Model\Order\ShipmentFactory|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $shipmentFactory;
45+
46+
/**
47+
* @var \PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
protected $trackFactoryMock;
50+
51+
/**
52+
* @var \PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
protected $orderRepository;
55+
4156
/**
4257
* @var ShipmentDocumentFactory|\PHPUnit_Framework_MockObject_MockObject
4358
*/
44-
protected $documentFactoryMock;
59+
private $documentFactoryMock;
4560

4661
/**
4762
* @var ShipmentTrackCreationInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
4863
*/
49-
protected $trackFactoryMock;
64+
private $shipmentTrackCreationFactoryMock;
5065

5166
/**
5267
* @var ShipmentItemCreationInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
5368
*/
54-
private $itemFactoryMock;
69+
private $shipmentItemCreationFactoryMock;
5570

5671
/**
57-
* @var \PHPUnit_Framework_MockObject_MockObject
72+
* @var array
5873
*/
59-
protected $orderRepositoryMock;
74+
private $data;
6075

6176
/**
6277
* @var \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader
6378
*/
6479
protected $loader;
6580

81+
/**
82+
* @inheritdoc
83+
*/
6684
protected function setUp()
6785
{
6886
$this->objectManagerMock = new ObjectManager($this);
69-
$this->shipmentRepositoryMock = $this->getMockBuilder(\Magento\Sales\Model\Order\ShipmentRepository::class)
87+
$this->shipmentRepositoryMock = $this->getMockBuilder('Magento\Sales\Model\Order\ShipmentRepository')
7088
->disableOriginalConstructor()
7189
->setMethods(['get'])
7290
->getMock();
73-
$this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
91+
$this->registryMock = $this->getMockBuilder('Magento\Framework\Registry')
7492
->disableOriginalConstructor()
7593
->setMethods([])
7694
->getMock();
77-
$this->trackFactoryMock = $this->getMockBuilder(ShipmentTrackCreationInterfaceFactory::class)
95+
$this->shipmentFactory = $this->getMockBuilder('Magento\Sales\Model\Order\ShipmentFactory')
7896
->disableOriginalConstructor()
7997
->setMethods(['create'])
8098
->getMock();
81-
$this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\Manager::class)
99+
$this->trackFactoryMock = $this->getMockBuilder('Magento\Sales\Model\Order\Shipment\TrackFactory')
82100
->disableOriginalConstructor()
83-
->setMethods([])
101+
->setMethods(['create'])
84102
->getMock();
85-
$this->orderRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
103+
$this->messageManagerMock = $this->getMockBuilder('Magento\Framework\Message\Manager')
86104
->disableOriginalConstructor()
87105
->setMethods([])
88106
->getMock();
89-
$this->itemFactoryMock = $this->getMockBuilder(ShipmentItemCreationInterfaceFactory::class)
107+
$this->orderRepository = $this->getMockBuilder('Magento\Sales\Api\OrderRepositoryInterface')
90108
->disableOriginalConstructor()
91-
->setMethods(['create'])
109+
->setMethods([])
92110
->getMock();
93111
$this->documentFactoryMock = $this->getMockBuilder(ShipmentDocumentFactory::class)
94112
->disableOriginalConstructor()
95113
->setMethods([])
96114
->getMock();
115+
$this->shipmentTrackCreationFactoryMock = $this->getMockBuilder(ShipmentTrackCreationInterfaceFactory::class)
116+
->disableOriginalConstructor()
117+
->setMethods(['create'])
118+
->getMock();
119+
$this->shipmentItemCreationFactoryMock = $this->getMockBuilder(ShipmentItemCreationInterfaceFactory::class)
120+
->disableOriginalConstructor()
121+
->setMethods(['create'])
122+
->getMock();
97123

98-
$data = [
124+
$this->data = [
99125
'order_id' => 100032,
100126
'shipment_id' => 1000065,
101127
'shipment' => ['items' => [1 => 1, 2 => 2]],
@@ -111,18 +137,20 @@ protected function setUp()
111137
'messageManager' => $this->messageManagerMock,
112138
'registry' => $this->registryMock,
113139
'shipmentRepository' => $this->shipmentRepositoryMock,
114-
'orderRepository' => $this->orderRepositoryMock,
115-
'documentFactory' => $this->documentFactoryMock,
140+
'shipmentFactory' => $this->shipmentFactory,
116141
'trackFactory' => $this->trackFactoryMock,
117-
'itemFactory' => $this->itemFactoryMock,
118-
'data' => $data
142+
'orderRepository' => $this->orderRepository,
143+
'data' => $this->data,
144+
'documentFactory' => $this->documentFactoryMock,
145+
'shipmentTrackCreationFactory' => $this->shipmentTrackCreationFactoryMock,
146+
'shipmentItemCreationFactory' => $this->shipmentItemCreationFactoryMock,
119147
]
120148
);
121149
}
122150

123151
public function testLoadShipmentId()
124152
{
125-
$shipmentModelMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class)
153+
$shipmentModelMock = $this->getMockBuilder('Magento\Sales\Model\Order\Shipment')
126154
->disableOriginalConstructor()
127155
->setMethods([])
128156
->getMock();
@@ -139,13 +167,14 @@ public function testLoadShipmentId()
139167
public function testLoadOrderId()
140168
{
141169
$this->loader->unsetData('shipment_id');
142-
$orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
170+
$orderMock = $this->getMockBuilder('Magento\Sales\Model\Order')
143171
->disableOriginalConstructor()
144172
->setMethods(['getForcedShipmentWithInvoice', 'getId', 'canShip'])
145173
->getMock();
146-
$this->orderRepositoryMock->expects($this->once())
174+
$this->orderRepository->expects($this->once())
147175
->method('get')
148176
->will($this->returnValue($orderMock));
177+
149178
$orderMock->expects($this->once())
150179
->method('getId')
151180
->will($this->returnValue($this->loader->getOrderId()));
@@ -155,30 +184,43 @@ public function testLoadOrderId()
155184
$orderMock->expects($this->once())
156185
->method('canShip')
157186
->will($this->returnValue(true));
158-
$shipmentModelMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class)
187+
$shipmentModelMock = $this->getMockBuilder('Magento\Sales\Model\Order\Shipment')
159188
->disableOriginalConstructor()
160189
->setMethods([])
161190
->getMock();
162191
$trackMock = $this->getMockBuilder(ShipmentTrackCreationInterface::class)
163192
->disableOriginalConstructor()
164193
->setMethods(['setCarrierCode', 'setTrackNumber', 'setTitle'])
165194
->getMockForAbstractClass();
166-
$this->trackFactoryMock->expects($this->any())
195+
196+
$trackMock->expects($this->exactly(count($this->data['tracking'])))->method('setCarrierCode')->willReturnSelf();
197+
$trackMock->expects($this->exactly(count($this->data['tracking'])))->method('setTrackNumber')->willReturnSelf();
198+
$trackMock->expects($this->exactly(count($this->data['tracking'])))->method('setTitle')->willReturnSelf();
199+
200+
$this->shipmentTrackCreationFactoryMock->expects($this->any())
167201
->method('create')
168-
->will($this->returnValue($trackMock));
202+
->willReturn($trackMock);
169203
$shipmentModelMock->expects($this->any())
170204
->method('addTrack')
171205
->with($this->equalTo($trackMock))
172206
->will($this->returnSelf());
173207
$this->registryMock->expects($this->once())
174208
->method('register')
175209
->with('current_shipment', $shipmentModelMock);
176-
$itemMock = $this->getMockBuilder(ShipmentItemCreationInterface::class)
210+
211+
$shipmentItemMock = $this->getMockBuilder(ShipmentItemCreationInterface::class)
177212
->disableOriginalConstructor()
213+
->setMethods(['setOrderItemId', 'setQty'])
178214
->getMockForAbstractClass();
179-
$this->itemFactoryMock->expects($this->any())
215+
216+
$shipmentItemMock->expects($this->exactly(count($this->data['shipment']['items'])))
217+
->method('setOrderItemId')->willReturnSelf();
218+
$shipmentItemMock->expects($this->exactly(count($this->data['shipment']['items'])))
219+
->method('setQty')->willReturnSelf();
220+
$this->shipmentItemCreationFactoryMock->expects($this->any())
180221
->method('create')
181-
->will($this->returnValue($itemMock));
222+
->will($this->returnValue($shipmentItemMock));
223+
182224
$this->documentFactoryMock->expects($this->once())->method('create')->willReturn($shipmentModelMock);
183225

184226
$this->assertEquals($shipmentModelMock, $this->loader->load());

0 commit comments

Comments
 (0)