Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CanInvoiceTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class CanInvoiceTest extends \PHPUnit\Framework\TestCase
15 {
19  private $model;
20 
24  private $objectManager;
25 
29  private $orderMock;
30 
34  private $orderItemMock;
35 
36  protected function setUp()
37  {
38  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
39 
40  $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class)
41  ->disableOriginalConstructor()
42  ->setMethods(['getStatus', 'getItems'])
43  ->getMockForAbstractClass();
44 
45  $this->orderItemMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderItemInterface::class)
46  ->disableOriginalConstructor()
47  ->setMethods(['getQtyToInvoice', 'getLockedDoInvoice'])
48  ->getMockForAbstractClass();
49 
50  $this->model = new \Magento\Sales\Model\Order\Validation\CanInvoice();
51  }
52 
58  public function testCanInvoiceWrongState($state)
59  {
60  $this->orderMock->expects($this->any())
61  ->method('getState')
62  ->willReturn($state);
63  $this->orderMock->expects($this->never())
64  ->method('getItems');
65  $this->orderMock->expects($this->once())
66  ->method('getStatus')
67  ->willReturn('status');
68  $this->assertEquals(
69  [__('An invoice cannot be created when an order has a status of %1', 'status')],
70  $this->model->validate($this->orderMock)
71  );
72  }
73 
79  {
80  return [
86  ];
87  }
88 
89  public function testCanInvoiceNoItems()
90  {
91  $this->orderMock->expects($this->any())
92  ->method('getState')
93  ->willReturn(Order::STATE_PROCESSING);
94 
95  $this->orderMock->expects($this->once())
96  ->method('getItems')
97  ->willReturn([]);
98 
99  $this->assertNotEmpty(
100  $this->model->validate($this->orderMock)
101  );
102  }
103 
111  public function testCanInvoice($qtyToInvoice, $itemLockedDoInvoice, $expectedResult)
112  {
113  $this->orderMock->expects($this->any())
114  ->method('getState')
115  ->willReturn(Order::STATE_PROCESSING);
116 
117  $items = [$this->orderItemMock];
118  $this->orderMock->expects($this->once())
119  ->method('getItems')
120  ->willReturn($items);
121  $this->orderItemMock->expects($this->any())
122  ->method('getQtyToInvoice')
123  ->willReturn($qtyToInvoice);
124  $this->orderItemMock->expects($this->any())
125  ->method('getLockedDoInvoice')
126  ->willReturn($itemLockedDoInvoice);
127 
128  $this->assertEquals(
129  $expectedResult,
130  $this->model->validate($this->orderMock)
131  );
132  }
133 
139  public function canInvoiceDataProvider()
140  {
141  return [
142  [0, null, [__('The order does not allow an invoice to be created.')]],
143  [-1, null, [__('The order does not allow an invoice to be created.')]],
144  [1, true, [__('The order does not allow an invoice to be created.')]],
145  [0.5, false, []],
146  ];
147  }
148 }
__()
Definition: __.php:13
testCanInvoice($qtyToInvoice, $itemLockedDoInvoice, $expectedResult)
$items