Skip to content

Commit 9050f94

Browse files
author
Yuri Kovsher
committed
MAGETWO-36727: Refactor controller actions in GroupedProduct & ImportExport modules
1 parent b0c7944 commit 9050f94

File tree

2 files changed

+52
-24
lines changed
  • app/code/Magento/GroupedProduct

2 files changed

+52
-24
lines changed

app/code/Magento/GroupedProduct/Controller/Adminhtml/Edit/Popup.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\GroupedProduct\Controller\Adminhtml\Edit;
87

9-
class Popup extends \Magento\Backend\App\AbstractAction
8+
use Magento\Backend\App\AbstractAction;
9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Framework\Registry;
11+
use Magento\Catalog\Model\ProductFactory;
12+
use Psr\Log\LoggerInterface;
13+
use Magento\Framework\Controller\ResultFactory;
14+
15+
class Popup extends AbstractAction
1016
{
1117
/**
1218
* @var \Magento\Framework\Registry
@@ -30,10 +36,10 @@ class Popup extends \Magento\Backend\App\AbstractAction
3036
* @param \Psr\Log\LoggerInterface $logger
3137
*/
3238
public function __construct(
33-
\Magento\Backend\App\Action\Context $context,
34-
\Magento\Framework\Registry $registry,
35-
\Magento\Catalog\Model\ProductFactory $factory,
36-
\Psr\Log\LoggerInterface $logger
39+
Context $context,
40+
Registry $registry,
41+
ProductFactory $factory,
42+
LoggerInterface $logger
3743
) {
3844
$this->registry = $registry;
3945
$this->factory = $factory;
@@ -54,7 +60,7 @@ protected function _isAllowed()
5460
/**
5561
* Get associated grouped products grid popup
5662
*
57-
* @return void
63+
* @return \Magento\Framework\View\Result\Layout
5864
*/
5965
public function execute()
6066
{
@@ -84,8 +90,8 @@ public function execute()
8490
$product->setAttributeSetId($setId);
8591
}
8692
$this->registry->register('current_product', $product);
87-
88-
$this->_view->loadLayout(false);
89-
$this->_view->renderLayout();
93+
/** @var \Magento\Framework\View\Result\Layout $resultLayout */
94+
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
95+
return $resultLayout;
9096
}
9197
}

app/code/Magento/GroupedProduct/Test/Unit/Controller/Adminhtml/Edit/PopupTest.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\GroupedProduct\Test\Unit\Controller\Adminhtml\Edit;
77

8+
use Magento\Framework\Controller\ResultFactory;
9+
810
class PopupTest extends \PHPUnit_Framework_TestCase
911
{
1012
/**
@@ -17,6 +19,11 @@ class PopupTest extends \PHPUnit_Framework_TestCase
1719
*/
1820
protected $action;
1921

22+
/**
23+
* @var \Magento\Backend\App\Action\Context
24+
*/
25+
protected $context;
26+
2027
/**
2128
* @var \PHPUnit_Framework_MockObject_MockObject
2229
*/
@@ -33,25 +40,46 @@ class PopupTest extends \PHPUnit_Framework_TestCase
3340
protected $registry;
3441

3542
/**
36-
* @var \PHPUnit_Framework_MockObject_MockObject
43+
* @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
protected $resultFactoryMock;
46+
47+
/**
48+
* @var \Magento\Framework\View\Result\Layout|\PHPUnit_Framework_MockObject_MockObject
3749
*/
38-
protected $view;
50+
protected $resultLayoutMock;
3951

4052
protected function setUp()
4153
{
4254
$this->request = $this->getMock('Magento\Framework\App\RequestInterface', [], [], '', false);
4355
$this->factory = $this->getMock('Magento\Catalog\Model\ProductFactory', ['create'], [], '', false);
4456
$this->registry = $this->getMock('Magento\Framework\Registry', [], [], '', false);
45-
$this->view = $this->getMock('Magento\Framework\App\ViewInterface', [], [], '', false);
57+
$this->resultFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\ResultFactory')
58+
->disableOriginalConstructor()
59+
->getMock();
60+
$this->resultLayoutMock = $this->getMockBuilder('Magento\Framework\View\Result\Layout')
61+
->disableOriginalConstructor()
62+
->getMock();
63+
64+
$this->resultFactoryMock->expects($this->any())
65+
->method('create')
66+
->with(ResultFactory::TYPE_LAYOUT, [])
67+
->willReturn($this->resultLayoutMock);
4668

4769
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
70+
$this->context = $this->objectManager->getObject(
71+
'Magento\Backend\App\Action\Context',
72+
[
73+
'request' => $this->request,
74+
'resultFactory' => $this->resultFactoryMock
75+
]
76+
);
4877
$this->action = $this->objectManager->getObject(
4978
'Magento\GroupedProduct\Controller\Adminhtml\Edit\Popup',
5079
[
51-
'request' => $this->request,
80+
'context' => $this->context,
5281
'factory' => $this->factory,
53-
'registry' => $this->registry,
54-
'view' => $this->view
82+
'registry' => $this->registry
5583
]
5684
);
5785
}
@@ -90,10 +118,7 @@ public function testPopupActionNoProductId()
90118
$this->request->expects($this->at(3))->method('getParam')->with('set')->will($this->returnValue($setId));
91119
$this->registry->expects($this->once())->method('register')->with('current_product', $product);
92120

93-
$this->view->expects($this->once())->method('loadLayout')->with(false);
94-
$this->view->expects($this->once())->method('renderLayout');
95-
96-
$this->action->execute();
121+
$this->assertSame($this->resultLayoutMock, $this->action->execute());
97122
}
98123

99124
public function testPopupActionWithProductIdNoSetId()
@@ -130,9 +155,6 @@ public function testPopupActionWithProductIdNoSetId()
130155
$this->request->expects($this->at(3))->method('getParam')->with('set')->will($this->returnValue($setId));
131156
$this->registry->expects($this->once())->method('register')->with('current_product', $product);
132157

133-
$this->view->expects($this->once())->method('loadLayout')->with(false);
134-
$this->view->expects($this->once())->method('renderLayout');
135-
136-
$this->action->execute();
158+
$this->assertSame($this->resultLayoutMock, $this->action->execute());
137159
}
138160
}

0 commit comments

Comments
 (0)