Skip to content

Commit 2b80526

Browse files
author
Maksym Aposov
committed
MAGETWO-35824: [MX] [Dragons] Code coverage - Sprint 67
- MAGETWO-36987: Coverage or refactoring method - Magento\Search\Controller\Adminhtml\Term\Save::execute
1 parent c71831f commit 2b80526

File tree

2 files changed

+36
-13
lines changed
  • app/code/Magento/Search

2 files changed

+36
-13
lines changed

app/code/Magento/Search/Controller/Adminhtml/Term/Save.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,31 @@
66
*/
77
namespace Magento\Search\Controller\Adminhtml\Term;
88

9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Framework\View\Result\PageFactory;
11+
use Magento\Search\Model\QueryFactory;
12+
913
class Save extends \Magento\Search\Controller\Adminhtml\Term
1014
{
15+
/**
16+
* @var QueryFactory
17+
*/
18+
private $queryFactory;
19+
20+
/**
21+
* @param Context $context
22+
* @param PageFactory $resultPageFactory
23+
* @param QueryFactory $queryFactory
24+
*/
25+
public function __construct(
26+
Context $context,
27+
PageFactory $resultPageFactory,
28+
QueryFactory $queryFactory
29+
) {
30+
parent::__construct($context, $resultPageFactory);
31+
$this->queryFactory = $queryFactory;
32+
}
33+
1134
/**
1235
* Save search query
1336
*
@@ -51,7 +74,7 @@ private function loadQuery()
5174
$queryId = $this->getRequest()->getPost('query_id', null);
5275

5376
/* @var $model \Magento\Search\Model\Query */
54-
$model = $this->_objectManager->create('Magento\Search\Model\Query');
77+
$model = $this->queryFactory->create();
5578
if ($queryText) {
5679
$storeId = $this->getRequest()->getPost('store_id', false);
5780
$model->setStoreId($storeId);
@@ -71,7 +94,7 @@ private function loadQuery()
7194
/**
7295
* Redirect to Edit page
7396
*
74-
* @param $data
97+
* @param array $data
7598
* @return \Magento\Backend\Model\View\Result\Redirect
7699
*/
77100
private function proceedToEdit($data)

app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/SaveTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ class SaveTest extends \PHPUnit_Framework_TestCase
1616
/** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
1717
private $redirect;
1818

19-
/** @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
20-
private $objectManager;
21-
2219
/** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
2320
private $messageManager;
2421

@@ -65,17 +62,17 @@ protected function setUp()
6562
->method('getRequest')
6663
->willReturn($this->request);
6764

68-
$this->objectManager = $this->getMockBuilder('\Magento\Framework\ObjectManagerInterface')
65+
$objectManager = $this->getMockBuilder('\Magento\Framework\ObjectManagerInterface')
6966
->disableOriginalConstructor()
7067
->setMethods(['create'])
7168
->getMockForAbstractClass();
7269
$this->context->expects($this->any())
7370
->method('getObjectManager')
74-
->willReturn($this->objectManager);
71+
->willReturn($objectManager);
7572

7673
$this->messageManager = $this->getMockBuilder('\Magento\Framework\Message\ManagerInterface')
7774
->disableOriginalConstructor()
78-
->setMethods(['addSuccess', 'addError' ,'addException'])
75+
->setMethods(['addSuccess', 'addError', 'addException'])
7976
->getMockForAbstractClass();
8077
$this->context->expects($this->any())
8178
->method('getMessageManager')
@@ -98,12 +95,20 @@ protected function setUp()
9895
->disableOriginalConstructor()
9996
->setMethods(['getId', 'load', 'addData', 'setIsProcessed', 'save', 'loadByQueryText', 'setStoreId'])
10097
->getMock();
98+
$queryFactory = $this->getMockBuilder('Magento\Search\Model\QueryFactory')
99+
->setMethods(['create'])
100+
->disableOriginalConstructor()
101+
->getMock();
102+
$queryFactory->expects($this->any())
103+
->method('create')
104+
->will($this->returnValue($this->query));
101105

102106
$this->controller = $objectManagerHelper->getObject(
103107
'Magento\Search\Controller\Adminhtml\Term\Save',
104108
[
105109
'context' => $this->context,
106110
'resultPageFactory' => $pageFactory,
111+
'queryFactory' => $queryFactory,
107112
]
108113
);
109114
}
@@ -138,7 +143,6 @@ public function testExecuteLoadQueryQueryId()
138143
$queryText = '';
139144
$this->mockGetRequestData($queryText, $queryId);
140145

141-
$this->objectManager->expects($this->once())->method('create')->willReturn($this->query);
142146
$this->query->expects($this->once())->method('getId')->willReturn(false);
143147
$this->query->expects($this->once())->method('load')->with($queryId);
144148

@@ -154,7 +158,6 @@ public function testExecuteLoadQueryQueryIdQueryText()
154158
$queryText = 'search';
155159
$this->mockGetRequestData($queryText, $queryId);
156160

157-
$this->objectManager->expects($this->once())->method('create')->willReturn($this->query);
158161
$this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
159162

160163
$this->query->expects($this->once())->method('setStoreId');
@@ -173,7 +176,6 @@ public function testExecuteLoadQueryQueryIdQueryText2()
173176
$queryText = 'search';
174177
$this->mockGetRequestData($queryText, $queryId);
175178

176-
$this->objectManager->expects($this->once())->method('create')->willReturn($this->query);
177179
$this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
178180

179181
$this->query->expects($this->once())->method('setStoreId');
@@ -194,7 +196,6 @@ public function testExecuteLoadQueryQueryIdQueryTextException()
194196
$queryText = 'search';
195197
$this->mockGetRequestData($queryText, $queryId);
196198

197-
$this->objectManager->expects($this->once())->method('create')->willReturn($this->query);
198199
$this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
199200

200201
$this->query->expects($this->once())->method('setStoreId');
@@ -213,7 +214,6 @@ public function testExecuteException()
213214
$queryText = 'search';
214215
$this->mockGetRequestData($queryText, $queryId);
215216

216-
$this->objectManager->expects($this->once())->method('create')->willReturn($this->query);
217217
$this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
218218

219219
$this->query->expects($this->once())->method('setStoreId');

0 commit comments

Comments
 (0)