/** * @covers \Magento\Shipping\Model\Shipping\LabelGenerator * @param array $info * @dataProvider labelInfoDataProvider */ public function testAddTrackingNumbersToShipment(array $info) { $order = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->getMock(); $order->expects(static::once())->method('getShippingMethod')->with(true)->willReturn($this->getShippingMethodMock()); /** * @var $shipmentMock \Magento\Sales\Model\Order\Shipment|\PHPUnit_Framework_MockObject_MockObject */ $shipmentMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Shipment')->disableOriginalConstructor()->getMock(); $shipmentMock->expects(static::once())->method('getOrder')->willReturn($order); $this->carrierFactory->expects(static::once())->method('create')->with(self::CARRIER_CODE)->willReturn($this->getCarrierMock()); $labelsMock = $this->getMockBuilder('\\Magento\\Shipping\\Model\\Shipping\\Labels')->disableOriginalConstructor()->getMock(); $labelsMock->expects(static::once())->method('requestToShipment')->with($shipmentMock)->willReturn($this->getResponseMock($info)); $this->labelsFactory->expects(static::once())->method('create')->willReturn($labelsMock); $this->filesystem->expects(static::once())->method('getDirectoryWrite')->willReturn($this->getMock('Magento\\Framework\\Filesystem\\Directory\\WriteInterface')); $this->scopeConfig->expects(static::once())->method('getValue')->with('carriers/' . self::CARRIER_CODE . '/title', ScopeInterface::SCOPE_STORE, null)->willReturn(self::CARRIER_TITLE); $this->labelsFactory->expects(static::once())->method('create')->willReturn($labelsMock); $trackMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Shipment\\Track')->setMethods(['setNumber', 'setCarrierCode', 'setTitle'])->disableOriginalConstructor()->getMock(); $i = 0; $trackingNumbers = is_array($info['tracking_number']) ? $info['tracking_number'] : [$info['tracking_number']]; foreach ($trackingNumbers as $trackingNumber) { $trackMock->expects(static::at($i++))->method('setNumber')->with($trackingNumber)->willReturnSelf(); $trackMock->expects(static::at($i++))->method('setCarrierCode')->with(self::CARRIER_CODE)->willReturnSelf(); $trackMock->expects(static::at($i++))->method('setTitle')->with(self::CARRIER_TITLE)->willReturnSelf(); } $this->trackFactory->expects(static::any())->method('create')->willReturn($trackMock); /** * @var $requestMock \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ $requestMock = $this->getMock('Magento\\Framework\\App\\RequestInterface'); $this->labelGenerator->create($shipmentMock, $requestMock); }
/** * Save shipment * We can save only new shipment. Existing shipments are not editable * * @return void */ public function execute() { $data = $this->getRequest()->getPost('shipment'); if (!empty($data['comment_text'])) { $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCommentText($data['comment_text']); } try { $shipment = $this->shipmentLoader->load($this->_request); if (!$shipment) { $this->_forward('noroute'); return; } $shipment->register(); $comment = ''; if (!empty($data['comment_text'])) { $shipment->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front'])); if (isset($data['comment_customer_notify'])) { $comment = $data['comment_text']; } } if (!empty($data['send_email'])) { $shipment->setEmailSent(true); } $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); $responseAjax = new \Magento\Framework\Object(); $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label']; if ($isNeedCreateLabel && $this->labelGenerator->create($shipment, $this->_request)) { $responseAjax->setOk(true); } $this->_saveShipment($shipment); $shipment->sendEmail(!empty($data['send_email']), $comment); $shipmentCreatedMessage = __('The shipment has been created.'); $labelCreatedMessage = __('You created the shipping label.'); $this->messageManager->addSuccess($isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage); $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true); } catch (\Magento\Framework\Model\Exception $e) { if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage($e->getMessage()); } else { $this->messageManager->addError($e->getMessage()); $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id'))); } } catch (\Exception $e) { $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e); if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage(__('An error occurred while creating shipping label.')); } else { $this->messageManager->addError(__('Cannot save shipment.')); $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id'))); } } if ($isNeedCreateLabel) { $this->getResponse()->representJson($responseAjax->toJson()); } else { $this->_redirect('sales/order/view', array('order_id' => $shipment->getOrderId())); } }
/** * Create shipping label action for specific shipment * * @return void */ public function execute() { $response = new \Magento\Framework\Object(); try { $shipment = $this->shipmentLoader->load($this->_request); if ($this->labelGenerator->create($shipment, $this->_request)) { $shipment->save(); $this->messageManager->addSuccess(__('You created the shipping label.')); $response->setOk(true); } } catch (\Magento\Framework\Model\Exception $e) { $response->setError(true); $response->setMessage($e->getMessage()); } catch (\Exception $e) { $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e); $response->setError(true); $response->setMessage(__('An error occurred while creating shipping label.')); } $this->getResponse()->representJson($response->toJson()); }
/** * Create shipping label action for specific shipment * * @return void */ public function execute() { $response = new \Magento\Framework\DataObject(); try { $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id')); $this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment')); $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking')); $shipment = $this->shipmentLoader->load(); $this->labelGenerator->create($shipment, $this->_request); $shipment->save(); $this->messageManager->addSuccess(__('You created the shipping label.')); $response->setOk(true); } catch (\Magento\Framework\Exception\LocalizedException $e) { $response->setError(true); $response->setMessage($e->getMessage()); } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); $response->setError(true); $response->setMessage(__('An error occurred while creating shipping label.')); } $this->getResponse()->representJson($response->toJson()); }
/** * Save shipment * We can save only new shipment. Existing shipments are not editable * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute() { /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); $formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest()); $isPost = $this->getRequest()->isPost(); if (!$formKeyIsValid || !$isPost) { $this->messageManager->addError(__('We can\'t save the shipment right now.')); return $resultRedirect->setPath('sales/order/index'); } $data = $this->getRequest()->getParam('shipment'); if (!empty($data['comment_text'])) { $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCommentText($data['comment_text']); } try { $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id')); $this->shipmentLoader->setShipment($data); $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking')); $shipment = $this->shipmentLoader->load(); if (!$shipment) { $this->_forward('noroute'); return; } if (!empty($data['comment_text'])) { $shipment->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front'])); $shipment->setCustomerNote($data['comment_text']); $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } $shipment->register(); $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); $responseAjax = new \Magento\Framework\DataObject(); $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label']; if ($isNeedCreateLabel) { $this->labelGenerator->create($shipment, $this->_request); $responseAjax->setOk(true); } $this->_saveShipment($shipment); if (!empty($data['send_email'])) { $this->shipmentSender->send($shipment); } $shipmentCreatedMessage = __('The shipment has been created.'); $labelCreatedMessage = __('You created the shipping label.'); $this->messageManager->addSuccess($isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage); $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true); } catch (\Magento\Framework\Exception\LocalizedException $e) { if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage($e->getMessage()); } else { $this->messageManager->addError($e->getMessage()); $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]); } } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage(__('An error occurred while creating shipping label.')); } else { $this->messageManager->addError(__('Cannot save shipment.')); $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]); } } if ($isNeedCreateLabel) { $this->getResponse()->representJson($responseAjax->toJson()); } else { $this->_redirect('sales/order/view', ['order_id' => $shipment->getOrderId()]); } }