@@ -65,6 +65,16 @@ class SaveTest extends \PHPUnit\Framework\TestCase
6565 */
6666 protected $ saveController ;
6767
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+
6878 /**
6979 * @var int
7080 */
@@ -129,11 +139,22 @@ protected function setUp()
129139 ->method ('getResultRedirectFactory ' )
130140 ->willReturn ($ this ->resultRedirectFactory );
131141
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+
132151 $ this ->saveController = $ this ->objectManager ->getObject (
133152 \Magento \Cms \Controller \Adminhtml \Block \Save::class,
134153 [
135154 'context ' => $ this ->contextMock ,
136155 'dataPersistor ' => $ this ->dataPersistorMock ,
156+ 'blockFactory ' => $ this ->blockFactory ,
157+ 'blockRepository ' => $ this ->blockRepository ,
137158 ]
138159 );
139160 }
@@ -158,26 +179,24 @@ public function testSaveAction()
158179 ]
159180 );
160181
161- $ this ->objectManagerMock ->expects ($ this ->atLeastOnce ())
182+ $ this ->blockFactory ->expects ($ this ->atLeastOnce ())
162183 ->method ('create ' )
163- ->with ($ this ->equalTo (\Magento \Cms \Model \Block::class))
164184 ->willReturn ($ this ->blockMock );
165185
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+
172191 $ 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 );
174193
175194 $ this ->dataPersistorMock ->expects ($ this ->any ())
176195 ->method ('clear ' )
177196 ->with ('cms_block ' );
178197
179198 $ this ->messageManagerMock ->expects ($ this ->once ())
180- ->method ('addSuccess ' )
199+ ->method ('addSuccessMessage ' )
181200 ->with (__ ('You saved the block. ' ));
182201
183202 $ this ->resultRedirect ->expects ($ this ->atLeastOnce ())->method ('setPath ' )->with ('*/*/ ' ) ->willReturnSelf ();
@@ -204,20 +223,17 @@ public function testSaveActionNoId()
204223 ]
205224 );
206225
207- $ this ->objectManagerMock ->expects ($ this ->atLeastOnce ())
226+ $ this ->blockFactory ->expects ($ this ->atLeastOnce ())
208227 ->method ('create ' )
209- ->with ($ this ->equalTo (\Magento \Cms \Model \Block::class))
210228 ->willReturn ($ this ->blockMock );
211229
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 ' )));
218234
219235 $ this ->messageManagerMock ->expects ($ this ->once ())
220- ->method ('addError ' )
236+ ->method ('addErrorMessage ' )
221237 ->with (__ ('This block no longer exists. ' ));
222238
223239 $ this ->resultRedirect ->expects ($ this ->atLeastOnce ())->method ('setPath ' )->with ('*/*/ ' ) ->willReturnSelf ();
@@ -237,22 +253,20 @@ public function testSaveAndContinue()
237253 ]
238254 );
239255
240- $ this ->objectManagerMock ->expects ($ this ->atLeastOnce ())
256+ $ this ->blockFactory ->expects ($ this ->atLeastOnce ())
241257 ->method ('create ' )
242- ->with ($ this ->equalTo (\Magento \Cms \Model \Block::class))
243258 ->willReturn ($ this ->blockMock );
244259
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+
251265 $ 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 );
253267
254268 $ this ->messageManagerMock ->expects ($ this ->once ())
255- ->method ('addSuccess ' )
269+ ->method ('addSuccessMessage ' )
256270 ->with (__ ('You saved the block. ' ));
257271
258272 $ this ->dataPersistorMock ->expects ($ this ->any ())
@@ -279,24 +293,24 @@ public function testSaveActionThrowsException()
279293 ]
280294 );
281295
282- $ this ->objectManagerMock ->expects ($ this ->atLeastOnce ())
296+ $ this ->blockFactory ->expects ($ this ->atLeastOnce ())
283297 ->method ('create ' )
284- ->with ($ this ->equalTo (\Magento \Cms \Model \Block::class))
285298 ->willReturn ($ this ->blockMock );
286299
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+
293305 $ 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. ' ));
295309
296310 $ this ->messageManagerMock ->expects ($ this ->never ())
297- ->method ('addSuccess ' );
311+ ->method ('addSuccessMessage ' );
298312 $ this ->messageManagerMock ->expects ($ this ->once ())
299- ->method ('addException ' );
313+ ->method ('addExceptionMessage ' );
300314
301315 $ this ->dataPersistorMock ->expects ($ this ->any ())
302316 ->method ('set ' )
0 commit comments