Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RefundInvoiceTest.php
Go to the documentation of this file.
1 <?php
7 
20 use Magento\Sales\Model\Order\Config as OrderConfig;
28 use Psr\Log\LoggerInterface;
29 
35 class RefundInvoiceTest extends \PHPUnit\Framework\TestCase
36 {
40  private $resourceConnectionMock;
41 
45  private $orderRepositoryMock;
46 
50  private $invoiceRepositoryMock;
51 
55  private $creditmemoDocumentFactoryMock;
56 
60  private $refundAdapterMock;
61 
65  private $orderStateResolverMock;
66 
70  private $configMock;
71 
75  private $creditmemoRepositoryMock;
76 
80  private $notifierMock;
81 
85  private $refundInvoice;
86 
90  private $creditmemoCommentCreationMock;
91 
95  private $creditmemoCreationArgumentsMock;
96 
100  private $orderMock;
101 
105  private $invoiceMock;
106 
110  private $creditmemoMock;
111 
115  private $adapterInterface;
116 
120  private $creditmemoItemCreationMock;
121 
125  private $refundInvoiceValidatorMock;
126 
130  private $validationMessagesMock;
131 
135  private $loggerMock;
136 
137  protected function setUp()
138  {
139  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
140  ->disableOriginalConstructor()
141  ->getMock();
142  $this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
143  ->disableOriginalConstructor()
144  ->getMock();
145  $this->invoiceRepositoryMock = $this->getMockBuilder(InvoiceRepositoryInterface::class)
146  ->disableOriginalConstructor()
147  ->getMock();
148  $this->creditmemoDocumentFactoryMock = $this->getMockBuilder(CreditmemoDocumentFactory::class)
149  ->disableOriginalConstructor()
150  ->getMock();
151  $this->refundAdapterMock = $this->getMockBuilder(RefundAdapterInterface::class)
152  ->disableOriginalConstructor()
153  ->getMock();
154  $this->refundInvoiceValidatorMock = $this->getMockBuilder(RefundInvoiceInterface::class)
155  ->disableOriginalConstructor()
156  ->getMock();
157  $this->orderStateResolverMock = $this->getMockBuilder(OrderStateResolverInterface::class)
158  ->disableOriginalConstructor()
159  ->getMockForAbstractClass();
160  $this->configMock = $this->getMockBuilder(OrderConfig::class)
161  ->disableOriginalConstructor()
162  ->getMock();
163 
164  $this->creditmemoRepositoryMock = $this->getMockBuilder(CreditmemoRepositoryInterface::class)
165  ->disableOriginalConstructor()
166  ->getMockForAbstractClass();
167 
168  $this->notifierMock = $this->getMockBuilder(NotifierInterface::class)
169  ->disableOriginalConstructor()
170  ->getMockForAbstractClass();
171 
172  $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
173  ->disableOriginalConstructor()
174  ->getMockForAbstractClass();
175 
176  $this->creditmemoCommentCreationMock = $this->getMockBuilder(CreditmemoCommentCreationInterface::class)
177  ->disableOriginalConstructor()
178  ->getMockForAbstractClass();
179 
180  $this->creditmemoCreationArgumentsMock = $this->getMockBuilder(CreditmemoCreationArgumentsInterface::class)
181  ->disableOriginalConstructor()
182  ->getMockForAbstractClass();
183 
184  $this->orderMock = $this->getMockBuilder(OrderInterface::class)
185  ->disableOriginalConstructor()
186  ->getMockForAbstractClass();
187 
188  $this->invoiceMock = $this->getMockBuilder(InvoiceInterface::class)
189  ->disableOriginalConstructor()
190  ->getMockForAbstractClass();
191 
192  $this->creditmemoMock = $this->getMockBuilder(CreditmemoInterface::class)
193  ->disableOriginalConstructor()
194  ->getMockForAbstractClass();
195 
196  $this->adapterInterface = $this->getMockBuilder(AdapterInterface::class)
197  ->disableOriginalConstructor()
198  ->getMockForAbstractClass();
199 
200  $this->creditmemoItemCreationMock = $this->getMockBuilder(CreditmemoItemCreationInterface::class)
201  ->disableOriginalConstructor()
202  ->getMockForAbstractClass();
203  $this->validationMessagesMock = $this->getMockBuilder(ValidatorResultInterface::class)
204  ->disableOriginalConstructor()
205  ->setMethods(['hasMessages', 'getMessages', 'addMessage'])
206  ->getMock();
207 
208  $this->refundInvoice = new RefundInvoice(
209  $this->resourceConnectionMock,
210  $this->orderStateResolverMock,
211  $this->orderRepositoryMock,
212  $this->invoiceRepositoryMock,
213  $this->refundInvoiceValidatorMock,
214  $this->creditmemoRepositoryMock,
215  $this->refundAdapterMock,
216  $this->creditmemoDocumentFactoryMock,
217  $this->notifierMock,
218  $this->configMock,
219  $this->loggerMock
220  );
221  }
222 
233  public function testOrderCreditmemo($invoiceId, $isOnline, $items, $notify, $appendComment)
234  {
235  $this->resourceConnectionMock->expects($this->once())
236  ->method('getConnection')
237  ->with('sales')
238  ->willReturn($this->adapterInterface);
239  $this->invoiceRepositoryMock->expects($this->once())
240  ->method('get')
241  ->willReturn($this->invoiceMock);
242  $this->orderRepositoryMock->expects($this->once())
243  ->method('get')
244  ->willReturn($this->orderMock);
245  $this->creditmemoDocumentFactoryMock->expects($this->once())
246  ->method('createFromInvoice')
247  ->with(
248  $this->invoiceMock,
249  $items,
250  $this->creditmemoCommentCreationMock,
251  ($appendComment && $notify),
252  $this->creditmemoCreationArgumentsMock
253  )->willReturn($this->creditmemoMock);
254  $this->refundInvoiceValidatorMock->expects($this->once())
255  ->method('validate')
256  ->with(
257  $this->invoiceMock,
258  $this->orderMock,
259  $this->creditmemoMock,
260  $items,
261  $isOnline,
262  $notify,
263  $appendComment,
264  $this->creditmemoCommentCreationMock,
265  $this->creditmemoCreationArgumentsMock
266  )
267  ->willReturn($this->validationMessagesMock);
268  $hasMessages = false;
269  $this->validationMessagesMock->expects($this->once())
270  ->method('hasMessages')->willReturn($hasMessages);
271  $this->refundAdapterMock->expects($this->once())
272  ->method('refund')
273  ->with($this->creditmemoMock, $this->orderMock)
274  ->willReturn($this->orderMock);
275  $this->orderStateResolverMock->expects($this->once())
276  ->method('getStateForOrder')
277  ->with($this->orderMock, [])
278  ->willReturn(Order::STATE_CLOSED);
279  $this->orderMock->expects($this->once())
280  ->method('setState')
281  ->with(Order::STATE_CLOSED)
282  ->willReturnSelf();
283  $this->orderMock->expects($this->once())
284  ->method('getState')
285  ->willReturn(Order::STATE_CLOSED);
286  $this->configMock->expects($this->once())
287  ->method('getStateDefaultStatus')
288  ->with(Order::STATE_CLOSED)
289  ->willReturn('Closed');
290  $this->orderMock->expects($this->once())
291  ->method('setStatus')
292  ->with('Closed')
293  ->willReturnSelf();
294  $this->creditmemoMock->expects($this->once())
295  ->method('setState')
296  ->with(\Magento\Sales\Model\Order\Creditmemo::STATE_REFUNDED)
297  ->willReturnSelf();
298  $this->creditmemoRepositoryMock->expects($this->once())
299  ->method('save')
300  ->with($this->creditmemoMock)
301  ->willReturn($this->creditmemoMock);
302  $this->orderRepositoryMock->expects($this->once())
303  ->method('save')
304  ->with($this->orderMock)
305  ->willReturn($this->orderMock);
306  if ($notify) {
307  $this->notifierMock->expects($this->once())
308  ->method('notify')
309  ->with($this->orderMock, $this->creditmemoMock, $this->creditmemoCommentCreationMock);
310  }
311  $this->creditmemoMock->expects($this->once())
312  ->method('getEntityId')
313  ->willReturn(2);
314 
315  $this->assertEquals(
316  2,
317  $this->refundInvoice->execute(
318  $invoiceId,
319  $items,
320  true,
321  $notify,
322  $appendComment,
323  $this->creditmemoCommentCreationMock,
324  $this->creditmemoCreationArgumentsMock
325  )
326  );
327  }
328 
333  {
334  $invoiceId = 1;
335  $items = [1 => $this->creditmemoItemCreationMock];
336  $notify = true;
337  $appendComment = true;
338  $isOnline = false;
339  $errorMessages = ['error1', 'error2'];
340 
341  $this->invoiceRepositoryMock->expects($this->once())
342  ->method('get')
343  ->willReturn($this->invoiceMock);
344  $this->orderRepositoryMock->expects($this->once())
345  ->method('get')
346  ->willReturn($this->orderMock);
347 
348  $this->creditmemoDocumentFactoryMock->expects($this->once())
349  ->method('createFromInvoice')
350  ->with(
351  $this->invoiceMock,
352  $items,
353  $this->creditmemoCommentCreationMock,
354  ($appendComment && $notify),
355  $this->creditmemoCreationArgumentsMock
356  )->willReturn($this->creditmemoMock);
357 
358  $this->refundInvoiceValidatorMock->expects($this->once())
359  ->method('validate')
360  ->with(
361  $this->invoiceMock,
362  $this->orderMock,
363  $this->creditmemoMock,
364  $items,
365  $isOnline,
366  $notify,
367  $appendComment,
368  $this->creditmemoCommentCreationMock,
369  $this->creditmemoCreationArgumentsMock
370  )
371  ->willReturn($this->validationMessagesMock);
372  $hasMessages = true;
373  $this->validationMessagesMock->expects($this->once())
374  ->method('hasMessages')->willReturn($hasMessages);
375  $this->validationMessagesMock->expects($this->once())
376  ->method('getMessages')->willReturn($errorMessages);
377 
378  $this->assertEquals(
379  $errorMessages,
380  $this->refundInvoice->execute(
381  $invoiceId,
382  $items,
383  false,
384  $notify,
385  $appendComment,
386  $this->creditmemoCommentCreationMock,
387  $this->creditmemoCreationArgumentsMock
388  )
389  );
390  }
391 
396  {
397  $invoiceId = 1;
398  $items = [1 => $this->creditmemoItemCreationMock];
399  $notify = true;
400  $appendComment = true;
401  $isOnline = false;
402  $this->resourceConnectionMock->expects($this->once())
403  ->method('getConnection')
404  ->with('sales')
405  ->willReturn($this->adapterInterface);
406 
407  $this->invoiceRepositoryMock->expects($this->once())
408  ->method('get')
409  ->willReturn($this->invoiceMock);
410  $this->orderRepositoryMock->expects($this->once())
411  ->method('get')
412  ->willReturn($this->orderMock);
413 
414  $this->creditmemoDocumentFactoryMock->expects($this->once())
415  ->method('createFromInvoice')
416  ->with(
417  $this->invoiceMock,
418  $items,
419  $this->creditmemoCommentCreationMock,
420  ($appendComment && $notify),
421  $this->creditmemoCreationArgumentsMock
422  )->willReturn($this->creditmemoMock);
423 
424  $this->refundInvoiceValidatorMock->expects($this->once())
425  ->method('validate')
426  ->with(
427  $this->invoiceMock,
428  $this->orderMock,
429  $this->creditmemoMock,
430  $items,
431  $isOnline,
432  $notify,
433  $appendComment,
434  $this->creditmemoCommentCreationMock,
435  $this->creditmemoCreationArgumentsMock
436  )
437  ->willReturn($this->validationMessagesMock);
438  $hasMessages = false;
439  $this->validationMessagesMock->expects($this->once())
440  ->method('hasMessages')->willReturn($hasMessages);
441  $e = new \Exception();
442 
443  $this->refundAdapterMock->expects($this->once())
444  ->method('refund')
445  ->with($this->creditmemoMock, $this->orderMock)
446  ->willThrowException($e);
447 
448  $this->loggerMock->expects($this->once())
449  ->method('critical')
450  ->with($e);
451 
452  $this->adapterInterface->expects($this->once())
453  ->method('rollBack');
454 
455  $this->refundInvoice->execute(
456  $invoiceId,
457  $items,
458  false,
459  $notify,
460  $appendComment,
461  $this->creditmemoCommentCreationMock,
462  $this->creditmemoCreationArgumentsMock
463  );
464  }
465 
469  public function dataProvider()
470  {
471  $creditmemoItemCreationMock = $this->getMockBuilder(CreditmemoItemCreationInterface::class)
472  ->disableOriginalConstructor()
473  ->getMockForAbstractClass();
474 
475  return [
476  'TestWithNotifyTrue' => [1, true, [1 => $creditmemoItemCreationMock], true, true],
477  'TestWithNotifyFalse' => [1, true, [1 => $creditmemoItemCreationMock], false, true],
478  ];
479  }
480 }
testOrderCreditmemo($invoiceId, $isOnline, $items, $notify, $appendComment)
$items