8
8
use Magento \Cron \Model \Schedule ;
9
9
use Magento \Cron \Observer \ProcessCronQueueObserver as ProcessCronQueueObserver ;
10
10
use Magento \Framework \App \State ;
11
+ use Magento \Framework \Profiler \Driver \Standard \StatFactory ;
12
+ use Magento \Framework \Profiler \Driver \Standard \Stat ;
11
13
12
14
/**
13
15
* Class \Magento\Cron\Test\Unit\Model\ObserverTest
@@ -121,6 +123,7 @@ protected function setUp()
121
123
)->disableOriginalConstructor ()->getMock ();
122
124
$ this ->_collection ->expects ($ this ->any ())->method ('addFieldToFilter ' )->will ($ this ->returnSelf ());
123
125
$ this ->_collection ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnSelf ());
126
+
124
127
$ this ->_scheduleFactory = $ this ->getMockBuilder (
125
128
\Magento \Cron \Model \ScheduleFactory::class
126
129
)->setMethods (
@@ -170,6 +173,15 @@ protected function setUp()
170
173
$ this ->scheduleResource ->method ('getConnection ' )->willReturn ($ connection );
171
174
$ connection ->method ('delete ' )->willReturn (1 );
172
175
176
+ $ this ->statFactory = $ this ->getMockBuilder (StatFactory::class)
177
+ ->setMethods (['create ' ])
178
+ ->getMockForAbstractClass ();
179
+
180
+ $ this ->stat = $ this ->getMockBuilder (\Magento \Framework \Profiler \Driver \Standard \Stat::class)
181
+ ->disableOriginalConstructor ()
182
+ ->getMock ();
183
+ $ this ->statFactory ->expects ($ this ->any ())->method ('create ' )->willReturn ($ this ->stat );
184
+
173
185
$ this ->_observer = new ProcessCronQueueObserver (
174
186
$ this ->_objectManager ,
175
187
$ this ->_scheduleFactory ,
@@ -182,58 +194,38 @@ protected function setUp()
182
194
$ phpExecutableFinderFactory ,
183
195
$ this ->loggerMock ,
184
196
$ this ->appStateMock ,
197
+ $ this ->statFactory ,
185
198
$ this ->lockManagerMock
186
199
);
187
200
}
188
201
189
- /**
190
- * Test case without saved cron jobs in data base
191
- */
192
- public function testDispatchNoPendingJobs ()
193
- {
194
- $ lastRun = $ this ->time + 10000000 ;
195
- $ this ->_cache ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnValue ($ lastRun ));
196
- $ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
197
-
198
- $ this ->_config ->expects ($ this ->once ())->method ('getJobs ' )->will ($ this ->returnValue ([]));
199
-
200
- $ scheduleMock = $ this ->getMockBuilder (
201
- \Magento \Cron \Model \Schedule::class
202
- )->disableOriginalConstructor ()->getMock ();
203
- $ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
204
- $ this ->_scheduleFactory ->expects ($ this ->once ())->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
205
-
206
- $ this ->_observer ->execute ($ this ->observer );
207
- }
208
-
209
202
/**
210
203
* Test case for not existed cron jobs in files but in data base is presented
211
204
*/
212
205
public function testDispatchNoJobConfig ()
213
206
{
214
207
$ lastRun = $ this ->time + 10000000 ;
215
- $ this ->_cache ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnValue ($ lastRun ));
216
- $ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
208
+ $ this ->_cache ->expects ($ this ->atLeastOnce ())->method ('load ' )->will ($ this ->returnValue ($ lastRun ));
209
+ $ this ->_scopeConfig ->expects ($ this ->atLeastOnce ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
217
210
218
211
$ this ->_config ->expects (
219
- $ this ->any ()
212
+ $ this ->atLeastOnce ()
220
213
)->method (
221
214
'getJobs '
222
215
)->will (
223
216
$ this ->returnValue (['test_job1 ' => ['test_data ' ]])
224
217
);
225
218
226
219
$ schedule = $ this ->createPartialMock (\Magento \Cron \Model \Schedule::class, ['getJobCode ' , '__wakeup ' ]);
227
- $ schedule ->expects ($ this ->once ())->method ('getJobCode ' )->will ($ this ->returnValue ('not_existed_job_code ' ));
220
+ $ schedule ->expects ($ this ->atLeastOnce ())->method ('getJobCode ' )->will ($ this ->returnValue ('not_existed_job_code ' ));
228
221
229
222
$ this ->_collection ->addItem ($ schedule );
230
223
231
224
$ scheduleMock = $ this ->getMockBuilder (
232
225
\Magento \Cron \Model \Schedule::class
233
226
)->disableOriginalConstructor ()->getMock ();
234
- $ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
235
- $ scheduleMock ->expects ($ this ->any ())->method ('getResource ' )->will ($ this ->returnValue ($ this ->scheduleResource ));
236
- $ this ->_scheduleFactory ->expects ($ this ->any ())->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
227
+ $ scheduleMock ->expects ($ this ->atLeastOnce ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
228
+ $ this ->_scheduleFactory ->expects ($ this ->atLeastOnce ())->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
237
229
238
230
$ this ->_observer ->execute ($ this ->observer );
239
231
}
@@ -252,11 +244,13 @@ public function testDispatchCanNotLock()
252
244
$ schedule = $ this ->getMockBuilder (
253
245
\Magento \Cron \Model \Schedule::class
254
246
)->setMethods (
255
- ['getJobCode ' , 'tryLockJob ' , 'getScheduledAt ' , '__wakeup ' , 'save ' ]
247
+ ['getJobCode ' , 'tryLockJob ' , 'getScheduledAt ' , '__wakeup ' , 'save ' , ' setFinishedAt ' ]
256
248
)->disableOriginalConstructor ()->getMock ();
257
249
$ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->will ($ this ->returnValue ('test_job1 ' ));
258
- $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
250
+ $ schedule ->expects ($ this ->atLeastOnce ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
259
251
$ schedule ->expects ($ this ->once ())->method ('tryLockJob ' )->will ($ this ->returnValue (false ));
252
+ $ schedule ->expects ($ this ->never ())->method ('setFinishedAt ' );
253
+
260
254
$ abstractModel = $ this ->createMock (\Magento \Framework \Model \AbstractModel::class);
261
255
$ schedule ->expects ($ this ->any ())->method ('save ' )->will ($ this ->returnValue ($ abstractModel ));
262
256
$ this ->_collection ->addItem ($ schedule );
@@ -274,7 +268,7 @@ public function testDispatchCanNotLock()
274
268
)->disableOriginalConstructor ()->getMock ();
275
269
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
276
270
$ scheduleMock ->expects ($ this ->any ())->method ('getResource ' )->will ($ this ->returnValue ($ this ->scheduleResource ));
277
- $ this ->_scheduleFactory ->expects ($ this ->exactly ( 2 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
271
+ $ this ->_scheduleFactory ->expects ($ this ->atLeastOnce ( ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
278
272
279
273
$ this ->_observer ->execute ($ this ->observer );
280
274
}
@@ -284,7 +278,7 @@ public function testDispatchCanNotLock()
284
278
*/
285
279
public function testDispatchExceptionTooLate ()
286
280
{
287
- $ exceptionMessage = 'Too late for the schedule ' ;
281
+ $ exceptionMessage = 'Cron Job test_job1 is missed at 2017-07-30 15:00:00 ' ;
288
282
$ scheduleId = 42 ;
289
283
$ jobCode = 'test_job1 ' ;
290
284
$ exception = $ exceptionMessage . ' Schedule Id: ' . $ scheduleId . ' Job Code: ' . $ jobCode ;
@@ -311,25 +305,25 @@ public function testDispatchExceptionTooLate()
311
305
'getScheduleId ' ,
312
306
]
313
307
)->disableOriginalConstructor ()->getMock ();
314
- $ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->willReturn ($ jobCode );
315
- $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->willReturn ($ dateScheduledAt );
308
+ $ schedule ->expects ($ this ->atLeastOnce ())->method ('getJobCode ' )->willReturn ($ jobCode );
309
+ $ schedule ->expects ($ this ->atLeastOnce ())->method ('getScheduledAt ' )->willReturn ($ dateScheduledAt );
316
310
$ schedule ->expects ($ this ->once ())->method ('tryLockJob ' )->willReturn (true );
317
311
$ schedule ->expects (
318
- $ this ->once ()
312
+ $ this ->any ()
319
313
)->method (
320
314
'setStatus '
321
315
)->with (
322
316
$ this ->equalTo (\Magento \Cron \Model \Schedule::STATUS_MISSED )
323
317
)->willReturnSelf ();
324
318
$ schedule ->expects ($ this ->once ())->method ('setMessages ' )->with ($ this ->equalTo ($ exceptionMessage ));
325
- $ schedule ->expects ($ this ->any ())->method ('getStatus ' )->willReturn (Schedule::STATUS_MISSED );
326
- $ schedule ->expects ($ this ->once ())->method ('getMessages ' )->willReturn ($ exceptionMessage );
327
- $ schedule ->expects ($ this ->once ())->method ('getScheduleId ' )->willReturn ($ scheduleId );
319
+ $ schedule ->expects ($ this ->atLeastOnce ())->method ('getStatus ' )->willReturn (Schedule::STATUS_MISSED );
320
+ $ schedule ->expects ($ this ->atLeastOnce ())->method ('getMessages ' )->willReturn ($ exceptionMessage );
328
321
$ schedule ->expects ($ this ->once ())->method ('save ' );
329
322
330
323
$ this ->appStateMock ->expects ($ this ->once ())->method ('getMode ' )->willReturn (State::MODE_DEVELOPER );
331
324
332
- $ this ->loggerMock ->expects ($ this ->once ())->method ('info ' )->with ($ exception );
325
+ $ this ->loggerMock ->expects ($ this ->once ())->method ('info ' )
326
+ ->with ('Cron Job test_job1 is missed at 2017-07-30 15:00:00 ' );
333
327
334
328
$ this ->_collection ->addItem ($ schedule );
335
329
@@ -345,7 +339,7 @@ public function testDispatchExceptionTooLate()
345
339
->disableOriginalConstructor ()->getMock ();
346
340
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->willReturn ($ this ->_collection );
347
341
$ scheduleMock ->expects ($ this ->any ())->method ('getResource ' )->will ($ this ->returnValue ($ this ->scheduleResource ));
348
- $ this ->_scheduleFactory ->expects ($ this ->exactly ( 2 ))->method ('create ' )->willReturn ($ scheduleMock );
342
+ $ this ->_scheduleFactory ->expects ($ this ->atLeastOnce ( ))->method ('create ' )->willReturn ($ scheduleMock );
349
343
350
344
$ this ->_observer ->execute ($ this ->observer );
351
345
}
@@ -400,7 +394,7 @@ public function testDispatchExceptionNoCallback()
400
394
)->disableOriginalConstructor ()->getMock ();
401
395
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
402
396
$ scheduleMock ->expects ($ this ->any ())->method ('getResource ' )->will ($ this ->returnValue ($ this ->scheduleResource ));
403
- $ this ->_scheduleFactory ->expects ($ this ->exactly ( 2 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
397
+ $ this ->_scheduleFactory ->expects ($ this ->once ( ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
404
398
405
399
$ this ->_observer ->execute ($ this ->observer );
406
400
}
@@ -465,7 +459,7 @@ public function testDispatchExceptionInCallback(
465
459
)->disableOriginalConstructor ()->getMock ();
466
460
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
467
461
$ scheduleMock ->expects ($ this ->any ())->method ('getResource ' )->will ($ this ->returnValue ($ this ->scheduleResource ));
468
- $ this ->_scheduleFactory ->expects ($ this ->exactly ( 2 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
462
+ $ this ->_scheduleFactory ->expects ($ this ->once ( ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
469
463
$ this ->_objectManager
470
464
->expects ($ this ->once ())
471
465
->method ('create ' )
@@ -541,23 +535,22 @@ public function testDispatchRunJob()
541
535
$ scheduleMethods
542
536
)->disableOriginalConstructor ()->getMock ();
543
537
$ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->will ($ this ->returnValue ('test_job1 ' ));
544
- $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
545
- $ schedule ->expects ($ this ->once ())->method ('tryLockJob ' )->will ($ this ->returnValue (true ));
538
+ $ schedule ->expects ($ this ->atLeastOnce ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
539
+ $ schedule ->expects ($ this ->atLeastOnce ())->method ('tryLockJob ' )->will ($ this ->returnValue (true ));
540
+ $ schedule ->expects ($ this ->any ())->method ('setFinishedAt ' )->willReturnSelf ();
546
541
547
542
// cron start to execute some job
548
543
$ schedule ->expects ($ this ->any ())->method ('setExecutedAt ' )->will ($ this ->returnSelf ());
549
- $ schedule ->expects ($ this ->at ( 5 ))->method ('save ' );
544
+ $ schedule ->expects ($ this ->atLeastOnce ( ))->method ('save ' );
550
545
551
546
// cron end execute some job
552
547
$ schedule ->expects (
553
- $ this ->at ( 6 )
548
+ $ this ->atLeastOnce ( )
554
549
)->method (
555
550
'setStatus '
556
551
)->with (
557
552
$ this ->equalTo (\Magento \Cron \Model \Schedule::STATUS_SUCCESS )
558
- )->will (
559
- $ this ->returnSelf ()
560
- );
553
+ )->willReturnSelf ();
561
554
562
555
$ schedule ->expects ($ this ->at (8 ))->method ('save ' );
563
556
@@ -576,7 +569,7 @@ public function testDispatchRunJob()
576
569
)->disableOriginalConstructor ()->getMock ();
577
570
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
578
571
$ scheduleMock ->expects ($ this ->any ())->method ('getResource ' )->will ($ this ->returnValue ($ this ->scheduleResource ));
579
- $ this ->_scheduleFactory ->expects ($ this ->exactly (2 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
572
+ $ this ->_scheduleFactory ->expects ($ this ->once (2 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
580
573
581
574
$ testCronJob = $ this ->getMockBuilder ('CronJob ' )->setMethods (['execute ' ])->getMock ();
582
575
$ testCronJob ->expects ($ this ->atLeastOnce ())->method ('execute ' )->with ($ schedule );
@@ -783,7 +776,7 @@ public function testDispatchCleanup()
783
776
)->setMethods (['getCollection ' , 'getResource ' ])->disableOriginalConstructor ()->getMock ();
784
777
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ collection ));
785
778
$ scheduleMock ->expects ($ this ->any ())->method ('getResource ' )->will ($ this ->returnValue ($ this ->scheduleResource ));
786
- $ this ->_scheduleFactory ->expects ($ this ->at ( 1 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
779
+ $ this ->_scheduleFactory ->expects ($ this ->any ( ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
787
780
788
781
$ this ->_observer ->execute ($ this ->observer );
789
782
}
@@ -807,55 +800,17 @@ public function testMissedJobsCleanedInTime()
807
800
$ this ->_cache ->expects ($ this ->at (2 ))->method ('load ' )->will ($ this ->returnValue ($ this ->time + 10000000 ));
808
801
$ this ->_scheduleFactory ->expects ($ this ->at (2 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
809
802
810
- // This item was scheduled 2 days and 2 hours ago
811
- $ dateScheduledAt = date ('Y-m-d H:i:s ' , $ this ->time - 180000 );
812
- /** @var \Magento\Cron\Model\Schedule|\PHPUnit_Framework_MockObject_MockObject $schedule1 */
813
- $ schedule1 = $ this ->getMockBuilder (
814
- \Magento \Cron \Model \Schedule::class
815
- )->disableOriginalConstructor ()->setMethods (
816
- ['getExecutedAt ' , 'getScheduledAt ' , 'getStatus ' , 'delete ' , '__wakeup ' ]
817
- )->getMock ();
818
- $ schedule1 ->expects ($ this ->any ())->method ('getExecutedAt ' )->will ($ this ->returnValue (null ));
819
- $ schedule1 ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
820
- $ schedule1 ->expects ($ this ->any ())->method ('getStatus ' )->will ($ this ->returnValue (Schedule::STATUS_MISSED ));
821
- //we expect this job be deleted from the list
822
- $ schedule1 ->expects ($ this ->once ())->method ('delete ' )->will ($ this ->returnValue (true ));
823
- $ this ->_collection ->addItem ($ schedule1 );
824
-
825
- // This item was scheduled 1 day ago
826
- $ dateScheduledAt = date ('Y-m-d H:i:s ' , $ this ->time - 86400 );
827
- $ schedule2 = $ this ->getMockBuilder (
828
- \Magento \Cron \Model \Schedule::class
829
- )->disableOriginalConstructor ()->setMethods (
830
- ['getExecutedAt ' , 'getScheduledAt ' , 'getStatus ' , 'delete ' , '__wakeup ' ]
831
- )->getMock ();
832
- $ schedule2 ->expects ($ this ->any ())->method ('getExecutedAt ' )->will ($ this ->returnValue (null ));
833
- $ schedule2 ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
834
- $ schedule2 ->expects ($ this ->any ())->method ('getStatus ' )->will ($ this ->returnValue (Schedule::STATUS_MISSED ));
835
- //we don't expect this job be deleted from the list
836
- $ schedule2 ->expects ($ this ->never ())->method ('delete ' );
837
- $ this ->_collection ->addItem ($ schedule2 );
838
-
839
803
$ this ->_config ->expects ($ this ->exactly (2 ))->method ('getJobs ' )->will ($ this ->returnValue ($ jobConfig ));
840
804
841
- $ this ->_scopeConfig ->expects ($ this ->at (0 ))->method ('getValue ' )
842
- ->with ($ this ->equalTo ('system/cron/test_group/use_separate_process ' ))
843
- ->will ($ this ->returnValue (0 ));
844
- $ this ->_scopeConfig ->expects ($ this ->at (1 ))->method ('getValue ' )
845
- ->with ($ this ->equalTo ('system/cron/test_group/history_cleanup_every ' ))
846
- ->will ($ this ->returnValue (10 ));
847
- $ this ->_scopeConfig ->expects ($ this ->at (2 ))->method ('getValue ' )
848
- ->with ($ this ->equalTo ('system/cron/test_group/schedule_lifetime ' ))
849
- ->will ($ this ->returnValue (2 *24 *60 ));
850
- $ this ->_scopeConfig ->expects ($ this ->at (3 ))->method ('getValue ' )
851
- ->with ($ this ->equalTo ('system/cron/test_group/history_success_lifetime ' ))
852
- ->will ($ this ->returnValue (0 ));
853
- $ this ->_scopeConfig ->expects ($ this ->at (4 ))->method ('getValue ' )
854
- ->with ($ this ->equalTo ('system/cron/test_group/history_failure_lifetime ' ))
855
- ->will ($ this ->returnValue (0 ));
856
- $ this ->_scopeConfig ->expects ($ this ->at (5 ))->method ('getValue ' )
857
- ->with ($ this ->equalTo ('system/cron/test_group/schedule_generate_every ' ))
858
- ->will ($ this ->returnValue (0 ));
805
+ $ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )
806
+ ->willReturnMap ([
807
+ ['system/cron/test_group/use_separate_process ' , 0 ],
808
+ ['system/cron/test_group/history_cleanup_every ' , 10 ],
809
+ ['system/cron/test_group/schedule_lifetime ' , 2 *24 *60 ],
810
+ ['system/cron/test_group/history_success_lifetime ' , 0 ],
811
+ ['system/cron/test_group/history_failure_lifetime ' , 0 ],
812
+ ['system/cron/test_group/schedule_generate_every ' , 0 ],
813
+ ]);
859
814
860
815
$ this ->_collection ->expects ($ this ->any ())->method ('addFieldToFilter ' )->will ($ this ->returnSelf ());
861
816
$ this ->_collection ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnSelf ());
0 commit comments