|
| 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 | +} |
0 commit comments