|
3 | 3 | * Copyright © 2016 Magento. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
6 |
| -namespace Magento\CatalogInventory\Test\Unit\Observer; |
| 6 | +namespace Magento\SalesInventory\Test\Unit\Observer; |
7 | 7 |
|
8 |
| -use Magento\CatalogInventory\Observer\RefundOrderInventoryObserver; |
| 8 | +use Magento\Sales\Api\Data\OrderInterface; |
| 9 | +use Magento\Sales\Model\OrderRepository; |
| 10 | +use Magento\SalesInventory\Model\Order\ReturnProcessor; |
| 11 | +use Magento\SalesInventory\Observer\RefundOrderInventoryObserver; |
9 | 12 |
|
10 | 13 | /**
|
11 | 14 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
@@ -47,6 +50,26 @@ class RefundOrderInventoryObserverTest extends \PHPUnit_Framework_TestCase
|
47 | 50 | */
|
48 | 51 | protected $eventObserver;
|
49 | 52 |
|
| 53 | + /** |
| 54 | + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager |
| 55 | + */ |
| 56 | + protected $objectManagerHelper; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var OrderRepository|\PHPUnit_Framework_MockObject_MockObject |
| 60 | + */ |
| 61 | + protected $orderRepositoryMock; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var ReturnProcessor|\PHPUnit_Framework_MockObject_MockObject |
| 65 | + */ |
| 66 | + protected $returnProcessorMock; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject |
| 70 | + */ |
| 71 | + protected $orderMock; |
| 72 | + |
50 | 73 | protected function setUp()
|
51 | 74 | {
|
52 | 75 | $this->stockIndexerProcessor = $this->getMock(
|
@@ -93,92 +116,82 @@ protected function setUp()
|
93 | 116 | ->method('getEvent')
|
94 | 117 | ->will($this->returnValue($this->event));
|
95 | 118 |
|
96 |
| - $this->observer = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( |
97 |
| - \Magento\CatalogInventory\Observer\RefundOrderInventoryObserver::class, |
| 119 | + $this->orderRepositoryMock = $this->getMockBuilder(OrderRepository::class) |
| 120 | + ->disableOriginalConstructor() |
| 121 | + ->getMock(); |
| 122 | + |
| 123 | + $this->returnProcessorMock = $this->getMockBuilder(ReturnProcessor::class) |
| 124 | + ->disableOriginalConstructor() |
| 125 | + ->getMock(); |
| 126 | + |
| 127 | + $this->orderMock = $this->getMockBuilder(OrderInterface::class) |
| 128 | + ->disableOriginalConstructor() |
| 129 | + ->getMock(); |
| 130 | + |
| 131 | + $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 132 | + |
| 133 | + $this->observer = $this->objectManagerHelper->getObject( |
| 134 | + \Magento\SalesInventory\Observer\RefundOrderInventoryObserver::class, |
98 | 135 | [
|
99 | 136 | 'stockConfiguration' => $this->stockConfiguration,
|
100 | 137 | 'stockManagement' => $this->stockManagement,
|
101 | 138 | 'stockIndexerProcessor' => $this->stockIndexerProcessor,
|
102 | 139 | 'priceIndexer' => $this->priceIndexer,
|
103 | 140 | ]
|
104 | 141 | );
|
| 142 | + |
| 143 | + $this->objectManagerHelper->setBackwardCompatibleProperty($this->observer, 'orderRepository', $this->orderRepositoryMock); |
| 144 | + $this->objectManagerHelper->setBackwardCompatibleProperty($this->observer, 'returnProcessor', $this->returnProcessorMock); |
105 | 145 | }
|
106 | 146 |
|
107 | 147 | public function testRefundOrderInventory()
|
108 | 148 | {
|
109 |
| - $websiteId = 0; |
110 | 149 | $ids = ['1', '14'];
|
111 | 150 | $items = [];
|
112 | 151 | $isAutoReturnEnabled = true;
|
113 | 152 |
|
114 |
| - $store = $this->getMock( |
115 |
| - \Magento\Store\Model\Store::class, |
116 |
| - ['getWebsiteId'], |
117 |
| - [], |
118 |
| - '', |
119 |
| - false |
120 |
| - ); |
121 |
| - $store->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); |
| 153 | + $creditMemo = $this->getMock(\Magento\Sales\Model\Order\Creditmemo::class, [], [], '', false); |
122 | 154 |
|
123 |
| - $itemsToUpdate = []; |
124 | 155 | foreach ($ids as $id) {
|
125 | 156 | $item = $this->getCreditMemoItem($id);
|
126 | 157 | $items[] = $item;
|
127 |
| - $itemsToUpdate[$item->getProductId()] = $item->getQty(); |
128 | 158 | }
|
129 |
| - $creditMemo = $this->getMock(\Magento\Sales\Model\Order\Creditmemo::class, [], [], '', false); |
| 159 | + |
130 | 160 | $creditMemo->expects($this->once())
|
131 |
| - ->method('getAllItems') |
| 161 | + ->method('getItems') |
132 | 162 | ->will($this->returnValue($items));
|
133 |
| - $creditMemo->expects($this->once())->method('getStore')->will($this->returnValue($store)); |
134 | 163 |
|
135 | 164 | $this->stockConfiguration->expects($this->any())
|
136 | 165 | ->method('isAutoReturnEnabled')
|
137 | 166 | ->will($this->returnValue($isAutoReturnEnabled));
|
138 | 167 |
|
139 |
| - $this->stockManagement->expects($this->once()) |
140 |
| - ->method('revertProductsSale') |
141 |
| - ->with($itemsToUpdate, $websiteId); |
142 |
| - |
143 |
| - $this->stockIndexerProcessor->expects($this->once()) |
144 |
| - ->method('reindexList') |
145 |
| - ->with($ids); |
146 |
| - |
147 |
| - $this->priceIndexer->expects($this->once()) |
148 |
| - ->method('reindexList') |
149 |
| - ->with($ids); |
150 |
| - |
151 | 168 | $this->event->expects($this->once())
|
152 | 169 | ->method('getCreditmemo')
|
153 | 170 | ->will($this->returnValue($creditMemo));
|
154 | 171 |
|
| 172 | + $this->orderRepositoryMock->expects($this->once()) |
| 173 | + ->method('get') |
| 174 | + ->willReturn($this->orderMock); |
| 175 | + |
| 176 | + $this->returnProcessorMock->expects($this->once()) |
| 177 | + ->method('execute') |
| 178 | + ->with($creditMemo, $this->orderMock, $ids, $isAutoReturnEnabled); |
| 179 | + |
155 | 180 | $this->observer->execute($this->eventObserver);
|
156 | 181 | }
|
157 | 182 |
|
158 | 183 | private function getCreditMemoItem($productId)
|
159 | 184 | {
|
160 |
| - $parentItemId = false; |
161 | 185 | $backToStock = true;
|
162 |
| - $qty = 1; |
163 | 186 | $item = $this->getMock(
|
164 | 187 | \Magento\Sales\Model\Order\Creditmemo\Item::class,
|
165 |
| - ['getProductId', 'getOrderItem', 'getBackToStock', 'getQty', '__wakeup'], |
166 |
| - [], |
167 |
| - '', |
168 |
| - false |
169 |
| - ); |
170 |
| - $orderItem = $this->getMock( |
171 |
| - \Magento\Sales\Model\Order\Item::class, |
172 |
| - ['getParentItemId', '__wakeup'], |
| 188 | + ['getOrderItemId', 'getBackToStock', 'getQty', '__wakeup'], |
173 | 189 | [],
|
174 | 190 | '',
|
175 | 191 | false
|
176 | 192 | );
|
177 |
| - $orderItem->expects($this->any())->method('getParentItemId')->willReturn($parentItemId); |
178 |
| - $item->expects($this->any())->method('getOrderItem')->willReturn($orderItem); |
179 |
| - $item->expects($this->any())->method('getProductId')->will($this->returnValue($productId)); |
180 | 193 | $item->expects($this->any())->method('getBackToStock')->willReturn($backToStock);
|
181 |
| - $item->expects($this->any())->method('getQty')->willReturn($qty); |
| 194 | + $item->expects($this->any())->method('getOrderItemId')->willReturn($productId); |
182 | 195 | return $item;
|
183 | 196 | }
|
184 | 197 | }
|
0 commit comments