25 private $objectManager;
35 private $orderRepositoryMock;
44 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
46 $this->orderMock = $this->getMockBuilder(\
Magento\Sales\Api\
Data\OrderInterface::class)
47 ->disableOriginalConstructor()
48 ->getMockForAbstractClass();
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]
67 $invoiceItemMock = $this->getInvoiceItemMock(1, 1);
68 $this->invoiceMock->expects($this->once())
70 ->willReturn([$invoiceItemMock]);
72 $orderItemMock = $this->getOrderItemMock(1, 1,
true);
73 $this->orderMock->expects($this->once())
75 ->willReturn([$orderItemMock]);
76 $this->invoiceMock->expects($this->exactly(2))
77 ->method(
'getOrderId')
81 $this->model->validate($this->invoiceMock)
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())
93 ->willReturn([$invoiceItemMock]);
95 $orderItemMock = $this->getOrderItemMock($orderItemId, 1,
false);
96 $this->orderMock->expects($this->once())
98 ->willReturn([$orderItemMock]);
99 $this->invoiceMock->expects($this->exactly(2))
100 ->method(
'getOrderId')
104 $this->model->validate($this->invoiceMock)
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())
114 ->willReturn([$invoiceItemMock]);
116 $this->orderMock->expects($this->once())
119 $this->invoiceMock->expects($this->exactly(2))
120 ->method(
'getOrderId')
124 $this->model->validate($this->invoiceMock)
130 $expectedResult = [
__(
'Order Id is required for invoice document')];
133 $this->model->validate($this->invoiceMock)
139 $expectedResult = [
__(
"The invoice can't be created without products. Add products and try again.")];
141 $invoiceItemMock = $this->getInvoiceItemMock($orderItemId, 0);
142 $this->invoiceMock->expects($this->once())
144 ->willReturn([$invoiceItemMock]);
146 $orderItemMock = $this->getOrderItemMock($orderItemId, 1,
false);
147 $this->orderMock->expects($this->once())
149 ->willReturn([$orderItemMock]);
150 $this->invoiceMock->expects($this->exactly(2))
151 ->method(
'getOrderId')
155 $this->model->validate($this->invoiceMock)
164 private function getInvoiceItemMock($orderItemId, $qty)
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;
181 private function getOrderItemMock(
$id, $qtyToInvoice, $isDummy)
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;
testValidateNoInvoiceItems()
testValidateNoOrderItems()
testValidateInvoiceQtyBiggerThanOrder()