|
| 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\Bundle\Test\Unit\Model\Sales\Order; |
| 9 | + |
| 10 | +use Magento\Bundle\Model\Sales\Order\BundleOrderTypeValidator; |
| 11 | +use Magento\Framework\Phrase; |
| 12 | +use Magento\Sales\Api\Data\OrderItemInterface; |
| 13 | +use Magento\Sales\Model\Order\Shipment; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Magento\Catalog\Model\Product\Type; |
| 17 | + |
| 18 | +class BundleOrderTypeValidatorTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @return void |
| 22 | + */ |
| 23 | + public function testIsValidSuccess(): void |
| 24 | + { |
| 25 | + $bundleOrderItem = $this->getBundleOrderItemMock(); |
| 26 | + $bundleOrderItem->expects($this->exactly(2))->method('getItemId')->willReturn(1); |
| 27 | + $bundleOrderItem->expects($this->once())->method('isDummy')->with(true)->willReturn(true); |
| 28 | + $bundleOrderItem->expects($this->once())->method('getHasChildren')->willReturn(false); |
| 29 | + $bundleOrderItem->expects($this->any())->method('getProductType')->willReturn(Type::TYPE_BUNDLE); |
| 30 | + $bundleOrderItem->expects($this->any())->method('getSku')->willReturn('bundleSKU'); |
| 31 | + |
| 32 | + $simpleProductOrderItem = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class) |
| 33 | + ->disableOriginalConstructor() |
| 34 | + ->addMethods(['getHasChildren']) |
| 35 | + ->onlyMethods(['getItemId', 'isDummy', 'getProductType']) |
| 36 | + ->getMock(); |
| 37 | + $simpleProductOrderItem->expects($this->exactly(2))->method('getItemId')->willReturn(2); |
| 38 | + $simpleProductOrderItem->expects($this->once())->method('isDummy')->with(true)->willReturn(true); |
| 39 | + $simpleProductOrderItem->expects($this->once())->method('getHasChildren')->willReturn(false); |
| 40 | + $simpleProductOrderItem->expects($this->any())->method('getProductType')->willReturn(Type::TYPE_SIMPLE); |
| 41 | + |
| 42 | + $order = $this->createMock(\Magento\Sales\Model\Order::class); |
| 43 | + $order->expects($this->once()) |
| 44 | + ->method('getAllItems') |
| 45 | + ->willReturn([$bundleOrderItem, $simpleProductOrderItem]); |
| 46 | + |
| 47 | + $bundleShipmentItem = $this->createMock(\Magento\Sales\Api\Data\ShipmentItemInterface::class); |
| 48 | + $bundleShipmentItem->expects($this->exactly(2))->method('getOrderItemId')->willReturn(1); |
| 49 | + $simpleProductShipmentItem = $this->createMock(\Magento\Sales\Api\Data\ShipmentItemInterface::class); |
| 50 | + $simpleProductShipmentItem->expects($this->exactly(2))->method('getOrderItemId')->willReturn(2); |
| 51 | + |
| 52 | + $shipment = $this->createMock(Shipment::class); |
| 53 | + $shipment->expects($this->exactly(2)) |
| 54 | + ->method('getItems') |
| 55 | + ->willReturn([$bundleShipmentItem, $simpleProductShipmentItem]); |
| 56 | + $shipment->expects($this->once())->method('getOrder')->willReturn($order); |
| 57 | + |
| 58 | + try { |
| 59 | + $validator = new BundleOrderTypeValidator(); |
| 60 | + $validator->isValid($shipment); |
| 61 | + $this->assertEmpty($validator->getMessages()); |
| 62 | + } catch (\Exception $e) { |
| 63 | + $this->fail('Could not perform shipment validation. ' . $e->getMessage()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + public function testIsValidFailSeparateShipmentType(): void |
| 71 | + { |
| 72 | + $bundleOrderItem = $this->getBundleOrderItemMock(); |
| 73 | + $bundleOrderItem->expects($this->once())->method('getItemId')->willReturn(1); |
| 74 | + $bundleOrderItem->expects($this->once())->method('isDummy')->with(true)->willReturn(true); |
| 75 | + $bundleOrderItem->expects($this->once())->method('getHasChildren')->willReturn(true); |
| 76 | + $bundleOrderItem->expects($this->any())->method('getProductType')->willReturn(Type::TYPE_BUNDLE); |
| 77 | + $bundleOrderItem->expects($this->any())->method('getSku')->willReturn('bundleSKU'); |
| 78 | + |
| 79 | + $order = $this->createMock(\Magento\Sales\Model\Order::class); |
| 80 | + $order->expects($this->once()) |
| 81 | + ->method('getAllItems') |
| 82 | + ->willReturn([$bundleOrderItem]); |
| 83 | + |
| 84 | + $bundleShipmentItem = $this->createMock(\Magento\Sales\Api\Data\ShipmentItemInterface::class); |
| 85 | + $bundleShipmentItem->expects($this->once())->method('getOrderItemId')->willReturn(1); |
| 86 | + |
| 87 | + $shipment = $this->createMock(Shipment::class); |
| 88 | + $shipment->expects($this->once()) |
| 89 | + ->method('getItems') |
| 90 | + ->willReturn([$bundleShipmentItem]); |
| 91 | + $shipment->expects($this->once())->method('getOrder')->willReturn($order); |
| 92 | + |
| 93 | + try { |
| 94 | + $validator = new BundleOrderTypeValidator(); |
| 95 | + $validator->isValid($shipment); |
| 96 | + $messages = $validator->getMessages(); |
| 97 | + $this->assertNotEmpty($messages); |
| 98 | + /** @var Phrase $validationMsg */ |
| 99 | + $validationMsg = current($messages)[0]; |
| 100 | + foreach ($validationMsg->getArguments() as $argument) { |
| 101 | + if (is_string($argument)) { |
| 102 | + $this->assertSame($argument, 'bundleSKU'); |
| 103 | + } else { |
| 104 | + $this->assertTrue(in_array($argument->getText(), ['Separately', 'Bundle product options'])); |
| 105 | + } |
| 106 | + } |
| 107 | + } catch (\Exception $e) { |
| 108 | + $this->fail('Could not perform shipment validation. ' . $e->getMessage()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + public function testIsValidFailTogetherShipmentType(): void |
| 116 | + { |
| 117 | + $parentItem = $this->createMock(OrderItemInterface::class); |
| 118 | + $parentItem->expects($this->once())->method('getProductType')->willReturn(Type::TYPE_BUNDLE); |
| 119 | + $parentItem->expects($this->any())->method('getSku')->willReturn('bundleSKU'); |
| 120 | + |
| 121 | + $bundleOrderItem = $this->getBundleOrderItemMock(); |
| 122 | + $bundleOrderItem->expects($this->once())->method('getItemId')->willReturn(1); |
| 123 | + $bundleOrderItem->expects($this->once())->method('isDummy')->with(true)->willReturn(true); |
| 124 | + $bundleOrderItem->expects($this->once())->method('getHasChildren')->willReturn(false); |
| 125 | + $bundleOrderItem->expects($this->any())->method('getProductType')->willReturn(Type::TYPE_BUNDLE); |
| 126 | + $bundleOrderItem->expects($this->exactly(3))->method('getParentItem')->willReturn($parentItem); |
| 127 | + |
| 128 | + $order = $this->createMock(\Magento\Sales\Model\Order::class); |
| 129 | + $order->expects($this->once()) |
| 130 | + ->method('getAllItems') |
| 131 | + ->willReturn([$bundleOrderItem]); |
| 132 | + |
| 133 | + $bundleShipmentItem = $this->createMock(\Magento\Sales\Api\Data\ShipmentItemInterface::class); |
| 134 | + $bundleShipmentItem->expects($this->once())->method('getOrderItemId')->willReturn(1); |
| 135 | + |
| 136 | + $shipment = $this->createMock(Shipment::class); |
| 137 | + $shipment->expects($this->once()) |
| 138 | + ->method('getItems') |
| 139 | + ->willReturn([$bundleShipmentItem]); |
| 140 | + $shipment->expects($this->once())->method('getOrder')->willReturn($order); |
| 141 | + |
| 142 | + try { |
| 143 | + $validator = new BundleOrderTypeValidator(); |
| 144 | + $validator->isValid($shipment); |
| 145 | + $messages = $validator->getMessages(); |
| 146 | + $this->assertNotEmpty($messages); |
| 147 | + /** @var Phrase $validationMsg */ |
| 148 | + $validationMsg = current($messages)[0]; |
| 149 | + foreach ($validationMsg->getArguments() as $argument) { |
| 150 | + if (is_string($argument)) { |
| 151 | + $this->assertSame($argument, 'bundleSKU'); |
| 152 | + } else { |
| 153 | + $this->assertTrue(in_array($argument->getText(), ['Together', 'Bundle product itself'])); |
| 154 | + } |
| 155 | + } |
| 156 | + } catch (\Exception $e) { |
| 157 | + $this->fail('Could not perform shipment validation. ' . $e->getMessage()); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @return MockObject |
| 163 | + */ |
| 164 | + private function getBundleOrderItemMock(): MockObject |
| 165 | + { |
| 166 | + return $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class) |
| 167 | + ->disableOriginalConstructor() |
| 168 | + ->addMethods(['getHasChildren']) |
| 169 | + ->onlyMethods(['getItemId', 'isDummy', 'getProductType', 'getSku', 'getParentItem']) |
| 170 | + ->getMock(); |
| 171 | + } |
| 172 | +} |
0 commit comments