|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
|
|
6
|
|
|
use Symfony\Component\Mime\Email; |
|
7
|
|
|
use Symfony\Component\Process\Process; |
|
8
|
|
|
use Zenstruck\ScheduleBundle\Schedule; |
|
9
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension; |
|
10
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\SingleServerExtension; |
|
11
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task; |
|
12
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\CallbackTask; |
|
13
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask; |
|
14
|
|
|
use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Kevin Bond <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ScheduleTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @test |
|
23
|
|
|
*/ |
|
24
|
|
|
public function can_add_tasks() |
|
25
|
|
|
{ |
|
26
|
|
|
$schedule = new Schedule(); |
|
27
|
|
|
|
|
28
|
|
|
$schedule->add(new CallbackTask(function () {}))->description('task1'); |
|
29
|
|
|
$schedule->addCallback(function () {})->description('task2'); |
|
30
|
|
|
$schedule->addProcess('php -v')->description('task3'); |
|
31
|
|
|
$schedule->addProcess(new Process(['php -v']))->description('task4'); |
|
32
|
|
|
$schedule->addCommand('my:command')->description('task5'); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertCount(5, $schedule->all()); |
|
35
|
|
|
$this->assertSame(['task1', 'task2', 'task3', 'task4', 'task5'], \array_map( |
|
36
|
|
|
function (Task $task) { |
|
37
|
|
|
return $task->getDescription(); |
|
38
|
|
|
}, |
|
39
|
|
|
$schedule->all() |
|
40
|
|
|
)); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertCount(5, $schedule->all(), 'Caches the tasks'); |
|
43
|
|
|
|
|
44
|
|
|
$schedule->addCommand('another:command'); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertCount(6, $schedule->all(), 'Resets the task cache on add'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @test |
|
51
|
|
|
*/ |
|
52
|
|
|
public function can_add_compound_tasks() |
|
53
|
|
|
{ |
|
54
|
|
|
$schedule = new Schedule(); |
|
55
|
|
|
|
|
56
|
|
|
$schedule->addCommand('my:command')->description('task1')->tuesdays(); |
|
57
|
|
|
$schedule->addCompound() |
|
58
|
|
|
->addCommand('another:command', [], 'task2') |
|
59
|
|
|
->addCallback(function () {}, 'task3') |
|
60
|
|
|
->addProcess('php -v', 'task4') |
|
61
|
|
|
->addProcess(new Process(['php -v']), 'task5') |
|
62
|
|
|
->add((new CommandTask('yet:another:command')) |
|
63
|
|
|
->description('task6') |
|
64
|
|
|
->sundays() |
|
65
|
|
|
->timezone('America/Los_Angeles') |
|
66
|
|
|
) |
|
67
|
|
|
->timezone('UTC') |
|
68
|
|
|
->mondays() |
|
69
|
|
|
->onSingleServer() |
|
70
|
|
|
; |
|
71
|
|
|
|
|
72
|
|
|
$this->assertCount(6, $schedule->all()); |
|
73
|
|
|
$this->assertSame('task1', $schedule->all()[0]->getDescription()); |
|
74
|
|
|
$this->assertSame('* * * * 2', (string) $schedule->all()[0]->getExpression()); |
|
75
|
|
|
$this->assertNull($schedule->all()[0]->getTimezone()); |
|
76
|
|
|
$this->assertCount(0, $schedule->all()[0]->getExtensions()); |
|
77
|
|
|
$this->assertSame('task2', $schedule->all()[1]->getDescription()); |
|
78
|
|
|
$this->assertSame('* * * * 1', (string) $schedule->all()[1]->getExpression()); |
|
79
|
|
|
$this->assertSame('UTC', $schedule->all()[1]->getTimezone()->getName()); |
|
80
|
|
|
$this->assertCount(1, $schedule->all()[1]->getExtensions()); |
|
81
|
|
|
$this->assertSame('task3', $schedule->all()[2]->getDescription()); |
|
82
|
|
|
$this->assertSame('* * * * 1', (string) $schedule->all()[2]->getExpression()); |
|
83
|
|
|
$this->assertSame('UTC', $schedule->all()[2]->getTimezone()->getName()); |
|
84
|
|
|
$this->assertCount(1, $schedule->all()[2]->getExtensions()); |
|
85
|
|
|
$this->assertSame('task4', $schedule->all()[3]->getDescription()); |
|
86
|
|
|
$this->assertSame('* * * * 1', (string) $schedule->all()[3]->getExpression()); |
|
87
|
|
|
$this->assertSame('UTC', $schedule->all()[3]->getTimezone()->getName()); |
|
88
|
|
|
$this->assertCount(1, $schedule->all()[3]->getExtensions()); |
|
89
|
|
|
$this->assertSame('task5', $schedule->all()[4]->getDescription()); |
|
90
|
|
|
$this->assertSame('* * * * 1', (string) $schedule->all()[4]->getExpression()); |
|
91
|
|
|
$this->assertSame('UTC', $schedule->all()[4]->getTimezone()->getName()); |
|
92
|
|
|
$this->assertCount(1, $schedule->all()[4]->getExtensions()); |
|
93
|
|
|
$this->assertSame('task6', $schedule->all()[5]->getDescription()); |
|
94
|
|
|
$this->assertSame('* * * * 1', (string) $schedule->all()[5]->getExpression()); |
|
95
|
|
|
$this->assertSame('UTC', $schedule->all()[5]->getTimezone()->getName()); |
|
96
|
|
|
$this->assertCount(1, $schedule->all()[5]->getExtensions()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @test |
|
101
|
|
|
*/ |
|
102
|
|
|
public function can_get_due_tasks() |
|
103
|
|
|
{ |
|
104
|
|
|
$schedule = new Schedule(); |
|
105
|
|
|
|
|
106
|
|
|
$schedule->addCallback(function () {})->description('task1'); |
|
107
|
|
|
$notDueTask = $schedule->addProcess('php -v')->description('task2')->sundays(); |
|
108
|
|
|
|
|
109
|
|
|
if ('Sun' === \date('D')) { |
|
110
|
|
|
$notDueTask->mondays(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->assertCount(2, $schedule->all()); |
|
114
|
|
|
$this->assertCount(1, $schedule->due(new \DateTime())); |
|
115
|
|
|
$this->assertCount(1, $schedule->due(new \DateTime()), 'Due tasks are cached'); |
|
116
|
|
|
$this->assertSame('task1', $schedule->due(new \DateTime())[0]->getDescription()); |
|
117
|
|
|
|
|
118
|
|
|
$schedule->addCommand('my:command')->description('task3'); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertCount(2, $schedule->due(new \DateTime()), 'Resets the due task cache'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @test |
|
125
|
|
|
*/ |
|
126
|
|
|
public function due_tasks_are_ordered_by_when_they_are_defined() |
|
127
|
|
|
{ |
|
128
|
|
|
$schedule = new Schedule(); |
|
129
|
|
|
|
|
130
|
|
|
$schedule->addCommand('my:command')->description('task1'); |
|
131
|
|
|
$schedule->addCompound() |
|
132
|
|
|
->addCommand('another:command', [], 'task2') |
|
133
|
|
|
->addCallback(function () {}, 'task3') |
|
134
|
|
|
->addProcess('php -v', 'task4') |
|
135
|
|
|
->addProcess(new Process(['php -v']), 'task5') |
|
136
|
|
|
->onSingleServer() |
|
137
|
|
|
; |
|
138
|
|
|
$schedule->addCommand('my:command')->description('task6'); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertSame( |
|
141
|
|
|
[ |
|
142
|
|
|
'task1', |
|
143
|
|
|
'task2', |
|
144
|
|
|
'task3', |
|
145
|
|
|
'task4', |
|
146
|
|
|
'task5', |
|
147
|
|
|
'task6', |
|
148
|
|
|
], \array_map(function (Task $task) { |
|
149
|
|
|
return $task->getDescription(); |
|
150
|
|
|
}, $schedule->due(new \DateTime())) |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @test |
|
156
|
|
|
*/ |
|
157
|
|
|
public function has_unique_id_based_on_tasks() |
|
158
|
|
|
{ |
|
159
|
|
|
$schedule1 = new Schedule(); |
|
160
|
|
|
$schedule1->addCommand('my:command'); |
|
161
|
|
|
$schedule2 = new Schedule(); |
|
162
|
|
|
$schedule2->addCommand('my:command'); |
|
163
|
|
|
$schedule3 = new Schedule(); |
|
164
|
|
|
$schedule3->addCommand('another:command'); |
|
165
|
|
|
$schedule4 = new Schedule(); |
|
166
|
|
|
$schedule4->addCommand('my:command'); |
|
167
|
|
|
$schedule4->addCommand('another:command'); |
|
168
|
|
|
|
|
169
|
|
|
$this->assertSame((new Schedule())->getId(), (new Schedule())->getId()); |
|
170
|
|
|
$this->assertSame($schedule1->getId(), $schedule2->getId()); |
|
171
|
|
|
$this->assertNotSame($schedule2->getId(), $schedule3->getId()); |
|
172
|
|
|
$this->assertNotSame($schedule2->getId(), $schedule4->getId()); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @test |
|
177
|
|
|
*/ |
|
178
|
|
|
public function can_add_single_server_extension() |
|
179
|
|
|
{ |
|
180
|
|
|
$schedule = new Schedule(); |
|
181
|
|
|
$schedule->addCommand('my:command'); |
|
182
|
|
|
$schedule->onSingleServer(); |
|
183
|
|
|
|
|
184
|
|
|
$this->assertInstanceOf(SingleServerExtension::class, $schedule->getExtensions()[0]); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @test |
|
189
|
|
|
*/ |
|
190
|
|
|
public function can_add_email_on_failure_extension() |
|
191
|
|
|
{ |
|
192
|
|
|
$schedule = new Schedule(); |
|
193
|
|
|
$schedule->emailOnFailure('[email protected]', 'my subject', function (Email $email) { |
|
194
|
|
|
$email->cc('[email protected]'); |
|
195
|
|
|
}); |
|
196
|
|
|
|
|
197
|
|
|
$this->assertTrue($schedule->getExtensions()[0]->isHook(Extension::SCHEDULE_FAILURE)); |
|
|
|
|
|
|
198
|
|
|
$this->assertSame('[email protected]', $schedule->getExtensions()[0]->getEmail()->getTo()[0]->toString()); |
|
|
|
|
|
|
199
|
|
|
$this->assertSame('[email protected]', $schedule->getExtensions()[0]->getEmail()->getCc()[0]->toString()); |
|
200
|
|
|
$this->assertSame('my subject', $schedule->getExtensions()[0]->getEmail()->getSubject()); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @test |
|
205
|
|
|
*/ |
|
206
|
|
|
public function can_add_environment_extension() |
|
207
|
|
|
{ |
|
208
|
|
|
$schedule = new Schedule(); |
|
209
|
|
|
$schedule->environments('prod', 'stage'); |
|
210
|
|
|
|
|
211
|
|
|
$this->assertSame(['prod', 'stage'], $schedule->getExtensions()[0]->getRunEnvironments()); |
|
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @test |
|
216
|
|
|
*/ |
|
217
|
|
|
public function can_set_timezone() |
|
218
|
|
|
{ |
|
219
|
|
|
$schedule = new Schedule(); |
|
220
|
|
|
$schedule->add((new MockTask())->description('task1')); |
|
221
|
|
|
$schedule->add((new MockTask())->description('task2')->timezone('America/Toronto')); |
|
222
|
|
|
|
|
223
|
|
|
$this->assertNull($schedule->getTimezone()); |
|
224
|
|
|
$this->assertNull($schedule->all()[0]->getTimezone()); |
|
225
|
|
|
$this->assertNull($schedule->due(new \DateTime())[0]->getTimezone()); |
|
226
|
|
|
$this->assertSame('America/Toronto', $schedule->all()[1]->getTimezone()->getName()); |
|
227
|
|
|
$this->assertSame('America/Toronto', $schedule->due(new \DateTime())[1]->getTimezone()->getName()); |
|
228
|
|
|
|
|
229
|
|
|
$schedule->timezone('UTC'); |
|
230
|
|
|
|
|
231
|
|
|
$this->assertSame('UTC', $schedule->getTimezone()->getName()); |
|
232
|
|
|
$this->assertSame('UTC', $schedule->all()[0]->getTimezone()->getName()); |
|
233
|
|
|
$this->assertSame('UTC', $schedule->due(new \DateTime())[0]->getTimezone()->getName()); |
|
234
|
|
|
$this->assertSame('America/Toronto', $schedule->all()[1]->getTimezone()->getName()); |
|
235
|
|
|
$this->assertSame('America/Toronto', $schedule->due(new \DateTime())[1]->getTimezone()->getName()); |
|
236
|
|
|
|
|
237
|
|
|
$schedule->timezone(new \DateTimeZone('America/Los_Angeles')); |
|
238
|
|
|
|
|
239
|
|
|
$this->assertSame('America/Los_Angeles', $schedule->getTimezone()->getName()); |
|
240
|
|
|
$this->assertSame('UTC', $schedule->all()[0]->getTimezone()->getName()); |
|
241
|
|
|
$this->assertSame('UTC', $schedule->due(new \DateTime())[0]->getTimezone()->getName()); |
|
242
|
|
|
$this->assertSame('America/Toronto', $schedule->all()[1]->getTimezone()->getName()); |
|
243
|
|
|
$this->assertSame('America/Toronto', $schedule->due(new \DateTime())[1]->getTimezone()->getName()); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths