@@ -49,6 +49,11 @@ class InvoiceTest extends \PHPUnit_Framework_TestCase
49
49
*/
50
50
protected $ paymentMock ;
51
51
52
+ /**
53
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\ManagerInterface
54
+ */
55
+ protected $ eventManagerMock ;
56
+
52
57
/**
53
58
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
54
59
*/
@@ -73,12 +78,19 @@ protected function setUp()
73
78
$ this ->paymentMock = $ this ->getMockBuilder (
74
79
'Magento\Sales\Model\Order\Payment '
75
80
)->disableOriginalConstructor ()->setMethods (
76
- ['canVoid ' , '__wakeup ' , 'canCapture ' ]
81
+ ['canVoid ' , '__wakeup ' , 'canCapture ' , ' capture ' , ' pay ' , ' hasForcedState ' , ' getForcedState ' ]
77
82
)->getMock ();
78
83
79
84
$ this ->orderFactory = $ this ->getMock ('Magento\Sales\Model\OrderFactory ' , ['create ' ], [], '' , false );
80
85
86
+ $ this ->eventManagerMock = $ this ->getMock ('\Magento\Framework\Event\ManagerInterface ' , [], [], '' , false );
87
+ $ contextMock = $ this ->getMock ('\Magento\Framework\Model\Context ' , [], [], '' , false );
88
+ $ contextMock ->expects ($ this ->any ())
89
+ ->method ('getEventDispatcher ' )
90
+ ->willReturn ($ this ->eventManagerMock );
91
+
81
92
$ arguments = [
93
+ 'context ' => $ contextMock ,
82
94
'orderFactory ' => $ this ->orderFactory ,
83
95
'orderResourceFactory ' => $ this ->getMock (
84
96
'Magento\Sales\Model\Resource\OrderFactory ' ,
@@ -284,4 +296,109 @@ public function canCancelDataProvider()
284
296
[Invoice::STATE_PAID , false ]
285
297
];
286
298
}
299
+
300
+ /**
301
+ * @dataProvider canRefundDataProvider
302
+ * @param string $state
303
+ * @param float $baseGrandTotal
304
+ * @param float $baseTotalRefunded
305
+ * @param bool $expectedResult
306
+ */
307
+ public function testCanRefund ($ state , $ baseGrandTotal , $ baseTotalRefunded , $ expectedResult )
308
+ {
309
+ $ this ->model ->setState ($ state );
310
+ $ this ->model ->setBaseGrandTotal ($ baseGrandTotal );
311
+ $ this ->model ->setBaseTotalRefunded ($ baseTotalRefunded );
312
+ $ this ->assertEquals ($ expectedResult , $ this ->model ->canRefund ());
313
+ }
314
+
315
+ /**
316
+ * Data provider for testCanRefund
317
+ *
318
+ * @return array
319
+ */
320
+ public function canRefundDataProvider ()
321
+ {
322
+ return [
323
+ [Invoice::STATE_OPEN , 0.00 , 0.00 , false ],
324
+ [Invoice::STATE_CANCELED , 1.00 , 0.01 , false ],
325
+ [Invoice::STATE_PAID , 1.00 , 0.00 , true ],
326
+ [Invoice::STATE_PAID , 1.00 , 1.00 , false ],//!!!Wty it must be false?
327
+ [Invoice::STATE_PAID , 1.000101 , 1.0000 , true ],
328
+ [Invoice::STATE_PAID , 1.0001 , 1.00 , false ],
329
+ [Invoice::STATE_PAID , 1.00 , 1.0001 , false ],
330
+ ];
331
+ }
332
+
333
+ public function testCaptureNotPaid ()
334
+ {
335
+ $ this ->model ->setIsPaid (false );
336
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
337
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('capture ' )->with ($ this ->model )->willReturnSelf ();
338
+ $ this ->paymentMock ->expects ($ this ->never ())->method ('pay ' );
339
+ $ this ->assertEquals ($ this ->model , $ this ->model ->capture ());
340
+ }
341
+
342
+ public function testCapturePaid ()
343
+ {
344
+ $ this ->model ->setIsPaid (true );
345
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
346
+ $ this ->paymentMock ->expects ($ this ->any ())->method ('capture ' )->with ($ this ->model )->willReturnSelf ();
347
+ $ this ->mockPay (false , null );
348
+ $ this ->assertEquals ($ this ->model , $ this ->model ->capture ());
349
+ }
350
+
351
+ public function mockPay ($ hasForcedState , $ forcedState )
352
+ {
353
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
354
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('hasForcedState ' )->willReturn ($ hasForcedState );
355
+ if ($ hasForcedState ) {
356
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('getForcedState ' )->willReturn ($ forcedState );
357
+ } else {
358
+ $ this ->paymentMock ->expects ($ this ->never ())->method ('getForcedState ' );
359
+ }
360
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('pay ' )->with ($ this ->model )->willReturnSelf ();
361
+ $ this ->eventManagerMock
362
+ ->expects ($ this ->once ())
363
+ ->method ('dispatch ' )
364
+ ->with ('sales_order_invoice_pay ' );
365
+ }
366
+
367
+ /**
368
+ * @dataProvider payDataProvider
369
+ * @param bool $hasForcedState
370
+ * @param string|null $forcedState
371
+ * @param float $orderTotalPaid
372
+ * @param float $orderBaseTotalPaid
373
+ * @param float $grandTotal
374
+ * @param float $baseGrandTotal
375
+ * @param float $expectedState
376
+ * @param float $expectedTotalPaid
377
+ * @param float $expectedBaseTotalPaid
378
+ */
379
+ public function testPay ($ hasForcedState , $ forcedState , $ orderTotalPaid , $ orderBaseTotalPaid , $ grandTotal ,
380
+ $ baseGrandTotal , $ expectedState , $ expectedTotalPaid , $ expectedBaseTotalPaid )
381
+ {
382
+ $ this ->mockPay ($ hasForcedState , $ forcedState );
383
+ $ this ->model ->setGrandTotal ($ grandTotal );
384
+ $ this ->model ->setBaseGrandTotal ($ baseGrandTotal );
385
+ $ this ->orderMock ->setTotalPaid ($ orderTotalPaid );
386
+ $ this ->orderMock ->setBaseTotalPaid ($ orderBaseTotalPaid );
387
+ $ this ->assertFalse ($ this ->model ->wasPayCalled ());
388
+ $ this ->assertEquals ($ this ->model , $ this ->model ->pay ());
389
+ $ this ->assertTrue ($ this ->model ->wasPayCalled ());
390
+ $ this ->assertEquals ($ expectedState , $ this ->model ->getState ());
391
+ //$this->assertEquals($expectedTotalPaid, $this->model->getTotalPaid());
392
+ //$this->assertEquals($expectedBaseTotalPaid, $this->model->getBaseTotalPaid());
393
+ #second call of pay() method must do nothing
394
+ $ this ->model ->pay ();
395
+ }
396
+
397
+ public function payDataProvider ()
398
+ {
399
+ //ToDo: fill data provider and uncomment assertings totals in testPay
400
+ return [
401
+ [true , 'payment_state ' , 10.99 , 1.00 , 10.99 , 1.00 , 'payment_state ' , 11.99 , 11.99 ]
402
+ ];
403
+ }
287
404
}
0 commit comments