Skip to content

Commit 243e6a8

Browse files
author
Yurii Torbyk
committed
MAGETWO-34993: Refactor controllers from the list (Part1)
1 parent e72b280 commit 243e6a8

File tree

2 files changed

+125
-172
lines changed

2 files changed

+125
-172
lines changed

app/code/Magento/Integration/Test/Unit/Controller/Adminhtml/Integration/DeleteTest.php

Lines changed: 114 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -14,144 +14,109 @@
1414

1515
class DeleteTest extends \Magento\Integration\Test\Unit\Controller\Adminhtml\IntegrationTest
1616
{
17+
/**
18+
* @var \Magento\Integration\Controller\Adminhtml\Integration\Delete
19+
*/
20+
protected $integrationContr;
21+
22+
protected function setUp()
23+
{
24+
parent::setUp();
25+
26+
$this->integrationContr = $this->_createIntegrationController('Delete');
27+
28+
$resultRedirect = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect')
29+
->disableOriginalConstructor()
30+
->getMock();
31+
$resultRedirect->expects($this->any())
32+
->method('setPath')
33+
->with('*/*/')
34+
->willReturnSelf();
35+
36+
$this->resultRedirectFactory->expects($this->any())
37+
->method('create')
38+
->willReturn($resultRedirect);
39+
}
40+
1741
public function testDeleteAction()
1842
{
1943
$intData = $this->_getSampleIntegrationData();
20-
$this->_requestMock->expects(
21-
$this->once()
22-
)->method(
23-
'getParam'
24-
)->will(
25-
$this->returnValue(self::INTEGRATION_ID)
26-
);
27-
$this->_integrationSvcMock->expects(
28-
$this->any()
29-
)->method(
30-
'get'
31-
)->with(
32-
$this->anything()
33-
)->will(
34-
$this->returnValue($intData)
35-
);
36-
$this->_integrationSvcMock->expects(
37-
$this->any()
38-
)->method(
39-
'delete'
40-
)->with(
41-
$this->anything()
42-
)->will(
43-
$this->returnValue($intData)
44-
);
44+
$this->_requestMock->expects($this->once())
45+
->method('getParam')
46+
->willReturn(self::INTEGRATION_ID);
47+
$this->_integrationSvcMock->expects($this->any())
48+
->method('get')
49+
->with($this->anything())
50+
->willReturn($intData);
51+
$this->_integrationSvcMock->expects($this->any())
52+
->method('delete')
53+
->with($this->anything())
54+
->willReturn($intData);
4555
// Use real translate model
4656
$this->_translateModelMock = null;
4757
// verify success message
48-
$this->_messageManager->expects(
49-
$this->once()
50-
)->method(
51-
'addSuccess'
52-
)->with(
53-
__('The integration \'%1\' has been deleted.', $intData[Info::DATA_NAME])
54-
);
55-
$integrationContr = $this->_createIntegrationController('Delete');
56-
$integrationContr->execute();
58+
$this->_messageManager->expects($this->once())
59+
->method('addSuccess')
60+
->with(__('The integration \'%1\' has been deleted.', $intData[Info::DATA_NAME]));
61+
62+
$this->integrationContr->execute();
5763
}
5864

5965
public function testDeleteActionWithConsumer()
6066
{
6167
$intData = $this->_getSampleIntegrationData();
6268
$intData[Info::DATA_CONSUMER_ID] = 1;
63-
$this->_requestMock->expects(
64-
$this->once()
65-
)->method(
66-
'getParam'
67-
)->will(
68-
$this->returnValue(self::INTEGRATION_ID)
69-
);
70-
$this->_integrationSvcMock->expects(
71-
$this->any()
72-
)->method(
73-
'get'
74-
)->with(
75-
$this->anything()
76-
)->will(
77-
$this->returnValue($intData)
78-
);
79-
$this->_integrationSvcMock->expects(
80-
$this->once()
81-
)->method(
82-
'delete'
83-
)->with(
84-
$this->anything()
85-
)->will(
86-
$this->returnValue($intData)
87-
);
88-
$this->_oauthSvcMock->expects(
89-
$this->once()
90-
)->method(
91-
'deleteConsumer'
92-
)->with(
93-
$this->anything()
94-
)->will(
95-
$this->returnValue($intData)
96-
);
69+
$this->_requestMock->expects($this->once())
70+
->method('getParam')
71+
->willReturn(self::INTEGRATION_ID);
72+
$this->_integrationSvcMock->expects($this->any())
73+
->method('get')
74+
->with($this->anything())
75+
->willReturn($intData);
76+
$this->_integrationSvcMock->expects($this->once())
77+
->method('delete')
78+
->with($this->anything())
79+
->willReturn($intData);
80+
$this->_oauthSvcMock->expects($this->once())
81+
->method('deleteConsumer')
82+
->with($this->anything())
83+
->willReturn($intData);
9784
// Use real translate model
9885
$this->_translateModelMock = null;
9986
// verify success message
100-
$this->_messageManager->expects(
101-
$this->once()
102-
)->method(
103-
'addSuccess'
104-
)->with(
105-
__('The integration \'%1\' has been deleted.', $intData[Info::DATA_NAME])
106-
);
107-
$integrationContr = $this->_createIntegrationController('Delete');
108-
$integrationContr->execute();
87+
$this->_messageManager->expects($this->once())
88+
->method('addSuccess')
89+
->with(__('The integration \'%1\' has been deleted.', $intData[Info::DATA_NAME]));
90+
91+
$this->integrationContr->execute();
10992
}
11093

11194
public function testDeleteActionConfigSetUp()
11295
{
11396
$intData = $this->_getSampleIntegrationData();
11497
$intData[Info::DATA_SETUP_TYPE] = IntegrationModel::TYPE_CONFIG;
115-
$this->_requestMock->expects(
116-
$this->once()
117-
)->method(
118-
'getParam'
119-
)->will(
120-
$this->returnValue(self::INTEGRATION_ID)
121-
);
122-
$this->_integrationSvcMock->expects(
123-
$this->any()
124-
)->method(
125-
'get'
126-
)->with(
127-
$this->anything()
128-
)->will(
129-
$this->returnValue($intData)
130-
);
131-
$this->_integrationHelperMock->expects(
132-
$this->once()
133-
)->method(
134-
'isConfigType'
135-
)->with(
136-
$intData
137-
)->will(
138-
$this->returnValue(true)
139-
);
98+
$this->_requestMock->expects($this->once())
99+
->method('getParam')
100+
->willReturn(self::INTEGRATION_ID);
101+
$this->_integrationSvcMock->expects($this->any())
102+
->method('get')
103+
->with($this->anything())
104+
->willReturn($intData);
105+
$this->_integrationHelperMock->expects($this->once())
106+
->method('isConfigType')
107+
->with($intData)
108+
->willReturn(true);
140109
// verify error message
141-
$this->_messageManager->expects(
142-
$this->once()
143-
)->method(
144-
'addError'
145-
)->with(
146-
__('Uninstall the extension to remove integration \'%1\'.', $intData[Info::DATA_NAME])
147-
);
110+
$this->_messageManager->expects($this->once())
111+
->method('addError')
112+
->with(__('Uninstall the extension to remove integration \'%1\'.', $intData[Info::DATA_NAME]));
148113
$this->_integrationSvcMock->expects($this->never())->method('delete');
149114
// Use real translate model
150115
$this->_translateModelMock = null;
151116
// verify success message
152117
$this->_messageManager->expects($this->never())->method('addSuccess');
153-
$integrationContr = $this->_createIntegrationController('Delete');
154-
$integrationContr->execute();
118+
119+
$this->integrationContr->execute();
155120
}
156121

157122
public function testDeleteActionMissingId()
@@ -161,85 +126,62 @@ public function testDeleteActionMissingId()
161126
// Use real translate model
162127
$this->_translateModelMock = null;
163128
// verify error message
164-
$this->_messageManager->expects(
165-
$this->once()
166-
)->method(
167-
'addError'
168-
)->with(
169-
__('Integration ID is not specified or is invalid.')
170-
);
171-
$integrationContr = $this->_createIntegrationController('Delete');
172-
$integrationContr->execute();
129+
$this->_messageManager->expects($this->once())
130+
->method('addError')
131+
->with(__('Integration ID is not specified or is invalid.'));
132+
133+
$this->integrationContr->execute();
173134
}
174135

