8
8
9
9
namespace Magento \Sales \Test \Unit \Model \Order ;
10
10
11
+ use Magento \Sales \Api \Data \InvoiceInterface ;
11
12
use Magento \Sales \Model \Order \Invoice ;
12
13
use Magento \Sales \Model \ResourceModel \OrderFactory ;
13
14
use Magento \Sales \Model \Order ;
@@ -72,7 +73,7 @@ protected function setUp()
72
73
->setMethods (
73
74
[
74
75
'getPayment ' , '__wakeup ' , 'load ' , 'setHistoryEntityName ' , 'getStore ' , 'getBillingAddress ' ,
75
- 'getShippingAddress '
76
+ 'getShippingAddress ' , ' getConfig ' ,
76
77
]
77
78
)
78
79
->getMock ();
@@ -83,7 +84,7 @@ protected function setUp()
83
84
$ this ->paymentMock = $ this ->getMockBuilder (
84
85
\Magento \Sales \Model \Order \Payment::class
85
86
)->disableOriginalConstructor ()->setMethods (
86
- ['canVoid ' , '__wakeup ' , 'canCapture ' , 'capture ' , 'pay ' ]
87
+ ['canVoid ' , '__wakeup ' , 'canCapture ' , 'capture ' , 'pay ' , ' cancelInvoice ' ]
87
88
)->getMock ();
88
89
89
90
$ this ->orderFactory = $ this ->createPartialMock (\Magento \Sales \Model \OrderFactory::class, ['create ' ]);
@@ -407,4 +408,58 @@ private function getOrderInvoiceCollection()
407
408
408
409
return $ collection ;
409
410
}
411
+
412
+ /**
413
+ * Assert open invoice can be canceled, and its status changes
414
+ */
415
+ public function testCancelOpenInvoice ()
416
+ {
417
+ $ orderConfigMock = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Config::class)
418
+ ->disableOriginalConstructor ()->setMethods (
419
+ ['getStateDefaultStatus ' ]
420
+ )->getMock ();
421
+ $ orderConfigMock ->expects ($ this ->once ())->method ('getStateDefaultStatus ' )
422
+ ->with (Order::STATE_PROCESSING )
423
+ ->willReturn (Order::STATE_PROCESSING );
424
+ $ this ->order ->expects ($ this ->once ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
425
+ $ this ->order ->expects ($ this ->once ())->method ('getConfig ' )->willReturn ($ orderConfigMock );
426
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('cancelInvoice ' )->willReturn ($ this ->paymentMock );
427
+ $ this ->eventManagerMock ->expects ($ this ->once ())
428
+ ->method ('dispatch ' )
429
+ ->with ('sales_order_invoice_cancel ' );
430
+ $ this ->model ->setData (InvoiceInterface::ITEMS , []);
431
+ $ this ->model ->setState (Invoice::STATE_OPEN );
432
+ $ this ->model ->cancel ();
433
+ self ::assertEquals (Invoice::STATE_CANCELED , $ this ->model ->getState ());
434
+ }
435
+
436
+ /**
437
+ * Assert open invoice can be canceled, and its status changes
438
+ *
439
+ * @param $initialInvoiceStatus
440
+ * @param $finalInvoiceStatus
441
+ * @dataProvider getNotOpenedInvoiceStatuses
442
+ */
443
+ public function testCannotCancelNotOpenedInvoice ($ initialInvoiceStatus , $ finalInvoiceStatus )
444
+ {
445
+ $ this ->order ->expects ($ this ->never ())->method ('getPayment ' );
446
+ $ this ->paymentMock ->expects ($ this ->never ())->method ('cancelInvoice ' );
447
+ $ this ->eventManagerMock ->expects ($ this ->never ())
448
+ ->method ('dispatch ' )
449
+ ->with ('sales_order_invoice_cancel ' );
450
+ $ this ->model ->setState ($ initialInvoiceStatus );
451
+ $ this ->model ->cancel ();
452
+ self ::assertEquals ($ finalInvoiceStatus , $ this ->model ->getState ());
453
+ }
454
+
455
+ /**
456
+ * @return array
457
+ */
458
+ public function getNotOpenedInvoiceStatuses ()
459
+ {
460
+ return [
461
+ [Invoice::STATE_PAID , Invoice::STATE_PAID ],
462
+ [Invoice::STATE_CANCELED , Invoice::STATE_CANCELED ],
463
+ ];
464
+ }
410
465
}
0 commit comments