Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PayOperationTest.php
Go to the documentation of this file.
1 <?php
7 
11 class PayOperationTest extends \PHPUnit\Framework\TestCase
12 {
16  private $subject;
17 
21  private $orderMock;
22 
26  private $invoiceMock;
27 
31  private $contextMock;
32 
36  private $invoiceItemMock;
37 
41  private $orderPaymentMock;
42 
46  private $eventManagerMock;
47 
51  private $paymentMethodMock;
52 
56  protected function setUp()
57  {
58  $this->orderMock = $this->getMockForAbstractClass(
59  \Magento\Sales\Api\Data\OrderInterface::class,
60  [],
61  '',
62  false,
63  false,
64  true,
65  [
66  'getPayment',
67  'setTotalInvoiced',
68  'getTotalInvoiced',
69  'setBaseTotalInvoiced',
70  'getBaseTotalInvoiced',
71  'setSubtotalInvoiced',
72  'getSubtotalInvoiced',
73  'setBaseSubtotalInvoiced',
74  'getBaseSubtotalInvoiced',
75  'setTaxInvoiced',
76  'getTaxInvoiced',
77  'setBaseTaxInvoiced',
78  'getBaseTaxInvoiced',
79  'setDiscountTaxCompensationInvoiced',
80  'getDiscountTaxCompensationInvoiced',
81  'setBaseDiscountTaxCompensationInvoiced',
82  'getBaseDiscountTaxCompensationInvoiced',
83  'setShippingTaxInvoiced',
84  'getShippingTaxInvoiced',
85  'setBaseShippingTaxInvoiced',
86  'getBaseShippingTaxInvoiced',
87  'setShippingInvoiced',
88  'getShippingInvoiced',
89  'setBaseShippingInvoiced',
90  'getBaseShippingInvoiced',
91  'setDiscountInvoiced',
92  'getDiscountInvoiced',
93  'setBaseDiscountInvoiced',
94  'getBaseDiscountInvoiced',
95  'setBaseTotalInvoicedCost',
96  'getBaseTotalInvoicedCost',
97  ]
98  );
99  $this->orderMock->expects($this->any())
100  ->method('getTotalInvoiced')
101  ->willReturn(43);
102  $this->orderMock->expects($this->any())
103  ->method('getBaseTotalInvoiced')
104  ->willReturn(43);
105  $this->orderMock->expects($this->any())
106  ->method('getSubtotalInvoiced')
107  ->willReturn(22);
108  $this->orderMock->expects($this->any())
109  ->method('getBaseSubtotalInvoiced')
110  ->willReturn(22);
111  $this->orderMock->expects($this->any())
112  ->method('getTaxInvoiced')
113  ->willReturn(15);
114  $this->orderMock->expects($this->any())
115  ->method('getBaseTaxInvoiced')
116  ->willReturn(15);
117  $this->orderMock->expects($this->any())
118  ->method('getDiscountTaxCompensationInvoiced')
119  ->willReturn(11);
120  $this->orderMock->expects($this->any())
121  ->method('getBaseDiscountTaxCompensationInvoiced')
122  ->willReturn(11);
123  $this->orderMock->expects($this->any())
124  ->method('getShippingTaxInvoiced')
125  ->willReturn(12);
126  $this->orderMock->expects($this->any())
127  ->method('getBaseShippingTaxInvoiced')
128  ->willReturn(12);
129  $this->orderMock->expects($this->any())
130  ->method('getShippingInvoiced')
131  ->willReturn(28);
132  $this->orderMock->expects($this->any())
133  ->method('getBaseShippingInvoiced')
134  ->willReturn(28);
135  $this->orderMock->expects($this->any())
136  ->method('getDiscountInvoiced')
137  ->willReturn(19);
138  $this->orderMock->expects($this->any())
139  ->method('getBaseDiscountInvoiced')
140  ->willReturn(19);
141  $this->orderMock->expects($this->any())
142  ->method('getBaseTotalInvoicedCost')
143  ->willReturn(31);
144 
145  $this->invoiceMock = $this->getMockForAbstractClass(
146  \Magento\Sales\Api\Data\InvoiceInterface::class,
147  [],
148  '',
149  false,
150  false,
151  true,
152  [
153  'getItems',
154  'getState',
155  'capture',
156  'setCanVoidFlag',
157  'pay',
158  'getGrandTotal',
159  'getBaseGrandTotal',
160  'getSubtotal',
161  'getBaseSubtotal',
162  'getTaxAmount',
163  'getBaseTaxAmount',
164  'getDiscountTaxCompensationAmount',
165  'getBaseDiscountTaxCompensationAmount',
166  'getShippingTaxAmount',
167  'getBaseShippingTaxAmount',
168  'getShippingAmount',
169  'getBaseShippingAmount',
170  'getDiscountAmount',
171  'getBaseDiscountAmount',
172  'getBaseCost',
173  ]
174  );
175  $this->invoiceMock->expects($this->any())
176  ->method('getGrandTotal')
177  ->willReturn(43);
178  $this->invoiceMock->expects($this->any())
179  ->method('getBaseGrandTotal')
180  ->willReturn(43);
181  $this->invoiceMock->expects($this->any())
182  ->method('getSubtotal')
183  ->willReturn(22);
184  $this->invoiceMock->expects($this->any())
185  ->method('getBaseSubtotal')
186  ->willReturn(22);
187  $this->invoiceMock->expects($this->any())
188  ->method('getTaxAmount')
189  ->willReturn(15);
190  $this->invoiceMock->expects($this->any())
191  ->method('getBaseTaxAmount')
192  ->willReturn(15);
193  $this->invoiceMock->expects($this->any())
194  ->method('getDiscountTaxCompensationAmount')
195  ->willReturn(11);
196  $this->invoiceMock->expects($this->any())
197  ->method('getBaseDiscountTaxCompensationAmount')
198  ->willReturn(11);
199  $this->invoiceMock->expects($this->any())
200  ->method('getShippingTaxAmount')
201  ->willReturn(12);
202  $this->invoiceMock->expects($this->any())
203  ->method('getBaseShippingTaxAmount')
204  ->willReturn(12);
205  $this->invoiceMock->expects($this->any())
206  ->method('getShippingAmount')
207  ->willReturn(28);
208  $this->invoiceMock->expects($this->any())
209  ->method('getBaseShippingAmount')
210  ->willReturn(28);
211  $this->invoiceMock->expects($this->any())
212  ->method('getDiscountAmount')
213  ->willReturn(19);
214  $this->invoiceMock->expects($this->any())
215  ->method('getBaseDiscountAmount')
216  ->willReturn(19);
217  $this->invoiceMock->expects($this->any())
218  ->method('getBaseCost')
219  ->willReturn(31);
220 
221  $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
222 
223  $this->invoiceItemMock = $this->getMockForAbstractClass(
224  \Magento\Sales\Api\Data\InvoiceItemInterface::class,
225  [],
226  '',
227  false,
228  false,
229  true,
230  [
231  'isDeleted',
232  'register',
233  ]
234  );
235  $this->invoiceItemMock->expects($this->any())
236  ->method('isDeleted')
237  ->willReturn(false);
238  $this->invoiceItemMock->expects($this->any())
239  ->method('getQty')
240  ->willReturn(1);
241 
242  $this->orderPaymentMock = $this->getMockForAbstractClass(
243  \Magento\Sales\Api\Data\OrderPaymentInterface::class,
244  [],
245  '',
246  false,
247  false,
248  true,
249  [
250  'canCapture',
251  'getMethodInstance',
252  'getIsTransactionPending',
253  ]
254  );
255  $this->orderMock->expects($this->any())
256  ->method('getPayment')
257  ->willReturn($this->orderPaymentMock);
258 
259  $this->eventManagerMock = $this->getMockForAbstractClass(
260  \Magento\Framework\Event\ManagerInterface::class,
261  [],
262  '',
263  false,
264  false,
265  true,
266  []
267  );
268  $this->contextMock->expects($this->any())
269  ->method('getEventDispatcher')
270  ->willReturn($this->eventManagerMock);
271 
272  $this->paymentMethodMock = $this->getMockForAbstractClass(
273  \Magento\Payment\Model\MethodInterface::class,
274  [],
275  '',
276  false,
277  false,
278  true,
279  []
280  );
281  $this->orderPaymentMock->expects($this->any())
282  ->method('getMethodInstance')
283  ->willReturn($this->paymentMethodMock);
284 
285  $this->subject = new \Magento\Sales\Model\Order\Invoice\PayOperation(
286  $this->contextMock
287  );
288  }
289 
301  public function testExecute($canCapture, $isOnline, $isGateway, $isTransactionPending)
302  {
303  $this->invoiceMock->expects($this->any())
304  ->method('getItems')
305  ->willReturn([$this->invoiceItemMock]);
306 
307  if ($canCapture) {
308  $this->invoiceMock->expects($this->any())
309  ->method('getState')
310  ->willReturn(\Magento\Sales\Model\Order\Invoice::STATE_OPEN);
311 
312  $this->orderPaymentMock->expects($this->any())
313  ->method('canCapture')
314  ->willReturn(true);
315 
316  if ($isOnline) {
317  $this->invoiceMock->expects($this->once())
318  ->method('capture');
319  } else {
320  $this->invoiceMock->expects($this->never())
321  ->method('capture');
322 
323  $this->invoiceMock->expects($this->once())
324  ->method('setCanVoidFlag')
325  ->with(false);
326 
327  $this->invoiceMock->expects($this->once())
328  ->method('pay');
329  }
330  } else {
331  $this->paymentMethodMock->expects($this->any())
332  ->method('isGateway')
333  ->willReturn($isGateway);
334 
335  $this->orderPaymentMock->expects($this->any())
336  ->method('getIsTransactionPending')
337  ->willReturn($isTransactionPending);
338 
339  $this->invoiceMock->expects($this->never())
340  ->method('capture');
341 
342  if ((!$isGateway || !$isOnline) && !$isTransactionPending) {
343  $this->invoiceMock->expects($this->once())
344  ->method('setCanVoidFlag')
345  ->with(false);
346 
347  $this->invoiceMock->expects($this->once())
348  ->method('pay');
349  }
350  }
351 
352  $this->orderMock->expects($this->once())
353  ->method('setTotalInvoiced')
354  ->with(86);
355  $this->orderMock->expects($this->once())
356  ->method('setBaseTotalInvoiced')
357  ->with(86);
358  $this->orderMock->expects($this->once())
359  ->method('setSubtotalInvoiced')
360  ->with(44);
361  $this->orderMock->expects($this->once())
362  ->method('setBaseSubtotalInvoiced')
363  ->with(44);
364  $this->orderMock->expects($this->once())
365  ->method('setTaxInvoiced')
366  ->with(30);
367  $this->orderMock->expects($this->once())
368  ->method('setBaseTaxInvoiced')
369  ->with(30);
370  $this->orderMock->expects($this->once())
371  ->method('setDiscountTaxCompensationInvoiced')
372  ->with(22);
373  $this->orderMock->expects($this->once())
374  ->method('setBaseDiscountTaxCompensationInvoiced')
375  ->with(22);
376  $this->orderMock->expects($this->once())
377  ->method('setShippingTaxInvoiced')
378  ->with(24);
379  $this->orderMock->expects($this->once())
380  ->method('setBaseShippingTaxInvoiced')
381  ->with(24);
382  $this->orderMock->expects($this->once())
383  ->method('setShippingInvoiced')
384  ->with(56);
385  $this->orderMock->expects($this->once())
386  ->method('setBaseShippingInvoiced')
387  ->with(56);
388  $this->orderMock->expects($this->once())
389  ->method('setDiscountInvoiced')
390  ->with(38);
391  $this->orderMock->expects($this->once())
392  ->method('setBaseDiscountInvoiced')
393  ->with(38);
394  $this->orderMock->expects($this->once())
395  ->method('setBaseTotalInvoicedCost')
396  ->with(62);
397 
398  $this->eventManagerMock->expects($this->once())
399  ->method('dispatch')
400  ->with(
401  'sales_order_invoice_register',
402  [
403  'invoice' => $this->invoiceMock,
404  'order' => $this->orderMock,
405  ]
406  );
407 
408  $this->assertEquals(
409  $this->orderMock,
410  $this->subject->execute(
411  $this->orderMock,
412  $this->invoiceMock,
413  $isOnline
414  )
415  );
416  }
417 
421  public function payDataProvider()
422  {
423  return [
424  'Invoice can capture, online' => [
425  true, true, null, null,
426  ],
427  'Invoice can capture, offline' => [
428  true, false, null, null,
429  ],
430  'Invoice can not capture, online, is not gateway, transaction is not pending' => [
431  false, true, false, false,
432  ],
433  'Invoice can not capture, offline, gateway, transaction is not pending' => [
434  false, false, true, false,
435  ],
436  ];
437  }
438 }
testExecute($canCapture, $isOnline, $isGateway, $isTransactionPending)