12
12
/**
13
13
* Class \Magento\Cron\Test\Unit\Model\ObserverTest
14
14
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
+ * @SuppressWarnings(PHPMD.TooManyFields)
15
16
*/
16
17
class ProcessCronQueueObserverTest extends \PHPUnit \Framework \TestCase
17
18
{
@@ -88,6 +89,11 @@ class ProcessCronQueueObserverTest extends \PHPUnit\Framework\TestCase
88
89
*/
89
90
protected $ scheduleResource ;
90
91
92
+ /**
93
+ * @var int
94
+ */
95
+ protected $ time = 1501538400 ;
96
+
91
97
/**
92
98
* Prepare parameters
93
99
*/
@@ -134,7 +140,7 @@ protected function setUp()
134
140
$ this ->dateTimeMock = $ this ->getMockBuilder (\Magento \Framework \Stdlib \DateTime \DateTime::class)
135
141
->disableOriginalConstructor ()
136
142
->getMock ();
137
- $ this ->dateTimeMock ->expects ($ this ->any ())->method ('gmtTimestamp ' )->will ($ this ->returnValue (time () ));
143
+ $ this ->dateTimeMock ->expects ($ this ->any ())->method ('gmtTimestamp ' )->will ($ this ->returnValue ($ this -> time ));
138
144
139
145
$ phpExecutableFinder = $ this ->createMock (\Symfony \Component \Process \PhpExecutableFinder::class);
140
146
$ phpExecutableFinder ->expects ($ this ->any ())->method ('find ' )->willReturn ('php ' );
@@ -173,7 +179,7 @@ protected function setUp()
173
179
*/
174
180
public function testDispatchNoPendingJobs ()
175
181
{
176
- $ lastRun = time () + 10000000 ;
182
+ $ lastRun = $ this -> time + 10000000 ;
177
183
$ this ->_cache ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnValue ($ lastRun ));
178
184
$ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
179
185
@@ -193,7 +199,7 @@ public function testDispatchNoPendingJobs()
193
199
*/
194
200
public function testDispatchNoJobConfig ()
195
201
{
196
- $ lastRun = time () + 10000000 ;
202
+ $ lastRun = $ this -> time + 10000000 ;
197
203
$ this ->_cache ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnValue ($ lastRun ));
198
204
$ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
199
205
@@ -225,17 +231,19 @@ public function testDispatchNoJobConfig()
225
231
*/
226
232
public function testDispatchCanNotLock ()
227
233
{
228
- $ lastRun = time () + 10000000 ;
234
+ $ lastRun = $ this -> time + 10000000 ;
229
235
$ this ->_cache ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnValue ($ lastRun ));
230
236
$ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
231
237
$ this ->_request ->expects ($ this ->any ())->method ('getParam ' )->will ($ this ->returnValue ('test_group ' ));
238
+
239
+ $ dateScheduledAt = date ('Y-m-d H:i:s ' , $ this ->time - 86400 );
232
240
$ schedule = $ this ->getMockBuilder (
233
241
\Magento \Cron \Model \Schedule::class
234
242
)->setMethods (
235
243
['getJobCode ' , 'tryLockJob ' , 'getScheduledAt ' , '__wakeup ' , 'save ' ]
236
244
)->disableOriginalConstructor ()->getMock ();
237
245
$ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->will ($ this ->returnValue ('test_job1 ' ));
238
- $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue (' -1 day ' ));
246
+ $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
239
247
$ schedule ->expects ($ this ->once ())->method ('tryLockJob ' )->will ($ this ->returnValue (false ));
240
248
$ abstractModel = $ this ->createMock (\Magento \Framework \Model \AbstractModel::class);
241
249
$ schedule ->expects ($ this ->any ())->method ('save ' )->will ($ this ->returnValue ($ abstractModel ));
@@ -269,10 +277,12 @@ public function testDispatchExceptionTooLate()
269
277
$ jobCode = 'test_job1 ' ;
270
278
$ exception = $ exceptionMessage . ' Schedule Id: ' . $ scheduleId . ' Job Code: ' . $ jobCode ;
271
279
272
- $ lastRun = time () + 10000000 ;
280
+ $ lastRun = $ this -> time + 10000000 ;
273
281
$ this ->_cache ->expects ($ this ->any ())->method ('load ' )->willReturn ($ lastRun );
274
282
$ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->willReturn (0 );
275
283
$ this ->_request ->expects ($ this ->any ())->method ('getParam ' )->willReturn ('test_group ' );
284
+
285
+ $ dateScheduledAt = date ('Y-m-d H:i:s ' , $ this ->time - 86400 );
276
286
$ schedule = $ this ->getMockBuilder (
277
287
\Magento \Cron \Model \Schedule::class
278
288
)->setMethods (
@@ -290,7 +300,7 @@ public function testDispatchExceptionTooLate()
290
300
]
291
301
)->disableOriginalConstructor ()->getMock ();
292
302
$ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->willReturn ($ jobCode );
293
- $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->willReturn (' -1 day ' );
303
+ $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->willReturn ($ dateScheduledAt );
294
304
$ schedule ->expects ($ this ->once ())->method ('tryLockJob ' )->willReturn (true );
295
305
$ schedule ->expects (
296
306
$ this ->once ()
@@ -336,13 +346,14 @@ public function testDispatchExceptionNoCallback()
336
346
$ exceptionMessage = 'No callbacks found ' ;
337
347
$ exception = new \Exception (__ ($ exceptionMessage ));
338
348
349
+ $ dateScheduledAt = date ('Y-m-d H:i:s ' , $ this ->time - 86400 );
339
350
$ schedule = $ this ->getMockBuilder (
340
351
\Magento \Cron \Model \Schedule::class
341
352
)->setMethods (
342
353
['getJobCode ' , 'tryLockJob ' , 'getScheduledAt ' , 'save ' , 'setStatus ' , 'setMessages ' , '__wakeup ' , 'getStatus ' ]
343
354
)->disableOriginalConstructor ()->getMock ();
344
355
$ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->will ($ this ->returnValue ('test_job1 ' ));
345
- $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue (' -1 day ' ));
356
+ $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
346
357
$ schedule ->expects ($ this ->once ())->method ('tryLockJob ' )->will ($ this ->returnValue (true ));
347
358
$ schedule ->expects (
348
359
$ this ->once ()
@@ -365,10 +376,12 @@ public function testDispatchExceptionNoCallback()
365
376
366
377
$ this ->_config ->expects ($ this ->exactly (2 ))->method ('getJobs ' )->will ($ this ->returnValue ($ jobConfig ));
367
378
368
- $ lastRun = time () + 10000000 ;
379
+ $ lastRun = $ this -> time + 10000000 ;
369
380
$ this ->_cache ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnValue ($ lastRun ));
370
381
371
- $ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (strtotime ('+1 day ' )));
382
+ $ this ->_scopeConfig ->expects ($ this ->any ())
383
+ ->method ('getValue ' )
384
+ ->will ($ this ->returnValue ($ this ->time + 86400 ));
372
385
373
386
$ scheduleMock = $ this ->getMockBuilder (
374
387
\Magento \Cron \Model \Schedule::class
@@ -405,13 +418,15 @@ public function testDispatchExceptionInCallback(
405
418
];
406
419
407
420
$ this ->_request ->expects ($ this ->any ())->method ('getParam ' )->will ($ this ->returnValue ('test_group ' ));
421
+
422
+ $ dateScheduledAt = date ('Y-m-d H:i:s ' , $ this ->time - 86400 );
408
423
$ schedule = $ this ->getMockBuilder (
409
424
\Magento \Cron \Model \Schedule::class
410
425
)->setMethods (
411
426
['getJobCode ' , 'tryLockJob ' , 'getScheduledAt ' , 'save ' , 'setStatus ' , 'setMessages ' , '__wakeup ' , 'getStatus ' ]
412
427
)->disableOriginalConstructor ()->getMock ();
413
428
$ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->will ($ this ->returnValue ('test_job1 ' ));
414
- $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue (' -1 day ' ));
429
+ $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
415
430
$ schedule ->expects ($ this ->once ())->method ('tryLockJob ' )->will ($ this ->returnValue (true ));
416
431
$ schedule ->expects ($ this ->once ())
417
432
->method ('setStatus ' )
@@ -427,9 +442,11 @@ public function testDispatchExceptionInCallback(
427
442
428
443
$ this ->_config ->expects ($ this ->exactly (2 ))->method ('getJobs ' )->will ($ this ->returnValue ($ jobConfig ));
429
444
430
- $ lastRun = time () + 10000000 ;
445
+ $ lastRun = $ this -> time + 10000000 ;
431
446
$ this ->_cache ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnValue ($ lastRun ));
432
- $ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (strtotime ('+1 day ' )));
447
+ $ this ->_scopeConfig ->expects ($ this ->any ())
448
+ ->method ('getValue ' )
449
+ ->will ($ this ->returnValue ($ this ->time + 86400 ));
433
450
434
451
$ scheduleMock = $ this ->getMockBuilder (
435
452
\Magento \Cron \Model \Schedule::class
@@ -479,6 +496,7 @@ public function testDispatchRunJob()
479
496
];
480
497
$ this ->_request ->expects ($ this ->any ())->method ('getParam ' )->will ($ this ->returnValue ('test_group ' ));
481
498
499
+ $ dateScheduledAt = date ('Y-m-d H:i:s ' , $ this ->time - 86400 );
482
500
$ scheduleMethods = [
483
501
'getJobCode ' ,
484
502
'tryLockJob ' ,
@@ -497,7 +515,7 @@ public function testDispatchRunJob()
497
515
$ scheduleMethods
498
516
)->disableOriginalConstructor ()->getMock ();
499
517
$ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->will ($ this ->returnValue ('test_job1 ' ));
500
- $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue (' -1 day ' ));
518
+ $ schedule ->expects ($ this ->once ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
501
519
$ schedule ->expects ($ this ->once ())->method ('tryLockJob ' )->will ($ this ->returnValue (true ));
502
520
503
521
// cron start to execute some job
@@ -521,9 +539,11 @@ public function testDispatchRunJob()
521
539
522
540
$ this ->_config ->expects ($ this ->exactly (2 ))->method ('getJobs ' )->will ($ this ->returnValue ($ jobConfig ));
523
541
524
- $ lastRun = time () + 10000000 ;
542
+ $ lastRun = $ this -> time + 10000000 ;
525
543
$ this ->_cache ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnValue ($ lastRun ));
526
- $ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (strtotime ('+1 day ' )));
544
+ $ this ->_scopeConfig ->expects ($ this ->any ())
545
+ ->method ('getValue ' )
546
+ ->will ($ this ->returnValue ($ this ->time + 86400 ));
527
547
528
548
$ scheduleMock = $ this ->getMockBuilder (
529
549
\Magento \Cron \Model \Schedule::class
@@ -573,7 +593,7 @@ public function testDispatchNotGenerate()
573
593
)->with (
574
594
$ this ->equalTo (ProcessCronQueueObserver::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . 'test_group ' )
575
595
)->will (
576
- $ this ->returnValue (time () + 10000000 )
596
+ $ this ->returnValue ($ this -> time + 10000000 )
577
597
);
578
598
$ this ->_cache ->expects (
579
599
$ this ->at (1 )
@@ -582,7 +602,7 @@ public function testDispatchNotGenerate()
582
602
)->with (
583
603
$ this ->equalTo (ProcessCronQueueObserver::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . 'test_group ' )
584
604
)->will (
585
- $ this ->returnValue (time () - 10000000 )
605
+ $ this ->returnValue ($ this -> time - 10000000 )
586
606
);
587
607
588
608
$ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
@@ -641,14 +661,14 @@ public function testDispatchGenerate()
641
661
'load '
642
662
)->with (
643
663
$ this ->equalTo (ProcessCronQueueObserver::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . 'default ' )
644
- )->willReturn (time () + 10000000 );
664
+ )->willReturn ($ this -> time + 10000000 );
645
665
$ this ->_cache ->expects (
646
666
$ this ->at (1 )
647
667
)->method (
648
668
'load '
649
669
)->with (
650
670
$ this ->equalTo (ProcessCronQueueObserver::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . 'default ' )
651
- )->willReturn (time () - 10000000 );
671
+ )->willReturn ($ this -> time - 10000000 );
652
672
653
673
$ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->willReturnMap (
654
674
[
@@ -699,20 +719,21 @@ public function testDispatchCleanup()
699
719
'test_group ' => ['test_job1 ' => ['instance ' => 'CronJob ' , 'method ' => 'execute ' ]],
700
720
];
701
721
722
+ $ dateExecutedAt = date ('Y-m-d H:i:s ' , $ this ->time - 86400 );
702
723
$ schedule = $ this ->getMockBuilder (
703
724
\Magento \Cron \Model \Schedule::class
704
725
)->disableOriginalConstructor ()->setMethods (
705
726
['getExecutedAt ' , 'getStatus ' , 'delete ' , '__wakeup ' ]
706
727
)->getMock ();
707
- $ schedule ->expects ($ this ->any ())->method ('getExecutedAt ' )->will ($ this ->returnValue (' -1 day ' ));
728
+ $ schedule ->expects ($ this ->any ())->method ('getExecutedAt ' )->will ($ this ->returnValue ($ dateExecutedAt ));
708
729
$ schedule ->expects ($ this ->any ())->method ('getStatus ' )->will ($ this ->returnValue ('success ' ));
709
730
$ this ->_request ->expects ($ this ->any ())->method ('getParam ' )->will ($ this ->returnValue ('test_group ' ));
710
731
$ this ->_collection ->addItem ($ schedule );
711
732
712
733
$ this ->_config ->expects ($ this ->exactly (2 ))->method ('getJobs ' )->will ($ this ->returnValue ($ jobConfig ));
713
734
714
- $ this ->_cache ->expects ($ this ->at (0 ))->method ('load ' )->will ($ this ->returnValue (time () + 10000000 ));
715
- $ this ->_cache ->expects ($ this ->at (1 ))->method ('load ' )->will ($ this ->returnValue (time () - 10000000 ));
735
+ $ this ->_cache ->expects ($ this ->at (0 ))->method ('load ' )->will ($ this ->returnValue ($ this -> time + 10000000 ));
736
+ $ this ->_cache ->expects ($ this ->at (1 ))->method ('load ' )->will ($ this ->returnValue ($ this -> time - 10000000 ));
716
737
717
738
$ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
718
739
@@ -749,39 +770,41 @@ public function testMissedJobsCleanedInTime()
749
770
)->disableOriginalConstructor ()->getMock ();
750
771
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
751
772
//get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_cleanup()"
752
- $ this ->_cache ->expects ($ this ->at (0 ))->method ('load ' )->will ($ this ->returnValue (time () - 10000000 ));
773
+ $ this ->_cache ->expects ($ this ->at (0 ))->method ('load ' )->will ($ this ->returnValue ($ this -> time - 10000000 ));
753
774
$ this ->_scheduleFactory ->expects ($ this ->at (0 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
754
775
755
776
/* 2. Initialize dependencies of _generate() method which is called second */
756
777
$ jobConfig = [
757
778
'test_group ' => ['test_job1 ' => ['instance ' => 'CronJob ' , 'method ' => 'execute ' ]],
758
779
];
759
780
//get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_generate()"
760
- $ this ->_cache ->expects ($ this ->at (2 ))->method ('load ' )->will ($ this ->returnValue (time () + 10000000 ));
781
+ $ this ->_cache ->expects ($ this ->at (2 ))->method ('load ' )->will ($ this ->returnValue ($ this -> time + 10000000 ));
761
782
$ this ->_scheduleFactory ->expects ($ this ->at (2 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
762
783
763
- // This item was scheduled 2 days ago
784
+ // This item was scheduled 2 days and 2 hours ago
785
+ $ dateScheduledAt = date ('Y-m-d H:i:s ' , $ this ->time - 180000 );
764
786
/** @var \Magento\Cron\Model\Schedule|\PHPUnit_Framework_MockObject_MockObject $schedule1 */
765
787
$ schedule1 = $ this ->getMockBuilder (
766
788
\Magento \Cron \Model \Schedule::class
767
789
)->disableOriginalConstructor ()->setMethods (
768
790
['getExecutedAt ' , 'getScheduledAt ' , 'getStatus ' , 'delete ' , '__wakeup ' ]
769
791
)->getMock ();
770
792
$ schedule1 ->expects ($ this ->any ())->method ('getExecutedAt ' )->will ($ this ->returnValue (null ));
771
- $ schedule1 ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue (' -2 day -2 hour ' ));
793
+ $ schedule1 ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
772
794
$ schedule1 ->expects ($ this ->any ())->method ('getStatus ' )->will ($ this ->returnValue (Schedule::STATUS_MISSED ));
773
795
//we expect this job be deleted from the list
774
796
$ schedule1 ->expects ($ this ->once ())->method ('delete ' )->will ($ this ->returnValue (true ));
775
797
$ this ->_collection ->addItem ($ schedule1 );
776
798
777
799
// This item was scheduled 1 day ago
800
+ $ dateScheduledAt = date ('Y-m-d H:i:s ' , $ this ->time - 86400 );
778
801
$ schedule2 = $ this ->getMockBuilder (
779
802
\Magento \Cron \Model \Schedule::class
780
803
)->disableOriginalConstructor ()->setMethods (
781
804
['getExecutedAt ' , 'getScheduledAt ' , 'getStatus ' , 'delete ' , '__wakeup ' ]
782
805
)->getMock ();
783
806
$ schedule2 ->expects ($ this ->any ())->method ('getExecutedAt ' )->will ($ this ->returnValue (null ));
784
- $ schedule2 ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue (' -1 day ' ));
807
+ $ schedule2 ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ dateScheduledAt ));
785
808
$ schedule2 ->expects ($ this ->any ())->method ('getStatus ' )->will ($ this ->returnValue (Schedule::STATUS_MISSED ));
786
809
//we don't expect this job be deleted from the list
787
810
$ schedule2 ->expects ($ this ->never ())->method ('delete ' );
0 commit comments