/** * @param \Magento\Sales\Service\V1\Data\Invoice $invoiceDataObject * @return bool * @throws \Exception */ public function invoke(\Magento\Sales\Service\V1\Data\Invoice $invoiceDataObject) { try { /** @var \Magento\Sales\Model\Order\Invoice $invoice */ $invoice = $this->invoiceConverter->getModel($invoiceDataObject); if (!$invoice) { return false; } $invoice->register(); $invoice->save(); return true; } catch (\Exception $e) { $this->logger->logException($e); throw new \Exception(__('An error has occurred during creating Invoice')); } }
/** * test for Invoice converter */ public function testGetModel() { $orderId = 1; $invoiceId = 2; $itemId = 3; $itemQty = 4; $this->invoiceMock->expects($this->once())->method('getOrderId')->will($this->returnValue($orderId)); $this->invoiceMock->expects($this->once())->method('getEntityId')->will($this->returnValue($invoiceId)); $this->invoiceMock->expects($this->once())->method('getItems')->will($this->returnValue([$this->invoiceItemMock])); $this->invoiceItemMock->expects($this->once())->method('getOrderItemId')->will($this->returnValue($itemId)); $this->invoiceItemMock->expects($this->once())->method('getQty')->will($this->returnValue($itemQty)); $this->invoiceLoaderMock->expects($this->once())->method('setOrderId')->with($this->equalTo($orderId))->will($this->returnSelf()); $this->invoiceLoaderMock->expects($this->once())->method('setInvoiceId')->with($this->equalTo($invoiceId))->will($this->returnSelf()); $this->invoiceLoaderMock->expects($this->once())->method('setInvoiceItems')->with($this->equalTo([$itemId => $itemQty]))->will($this->returnSelf()); $this->invoiceLoaderMock->expects($this->once())->method('create')->will($this->returnValue($this->modelInvoiceMock)); $this->invoiceLoaderMock->expects($this->once())->method('create')->will($this->returnValue($this->modelInvoiceMock)); $this->assertInstanceOf('Magento\\Sales\\Model\\Order\\Invoice', $this->converter->getModel($this->invoiceMock)); }