1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SilverStripe\Dev\SapphireTest; |
4
|
|
|
use SilverStripe\ORM\DataObject; |
5
|
|
|
use Symbiote\QueuedJobs\Tests\ScheduledExecutionTest\TestScheduledDataObject; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author [email protected] |
9
|
|
|
* @license BSD License http://silverstripe.org/bsd-license/ |
10
|
|
|
*/ |
11
|
|
|
class ScheduledExecutionTest extends SapphireTest |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* We need the DB for this test |
15
|
|
|
* |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
protected $usesDatabase = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected static $extra_dataobjects = array( |
25
|
|
|
TestScheduledDataObject::class |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
public function testScheduledExecutionTimes() |
29
|
|
|
{ |
30
|
|
|
$test = new TestScheduledDataObject; |
31
|
|
|
|
32
|
|
|
$test->Title = 'Test execute of stuff'; |
33
|
|
|
$test->write(); |
34
|
|
|
|
35
|
|
|
$test->FirstExecution = '1980-09-22 09:15:00'; |
36
|
|
|
$test->ExecuteEvery = 'Hour'; |
37
|
|
|
|
38
|
|
|
$test->write(); |
39
|
|
|
|
40
|
|
|
// should now have a job |
41
|
|
|
$this->assertTrue($test->ScheduledJobID > 0, 'Scheduled job has not been created'); |
42
|
|
|
|
43
|
|
|
$jobId = $test->ScheduledJobID; |
44
|
|
|
|
45
|
|
|
// execute said job |
46
|
|
|
$job = $test->ScheduledJob(); |
47
|
|
|
|
48
|
|
|
$job->execute(); |
49
|
|
|
|
50
|
|
|
// reload the test object and make sure its job has now changed |
51
|
|
|
$test = DataObject::get_by_id(TestScheduledDataObject::class, $test->ID); |
52
|
|
|
|
53
|
|
|
$this->assertNotEquals($test->ScheduledJobID, $jobId); |
54
|
|
|
$this->assertEquals('EXECUTED', $test->Message); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testScheduledExecutionInterval() |
58
|
|
|
{ |
59
|
|
|
$test = new TestScheduledDataObject; |
60
|
|
|
|
61
|
|
|
$test->Title = 'Test execute at custom interval sizes'; |
62
|
|
|
$test->write(); |
63
|
|
|
|
64
|
|
|
$test->FirstExecution = '1980-09-22 09:15:00'; |
65
|
|
|
$test->ExecuteEvery = 'Minute'; |
66
|
|
|
|
67
|
|
|
$test->write(); |
68
|
|
|
|
69
|
|
|
// should now have a job |
70
|
|
|
$this->assertTrue($test->ScheduledJobID > 0, 'Scheduled job has not been created'); |
71
|
|
|
// should default the ExecuteInterval |
72
|
|
|
$this->assertEquals(1, $test->ExecuteInterval, 'ExecuteInterval did not default to 1'); |
73
|
|
|
|
74
|
|
|
// should check the interval in code also |
75
|
|
|
$test->ExecuteInterval = 0; |
76
|
|
|
$test->write(); |
77
|
|
|
|
78
|
|
|
$jobId = $test->ScheduledJobID; |
79
|
|
|
|
80
|
|
|
// execute said job |
81
|
|
|
$job = $test->ScheduledJob(); |
82
|
|
|
$job->execute(); |
83
|
|
|
|
84
|
|
|
// reload the test object and make sure its job has now changed |
85
|
|
|
$test = DataObject::get_by_id(TestScheduledDataObject::class, $test->ID); |
86
|
|
|
|
87
|
|
|
$this->assertNotEquals($test->ScheduledJobID, $jobId); |
88
|
|
|
$this->assertEquals('EXECUTED', $test->Message); |
89
|
|
|
|
90
|
|
|
$job = $test->ScheduledJob(); |
91
|
|
|
|
92
|
|
|
// should reschedule in 1 minute time |
93
|
|
|
$expectedMinutes = date('i', time()); |
94
|
|
|
$expectedMinutes = intval($expectedMinutes, 10); |
95
|
|
|
if ($expectedMinutes + 1 > 59) { // Wrap around the hour |
96
|
|
|
$expectedMinutes = $expectedMinutes - 59; |
97
|
|
|
} |
98
|
|
|
$scheduledMinutes = substr($job->StartAfter, 14, 2); |
99
|
|
|
$scheduledMinutes = intval($scheduledMinutes, 10); |
100
|
|
|
|
101
|
|
|
$this->assertEquals($expectedMinutes + 1, $scheduledMinutes, 'Did not reschedule 1 minute later'); |
102
|
|
|
|
103
|
|
|
// test a custom interval of 3 minutes |
104
|
|
|
|
105
|
|
|
$test->ExecuteInterval = 3; |
106
|
|
|
$test->write(); |
107
|
|
|
|
108
|
|
|
$job = $test->ScheduledJob(); |
109
|
|
|
$job->execute(); |
110
|
|
|
|
111
|
|
|
$test = DataObject::get_by_id(TestScheduledDataObject::class, $test->ID); |
112
|
|
|
|
113
|
|
|
$job = $test->ScheduledJob(); |
114
|
|
|
|
115
|
|
|
// should reschedule in 3 minutes time |
116
|
|
|
$expectedMinutes = date('i', time()); |
117
|
|
|
$expectedMinutes = intval($expectedMinutes, 10); |
118
|
|
|
if ($expectedMinutes + 3 > 59) { |
119
|
|
|
$expectedMinutes = $expectedMinutes - 59; |
120
|
|
|
} |
121
|
|
|
$scheduledMinutes = substr($job->StartAfter, 14, 2); |
122
|
|
|
$scheduledMinutes = intval($scheduledMinutes, 10); |
123
|
|
|
|
124
|
|
|
$this->assertEquals($expectedMinutes + 3, $scheduledMinutes, 'Did not reschedule 3 minutes later'); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.