Skip to content

Commit c4e1b29

Browse files
committed
Add Test to cover cancel Invoice
1 parent 82eb45d commit c4e1b29

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceTest.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
namespace Magento\Sales\Test\Unit\Model\Order;
1010

11+
use Magento\Sales\Api\Data\InvoiceInterface;
12+
use Magento\Sales\Model\Order;
1113
use Magento\Sales\Model\Order\Invoice;
14+
use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection as InvoiceCollection;
1215
use Magento\Sales\Model\ResourceModel\OrderFactory;
13-
use Magento\Sales\Model\Order;
14-
use Magento\TestFramework\Helper\Bootstrap;
1516
use PHPUnit_Framework_MockObject_MockObject as MockObject;
16-
use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection as InvoiceCollection;
1717

1818
/**
1919
* Class InvoiceTest
@@ -72,7 +72,7 @@ protected function setUp()
7272
->setMethods(
7373
[
7474
'getPayment', '__wakeup', 'load', 'setHistoryEntityName', 'getStore', 'getBillingAddress',
75-
'getShippingAddress'
75+
'getShippingAddress', 'getConfig',
7676
]
7777
)
7878
->getMock();
@@ -83,7 +83,7 @@ protected function setUp()
8383
$this->paymentMock = $this->getMockBuilder(
8484
\Magento\Sales\Model\Order\Payment::class
8585
)->disableOriginalConstructor()->setMethods(
86-
['canVoid', '__wakeup', 'canCapture', 'capture', 'pay']
86+
['canVoid', '__wakeup', 'canCapture', 'capture', 'pay', 'cancelInvoice']
8787
)->getMock();
8888

8989
$this->orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
@@ -407,4 +407,62 @@ private function getOrderInvoiceCollection()
407407

408408
return $collection;
409409
}
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+
}
410468
}

0 commit comments

Comments
 (0)