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,68 @@ 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 ())
429
+ ->method ('cancelInvoice ' )
430
+ ->willReturn ($ this ->paymentMock );
431
+
432
+ $ this ->eventManagerMock ->expects ($ this ->once ())
433
+ ->method ('dispatch ' )
434
+ ->with ('sales_order_invoice_cancel ' );
435
+
436
+ $ this ->model ->setData (InvoiceInterface::ITEMS , []);
437
+ $ this ->model ->setState (Invoice::STATE_OPEN );
438
+ $ this ->model ->cancel ();
439
+
440
+ self ::assertEquals (Invoice::STATE_CANCELED , $ this ->model ->getState ());
441
+ }
442
+
443
+ /**
444
+ * Assert open invoice can be canceled, and its status changes
445
+ *
446
+ * @param $initialInvoiceStatus
447
+ * @param $finalInvoiceStatus
448
+ * @dataProvider getNotOpenedInvoiceStatuses
449
+ */
450
+ public function testCannotCancelNotOpenedInvoice ($ initialInvoiceStatus , $ finalInvoiceStatus )
451
+ {
452
+ $ this ->order ->expects ($ this ->never ())->method ('getPayment ' );
453
+ $ this ->paymentMock ->expects ($ this ->never ())->method ('cancelInvoice ' );
454
+ $ this ->eventManagerMock ->expects ($ this ->never ())
455
+ ->method ('dispatch ' )
456
+ ->with ('sales_order_invoice_cancel ' );
457
+
458
+ $ this ->model ->setState ($ initialInvoiceStatus );
459
+ $ this ->model ->cancel ();
460
+
461
+ self ::assertEquals ($ finalInvoiceStatus , $ this ->model ->getState ());
462
+ }
463
+
464
+ /**
465
+ * @return array
466
+ */
467
+ public function getNotOpenedInvoiceStatuses ()
468
+ {
469
+ return [
470
+ [Invoice::STATE_PAID , Invoice::STATE_PAID ],
471
+ [Invoice::STATE_CANCELED , Invoice::STATE_CANCELED ],
472
+ ];
473
+ }
410
474
}
0 commit comments