Skip to content

Commit 61176a3

Browse files
committed
MAGETWO-71024: [Tech Debt] Skipped unit/integration tests needs to be re-examined.
- Avoid relying on current time in unit test
1 parent 5ce61d2 commit 61176a3

File tree

1 file changed

+51
-28
lines changed

1 file changed

+51
-28
lines changed

app/code/Magento/Cron/Test/Unit/Observer/ProcessCronQueueObserverTest.php

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/**
1313
* Class \Magento\Cron\Test\Unit\Model\ObserverTest
1414
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15+
* @SuppressWarnings(PHPMD.TooManyFields)
1516
*/
1617
class ProcessCronQueueObserverTest extends \PHPUnit\Framework\TestCase
1718
{
@@ -88,6 +89,11 @@ class ProcessCronQueueObserverTest extends \PHPUnit\Framework\TestCase
8889
*/
8990
protected $scheduleResource;
9091

92+
/**
93+
* @var int
94+
*/
95+
protected $time = 1501538400;
96+
9197
/**
9298
* Prepare parameters
9399
*/
@@ -134,7 +140,7 @@ protected function setUp()
134140
$this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
135141
->disableOriginalConstructor()
136142
->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));
138144

139145
$phpExecutableFinder = $this->createMock(\Symfony\Component\Process\PhpExecutableFinder::class);
140146
$phpExecutableFinder->expects($this->any())->method('find')->willReturn('php');
@@ -173,7 +179,7 @@ protected function setUp()
173179
*/
174180
public function testDispatchNoPendingJobs()
175181
{
176-
$lastRun = time() + 10000000;
182+
$lastRun = $this->time + 10000000;
177183
$this->_cache->expects($this->any())->method('load')->will($this->returnValue($lastRun));
178184
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
179185

@@ -193,7 +199,7 @@ public function testDispatchNoPendingJobs()
193199
*/
194200
public function testDispatchNoJobConfig()
195201
{
196-
$lastRun = time() + 10000000;
202+
$lastRun = $this->time + 10000000;
197203
$this->_cache->expects($this->any())->method('load')->will($this->returnValue($lastRun));
198204
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
199205

@@ -225,17 +231,19 @@ public function testDispatchNoJobConfig()
225231
*/
226232
public function testDispatchCanNotLock()
227233
{
228-
$lastRun = time() + 10000000;
234+
$lastRun = $this->time + 10000000;
229235
$this->_cache->expects($this->any())->method('load')->will($this->returnValue($lastRun));
230236
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
231237
$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);
232240
$schedule = $this->getMockBuilder(
233241
\Magento\Cron\Model\Schedule::class
234242
)->setMethods(
235243
['getJobCode', 'tryLockJob', 'getScheduledAt', '__wakeup', 'save']
236244
)->disableOriginalConstructor()->getMock();
237245
$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));
239247
$schedule->expects($this->once())->method('tryLockJob')->will($this->returnValue(false));
240248
$abstractModel = $this->createMock(\Magento\Framework\Model\AbstractModel::class);
241249
$schedule->expects($this->any())->method('save')->will($this->returnValue($abstractModel));
@@ -269,10 +277,12 @@ public function testDispatchExceptionTooLate()
269277
$jobCode = 'test_job1';
270278
$exception = $exceptionMessage . ' Schedule Id: ' . $scheduleId . ' Job Code: ' . $jobCode;
271279

