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