1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class CronTaskControllerTest extends SapphireTest |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
public function setUp() |
7
|
|
|
{ |
8
|
|
|
parent::setUp(); |
9
|
|
|
CronTaskTest_TestCron::$times_run = 0; |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Tests CronTaskController::isTaskDue |
14
|
|
|
*/ |
15
|
|
|
public function testIsTaskDue() |
16
|
|
|
{ |
17
|
|
|
$runner = CronTaskController::create(); |
18
|
|
|
$task = new CronTaskTest_TestCron(); |
19
|
|
|
$cron = Cron\CronExpression::factory($task->getSchedule()); |
20
|
|
|
|
21
|
|
|
// Assuming first run, match the exact time (seconds are ignored) |
22
|
|
|
SS_Datetime::set_mock_now('2010-06-20 13:00:10'); |
23
|
|
|
$this->assertTrue($runner->isTaskDue($task, $cron)); |
|
|
|
|
24
|
|
|
|
25
|
|
|
// Assume first run, do not match time just before or just after schedule |
26
|
|
|
SS_Datetime::set_mock_now('2010-06-20 13:01:10'); |
27
|
|
|
$this->assertFalse($runner->isTaskDue($task, $cron)); |
|
|
|
|
28
|
|
|
SS_Datetime::set_mock_now('2010-06-20 12:59:50'); |
29
|
|
|
$this->assertFalse($runner->isTaskDue($task, $cron)); |
|
|
|
|
30
|
|
|
|
31
|
|
|
// Mock a run and test that subsequent runs are properly scheduled |
32
|
|
|
SS_Datetime::set_mock_now('2010-06-20 13:30:10'); |
33
|
|
|
CronTaskStatus::update_status('CronTaskTest_TestCron', true); |
34
|
|
|
|
35
|
|
|
// Job prior to next hour mark should not run |
36
|
|
|
SS_Datetime::set_mock_now('2010-06-20 13:40:00'); |
37
|
|
|
$this->assertFalse($runner->isTaskDue($task, $cron)); |
|
|
|
|
38
|
|
|
|
39
|
|
|
// Jobs just after the next hour mark should run |
40
|
|
|
SS_Datetime::set_mock_now('2010-06-20 14:10:00'); |
41
|
|
|
$this->assertTrue($runner->isTaskDue($task, $cron)); |
|
|
|
|
42
|
|
|
|
43
|
|
|
// Jobs somehow delayed a whole day should be run |
44
|
|
|
SS_Datetime::set_mock_now('2010-06-21 13:40:00'); |
45
|
|
|
$this->assertTrue($runner->isTaskDue($task, $cron)); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Test CronTaskController::runTask |
50
|
|
|
*/ |
51
|
|
|
public function testRunTask() |
52
|
|
|
{ |
53
|
|
|
$runner = CronTaskController::create(); |
54
|
|
|
$runner->setQuiet(true); |
55
|
|
|
$task = new CronTaskTest_TestCron(); |
56
|
|
|
|
57
|
|
|
// Assuming first run, match the exact time (seconds are ignored) |
58
|
|
|
$this->assertEquals(0, CronTaskTest_TestCron::$times_run); |
|
|
|
|
59
|
|
|
SS_Datetime::set_mock_now('2010-06-20 13:00:10'); |
60
|
|
|
$runner->runTask($task); |
61
|
|
|
$this->assertEquals(1, CronTaskTest_TestCron::$times_run); |
|
|
|
|
62
|
|
|
|
63
|
|
|
// Test that re-requsting the task in the same minute do not retrigger another run |
64
|
|
|
SS_Datetime::set_mock_now('2010-06-20 13:00:40'); |
65
|
|
|
$runner->runTask($task); |
66
|
|
|
$this->assertEquals(1, CronTaskTest_TestCron::$times_run); |
|
|
|
|
67
|
|
|
|
68
|
|
|
// Job prior to next hour mark should not run |
69
|
|
|
SS_Datetime::set_mock_now('2010-06-20 13:40:00'); |
70
|
|
|
$runner->runTask($task); |
71
|
|
|
$this->assertEquals(1, CronTaskTest_TestCron::$times_run); |
|
|
|
|
72
|
|
|
|
73
|
|
|
// Jobs just after the next hour mark should run |
74
|
|
|
SS_Datetime::set_mock_now('2010-06-20 14:10:00'); |
75
|
|
|
$runner->runTask($task); |
76
|
|
|
$this->assertEquals(2, CronTaskTest_TestCron::$times_run); |
|
|
|
|
77
|
|
|
|
78
|
|
|
// Jobs run on the exact next expected date should run |
79
|
|
|
SS_Datetime::set_mock_now('2010-06-20 15:00:00'); |
80
|
|
|
$runner->runTask($task); |
81
|
|
|
$this->assertEquals(3, CronTaskTest_TestCron::$times_run); |
|
|
|
|
82
|
|
|
|
83
|
|
|
// Jobs somehow delayed a whole day should be run |
84
|
|
|
SS_Datetime::set_mock_now('2010-06-21 13:40:00'); |
85
|
|
|
$runner->runTask($task); |
86
|
|
|
$this->assertEquals(4, CronTaskTest_TestCron::$times_run); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
class CronTaskTest_TestCron implements TestOnly, CronTask |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
public static $times_run = 0; |
95
|
|
|
|
96
|
|
|
public function getSchedule() |
97
|
|
|
{ |
98
|
|
|
// Use hourly schedule |
99
|
|
|
return '0 * * * *'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function process() |
103
|
|
|
{ |
104
|
|
|
++self::$times_run; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.