Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CanRefundTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class CanRefundTest extends \PHPUnit\Framework\TestCase
14 {
18  private $invoiceMock;
19 
23  private $orderPaymentRepositoryMock;
24 
28  private $orderRepositoryMock;
29 
33  private $paymentMock;
34 
38  private $validator;
39 
40  protected function setUp()
41  {
42  $this->invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45  $this->orderPaymentRepositoryMock = $this->getMockBuilder(
46  \Magento\Sales\Api\OrderPaymentRepositoryInterface::class
47  )
48  ->disableOriginalConstructor()
49  ->getMockForAbstractClass();
50  $this->orderRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
51  ->disableOriginalConstructor()
52  ->getMockForAbstractClass();
53  $this->paymentMock = $this->getMockBuilder(\Magento\Payment\Model\InfoInterface::class)
54  ->disableOriginalConstructor()
55  ->getMockForAbstractClass();
56  $this->validator = new \Magento\Sales\Model\Order\Invoice\Validation\CanRefund(
57  $this->orderPaymentRepositoryMock,
58  $this->orderRepositoryMock
59  );
60  }
61 
63  {
64  $this->invoiceMock->expects($this->exactly(2))
65  ->method('getState')
66  ->willReturnOnConsecutiveCalls(
67  \Magento\Sales\Model\Order\Invoice::STATE_OPEN,
69  );
70  $this->assertEquals(
71  [__('We can\'t create creditmemo for the invoice.')],
72  $this->validator->validate($this->invoiceMock)
73  );
74  $this->assertEquals(
75  [__('We can\'t create creditmemo for the invoice.')],
76  $this->validator->validate($this->invoiceMock)
77  );
78  }
79 
81  {
82  $this->invoiceMock->expects($this->once())
83  ->method('getState')
84  ->willReturn(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
85  $this->invoiceMock->expects($this->once())
86  ->method('getBaseGrandTotal')
87  ->willReturn(1);
88  $this->invoiceMock->expects($this->once())
89  ->method('getBaseTotalRefunded')
90  ->willReturn(1);
91  $this->assertEquals(
92  [__('We can\'t create creditmemo for the invoice.')],
93  $this->validator->validate($this->invoiceMock)
94  );
95  }
96 
97  public function testValidate()
98  {
99  $this->invoiceMock->expects($this->once())
100  ->method('getState')
101  ->willReturn(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
102  $orderMock = $this->getMockBuilder(OrderInterface::class)
103  ->disableOriginalConstructor()
104  ->getMockForAbstractClass();
105  $this->orderRepositoryMock->expects($this->once())
106  ->method('get')
107  ->willReturn($orderMock);
108  $orderMock->expects($this->once())
109  ->method('getPayment')
110  ->willReturn($this->paymentMock);
111  $methodInstanceMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
112  ->disableOriginalConstructor()
113  ->getMockForAbstractClass();
114  $this->paymentMock->expects($this->once())
115  ->method('getMethodInstance')
116  ->willReturn($methodInstanceMock);
117  $methodInstanceMock->expects($this->atLeastOnce())
118  ->method('canRefund')
119  ->willReturn(true);
120  $this->invoiceMock->expects($this->once())
121  ->method('getBaseGrandTotal')
122  ->willReturn(1);
123  $this->invoiceMock->expects($this->once())
124  ->method('getBaseTotalRefunded')
125  ->willReturn(0);
126  $this->assertEquals(
127  [],
128  $this->validator->validate($this->invoiceMock)
129  );
130  }
131 }
__()
Definition: __.php:13