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