136+
/**
137+
* @expectedException \Exception
138+
* @expectedExceptionMessage Integration with ID '1' doesn't exist.
139+
*/
175140
public function testDeleteActionForServiceIntegrationException()
176141
{
177142
$intData = $this->_getSampleIntegrationData();
178-
$this->_integrationSvcMock->expects(
179-
$this->any()
180-
)->method(
181-
'get'
182-
)->with(
183-
$this->anything()
184-
)->will(
185-
$this->returnValue($intData)
186-
);
187-
$this->_requestMock->expects(
188-
$this->once()
189-
)->method(
190-
'getParam'
191-
)->will(
192-
$this->returnValue(self::INTEGRATION_ID)
193-
);
143+
$this->_integrationSvcMock->expects($this->any())
144+
->method('get')
145+
->with($this->anything())
146+
->willReturn($intData);
147+
$this->_requestMock->expects($this->once())
148+
->method('getParam')
149+
->willReturn(self::INTEGRATION_ID);
194150
// Use real translate model
195151
$this->_translateModelMock = null;
196152
$exceptionMessage = __("Integration with ID '%1' doesn't exist.", $intData[Info::DATA_ID]);
197153
$invalidIdException = new IntegrationException($exceptionMessage);
198-
$this->_integrationSvcMock->expects(
199-
$this->once()
200-
)->method(
201-
'delete'
202-
)->will(
203-
$this->throwException($invalidIdException)
204-
);
205-
$this->_messageManager->expects($this->once())->method('addError')->with($exceptionMessage);
206-
$integrationContr = $this->_createIntegrationController('Delete');
207-
$integrationContr->execute();
154+
$this->_integrationSvcMock->expects($this->once())
155+
->method('delete')
156+
->willThrowException($invalidIdException);
157+
$this->_messageManager->expects($this->never())->method('addError');
158+
159+
$this->integrationContr->execute();
208160
}
209161

