88
99namespace Magento \Sales \Test \Unit \Model \Order ;
1010
11+ use Magento \Sales \Api \Data \InvoiceInterface ;
1112use Magento \Sales \Model \Order \Invoice ;
1213use Magento \Sales \Model \ResourceModel \OrderFactory ;
1314use Magento \Sales \Model \Order ;
@@ -72,7 +73,7 @@ protected function setUp()
7273 ->setMethods (
7374 [
7475 'getPayment ' , '__wakeup ' , 'load ' , 'setHistoryEntityName ' , 'getStore ' , 'getBillingAddress ' ,
75- 'getShippingAddress '
76+ 'getShippingAddress ' , ' getConfig ' ,
7677 ]
7778 )
7879 ->getMock ();
@@ -83,7 +84,7 @@ protected function setUp()
8384 $ this ->paymentMock = $ this ->getMockBuilder (
8485 \Magento \Sales \Model \Order \Payment::class
8586 )->disableOriginalConstructor ()->setMethods (
86- ['canVoid ' , '__wakeup ' , 'canCapture ' , 'capture ' , 'pay ' ]
87+ ['canVoid ' , '__wakeup ' , 'canCapture ' , 'capture ' , 'pay ' , ' cancelInvoice ' ]
8788 )->getMock ();
8889
8990 $ this ->orderFactory = $ this ->createPartialMock (\Magento \Sales \Model \OrderFactory::class, ['create ' ]);
@@ -407,4 +408,58 @@ private function getOrderInvoiceCollection()
407408
408409 return $ collection ;
409410 }
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+ }
410465}
0 commit comments