Skip to content

Commit 912047e

Browse files
committed
ACP2E-1650:Create Shipment API
- using the bundle product in create shipment API adds all child products - added unit test
1 parent 647b34e commit 912047e

File tree

2 files changed

+138
-3
lines changed

2 files changed

+138
-3
lines changed

app/code/Magento/Sales/Model/Order/Shipment/Item.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Sales\Model\Order\Shipment;
88

99
use Magento\Framework\Api\AttributeValueFactory;
10+
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Sales\Api\Data\ShipmentItemInterface;
1112
use Magento\Sales\Model\AbstractModel;
1213

@@ -136,7 +137,9 @@ public function getOrderItem()
136137
} else {
137138
$this->_orderItem = $this->_orderItemFactory->create()->load($this->getOrderItemId());
138139
}
140+
$this->loadChildren();
139141
}
142+
140143
return $this->_orderItem;
141144
}
142145

@@ -145,7 +148,7 @@ public function getOrderItem()
145148
*
146149
* @param float $qty
147150
* @return \Magento\Sales\Model\Order\Shipment\Item
148-
* @throws \Magento\Framework\Exception\LocalizedException
151+
* @throws LocalizedException
149152
*/
150153
public function setQty($qty)
151154
{
@@ -157,7 +160,7 @@ public function setQty($qty)
157160
* Applying qty to order item
158161
*
159162
* @return $this
160-
* @throws \Magento\Framework\Exception\LocalizedException
163+
* @throws LocalizedException
161164
*/
162165
public function register()
163166
{
@@ -377,6 +380,25 @@ public function setExtensionAttributes(\Magento\Sales\Api\Data\ShipmentItemExten
377380
{
378381
return $this->_setExtensionAttributes($extensionAttributes);
379382
}
380-
381383
//@codeCoverageIgnoreEnd
384+
385+
/**
386+
* @return void
387+
*/
388+
private function loadChildren(): void
389+
{
390+
$collection = $this->_orderItem->getOrder()->getItemsCollection();
391+
$collection->filterByParent($this->_orderItem->getItemId());
392+
393+
if ($collection->count()) {
394+
$this->_orderItem->setData('has_children', true);
395+
396+
/** @var \Magento\Sales\Model\Order\Item $childItem */
397+
foreach ($collection as $childItem) {
398+
if ($childItem->getItemId() != $this->_orderItem->getItemId()) {
399+
$this->_orderItem->addChildItem($childItem);
400+
}
401+
}
402+
}
403+
}
382404
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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\Test\Unit\Model\Order\Shipment;
9+
10+
use Magento\Framework\Api\AttributeValueFactory;
11+
use Magento\Framework\Api\ExtensionAttributesFactory;
12+
use Magento\Framework\Model\Context;
13+
use Magento\Framework\Model\ResourceModel\AbstractResource;
14+
use Magento\Framework\Registry;
15+
use Magento\Sales\Model\Order;
16+
use Magento\Sales\Model\Order\ItemFactory;
17+
use Magento\Sales\Model\Order\Shipment\Item;
18+
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ItemCollection;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class ItemTest extends TestCase
23+
{
24+
/**
25+
* @var Context|Context&MockObject|MockObject
26+
*/
27+
private Context $context;
28+
29+
/**
30+
* @var Registry|Registry&MockObject|MockObject
31+
*/
32+
private Registry $registry;
33+
34+
/**
35+
* @var ExtensionAttributesFactory|ExtensionAttributesFactory&MockObject|MockObject
36+
*/
37+
private ExtensionAttributesFactory $extensionFactory;
38+
39+
/**
40+
* @var AttributeValueFactory|AttributeValueFactory&MockObject|MockObject
41+
*/
42+
private AttributeValueFactory $customAttributeFactory;
43+
44+
/**
45+
* @var ItemFactory|ItemFactory&MockObject|MockObject
46+
*/
47+
private ItemFactory $orderItemFactory;
48+
49+
/**
50+
* @var AbstractResource|AbstractResource&MockObject|MockObject
51+
*/
52+
private AbstractResource $resource;
53+
54+
/**
55+
* @var Item
56+
*/
57+
private Item $model;
58+
59+
/**
60+
* @return void
61+
*/
62+
protected function setUp(): void
63+
{
64+
$this->context = $this->createMock(Context::class);
65+
$this->registry = $this->createMock(Registry::class);
66+
$this->extensionFactory = $this->createMock(ExtensionAttributesFactory::class);
67+
$this->customAttributeFactory = $this->createMock(AttributeValueFactory::class);
68+
$this->orderItemFactory = $this->createMock(ItemFactory::class);
69+
$this->resource = $this->getMockBuilder(AbstractResource::class)
70+
->disableOriginalConstructor()
71+
->addMethods(['getIdFieldName'])
72+
->getMockForAbstractClass();
73+
$this->model = new Item(
74+
$this->context,
75+
$this->registry,
76+
$this->extensionFactory,
77+
$this->customAttributeFactory,
78+
$this->orderItemFactory,
79+
$this->resource
80+
);
81+
}
82+
83+
/**
84+
* @return void
85+
*/
86+
public function testGetOrderItem(): void
87+
{
88+
$childItem = $this->createMock(Order\Item::class);
89+
$childItem->expects($this->once())->method('getItemId')->willReturn(2);
90+
$collection = $this->createMock(ItemCollection::class);
91+
$collection->expects($this->once())->method('count')->willReturn(1);
92+
$collection->expects($this->once())->method('getIterator')
93+
->willReturn(new \ArrayIterator([$childItem]));
94+
95+
$order = $this->createMock(Order::class);
96+
$order->expects($this->once())->method('getItemsCollection')->willReturn($collection);
97+
98+
$shipmentItem = $this->createMock(Order\Item::class);
99+
$shipmentItem->expects($this->once())->method('getOrder')->willReturn($order);
100+
$shipmentItem->expects($this->once())->method('setData')->with('has_children', true);
101+
$shipmentItem->expects($this->once())->method('addChildItem')->with($childItem);
102+
$shipmentItem->expects($this->any())->method('getItemId')->willReturn(1);
103+
104+
$item = $this->createMock(Order\Item::class);
105+
$item->expects($this->once())->method('load')->willReturn($shipmentItem);
106+
107+
$this->orderItemFactory->expects($this->once())
108+
->method('create')
109+
->willReturn($item);
110+
111+
$this->model->getOrderItem();
112+
}
113+
}

0 commit comments

Comments
 (0)