Skip to content

Commit 3561da7

Browse files
authored
ENGCOM-3831: Refactor \Order\Shipment\Save Controller to use ResultInterface #20057
2 parents 5306a5b + 2e2a5d3 commit 3561da7

File tree

2 files changed

+31
-51
lines changed
  • app/code/Magento/Shipping
    • Controller/Adminhtml/Order/Shipment
    • Test/Unit/Controller/Adminhtml/Order/Shipment

2 files changed

+31
-51
lines changed

app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
77
namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment;
88

9-
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
10-
use Magento\Backend\App\Action;
9+
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
use Magento\Framework\Controller\ResultFactory;
1111
use Magento\Sales\Model\Order\Shipment\Validation\QuantityValidator;
1212

1313
/**
@@ -48,17 +48,22 @@ class Save extends \Magento\Backend\App\Action implements HttpPostActionInterfac
4848
* @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
4949
* @param \Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator
5050
* @param \Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender
51+
* @param \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface|null $shipmentValidator
5152
*/
5253
public function __construct(
5354
\Magento\Backend\App\Action\Context $context,
5455
\Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader,
5556
\Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator,
56-
\Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender
57+
\Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender,
58+
\Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface $shipmentValidator = null
5759
) {
60+
parent::__construct($context);
61+
5862
$this->shipmentLoader = $shipmentLoader;
5963
$this->labelGenerator = $labelGenerator;
6064
$this->shipmentSender = $shipmentSender;
61-
parent::__construct($context);
65+
$this->shipmentValidator = $shipmentValidator ?: \Magento\Framework\App\ObjectManager::getInstance()
66+
->get(\Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface::class);
6267
}
6368

6469
/**
@@ -84,9 +89,10 @@ protected function _saveShipment($shipment)
8489

8590
/**
8691
* Save shipment
92+
*
8793
* We can save only new shipment. Existing shipments are not editable
8894
*
89-
* @return void
95+
* @return \Magento\Framework\Controller\ResultInterface
9096
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
9197
* @SuppressWarnings(PHPMD.NPathComplexity)
9298
*/
@@ -98,7 +104,7 @@ public function execute()
98104
$formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest());
99105
$isPost = $this->getRequest()->isPost();
100106
if (!$formKeyIsValid || !$isPost) {
101-
$this->messageManager->addError(__('We can\'t save the shipment right now.'));
107+
$this->messageManager->addErrorMessage(__('We can\'t save the shipment right now.'));
102108
return $resultRedirect->setPath('sales/order/index');
103109
}
104110

@@ -118,8 +124,7 @@ public function execute()
118124
$this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
119125
$shipment = $this->shipmentLoader->load();
120126
if (!$shipment) {
121-
$this->_forward('noroute');
122-
return;
127+
return $this->resultFactory->create(ResultFactory::TYPE_FORWARD)->forward('noroute');
123128
}
124129

125130
if (!empty($data['comment_text'])) {
@@ -132,15 +137,13 @@ public function execute()
132137
$shipment->setCustomerNote($data['comment_text']);
133138
$shipment->setCustomerNoteNotify(isset($data['comment_customer_notify']));
134139
}
135-
$validationResult = $this->getShipmentValidator()
136-
->validate($shipment, [QuantityValidator::class]);
140+
$validationResult = $this->shipmentValidator->validate($shipment, [QuantityValidator::class]);
137141

