@@ -293,37 +293,36 @@ public function testDispatchExceptionNoCallback()
293
293
}
294
294
295
295
/**
296
- * Test case catch exception if callback exists but can't be executed
296
+ * Test case catch exception if callback is not callable or throws exception
297
+ *
298
+ * @param string $cronJobType
299
+ * @param mixed $cronJobObject
300
+ * @param string $exceptionMessage
301
+ * @param int $saveCalls
302
+ *
303
+ * @dataProvider dispatchExceptionInCallbackDataProvider
297
304
*/
298
- public function testDispatchExceptionNotExecutable ( )
305
+ public function testDispatchExceptionInCallback ( $ cronJobType , $ cronJobObject , $ exceptionMessage , $ saveCalls )
299
306
{
300
307
$ jobConfig = [
301
308
'test_group ' => [
302
- 'test_job1 ' => ['instance ' => ' Not_Existed_Class ' , 'method ' => 'notExistedMethod ' ],
309
+ 'test_job1 ' => ['instance ' => $ cronJobType , 'method ' => 'execute ' ],
303
310
],
304
311
];
305
312
306
- $ exceptionMessage = 'Invalid callback: Not_Existed_Class::notExistedMethod can \'t be called ' ;
307
313
$ this ->_request ->expects ($ this ->any ())->method ('getParam ' )->will ($ this ->returnValue ('test_group ' ));
308
- $ schedule = $ this ->getMockBuilder (
309
- 'Magento\Cron\Model\Schedule '
310
- )->setMethods (
311
- ['getJobCode ' , 'tryLockJob ' , 'getScheduledAt ' , 'save ' , 'setStatus ' , 'setMessages ' , '__wakeup ' ]
312
- )->disableOriginalConstructor ()->getMock ();
314
+ $ schedule = $ this ->getMockBuilder ('Magento\Cron\Model\Schedule ' )
315
+ ->setMethods (['getJobCode ' , 'tryLockJob ' , 'getScheduledAt ' , 'save ' , 'setStatus ' , 'setMessages ' , '__wakeup ' ])
316
+ ->disableOriginalConstructor ()->getMock ();
313
317
$ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->will ($ this ->returnValue ('test_job1 ' ));
314
318
$ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue ('-1 day ' ));
315
319
$ schedule ->expects ($ this ->once ())->method ('tryLockJob ' )->will ($ this ->returnValue (true ));
316
- $ schedule ->expects (
317
- $ this ->once ()
318
- )->method (
319
- 'setStatus '
320
- )->with (
321
- $ this ->equalTo (\Magento \Cron \Model \Schedule::STATUS_ERROR )
322
- )->will (
323
- $ this ->returnSelf ()
324
- );
320
+ $ schedule ->expects ($ this ->once ())
321
+ ->method ('setStatus ' )
322
+ ->with ($ this ->equalTo (\Magento \Cron \Model \Schedule::STATUS_ERROR ))
323
+ ->will ($ this ->returnSelf ());
325
324
$ schedule ->expects ($ this ->once ())->method ('setMessages ' )->with ($ this ->equalTo ($ exceptionMessage ));
326
- $ schedule ->expects ($ this ->once ( ))->method ('save ' );
325
+ $ schedule ->expects ($ this ->exactly ( $ saveCalls ))->method ('save ' );
327
326
328
327
$ this ->_collection ->addItem ($ schedule );
329
328
@@ -336,25 +335,41 @@ public function testDispatchExceptionNotExecutable()
336
335
$ scheduleMock = $ this ->getMockBuilder ('Magento\Cron\Model\Schedule ' )->disableOriginalConstructor ()->getMock ();
337
336
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
338
337
$ this ->_scheduleFactory ->expects ($ this ->once ())->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
339
- $ this ->_objectManager ->expects (
340
- $ this ->once ()
341
- )->method (
342
- 'create '
343
- )->with (
344
- $ this ->equalTo ('Not_Existed_Class ' )
345
- )->will (
346
- $ this ->returnValue ('' )
347
- );
338
+ $ this ->_objectManager
339
+ ->expects ($ this ->once ())
340
+ ->method ('create ' )
341
+ ->with ($ this ->equalTo ($ cronJobType ))
342
+ ->will ($ this ->returnValue ($ cronJobObject ));
348
343
349
344
$ this ->_observer ->dispatch ('' );
350
345
}
351
346
347
+ /**
348
+ * @return array
349
+ */
350
+ public function dispatchExceptionInCallbackDataProvider ()
351
+ {
352
+ return [
353
+ 'non-callable callback ' => [
354
+ 'Not_Existed_Class ' ,
355
+ '' ,
356
+ 'Invalid callback: Not_Existed_Class::execute can \'t be called ' ,
357
+ 1
358
+ ],
359
+ 'exception in execution ' => [
360
+ 'CronJobException ' ,
361
+ new \Magento \Cron \Test \Unit \Model \CronJobException (),
362
+ 'Test exception ' ,
363
+ 2
364
+ ],
365
+ ];
366
+ }
367
+
352
368
/**
353
369
* Test case, successfully run job
354
370
*/
355
371
public function testDispatchRunJob ()
356
372
{
357
- require_once __DIR__ . '/CronJob.php ' ;
358
373
$ testCronJob = new \Magento \Cron \Test \Unit \Model \CronJob ();
359
374
360
375
$ jobConfig = [
0 commit comments