@@ -65,6 +65,16 @@ class SaveTest extends \PHPUnit\Framework\TestCase
65
65
*/
66
66
protected $ saveController ;
67
67
68
+ /**
69
+ * @var \Magento\Cms\Model\BlockFactory|\PHPUnit_Framework_MockObject_MockObject
70
+ */
71
+ private $ blockFactory ;
72
+
73
+ /**
74
+ * @var \Magento\Cms\Api\BlockRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
75
+ */
76
+ private $ blockRepository ;
77
+
68
78
/**
69
79
* @var int
70
80
*/
@@ -129,11 +139,22 @@ protected function setUp()
129
139
->method ('getResultRedirectFactory ' )
130
140
->willReturn ($ this ->resultRedirectFactory );
131
141
142
+ $ this ->blockFactory = $ this ->getMockBuilder (\Magento \Cms \Model \BlockFactory::class)
143
+ ->disableOriginalConstructor ()
144
+ ->setMethods (['create ' ])
145
+ ->getMock ();
146
+
147
+ $ this ->blockRepository = $ this ->getMockBuilder (\Magento \Cms \Api \BlockRepositoryInterface::class)
148
+ ->disableOriginalConstructor ()
149
+ ->getMockForAbstractClass ();
150
+
132
151
$ this ->saveController = $ this ->objectManager ->getObject (
133
152
\Magento \Cms \Controller \Adminhtml \Block \Save::class,
134
153
[
135
154
'context ' => $ this ->contextMock ,
136
155
'dataPersistor ' => $ this ->dataPersistorMock ,
156
+ 'blockFactory ' => $ this ->blockFactory ,
157
+ 'blockRepository ' => $ this ->blockRepository ,
137
158
]
138
159
);
139
160
}
@@ -158,26 +179,24 @@ public function testSaveAction()
158
179
]
159
180
);
160
181
161
- $ this ->objectManagerMock ->expects ($ this ->atLeastOnce ())
182
+ $ this ->blockFactory ->expects ($ this ->atLeastOnce ())
162
183
->method ('create ' )
163
- ->with ($ this ->equalTo (\Magento \Cms \Model \Block::class))
164
184
->willReturn ($ this ->blockMock );
165
185
166
- $ this ->blockMock ->expects ($ this ->any ())
167
- ->method ('load ' )
168
- ->willReturnSelf ();
169
- $ this ->blockMock ->expects ($ this ->any ())
170
- ->method ('getId ' )
171
- ->willReturn (true );
186
+ $ this ->blockRepository ->expects ($ this ->once ())
187
+ ->method ('getById ' )
188
+ ->with ($ this ->blockId )
189
+ ->willReturn ($ this ->blockMock );
190
+
172
191
$ this ->blockMock ->expects ($ this ->once ())->method ('setData ' );
173
- $ this ->blockMock ->expects ($ this ->once ())->method ('save ' );
192
+ $ this ->blockRepository ->expects ($ this ->once ())->method ('save ' )-> with ( $ this -> blockMock );
174
193
175
194
$ this ->dataPersistorMock ->expects ($ this ->any ())
176
195
->method ('clear ' )
177
196
->with ('cms_block ' );
178
197
179
198
$ this ->messageManagerMock ->expects ($ this ->once ())
180
- ->method ('addSuccess ' )
199
+ ->method ('addSuccessMessage ' )
181
200
->with (__ ('You saved the block. ' ));
182
201
183
202
$ this ->resultRedirect ->expects ($ this ->atLeastOnce ())->method ('setPath ' )->with ('*/*/ ' ) ->willReturnSelf ();
@@ -204,20 +223,17 @@ public function testSaveActionNoId()
204
223
]
205
224
);
206
225
207
- $ this ->objectManagerMock ->expects ($ this ->atLeastOnce ())
226
+ $ this ->blockFactory ->expects ($ this ->atLeastOnce ())
208
227
->method ('create ' )
209
- ->with ($ this ->equalTo (\Magento \Cms \Model \Block::class))
210
228
->willReturn ($ this ->blockMock );
211
229
212
- $ this ->blockMock ->expects ($ this ->any ())
213
- ->method ('load ' )
214
- ->willReturnSelf ();
215
- $ this ->blockMock ->expects ($ this ->any ())
216
- ->method ('getId ' )
217
- ->willReturn (false );
230
+ $ this ->blockRepository ->expects ($ this ->once ())
231
+ ->method ('getById ' )
232
+ ->with ($ this ->blockId )
233
+ ->willThrowException (new \Magento \Framework \Exception \NoSuchEntityException (__ ('Error message ' )));
218
234
219
235
$ this ->messageManagerMock ->expects ($ this ->once ())
220
- ->method ('addError ' )
236
+ ->method ('addErrorMessage ' )
221
237
->with (__ ('This block no longer exists. ' ));
222
238
223
239
$ this ->resultRedirect ->expects ($ this ->atLeastOnce ())->method ('setPath ' )->with ('*/*/ ' ) ->willReturnSelf ();
@@ -237,22 +253,20 @@ public function testSaveAndContinue()
237
253
]
238
254
);
239
255
240
- $ this ->objectManagerMock ->expects ($ this ->atLeastOnce ())
256
+ $ this ->blockFactory ->expects ($ this ->atLeastOnce ())
241
257
->method ('create ' )
242
- ->with ($ this ->equalTo (\Magento \Cms \Model \Block::class))
243
258
->willReturn ($ this ->blockMock );
244
259
245
- $ this ->blockMock ->expects ($ this ->any ())
246
- ->method ('load ' )
247
- ->willReturnSelf ();
248
- $ this ->blockMock ->expects ($ this ->any ())
249
- ->method ('getId ' )
250
- ->willReturn (true );
260
+ $ this ->blockRepository ->expects ($ this ->once ())
261
+ ->method ('getById ' )
262
+ ->with ($ this ->blockId )
263
+ ->willReturn ($ this ->blockMock );
264
+
251
265
$ this ->blockMock ->expects ($ this ->once ())->method ('setData ' );
252
- $ this ->blockMock ->expects ($ this ->once ())->method ('save ' );
266
+ $ this ->blockRepository ->expects ($ this ->once ())->method ('save ' )-> with ( $ this -> blockMock );
253
267
254
268
$ this ->messageManagerMock ->expects ($ this ->once ())
255
- ->method ('addSuccess ' )
269
+ ->method ('addSuccessMessage ' )
256
270
->with (__ ('You saved the block. ' ));
257
271
258
272
$ this ->dataPersistorMock ->expects ($ this ->any ())
@@ -279,24 +293,24 @@ public function testSaveActionThrowsException()
279
293
]
280
294
);
281
295
282
- $ this ->objectManagerMock ->expects ($ this ->atLeastOnce ())
296
+ $ this ->blockFactory ->expects ($ this ->atLeastOnce ())
283
297
->method ('create ' )
284
- ->with ($ this ->equalTo (\Magento \Cms \Model \Block::class))
285
298
->willReturn ($ this ->blockMock );
286
299
287
- $ this ->blockMock ->expects ($ this ->any ())
288
- ->method ('load ' )
289
- ->willReturnSelf ();
290
- $ this ->blockMock ->expects ($ this ->any ())
291
- ->method ('getId ' )
292
- ->willReturn (true );
300
+ $ this ->blockRepository ->expects ($ this ->once ())
301
+ ->method ('getById ' )
302
+ ->with ($ this ->blockId )
303
+ ->willReturn ($ this ->blockMock );
304
+
293
305
$ this ->blockMock ->expects ($ this ->once ())->method ('setData ' );
294
- $ this ->blockMock ->expects ($ this ->once ())->method ('save ' )->willThrowException (new \Exception ('Error message. ' ));
306
+ $ this ->blockRepository ->expects ($ this ->once ())->method ('save ' )
307
+ ->with ($ this ->blockMock )
308
+ ->willThrowException (new \Exception ('Error message. ' ));
295
309
296
310
$ this ->messageManagerMock ->expects ($ this ->never ())
297
- ->method ('addSuccess ' );
311
+ ->method ('addSuccessMessage ' );
298
312
$ this ->messageManagerMock ->expects ($ this ->once ())
299
- ->method ('addException ' );
313
+ ->method ('addExceptionMessage ' );
300
314
301
315
$ this ->dataPersistorMock ->expects ($ this ->any ())
302
316
->method ('set ' )
0 commit comments