28 private $creditmemoMock;
38 private $priceCurrencyMock;
43 private $eventManagerMock;
47 $this->orderMock = $this->getMockBuilder(\
Magento\Sales\Api\
Data\OrderInterface::class)
48 ->disableOriginalConstructor()
49 ->getMockForAbstractClass();
51 $this->creditmemoMock = $this->getMockBuilder(\
Magento\Sales\Api\
Data\CreditmemoInterface::class)
52 ->disableOriginalConstructor()
53 ->setMethods([
'getBaseCost',
'setDoTransaction',
'getPaymentRefundDisallowed'])
54 ->getMockForAbstractClass();
56 $this->paymentMock = $this->getMockBuilder(\
Magento\Framework\Pricing\PriceCurrencyInterface::class)
57 ->disableOriginalConstructor()
58 ->setMethods([
'refund'])
59 ->getMockForAbstractClass();
61 $this->priceCurrencyMock = $this->getMockBuilder(\
Magento\Framework\Pricing\PriceCurrencyInterface::class)
62 ->disableOriginalConstructor()
63 ->setMethods([
'round'])
64 ->getMockForAbstractClass();
66 $contextMock = $this->getMockBuilder(\
Magento\Framework\Model\Context::class)
67 ->disableOriginalConstructor()
68 ->setMethods([
'getEventDispatcher'])
71 $this->eventManagerMock = $this->getMockBuilder(\
Magento\Framework\Event\ManagerInterface::class)
72 ->disableOriginalConstructor()
75 $contextMock->expects($this->once())
76 ->method(
'getEventDispatcher')
77 ->willReturn($this->eventManagerMock);
79 $this->subject = new \Magento\Sales\Model\Order\Creditmemo\RefundOperation(
81 $this->priceCurrencyMock
91 $this->creditmemoMock->expects($this->once())
94 $this->orderMock->expects($this->never())
95 ->method(
'getEntityId');
98 $this->subject->execute(
99 $this->creditmemoMock,
119 $creditmemoOrderId = 1;
121 $this->creditmemoMock->expects($this->once())
124 $this->creditmemoMock->expects($this->once())
125 ->method(
'getOrderId')
126 ->willReturn($creditmemoOrderId);
127 $this->orderMock->expects($this->once())
128 ->method(
'getEntityId')
129 ->willReturn($orderId);
130 $this->orderMock->expects($this->never())
131 ->method(
'setTotalRefunded');
134 $this->subject->execute($this->creditmemoMock, $this->orderMock)
146 $this->creditmemoMock->expects($this->once())
149 $this->creditmemoMock->expects($this->once())
150 ->method(
'getOrderId')
151 ->willReturn($orderId);
152 $this->orderMock->expects($this->once())
153 ->method(
'getEntityId')
154 ->willReturn($orderId);
156 $this->registerItems();
158 $this->priceCurrencyMock->expects($this->any())
160 ->willReturnArgument(0);
162 $this->setBaseAmounts($amounts);
163 $this->orderMock->expects($this->once())
164 ->method(
'setTotalOfflineRefunded')
166 $this->orderMock->expects($this->once())
167 ->method(
'getTotalOfflineRefunded')
169 $this->orderMock->expects($this->once())
170 ->method(
'setBaseTotalOfflineRefunded')
172 $this->orderMock->expects($this->once())
173 ->method(
'getBaseTotalOfflineRefunded')
175 $this->orderMock->expects($this->never())
176 ->method(
'setTotalOnlineRefunded');
178 $this->orderMock->expects($this->once())
179 ->method(
'getPayment')
180 ->willReturn($this->paymentMock);
182 $this->paymentMock->expects($this->once())
184 ->with($this->creditmemoMock);
186 $this->creditmemoMock->expects($this->once())
187 ->method(
'setDoTransaction')
190 $this->eventManagerMock->expects($this->once())
193 'sales_order_creditmemo_refund',
194 [
'creditmemo' => $this->creditmemoMock]
199 $this->subject->execute($this->creditmemoMock, $this->orderMock, $online)
211 $this->creditmemoMock->expects($this->once())
214 $this->creditmemoMock->expects($this->once())
215 ->method(
'getOrderId')
216 ->willReturn($orderId);
217 $this->orderMock->expects($this->once())
218 ->method(
'getEntityId')
219 ->willReturn($orderId);
221 $this->registerItems();
223 $this->priceCurrencyMock->expects($this->any())
225 ->willReturnArgument(0);
227 $this->setBaseAmounts($amounts);
228 $this->orderMock->expects($this->once())
229 ->method(
'setTotalOnlineRefunded')
231 $this->orderMock->expects($this->once())
232 ->method(
'getTotalOnlineRefunded')
234 $this->orderMock->expects($this->once())
235 ->method(
'setBaseTotalOnlineRefunded')
237 $this->orderMock->expects($this->once())
238 ->method(
'getBaseTotalOnlineRefunded')
240 $this->orderMock->expects($this->never())
241 ->method(
'setTotalOfflineRefunded');
243 $this->creditmemoMock->expects($this->once())
244 ->method(
'setDoTransaction')
247 $this->orderMock->expects($this->once())
248 ->method(
'getPayment')
249 ->willReturn($this->paymentMock);
250 $this->paymentMock->expects($this->once())
252 ->with($this->creditmemoMock);
256 $this->subject->execute($this->creditmemoMock, $this->orderMock, $online)
268 'setBaseTotalRefunded' => [
270 'order' => [
'method' =>
'getBaseTotalRefunded',
'amount' => 1],
271 'creditmemo' => [
'method' =>
'getBaseGrandTotal',
'amount' => 1],
273 'setTotalRefunded' => [
275 'order' => [
'method' =>
'getTotalRefunded',
'amount' => 2],
276 'creditmemo' => [
'method' =>
'getGrandTotal',
'amount' => 2],
278 'setBaseSubtotalRefunded' => [
280 'order' => [
'method' =>
'getBaseSubtotalRefunded',
'amount' => 3],
281 'creditmemo' => [
'method' =>
'getBaseSubtotal',
'amount' => 3],
283 'setSubtotalRefunded' => [
285 'order' => [
'method' =>
'getSubtotalRefunded',
'amount' => 3],
286 'creditmemo' => [
'method' =>
'getSubtotal',
'amount' => 3],
288 'setBaseTaxRefunded' => [
290 'order' => [
'method' =>
'getBaseTaxRefunded',
'amount' => 4],
291 'creditmemo' => [
'method' =>
'getBaseTaxAmount',
'amount' => 4],
293 'setTaxRefunded' => [
295 'order' => [
'method' =>
'getTaxRefunded',
'amount' => 5],
296 'creditmemo' => [
'method' =>
'getTaxAmount',
'amount' => 5],
298 'setBaseDiscountTaxCompensationRefunded' => [
300 'order' => [
'method' =>
'getBaseDiscountTaxCompensationRefunded',
'amount' => 6],
301 'creditmemo' => [
'method' =>
'getBaseDiscountTaxCompensationAmount',
'amount' => 6],
303 'setDiscountTaxCompensationRefunded' => [
305 'order' => [
'method' =>
'getDiscountTaxCompensationRefunded',
'amount' => 7],
306 'creditmemo' => [
'method' =>
'getDiscountTaxCompensationAmount',
'amount' => 7],
308 'setBaseShippingRefunded' => [
310 'order' => [
'method' =>
'getBaseShippingRefunded',
'amount' => 8],
311 'creditmemo' => [
'method' =>
'getBaseShippingAmount',
'amount' => 8],
313 'setShippingRefunded' => [
315 'order' => [
'method' =>
'getShippingRefunded',
'amount' => 9],
316 'creditmemo' => [
'method' =>
'getShippingAmount',
'amount' => 9],
318 'setBaseShippingTaxRefunded' => [
320 'order' => [
'method' =>
'getBaseShippingTaxRefunded',
'amount' => 10],
321 'creditmemo' => [
'method' =>
'getBaseShippingTaxAmount',
'amount' => 10],
323 'setShippingTaxRefunded' => [
325 'order' => [
'method' =>
'getShippingTaxRefunded',
'amount' => 11],
326 'creditmemo' => [
'method' =>
'getShippingTaxAmount',
'amount' => 11],
328 'setAdjustmentPositive' => [
330 'order' => [
'method' =>
'getAdjustmentPositive',
'amount' => 12],
331 'creditmemo' => [
'method' =>
'getAdjustmentPositive',
'amount' => 12],
333 'setBaseAdjustmentPositive' => [
335 'order' => [
'method' =>
'getBaseAdjustmentPositive',
'amount' => 13],
336 'creditmemo' => [
'method' =>
'getBaseAdjustmentPositive',
'amount' => 13],
338 'setAdjustmentNegative' => [
340 'order' => [
'method' =>
'getAdjustmentNegative',
'amount' => 14],
341 'creditmemo' => [
'method' =>
'getAdjustmentNegative',
'amount' => 14],
343 'setBaseAdjustmentNegative' => [
345 'order' => [
'method' =>
'getBaseAdjustmentNegative',
'amount' => 15],
346 'creditmemo' => [
'method' =>
'getBaseAdjustmentNegative',
'amount' => 15],
348 'setDiscountRefunded' => [
350 'order' => [
'method' =>
'getDiscountRefunded',
'amount' => 16],
351 'creditmemo' => [
'method' =>
'getDiscountAmount',
'amount' => 16],
353 'setBaseDiscountRefunded' => [
355 'order' => [
'method' =>
'getBaseDiscountRefunded',
'amount' => 17],
356 'creditmemo' => [
'method' =>
'getBaseDiscountAmount',
'amount' => 17],
358 'setBaseTotalInvoicedCost' => [
360 'order' => [
'method' =>
'getBaseTotalInvoicedCost',
'amount' => 18],
361 'creditmemo' => [
'method' =>
'getBaseCost',
'amount' => 11],
370 private function setBaseAmounts($amounts)
372 foreach ($amounts as $amountName => $summands) {
373 $this->orderMock->expects($this->once())
374 ->method($amountName)
375 ->with($summands[
'result']);
376 $this->orderMock->expects($this->once())
377 ->method($summands[
'order'][
'method'])
378 ->willReturn($summands[
'order'][
'amount']);
379 $this->creditmemoMock->expects($this->any())
380 ->method($summands[
'creditmemo'][
'method'])
381 ->willReturn($summands[
'creditmemo'][
'amount']);
385 private function registerItems()
387 $item1 = $this->getCreditmemoItemMock();
388 $item1->expects($this->once())->method(
'isDeleted')->willReturn(
true);
389 $item1->expects($this->never())->method(
'setCreditMemo');
391 $item2 = $this->getCreditmemoItemMock();
392 $item2->expects($this->at(0))->method(
'isDeleted')->willReturn(
false);
393 $item2->expects($this->once())->method(
'setCreditMemo')->with($this->creditmemoMock);
394 $item2->expects($this->once())->method(
'getQty')->willReturn(0);
395 $item2->expects($this->at(3))->method(
'isDeleted')->with(
true);
396 $item2->expects($this->never())->method(
'register');
398 $item3 = $this->getCreditmemoItemMock();
399 $item3->expects($this->once())->method(
'isDeleted')->willReturn(
false);
400 $item3->expects($this->once())->method(
'setCreditMemo')->with($this->creditmemoMock);
401 $item3->expects($this->once())->method(
'getQty')->willReturn(1);
402 $item3->expects($this->once())->method(
'register');
404 $this->creditmemoMock->expects($this->any())
406 ->willReturn([$item1, $item2, $item3]);
412 private function getCreditmemoItemMock()
414 return $this->getMockBuilder(\
Magento\Sales\Api\Data\CreditmemoItemInterface::class)
415 ->disableOriginalConstructor()
416 ->setMethods([
'isDeleted',
'setCreditMemo',
'getQty',
'register'])
417 ->getMockForAbstractClass();
executeNotRefundedCreditmemoDataProvider()
testExecuteWithWrongOrder()
testExecuteOffline($amounts)
baseAmountsDataProvider()
testExecuteNotRefundedCreditmemo($state)
testExecuteOnline($amounts)