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