272-
$lastRun = time() + 10000000;
280+
$lastRun = $this->time + 10000000;
273281
$this->_cache->expects($this->any())->method('load')->willReturn($lastRun);
274282
$this->_scopeConfig->expects($this->any())->method('getValue')->willReturn(0);
275283
$this->_request->expects($this->any())->method('getParam')->willReturn('test_group');
284+
285+
$dateScheduledAt = date('Y-m-d H:i:s', $this->time - 86400);
276286
$schedule = $this->getMockBuilder(
277287
\Magento\Cron\Model\Schedule::class
278288
)->setMethods(
@@ -290,7 +300,7 @@ public function testDispatchExceptionTooLate()
290300
]
291301
)->disableOriginalConstructor()->getMock();
292302
$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);
294304
$schedule->expects($this->once())->method('tryLockJob')->willReturn(true);
295305
$schedule->expects(
296306
$this->once()
@@ -336,13 +346,14 @@ public function testDispatchExceptionNoCallback()
336346
$exceptionMessage = 'No callbacks found';
337347
$exception = new \Exception(__($exceptionMessage));
338348

349+
$dateScheduledAt = date('Y-m-d H:i:s', $this->time - 86400);
339350
$schedule = $this->getMockBuilder(
340351
\Magento\Cron\Model\Schedule::class
341352
)->setMethods(
342353
['getJobCode', 'tryLockJob', 'getScheduledAt', 'save', 'setStatus', 'setMessages', '__wakeup', 'getStatus']
343354
)->disableOriginalConstructor()->getMock();
344355
$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));
346357
$schedule->expects($this->once())->method('tryLockJob')->will($this->returnValue(true));
347358
$schedule->expects(
348359
$this->once()
@@ -365,10 +376,12 @@ public function testDispatchExceptionNoCallback()
365376

366377
$this->_config->expects($this->exactly(2))->method('getJobs')->will($this->returnValue($jobConfig));
367378

368-
$lastRun = time() + 10000000;
379+
$lastRun = $this->time + 10000000;
369380
$this->_cache->expects($this->any())->method('load')->will($this->returnValue($lastRun));
370381

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));
372385

373386
$scheduleMock = $this->getMockBuilder(
374387
\Magento\Cron\Model\Schedule::class
@@ -405,13 +418,15 @@ public function testDispatchExceptionInCallback(
405418
];
406419

407420
$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);
408423
$schedule = $this->getMockBuilder(
409424
\Magento\Cron\Model\Schedule::class
410425
)->setMethods(
411426
['getJobCode', 'tryLockJob', 'getScheduledAt', 'save', 'setStatus', 'setMessages', '__wakeup', 'getStatus']
412427
)->disableOriginalConstructor()->getMock();
413428
$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));
415430
$schedule->expects($this->once())->method('tryLockJob')->will($this->returnValue(true));
416431
$schedule->expects($this->once())
417432
->method('setStatus')
@@ -427,9 +442,11 @@ public function testDispatchExceptionInCallback(
427442

428443
$this->_config->expects($this->exactly(2))->method('getJobs')->will($this->returnValue($jobConfig));
429444

430-
$lastRun = time() + 10000000;
445+
$lastRun = $this->time + 10000000;
431446
$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));
433450

434451
$scheduleMock = $this->getMockBuilder(
435452
\Magento\Cron\Model\Schedule::class
@@ -479,6 +496,7 @@ public function testDispatchRunJob()
479496
];
480497
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
481498

499+
$dateScheduledAt = date('Y-m-d H:i:s', $this->time - 86400);
482500
$scheduleMethods = [
483501
'getJobCode',
484502
'tryLockJob',
@@ -497,7 +515,7 @@ public function testDispatchRunJob()
497515
$scheduleMethods
498516
)->disableOriginalConstructor()->getMock();
499517
$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));
501519
$schedule->expects($this->once())->method('tryLockJob')->will($this->returnValue(true));
502520

503521
// cron start to execute some job
@@ -521,9 +539,11 @@ public function testDispatchRunJob()
521539

522540
$this->_config->expects($this->exactly(2))->method('getJobs')->will($this->returnValue($jobConfig));
523541

524-
$lastRun = time() + 10000000;
542+
$lastRun = $this->time + 10000000;
525543
$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));
527547

