Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RefundOperationTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class RefundOperationTest extends \PHPUnit\Framework\TestCase
14 {
18  private $subject;
19 
23  private $orderMock;
24 
28  private $creditmemoMock;
29 
33  private $paymentMock;
34 
38  private $priceCurrencyMock;
39 
43  private $eventManagerMock;
44 
45  protected function setUp()
46  {
47  $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class)
48  ->disableOriginalConstructor()
49  ->getMockForAbstractClass();
50 
51  $this->creditmemoMock = $this->getMockBuilder(\Magento\Sales\Api\Data\CreditmemoInterface::class)
52  ->disableOriginalConstructor()
53  ->setMethods(['getBaseCost', 'setDoTransaction', 'getPaymentRefundDisallowed'])
54  ->getMockForAbstractClass();
55 
56  $this->paymentMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceCurrencyInterface::class)
57  ->disableOriginalConstructor()
58  ->setMethods(['refund'])
59  ->getMockForAbstractClass();
60 
61  $this->priceCurrencyMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceCurrencyInterface::class)
62  ->disableOriginalConstructor()
63  ->setMethods(['round'])
64  ->getMockForAbstractClass();
65 
66  $contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
67  ->disableOriginalConstructor()
68  ->setMethods(['getEventDispatcher'])
69  ->getMock();
70 
71  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74 
75  $contextMock->expects($this->once())
76  ->method('getEventDispatcher')
77  ->willReturn($this->eventManagerMock);
78 
79  $this->subject = new \Magento\Sales\Model\Order\Creditmemo\RefundOperation(
80  $contextMock,
81  $this->priceCurrencyMock
82  );
83  }
84 
89  public function testExecuteNotRefundedCreditmemo($state)
90  {
91  $this->creditmemoMock->expects($this->once())
92  ->method('getState')
93  ->willReturn($state);
94  $this->orderMock->expects($this->never())
95  ->method('getEntityId');
96  $this->assertEquals(
97  $this->orderMock,
98  $this->subject->execute(
99  $this->creditmemoMock,
100  $this->orderMock
101  )
102  );
103  }
104 
110  {
111  return [
114  ];
115  }
116 
117  public function testExecuteWithWrongOrder()
118  {
119  $creditmemoOrderId = 1;
120  $orderId = 2;
121  $this->creditmemoMock->expects($this->once())
122  ->method('getState')
123  ->willReturn(Creditmemo::STATE_REFUNDED);
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');
132  $this->assertEquals(
133  $this->orderMock,
134  $this->subject->execute($this->creditmemoMock, $this->orderMock)
135  );
136  }
137 
142  public function testExecuteOffline($amounts)
143  {
144  $orderId = 1;
145  $online = false;
146  $this->creditmemoMock->expects($this->once())
147  ->method('getState')
148  ->willReturn(Creditmemo::STATE_REFUNDED);
149  $this->creditmemoMock->expects($this->once())
150  ->method('getOrderId')
151  ->willReturn($orderId);
152  $this->orderMock->expects($this->once())
153  ->method('getEntityId')
154  ->willReturn($orderId);
155 
156  $this->registerItems();
157 
158  $this->priceCurrencyMock->expects($this->any())
159  ->method('round')
160  ->willReturnArgument(0);
161 
162  $this->setBaseAmounts($amounts);
163  $this->orderMock->expects($this->once())
164  ->method('setTotalOfflineRefunded')
165  ->with(2);
166  $this->orderMock->expects($this->once())
167  ->method('getTotalOfflineRefunded')
168  ->willReturn(0);
169  $this->orderMock->expects($this->once())
170  ->method('setBaseTotalOfflineRefunded')
171  ->with(1);
172  $this->orderMock->expects($this->once())
173  ->method('getBaseTotalOfflineRefunded')
174  ->willReturn(0);
175  $this->orderMock->expects($this->never())
176  ->method('setTotalOnlineRefunded');
177 
178  $this->orderMock->expects($this->once())
179  ->method('getPayment')
180  ->willReturn($this->paymentMock);
181 
182  $this->paymentMock->expects($this->once())
183  ->method('refund')
184  ->with($this->creditmemoMock);
185 
186  $this->creditmemoMock->expects($this->once())
187  ->method('setDoTransaction')
188  ->with($online);
189 
190  $this->eventManagerMock->expects($this->once())
191  ->method('dispatch')
192  ->with(
193  'sales_order_creditmemo_refund',
194  ['creditmemo' => $this->creditmemoMock]
195  );
196 
197  $this->assertEquals(
198  $this->orderMock,
199  $this->subject->execute($this->creditmemoMock, $this->orderMock, $online)
200  );
201  }
202 
207  public function testExecuteOnline($amounts)
208  {
209  $orderId = 1;
210  $online = true;
211  $this->creditmemoMock->expects($this->once())
212  ->method('getState')
213  ->willReturn(Creditmemo::STATE_REFUNDED);
214  $this->creditmemoMock->expects($this->once())
215  ->method('getOrderId')
216  ->willReturn($orderId);
217  $this->orderMock->expects($this->once())
218  ->method('getEntityId')
219  ->willReturn($orderId);
220 
221  $this->registerItems();
222 
223  $this->priceCurrencyMock->expects($this->any())
224  ->method('round')
225  ->willReturnArgument(0);
226 
227  $this->setBaseAmounts($amounts);
228  $this->orderMock->expects($this->once())
229  ->method('setTotalOnlineRefunded')
230  ->with(2);
231  $this->orderMock->expects($this->once())
232  ->method('getTotalOnlineRefunded')
233  ->willReturn(0);
234  $this->orderMock->expects($this->once())
235  ->method('setBaseTotalOnlineRefunded')
236  ->with(1);
237  $this->orderMock->expects($this->once())
238  ->method('getBaseTotalOnlineRefunded')
239  ->willReturn(0);
240  $this->orderMock->expects($this->never())
241  ->method('setTotalOfflineRefunded');
242 
243  $this->creditmemoMock->expects($this->once())
244  ->method('setDoTransaction')
245  ->with($online);
246 
247  $this->orderMock->expects($this->once())
248  ->method('getPayment')
249  ->willReturn($this->paymentMock);
250  $this->paymentMock->expects($this->once())
251  ->method('refund')
252  ->with($this->creditmemoMock);
253 
254  $this->assertEquals(
255  $this->orderMock,
256  $this->subject->execute($this->creditmemoMock, $this->orderMock, $online)
257  );
258  }
259 
264  public function baseAmountsDataProvider()
265  {
266  return [
267  [[
268  'setBaseTotalRefunded' => [
269  'result' => 2,
270  'order' => ['method' => 'getBaseTotalRefunded', 'amount' => 1],
271  'creditmemo' => ['method' => 'getBaseGrandTotal', 'amount' => 1],
272  ],
273  'setTotalRefunded' => [
274  'result' => 4,
275  'order' => ['method' => 'getTotalRefunded', 'amount' => 2],
276  'creditmemo' => ['method' => 'getGrandTotal', 'amount' => 2],
277  ],
278  'setBaseSubtotalRefunded' => [
279  'result' => 6,
280  'order' => ['method' => 'getBaseSubtotalRefunded', 'amount' => 3],
281  'creditmemo' => ['method' => 'getBaseSubtotal', 'amount' => 3],
282  ],
283  'setSubtotalRefunded' => [
284  'result' => 6,
285  'order' => ['method' => 'getSubtotalRefunded', 'amount' => 3],
286  'creditmemo' => ['method' => 'getSubtotal', 'amount' => 3],
287  ],
288  'setBaseTaxRefunded' => [
289  'result' => 8,
290  'order' => ['method' => 'getBaseTaxRefunded', 'amount' => 4],
291  'creditmemo' => ['method' => 'getBaseTaxAmount', 'amount' => 4],
292  ],
293  'setTaxRefunded' => [
294  'result' => 10,
295  'order' => ['method' => 'getTaxRefunded', 'amount' => 5],
296  'creditmemo' => ['method' => 'getTaxAmount', 'amount' => 5],
297  ],
298  'setBaseDiscountTaxCompensationRefunded' => [
299  'result' => 12,
300  'order' => ['method' => 'getBaseDiscountTaxCompensationRefunded', 'amount' => 6],
301  'creditmemo' => ['method' => 'getBaseDiscountTaxCompensationAmount', 'amount' => 6],
302  ],
303  'setDiscountTaxCompensationRefunded' => [
304  'result' => 14,
305  'order' => ['method' => 'getDiscountTaxCompensationRefunded', 'amount' => 7],
306  'creditmemo' => ['method' => 'getDiscountTaxCompensationAmount', 'amount' => 7],
307  ],
308  'setBaseShippingRefunded' => [
309  'result' => 16,
310  'order' => ['method' => 'getBaseShippingRefunded', 'amount' => 8],
311  'creditmemo' => ['method' => 'getBaseShippingAmount', 'amount' => 8],
312  ],
313  'setShippingRefunded' => [
314  'result' => 18,
315  'order' => ['method' => 'getShippingRefunded', 'amount' => 9],
316  'creditmemo' => ['method' => 'getShippingAmount', 'amount' => 9],
317  ],
318  'setBaseShippingTaxRefunded' => [
319  'result' => 20,
320  'order' => ['method' => 'getBaseShippingTaxRefunded', 'amount' => 10],
321  'creditmemo' => ['method' => 'getBaseShippingTaxAmount', 'amount' => 10],
322  ],
323  'setShippingTaxRefunded' => [
324  'result' => 22,
325  'order' => ['method' => 'getShippingTaxRefunded', 'amount' => 11],
326  'creditmemo' => ['method' => 'getShippingTaxAmount', 'amount' => 11],
327  ],
328  'setAdjustmentPositive' => [
329  'result' => 24,
330  'order' => ['method' => 'getAdjustmentPositive', 'amount' => 12],
331  'creditmemo' => ['method' => 'getAdjustmentPositive', 'amount' => 12],
332  ],
333  'setBaseAdjustmentPositive' => [
334  'result' => 26,
335  'order' => ['method' => 'getBaseAdjustmentPositive', 'amount' => 13],
336  'creditmemo' => ['method' => 'getBaseAdjustmentPositive', 'amount' => 13],
337  ],
338  'setAdjustmentNegative' => [
339  'result' => 28,
340  'order' => ['method' => 'getAdjustmentNegative', 'amount' => 14],
341  'creditmemo' => ['method' => 'getAdjustmentNegative', 'amount' => 14],
342  ],
343  'setBaseAdjustmentNegative' => [
344  'result' => 30,
345  'order' => ['method' => 'getBaseAdjustmentNegative', 'amount' => 15],
346  'creditmemo' => ['method' => 'getBaseAdjustmentNegative', 'amount' => 15],
347  ],
348  'setDiscountRefunded' => [
349  'result' => 32,
350  'order' => ['method' => 'getDiscountRefunded', 'amount' => 16],
351  'creditmemo' => ['method' => 'getDiscountAmount', 'amount' => 16],
352  ],
353  'setBaseDiscountRefunded' => [
354  'result' => 34,
355  'order' => ['method' => 'getBaseDiscountRefunded', 'amount' => 17],
356  'creditmemo' => ['method' => 'getBaseDiscountAmount', 'amount' => 17],
357  ],
358  'setBaseTotalInvoicedCost' => [
359  'result' => 7,
360  'order' => ['method' => 'getBaseTotalInvoicedCost', 'amount' => 18],
361  'creditmemo' => ['method' => 'getBaseCost', 'amount' => 11],
362  ],
363  ]],
364  ];
365  }
366 
370  private function setBaseAmounts($amounts)
371  {
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']);
382  }
383  }
384 
385  private function registerItems()
386  {
387  $item1 = $this->getCreditmemoItemMock();
388  $item1->expects($this->once())->method('isDeleted')->willReturn(true);
389  $item1->expects($this->never())->method('setCreditMemo');
390 
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');
397 
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');
403 
404  $this->creditmemoMock->expects($this->any())
405  ->method('getItems')
406  ->willReturn([$item1, $item2, $item3]);
407  }
408 
412  private function getCreditmemoItemMock()
413  {
414  return $this->getMockBuilder(\Magento\Sales\Api\Data\CreditmemoItemInterface::class)
415  ->disableOriginalConstructor()
416  ->setMethods(['isDeleted', 'setCreditMemo', 'getQty', 'register'])
417  ->getMockForAbstractClass();
418  }
419 }