@@ -81,7 +81,9 @@ protected function setUp()
81
81
->willReturn ($ this ->resultRedirect );
82
82
$ this ->dataProcessorMock = $ this ->getMockBuilder (
83
83
\Magento \Cms \Controller \Adminhtml \Page \PostDataProcessor::class
84
- )->setMethods (['filter ' ])->disableOriginalConstructor ()->getMock ();
84
+ )->disableOriginalConstructor ()
85
+ ->setMethods (['filter ' ])
86
+ ->getMock ();
85
87
$ this ->dataPersistorMock = $ this ->getMockBuilder (\Magento \Framework \App \Request \DataPersistorInterface::class)
86
88
->getMock ();
87
89
$ this ->requestMock = $ this ->getMockBuilder (\Magento \Framework \App \RequestInterface::class)
@@ -132,34 +134,19 @@ public function testSaveAction()
132
134
'content ' => '"><script>alert("cookie: "+document.cookie)</script> '
133
135
];
134
136
137
+ $ params = [
138
+ ['page_id ' , null , $ this ->pageId ],
139
+ ['back ' , null , false ],
140
+ ];
141
+
135
142
$ this ->dataProcessorMock ->expects ($ this ->any ())
136
143
->method ('filter ' )
137
144
->with ($ postData )
138
145
->willReturn ($ filteredPostData );
139
146
140
- $ this ->requestMock ->expects ($ this ->any ())->method ('getPostValue ' )->willReturn ($ postData );
141
- $ this ->requestMock ->expects ($ this ->atLeastOnce ())
142
- ->method ('getParam ' )
143
- ->willReturnMap (
144
- [
145
- ['page_id ' , null , $ this ->pageId ],
146
- ['back ' , null , false ],
147
- ]
148
- );
149
- $ page = $ this ->getMockBuilder (\Magento \Cms \Model \Page::class)
150
- ->disableOriginalConstructor ()
151
- ->getMock ();
152
- $ this ->pageFactory ->expects ($ this ->atLeastOnce ())
153
- ->method ('create ' )
154
- ->willReturn ($ page );
155
-
156
- $ page ->expects ($ this ->any ())
157
- ->method ('load ' )
158
- ->willReturnSelf ();
159
- $ page ->expects ($ this ->any ())
160
- ->method ('getId ' )
161
- ->willReturn (true );
162
- $ page ->expects ($ this ->once ())->method ('setData ' );
147
+ $ this ->processRequest ($ postData , $ params );
148
+ /** @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject $cmsPage */
149
+ $ page = $ this ->getPageMock ();
163
150
$ this ->pageRepository ->expects ($ this ->once ())->method ('save ' )->with ($ page );
164
151
165
152
$ this ->dataPersistorMock ->expects ($ this ->any ())
@@ -184,39 +171,25 @@ public function testSaveActionWithoutData()
184
171
185
172
public function testSaveAndContinue ()
186
173
{
187
- $ this ->requestMock ->expects ($ this ->any ())->method ('getPostValue ' )->willReturn (['page_id ' => $ this ->pageId ]);
188
- $ this ->requestMock ->expects ($ this ->atLeastOnce ())
189
- ->method ('getParam ' )
190
- ->willReturnMap (
191
- [
192
- ['page_id ' , null , $ this ->pageId ],
193
- ['back ' , null , true ],
194
- ]
195
- );
174
+ $ postData = ['page_id ' => $ this ->pageId ];
175
+ $ params = [
176
+ ['page_id ' , null , $ this ->pageId ],
177
+ ['back ' , null , true ],
178
+ ];
179
+
180
+ $ this ->processRequest ($ postData , $ params );
196
181
197
182
$ this ->dataProcessorMock ->expects ($ this ->any ())
198
183
->method ('filter ' )
199
184
->willReturnArgument (0 );
200
- $ page = $ this ->getMockBuilder (\Magento \Cms \Model \Page::class)
201
- ->disableOriginalConstructor ()
202
- ->getMock ();
203
- $ this ->pageFactory ->expects ($ this ->atLeastOnce ())
204
- ->method ('create ' )
205
- ->willReturn ($ page );
185
+ /** @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject $cmsPage */
186
+ $ page = $ this ->getPageMock ();
206
187
207
- $ page ->expects ($ this ->any ())
208
- ->method ('load ' )
209
- ->willReturnSelf ();
210
- $ page ->expects ($ this ->any ())
211
- ->method ('getId ' )
212
- ->willReturn (true );
213
- $ page ->expects ($ this ->once ())->method ('setData ' );
214
188
$ this ->pageRepository ->expects ($ this ->once ())->method ('save ' )->with ($ page );
215
189
216
190
$ this ->messageManagerMock ->expects ($ this ->once ())
217
191
->method ('addSuccess ' )
218
192
->with (__ ('You saved the page. ' ));
219
-
220
193
$ this ->dataPersistorMock ->expects ($ this ->any ())
221
194
->method ('clear ' )
222
195
->with ('cms_page ' );
@@ -231,33 +204,19 @@ public function testSaveAndContinue()
231
204
232
205
public function testSaveActionThrowsException ()
233
206
{
234
- $ this ->requestMock ->expects ($ this ->any ())->method ('getPostValue ' )->willReturn (['page_id ' => $ this ->pageId ]);
235
- $ this ->requestMock ->expects ($ this ->atLeastOnce ())
236
- ->method ('getParam ' )
237
- ->willReturnMap (
238
- [
239
- ['page_id ' , null , $ this ->pageId ],
240
- ['back ' , null , true ],
241
- ]
242
- );
207
+ $ postData = ['page_id ' => $ this ->pageId ];
208
+ $ params = [
209
+ ['page_id ' , null , $ this ->pageId ],
210
+ ['back ' , null , true ],
211
+ ];
212
+
213
+ $ this ->processRequest ($ postData , $ params );
243
214
244
215
$ this ->dataProcessorMock ->expects ($ this ->any ())
245
216
->method ('filter ' )
246
217
->willReturnArgument (0 );
247
- $ page = $ this ->getMockBuilder (\Magento \Cms \Model \Page::class)
248
- ->disableOriginalConstructor ()
249
- ->getMock ();
250
- $ this ->pageFactory ->expects ($ this ->atLeastOnce ())
251
- ->method ('create ' )
252
- ->willReturn ($ page );
253
-
254
- $ page ->expects ($ this ->any ())
255
- ->method ('load ' )
256
- ->willReturnSelf ();
257
- $ page ->expects ($ this ->any ())
258
- ->method ('getId ' )
259
- ->willReturn (true );
260
- $ page ->expects ($ this ->once ())->method ('setData ' );
218
+ /** @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject $cmsPage */
219
+ $ page = $ this ->getPageMock ();
261
220
$ this ->pageRepository ->expects ($ this ->once ())
262
221
->method ('save ' )
263
222
->with ($ page )
@@ -279,4 +238,44 @@ public function testSaveActionThrowsException()
279
238
280
239
$ this ->assertSame ($ this ->resultRedirect , $ this ->saveController ->execute ());
281
240
}
241
+
242
+ /**
243
+ * Create Cms Page Mock.
244
+ *
245
+ * @return \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject
246
+ */
247
+ private function getPageMock ()
248
+ {
249
+ $ page = $ this ->getMockBuilder (\Magento \Cms \Model \Page::class)
250
+ ->disableOriginalConstructor ()
251
+ ->getMock ();
252
+ $ this ->pageFactory ->expects ($ this ->atLeastOnce ())
253
+ ->method ('create ' )
254
+ ->willReturn ($ page );
255
+
256
+ $ page ->expects ($ this ->any ())
257
+ ->method ('load ' )
258
+ ->willReturnSelf ();
259
+ $ page ->expects ($ this ->any ())
260
+ ->method ('getId ' )
261
+ ->willReturn (true );
262
+ $ page ->expects ($ this ->once ())->method ('setData ' );
263
+
264
+ return $ page ;
265
+ }
266
+
267
+ /**
268
+ * Process save page action request.
269
+ *
270
+ * @param array $postData
271
+ * @param array $params
272
+ * @return void
273
+ */
274
+ private function processRequest ($ postData , $ params )
275
+ {
276
+ $ this ->requestMock ->expects ($ this ->any ())->method ('getPostValue ' )->willReturn ($ postData );
277
+ $ this ->requestMock ->expects ($ this ->atLeastOnce ())
278
+ ->method ('getParam ' )
279
+ ->willReturnMap ($ params );
280
+ }
282
281
}
0 commit comments