138142
if ($validationResult->hasMessages()) {
139-
$this->messageManager->addError(
143+
$this->messageManager->addErrorMessage(
140144
__("Shipment Document Validation Error(s):\n" . implode("\n", $validationResult->getMessages()))
141145
);
142-
$this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
143-
return;
146+
return $resultRedirect->setPath('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
144147
}
145148
$shipment->register();
146149

@@ -160,7 +163,7 @@ public function execute()
160163
$shipmentCreatedMessage = __('The shipment has been created.');
161164
$labelCreatedMessage = __('You created the shipping label.');
162165

163-
$this->messageManager->addSuccess(
166+
$this->messageManager->addSuccessMessage(
164167
$isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage
165168
);
166169
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->getCommentText(true);
@@ -169,38 +172,23 @@ public function execute()
169172
$responseAjax->setError(true);
170173
$responseAjax->setMessage($e->getMessage());
171174
} else {
172-
$this->messageManager->addError($e->getMessage());
173-
$this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
175+
$this->messageManager->addErrorMessage($e->getMessage());
176+
return $resultRedirect->setPath('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
174177
}
175178
} catch (\Exception $e) {
176179
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
177180
if ($isNeedCreateLabel) {
178181
$responseAjax->setError(true);
179182
$responseAjax->setMessage(__('An error occurred while creating shipping label.'));
180183
} else {
181-
$this->messageManager->addError(__('Cannot save shipment.'));
182-
$this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
184+
$this->messageManager->addErrorMessage(__('Cannot save shipment.'));
185+
return $resultRedirect->setPath('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
183186
}
184187
}
185188
if ($isNeedCreateLabel) {
186-
$this->getResponse()->representJson($responseAjax->toJson());
187-
} else {
188-
$this->_redirect('sales/order/view', ['order_id' => $shipment->getOrderId()]);
189-
}
190-
}
191-
192-
/**
193-
* @return \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface
194-
* @deprecated 100.1.1
195-
*/
196-
private function getShipmentValidator()
197-
{
198-
if ($this->shipmentValidator === null) {
199-
$this->shipmentValidator = $this->_objectManager->get(
200-
\Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface::class
201-
);
189+
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setJsonData($responseAjax->toJson());
202190
}
203191

204-
return $this->shipmentValidator;
192+
return $resultRedirect->setPath('sales/order/view', ['order_id' => $shipment->getOrderId()]);
205193
}
206194
}

app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function setUp()
142142
);
143143
$this->messageManager = $this->createPartialMock(
144144
\Magento\Framework\Message\Manager::class,
145-
['addSuccess', 'addError']
145+
['addSuccessMessage', 'addErrorMessage']
146146
);
147147
$this->session = $this->createPartialMock(
148148
\Magento\Backend\Model\Session::class,
@@ -236,7 +236,7 @@ public function testExecute($formKeyIsValid, $isPost)
236236

237237
if (!$formKeyIsValid || !$isPost) {
238238
$this->messageManager->expects($this->once())
239-
->method('addError');
239+
->method('addErrorMessage');
240240

241241
$this->resultRedirect->expects($this->once())
242242
->method('setPath')
@@ -325,12 +325,11 @@ public function testExecute($formKeyIsValid, $isPost)
325325
->method('get')
326326
->with(\Magento\Backend\Model\Session::class)
327327
->will($this->returnValue($this->session));
328-
$path = 'sales/order/view';
329328
$arguments = ['order_id' => $orderId];
330329
$shipment->expects($this->once())
331330
->method('getOrderId')
332331
->will($this->returnValue($orderId));
333-
$this->prepareRedirect($path, $arguments);
332+
$this->prepareRedirect($arguments);
334333

335334
$this->shipmentValidatorMock->expects($this->once())
336335
->method('validate')
@@ -360,10 +359,9 @@ public function executeDataProvider()
360359
}
361360

362361
/**
363-
* @param string $path
364362
* @param array $arguments
365363
*/
366-
protected function prepareRedirect($path, array $arguments = [])
364+
protected function prepareRedirect(array $arguments = [])
367365
{
368366
$this->actionFlag->expects($this->any())
369367
->method('get')
@@ -372,14 +370,8 @@ protected function prepareRedirect($path, array $arguments = [])
372370
$this->session->expects($this->any())
373371
->method('setIsUrlNotice')
374372
->with(true);
375-
376-
$url = $path . '/' . (!empty($arguments) ? $arguments['order_id'] : '');
377-
$this->helper->expects($this->atLeastOnce())
378-
->method('getUrl')
379-
->with($path, $arguments)
380-
->will($this->returnValue($url));
381-
$this->response->expects($this->atLeastOnce())
382-
->method('setRedirect')
383-
->with($url);
373+
$this->resultRedirect->expects($this->once())
374+
->method('setPath')
375+
->with('sales/order/view', $arguments);
384376
}
385377
}

0 commit comments

Comments
 (0)