162+
/**
163+
* @expectedException \Exception
164+
* @expectedExceptionMessage Integration with ID '1' doesn't exist.
165+
*/
210166
public function testDeleteActionForServiceGenericException()
211167
{
212168
$intData = $this->_getSampleIntegrationData();
213-
$this->_integrationSvcMock->expects(
214-
$this->any()
215-
)->method(
216-
'get'
217-
)->with(
218-
$this->anything()
219-
)->will(
220-
$this->returnValue($intData)
221-
);
222-
$this->_requestMock->expects(
223-
$this->once()
224-
)->method(
225-
'getParam'
226-
)->will(
227-
$this->returnValue(self::INTEGRATION_ID)
228-
);
169+
$this->_integrationSvcMock->expects($this->any())
170+
->method('get')
171+
->with($this->anything())
172+
->willReturn($intData);
173+
$this->_requestMock->expects($this->once())
174+
->method('getParam')
175+
->willReturn(self::INTEGRATION_ID);
229176
// Use real translate model
230177
$this->_translateModelMock = null;
231178
$exceptionMessage = __("Integration with ID '%1' doesn't exist.", $intData[Info::DATA_ID]);
232179
$invalidIdException = new \Exception($exceptionMessage);
233-
$this->_integrationSvcMock->expects(
234-
$this->once()
235-
)->method(
236-
'delete'
237-
)->will(
238-
$this->throwException($invalidIdException)
239-
);
240-
//Generic Exception(non-Service) should never add the message in session for user display
180+
$this->_integrationSvcMock->expects($this->once())
181+
->method('delete')
182+
->willThrowException($invalidIdException);
241183
$this->_messageManager->expects($this->never())->method('addError');
242-
$integrationContr = $this->_createIntegrationController('Delete');
243-
$integrationContr->execute();
184+
185+
$this->integrationContr->execute();
244186
}
245187
}

app/code/Magento/Integration/Test/Unit/Controller/Adminhtml/IntegrationTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ abstract class IntegrationTest extends \PHPUnit_Framework_TestCase
9696
*/
9797
protected $_escaper;
9898

99+
/**
100+
* @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
101+
*/
102+
protected $resultRedirectFactory;
103+
99104
/** Sample integration ID */
100105
const INTEGRATION_ID = 1;
101106

@@ -210,6 +215,11 @@ protected function _createIntegrationController($actionName)
210215
->willReturn($this->pageTitleMock);
211216
$this->_escaper->expects($this->any())->method('escapeHtml')->will($this->returnArgument(0));
212217

218+
$this->resultRedirectFactory = $this->getMockBuilder('Magento\Backend\Model\View\Result\RedirectFactory')
219+
->disableOriginalConstructor()
220+
->setMethods(['create'])
221+
->getMock();
222+
213223
$contextParameters = [
214224
'view' => $this->_viewMock,
215225
'objectManager' => $this->_objectManagerMock,
@@ -218,6 +228,7 @@ protected function _createIntegrationController($actionName)
218228
'request' => $this->_requestMock,
219229
'response' => $this->_responseMock,
220230
'messageManager' => $this->_messageManager,
231+
'resultRedirectFactory' => $this->resultRedirectFactory
221232
];
222233

223234
$this->_backendActionCtxMock = $this->_objectManagerHelper->getObject(

0 commit comments

Comments
 (0)