Skip to content

Commit 7240671

Browse files
committed
Merge branch 'MC-38666' of https://github.com/magento-mpi/magento2ce into TANGO-PR-11-05-2020_24
2 parents 9d6e30f + d394304 commit 7240671

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\ConfigurableProduct\Plugin\Model\Order\Invoice;
9+
10+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
11+
use Magento\Sales\Model\Order\Invoice;
12+
13+
/**
14+
* Update total quantity for configurable product invoice
15+
*/
16+
class UpdateConfigurableProductTotalQty
17+
{
18+
/**
19+
* Set total quantity for configurable product invoice
20+
*
21+
* @param Invoice $invoice
22+
* @param float $totalQty
23+
* @return float
24+
*/
25+
public function beforeSetTotalQty(
26+
Invoice $invoice,
27+
float $totalQty
28+
): float {
29+
$order = $invoice->getOrder();
30+
$productTotalQty = 0;
31+
$hasConfigurableProduct = false;
32+
foreach ($order->getAllItems() as $orderItem) {
33+
if ($orderItem->getParentItemId() === null &&
34+
$orderItem->getProductType() == Configurable::TYPE_CODE
35+
) {
36+
$hasConfigurableProduct = true;
37+
continue;
38+
}
39+
$productTotalQty += (float) $orderItem->getQtyOrdered();
40+
}
41+
return $hasConfigurableProduct ? $productTotalQty : $totalQty;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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\ConfigurableProduct\Test\Unit\Plugin\Model\Order\Invoice;
9+
10+
use Magento\Bundle\Model\Product\Type as Bundle;
11+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
12+
use Magento\ConfigurableProduct\Plugin\Model\Order\Invoice\UpdateConfigurableProductTotalQty;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
14+
use Magento\Sales\Model\Order\Invoice;
15+
use Magento\Sales\Model\Order;
16+
use Magento\Sales\Model\Order\Item;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Test for class UpdateConfigurableProductTotalQty.
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
*/
24+
class UpdateConfigurableProductTotalQtyTest extends TestCase
25+
{
26+
/**
27+
* @var UpdateConfigurableProductTotalQty
28+
*/
29+
private $model;
30+
31+
/**
32+
* @var ObjectManagerHelper|null
33+
*/
34+
private $objectManagerHelper;
35+
36+
/**
37+
* @var Invoice|MockObject
38+
*/
39+
private $invoiceMock;
40+
41+
/**
42+
* @var Order|MockObject
43+
*/
44+
private $orderMock;
45+
46+
/**
47+
* @var Item[]|MockObject
48+
*/
49+
private $orderItemsMock;
50+
51+
protected function setUp(): void
52+
{
53+
$this->invoiceMock = $this->createMock(Invoice::class);
54+
$this->orderMock = $this->createMock(Order::class);
55+
$this->orderItemsMock = $this->getMockBuilder(Item::class)
56+
->disableOriginalConstructor()
57+
->getMock();
58+
$this->objectManagerHelper = new ObjectManagerHelper($this);
59+
$this->model = $this->objectManagerHelper->getObject(
60+
UpdateConfigurableProductTotalQty::class,
61+
[]
62+
);
63+
}
64+
65+
/**
66+
* Test Set total quantity for configurable product invoice
67+
*
68+
* @param array $orderItems
69+
* @param float $totalQty
70+
* @param float $productTotalQty
71+
* @dataProvider getOrdersForConfigurableProducts
72+
*/
73+
public function testBeforeSetTotalQty(
74+
array $orderItems,
75+
float $totalQty,
76+
float $productTotalQty
77+
): void {
78+
$this->invoiceMock->expects($this->any())
79+
->method('getOrder')
80+
->willReturn($this->orderMock);
81+
$this->orderMock->expects($this->any())
82+
->method('getAllItems')
83+
->willReturn($orderItems);
84+
$expectedQty= $this->model->beforeSetTotalQty($this->invoiceMock, $totalQty);
85+
$this->assertEquals($expectedQty, $productTotalQty);
86+
}
87+
88+
/**
89+
* DataProvider for beforeSetTotalQty.
90+
*
91+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
92+
* @return array
93+
*/
94+
public function getOrdersForConfigurableProducts(): array
95+
{
96+
97+
return [
98+
'verify productQty for simple products' => [
99+
'orderItems' => $this->getOrderItems(
100+
[
101+
[
102+
'parent_item_id' => null,
103+
'product_type' => 'simple',
104+
'qty_ordered' => 10
105+
]
106+
]
107+
),
108+
'totalQty' => 10.00,
109+
'productTotalQty' => 10.00
110+
],
111+
'verify productQty for configurable products' => [
112+
'orderItems' => $this->getOrderItems(
113+
[
114+
[
115+
'parent_item_id' => '2',
116+
'product_type' => Configurable::TYPE_CODE,
117+
'qty_ordered' => 10
118+
]
119+
]
120+
),
121+
'totalQty' => 10.00,
122+
'productTotalQty' => 10.00
123+
],
124+
'verify productQty for simple configurable products' => [
125+
'orderItems' => $this->getOrderItems(
126+
[
127+
[
128+
'parent_item_id' => null,
129+
'product_type' => 'simple',
130+
'qty_ordered' => 10
131+
],
132+
[
133+
'parent_item_id' => '2',
134+
'product_type' => Configurable::TYPE_CODE,
135+
'qty_ordered' => 10
136+
],
137+
[
138+
'parent_item_id' => '2',
139+
'product_type' => Bundle::TYPE_CODE,
140+
'qty_ordered' => 10
141+
]
142+
]
143+
),
144+
'totalQty' => 30.00,
145+
'productTotalQty' => 30.00
146+
]
147+
];
148+
}
149+
150+
/**
151+
* Get Order Items.
152+
*
153+
* @param array $orderItems
154+
* @return array
155+
*/
156+
public function getOrderItems(array $orderItems): array
157+
{
158+
$orderItemsMock = [];
159+
foreach ($orderItems as $key => $orderItem) {
160+
$orderItemsMock[$key] = $this->getMockBuilder(Item::class)
161+
->disableOriginalConstructor()
162+
->getMock();
163+
$orderItemsMock[$key]->expects($this->any())
164+
->method('getParentItemId')
165+
->willReturn($orderItem['parent_item_id']);
166+
$orderItemsMock[$key]->expects($this->any())
167+
->method('getProductType')
168+
->willReturn($orderItem['product_type']);
169+
$orderItemsMock[$key]->expects($this->any())
170+
->method('getQtyOrdered')
171+
->willReturn($orderItem['qty_ordered']);
172+
}
173+
return $orderItemsMock;
174+
}
175+
176+
protected function tearDown(): void
177+
{
178+
unset($this->invoiceMock);
179+
unset($this->orderMock);
180+
unset($this->orderItemsMock);
181+
}
182+
}

app/code/Magento/ConfigurableProduct/etc/adminhtml/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,7 @@
7878
</argument>
7979
</arguments>
8080
</virtualType>
81+
<type name="Magento\Sales\Model\Order\Invoice">
82+
<plugin name="update_configurable_product_total_qty" type="Magento\ConfigurableProduct\Plugin\Model\Order\Invoice\UpdateConfigurableProductTotalQty"/>
83+
</type>
8184
</config>

0 commit comments

Comments
 (0)