Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InvoiceQuantityValidatorTest.php
Go to the documentation of this file.
1 <?php
8 
11 
15 class InvoiceQuantityValidatorTest extends \PHPUnit\Framework\TestCase
16 {
20  private $model;
21 
25  private $objectManager;
26 
30  private $orderMock;
31 
35  private $orderRepositoryMock;
36 
40  private $invoiceMock;
41 
42  protected function setUp()
43  {
44  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
45 
46  $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class)
47  ->disableOriginalConstructor()
48  ->getMockForAbstractClass();
49 
50  $this->invoiceMock = $this->getMockBuilder(\Magento\Sales\Api\Data\InvoiceInterface::class)
51  ->disableOriginalConstructor()
52  ->setMethods(['getTotalQty', 'getItems'])
53  ->getMockForAbstractClass();
54  $this->orderRepositoryMock = $this->getMockBuilder(
55  OrderRepositoryInterface::class
56  )->disableOriginalConstructor()->getMockForAbstractClass();
57  $this->orderRepositoryMock->expects($this->any())->method('get')->willReturn($this->orderMock);
58  $this->model = $this->objectManager->getObject(
59  \Magento\Sales\Model\Order\InvoiceQuantityValidator::class,
60  ['orderRepository' => $this->orderRepositoryMock]
61  );
62  }
63 
64  public function testValidate()
65  {
66  $expectedResult = [];
67  $invoiceItemMock = $this->getInvoiceItemMock(1, 1);
68  $this->invoiceMock->expects($this->once())
69  ->method('getItems')
70  ->willReturn([$invoiceItemMock]);
71 
72  $orderItemMock = $this->getOrderItemMock(1, 1, true);
73  $this->orderMock->expects($this->once())
74  ->method('getItems')
75  ->willReturn([$orderItemMock]);
76  $this->invoiceMock->expects($this->exactly(2))
77  ->method('getOrderId')
78  ->willReturn(1);
79  $this->assertEquals(
80  $expectedResult,
81  $this->model->validate($this->invoiceMock)
82  );
83  }
84 
86  {
87  $orderItemId = 1;
88  $message = 'The quantity to invoice must not be greater than the uninvoiced quantity for product SKU "%1".';
89  $expectedResult = [__($message, $orderItemId)];
90  $invoiceItemMock = $this->getInvoiceItemMock($orderItemId, 2);
91  $this->invoiceMock->expects($this->once())
92  ->method('getItems')
93  ->willReturn([$invoiceItemMock]);
94 
95  $orderItemMock = $this->getOrderItemMock($orderItemId, 1, false);
96  $this->orderMock->expects($this->once())
97  ->method('getItems')
98  ->willReturn([$orderItemMock]);
99  $this->invoiceMock->expects($this->exactly(2))
100  ->method('getOrderId')
101  ->willReturn(1);
102  $this->assertEquals(
103  $expectedResult,
104  $this->model->validate($this->invoiceMock)
105  );
106  }
107 
108  public function testValidateNoOrderItems()
109  {
110  $expectedResult = [__('The invoice contains one or more items that are not part of the original order.')];
111  $invoiceItemMock = $this->getInvoiceItemMock(1, 1);
112  $this->invoiceMock->expects($this->once())
113  ->method('getItems')
114  ->willReturn([$invoiceItemMock]);
115 
116  $this->orderMock->expects($this->once())
117  ->method('getItems')
118  ->willReturn([]);
119  $this->invoiceMock->expects($this->exactly(2))
120  ->method('getOrderId')
121  ->willReturn(1);
122  $this->assertEquals(
123  $expectedResult,
124  $this->model->validate($this->invoiceMock)
125  );
126  }
127 
128  public function testValidateNoOrder()
129  {
130  $expectedResult = [__('Order Id is required for invoice document')];
131  $this->assertEquals(
132  $expectedResult,
133  $this->model->validate($this->invoiceMock)
134  );
135  }
136 
137  public function testValidateNoInvoiceItems()
138  {
139  $expectedResult = [__("The invoice can't be created without products. Add products and try again.")];
140  $orderItemId = 1;
141  $invoiceItemMock = $this->getInvoiceItemMock($orderItemId, 0);
142  $this->invoiceMock->expects($this->once())
143  ->method('getItems')
144  ->willReturn([$invoiceItemMock]);
145 
146  $orderItemMock = $this->getOrderItemMock($orderItemId, 1, false);
147  $this->orderMock->expects($this->once())
148  ->method('getItems')
149  ->willReturn([$orderItemMock]);
150  $this->invoiceMock->expects($this->exactly(2))
151  ->method('getOrderId')
152  ->willReturn(1);
153  $this->assertEquals(
154  $expectedResult,
155  $this->model->validate($this->invoiceMock)
156  );
157  }
158 
164  private function getInvoiceItemMock($orderItemId, $qty)
165  {
166  $invoiceItemMock = $this->getMockBuilder(\Magento\Sales\Api\Data\InvoiceItemInterface::class)
167  ->disableOriginalConstructor()
168  ->setMethods(['getOrderItemId', 'getQty'])
169  ->getMockForAbstractClass();
170  $invoiceItemMock->expects($this->once())->method('getOrderItemId')->willReturn($orderItemId);
171  $invoiceItemMock->expects($this->once())->method('getQty')->willReturn($qty);
172  return $invoiceItemMock;
173  }
174 
181  private function getOrderItemMock($id, $qtyToInvoice, $isDummy)
182  {
183  $orderItemMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderItemInterface::class)
184  ->disableOriginalConstructor()
185  ->setMethods(['getId', 'getQtyToInvoice', 'isDummy', 'getSku'])
186  ->getMockForAbstractClass();
187  $orderItemMock->expects($this->any())->method('getId')->willReturn($id);
188  $orderItemMock->expects($this->any())->method('getQtyToInvoice')->willReturn($qtyToInvoice);
189  $orderItemMock->expects($this->any())->method('isDummy')->willReturn($isDummy);
190  $orderItemMock->expects($this->any())->method('getSku')->willReturn($id);
191  return $orderItemMock;
192  }
193 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$message