Skip to content

Commit dd4d301

Browse files
author
vpaladiychuk
committed
MAGETWO-34995: Refactor controllers from the list (Part2)
1 parent 97b1936 commit dd4d301

File tree

3 files changed

+62
-38
lines changed
  • app/code/Magento
    • Sales/Controller/Adminhtml/Transactions
    • Shipping

3 files changed

+62
-38
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Transactions/Fetch.php

100644100755
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ public function execute()
2424
if (!$txn) {
2525
return $resultRedirect->setPath('sales/*/');
2626
}
27-
try {
28-
$txn->getOrderPaymentObject()->setOrder($txn->getOrder())->importTransactionInfo($txn);
29-
$txn->save();
30-
$this->messageManager->addSuccess(__('The transaction details have been updated.'));
31-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
32-
$this->messageManager->addError($e->getMessage());
33-
} catch (\Exception $e) {
34-
$this->messageManager->addError(__('We can\'t update the transaction details.'));
35-
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
36-
}
27+
$txn->getOrderPaymentObject()->setOrder($txn->getOrder())->importTransactionInfo($txn);
28+
$txn->save();
29+
$this->messageManager->addSuccess(__('The transaction details have been updated.'));
30+
return $this->getDefaultRedirect();
31+
}
32+
33+
/**
34+
* @return Redirect
35+
*/
36+
public function getDefaultRedirect()
37+
{
38+
$resultRedirect = $this->resultRedirectFactory->create();
3739
return $resultRedirect->setPath('sales/transactions/view', ['_current' => true]);
3840
}
3941
}

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

100644100755
Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,32 @@ protected function _isAllowed()
4545
/**
4646
* Send email with shipment data to customer
4747
*
48-
* @return void
48+
* @return \Magento\Framework\Controller\Result\Redirect
4949
*/
5050
public function execute()
5151
{
52-
try {
53-
$this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
54-
$this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
55-
$this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment'));
56-
$this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
57-
$shipment = $this->shipmentLoader->load();
58-
if ($shipment) {
59-
$this->_objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
60-
->notify($shipment);
61-
$shipment->save();
62-
$this->messageManager->addSuccess(__('You sent the shipment.'));
63-
}
64-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
65-
$this->messageManager->addError($e->getMessage());
66-
} catch (\Exception $e) {
67-
$this->messageManager->addError(__('Cannot send shipment information.'));
52+
$this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
53+
$this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
54+
$this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment'));
55+
$this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
56+
$shipment = $this->shipmentLoader->load();
57+
if ($shipment) {
58+
$this->_objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
59+
->notify($shipment);
60+
$shipment->save();
61+
$this->messageManager->addSuccess(__('You sent the shipment.'));
6862
}
69-
$this->_redirect('*/*/view', ['shipment_id' => $this->getRequest()->getParam('shipment_id')]);
63+
return $this->getDefaultRedirect();
64+
}
65+
66+
/**
67+
* @inheritdoc
68+
*
69+
* @return \Magento\Framework\Controller\Result\Redirect
70+
*/
71+
public function getDefaultRedirect()
72+
{
73+
$resultRedirect = $this->resultRedirectFactory->create();
74+
return $resultRedirect->setPath('*/*/view', ['shipment_id' => $this->getRequest()->getParam('shipment_id')]);
7075
}
7176
}

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

100644100755
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ class EmailTest extends \PHPUnit_Framework_TestCase
6363
*/
6464
protected $helper;
6565

66+
/**
67+
* @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
68+
*/
69+
protected $resultRedirectFactory;
70+
71+
/**
72+
* @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
73+
*/
74+
protected $resultRedirect;
75+
6676
/**
6777
* @var \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader|\PHPUnit_Framework_MockObject_MockObject
6878
*/
@@ -88,7 +98,8 @@ public function setUp()
8898
'getObjectManager',
8999
'getSession',
90100
'getActionFlag',
91-
'getHelper'
101+
'getHelper',
102+
'getResultRedirectFactory'
92103
],
93104
[],
94105
'',
@@ -129,6 +140,15 @@ public function setUp()
129140
$this->session = $this->getMock('Magento\Backend\Model\Session', ['setIsUrlNotice'], [], '', false);
130141
$this->actionFlag = $this->getMock('Magento\Framework\App\ActionFlag', ['get'], [], '', false);
131142
$this->helper = $this->getMock('\Magento\Backend\Helper\Data', ['getUrl'], [], '', false);
143+
$this->resultRedirect = $this->getMock('Magento\Framework\Controller\Result\Redirect', [], [], '', false);
144+
$this->resultRedirectFactory = $this->getMock(
145+
'Magento\Framework\Controller\Result\RedirectFactory',
146+
['create'],
147+
[],
148+
'',
149+
false
150+
);
151+
$this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($this->resultRedirect);
132152
$this->context->expects($this->once())
133153
->method('getMessageManager')
134154
->will($this->returnValue($this->messageManager));
@@ -150,6 +170,9 @@ public function setUp()
150170
$this->context->expects($this->once())
151171
->method('getHelper')
152172
->will($this->returnValue($this->helper));
173+
$this->context->expects($this->once())
174+
->method('getResultRedirectFactory')
175+
->willReturn($this->resultRedirectFactory);
153176
$this->shipmentEmail = $objectManagerHelper->getObject(
154177
'Magento\Shipping\Controller\Adminhtml\Order\Shipment\Email',
155178
[
@@ -240,14 +263,8 @@ protected function prepareRedirect($path, $arguments, $index)
240263
$this->session->expects($this->any())
241264
->method('setIsUrlNotice')
242265
->with(true);
243-
244-
$url = $path . '/' . (!empty($arguments) ? $arguments['shipment_id'] : '');
245-
$this->helper->expects($this->at($index))
246-
->method('getUrl')
247-
->with($path, $arguments)
248-
->will($this->returnValue($url));
249-
$this->response->expects($this->at($index))
250-
->method('setRedirect')
251-
->with($url);
266+
$this->resultRedirect->expects($this->at($index))
267+
->method('setPath')
268+
->with($path, ['shipment_id' => $arguments['shipment_id']]);
252269
}
253270
}

0 commit comments

Comments
 (0)