528548
$scheduleMock = $this->getMockBuilder(
529549
\Magento\Cron\Model\Schedule::class
@@ -573,7 +593,7 @@ public function testDispatchNotGenerate()
573593
)->with(
574594
$this->equalTo(ProcessCronQueueObserver::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . 'test_group')
575595
)->will(
576-
$this->returnValue(time() + 10000000)
596+
$this->returnValue($this->time + 10000000)
577597
);
578598
$this->_cache->expects(
579599
$this->at(1)
@@ -582,7 +602,7 @@ public function testDispatchNotGenerate()
582602
)->with(
583603
$this->equalTo(ProcessCronQueueObserver::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . 'test_group')
584604
)->will(
585-
$this->returnValue(time() - 10000000)
605+
$this->returnValue($this->time - 10000000)
586606
);
587607

588608
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
@@ -641,14 +661,14 @@ public function testDispatchGenerate()
641661
'load'
642662
)->with(
643663
$this->equalTo(ProcessCronQueueObserver::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . 'default')
644-
)->willReturn(time() + 10000000);
664+
)->willReturn($this->time + 10000000);
645665
$this->_cache->expects(
646666
$this->at(1)
647667
)->method(
648668
'load'
649669
)->with(
650670
$this->equalTo(ProcessCronQueueObserver::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . 'default')
651-
)->willReturn(time() - 10000000);
671+
)->willReturn($this->time - 10000000);
652672

653673
$this->_scopeConfig->expects($this->any())->method('getValue')->willReturnMap(
654674
[
@@ -699,20 +719,21 @@ public function testDispatchCleanup()
699719
'test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']],
700720
];
701721

722+
$dateExecutedAt = date('Y-m-d H:i:s', $this->time - 86400);
702723
$schedule = $this->getMockBuilder(
703724
\Magento\Cron\Model\Schedule::class
704725
)->disableOriginalConstructor()->setMethods(
705726
['getExecutedAt', 'getStatus', 'delete', '__wakeup']
706727
)->getMock();
707-
$schedule->expects($this->any())->method('getExecutedAt')->will($this->returnValue('-1 day'));
728+
$schedule->expects($this->any())->method('getExecutedAt')->will($this->returnValue($dateExecutedAt));
708729
$schedule->expects($this->any())->method('getStatus')->will($this->returnValue('success'));
709730
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
710731
$this->_collection->addItem($schedule);
711732

712733
$this->_config->expects($this->exactly(2))->method('getJobs')->will($this->returnValue($jobConfig));
713734

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));
716737

717738
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
718739

@@ -749,39 +770,41 @@ public function testMissedJobsCleanedInTime()
749770
)->disableOriginalConstructor()->getMock();
750771
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
751772
//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));
753774
$this->_scheduleFactory->expects($this->at(0))->method('create')->will($this->returnValue($scheduleMock));
754775

755776
/* 2. Initialize dependencies of _generate() method which is called second */
756777
$jobConfig = [
757778
'test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']],
758779
];
759780
//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));
761782
$this->_scheduleFactory->expects($this->at(2))->method('create')->will($this->returnValue($scheduleMock));
762783

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);
764786
/** @var \Magento\Cron\Model\Schedule|\PHPUnit_Framework_MockObject_MockObject $schedule1 */
765787
$schedule1 = $this->getMockBuilder(
766788
\Magento\Cron\Model\Schedule::class
767789
)->disableOriginalConstructor()->setMethods(
768790
['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup']
769791
)->getMock();
770792
$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));
772794
$schedule1->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED));
773795
//we expect this job be deleted from the list
774796
$schedule1->expects($this->once())->method('delete')->will($this->returnValue(true));
775797
$this->_collection->addItem($schedule1);
776798

777799
// This item was scheduled 1 day ago
800+
$dateScheduledAt = date('Y-m-d H:i:s', $this->time - 86400);
778801
$schedule2 = $this->getMockBuilder(
779802
\Magento\Cron\Model\Schedule::class
780803
)->disableOriginalConstructor()->setMethods(
781804
['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup']
782805
)->getMock();
783806
$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));
785808
$schedule2->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED));
786809
//we don't expect this job be deleted from the list
787810
$schedule2->expects($this->never())->method('delete');

0 commit comments

Comments
 (0)