|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Tests\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
|
|
6
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
7
|
|
|
use Zenstruck\ScheduleBundle\DependencyInjection\Configuration; |
|
8
|
|
|
use Zenstruck\ScheduleBundle\EventListener\TaskConfigurationSubscriber; |
|
9
|
|
|
use Zenstruck\ScheduleBundle\Schedule; |
|
10
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask; |
|
11
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\PingTask; |
|
12
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\ProcessTask; |
|
13
|
|
|
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Kevin Bond <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
final class TaskConfigurationSubscriberTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @test |
|
22
|
|
|
*/ |
|
23
|
|
|
public function minimal_task_configuration() |
|
24
|
|
|
{ |
|
25
|
|
|
$schedule = $this->createSchedule([ |
|
26
|
|
|
[ |
|
27
|
|
|
'task' => 'my:command', |
|
28
|
|
|
'frequency' => '0 * * * *', |
|
29
|
|
|
], |
|
30
|
|
|
[ |
|
31
|
|
|
'task' => 'another:command', |
|
32
|
|
|
'frequency' => '@yearly', |
|
33
|
|
|
], |
|
34
|
|
|
]); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertCount(2, $schedule->all()); |
|
37
|
|
|
|
|
38
|
|
|
[$task1, $task2] = $schedule->all(); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertInstanceOf(CommandTask::class, $task1); |
|
41
|
|
|
$this->assertSame('my:command', $task1->getDescription()); |
|
42
|
|
|
$this->assertNull($task1->getTimezone()); |
|
43
|
|
|
$this->assertSame('0 * * * *', (string) $task1->getExpression()); |
|
44
|
|
|
$this->assertCount(0, $task1->getExtensions()); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertInstanceOf(CommandTask::class, $task2); |
|
47
|
|
|
$this->assertSame('another:command', $task2->getDescription()); |
|
48
|
|
|
$this->assertNull($task1->getTimezone()); |
|
49
|
|
|
$this->assertSame('@yearly', (string) $task2->getExpression()); |
|
50
|
|
|
$this->assertCount(0, $task2->getExtensions()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @test |
|
55
|
|
|
*/ |
|
56
|
|
|
public function can_configure_process_tasks() |
|
57
|
|
|
{ |
|
58
|
|
|
$schedule = $this->createSchedule([ |
|
59
|
|
|
[ |
|
60
|
|
|
'task' => 'bash: /bin/script', |
|
61
|
|
|
'frequency' => '0 * * * *', |
|
62
|
|
|
], |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertCount(1, $schedule->all()); |
|
66
|
|
|
$this->assertInstanceOf(ProcessTask::class, $schedule->all()[0]); |
|
67
|
|
|
$this->assertSame('/bin/script', $schedule->all()[0]->getDescription()); |
|
68
|
|
|
$this->assertSame('0 * * * *', (string) $schedule->all()[0]->getExpression()); |
|
69
|
|
|
$this->assertCount(0, $schedule->all()[0]->getExtensions()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @test |
|
74
|
|
|
*/ |
|
75
|
|
|
public function can_configure_ping_tasks() |
|
76
|
|
|
{ |
|
77
|
|
|
$schedule = $this->createSchedule([ |
|
78
|
|
|
[ |
|
79
|
|
|
'task' => 'ping: https://example.com', |
|
80
|
|
|
'frequency' => '0 * * * *', |
|
81
|
|
|
], |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertCount(1, $schedule->all()); |
|
85
|
|
|
$this->assertInstanceOf(PingTask::class, $schedule->all()[0]); |
|
86
|
|
|
$this->assertSame('Ping https://example.com', $schedule->all()[0]->getDescription()); |
|
87
|
|
|
$this->assertSame('0 * * * *', (string) $schedule->all()[0]->getExpression()); |
|
88
|
|
|
$this->assertCount(0, $schedule->all()[0]->getExtensions()); |
|
89
|
|
|
$this->assertSame('https://example.com', $schedule->all()[0]->getUrl()); |
|
90
|
|
|
$this->assertSame('GET', $schedule->all()[0]->getMethod()); |
|
91
|
|
|
$this->assertSame([], $schedule->all()[0]->getOptions()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @test |
|
96
|
|
|
*/ |
|
97
|
|
|
public function can_configure_compound_task() |
|
98
|
|
|
{ |
|
99
|
|
|
$schedule = $this->createSchedule([ |
|
100
|
|
|
[ |
|
101
|
|
|
'task' => [ |
|
102
|
|
|
'my:command arg --option=foo', |
|
103
|
|
|
'bash:/my-script', |
|
104
|
|
|
'ping:https://example.com', |
|
105
|
|
|
], |
|
106
|
|
|
'frequency' => '0 * * * *', |
|
107
|
|
|
'without_overlapping' => null, |
|
108
|
|
|
], |
|
109
|
|
|
]); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertCount(3, $schedule->all()); |
|
112
|
|
|
|
|
113
|
|
|
[$task1, $task2, $task3] = $schedule->all(); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertInstanceOf(CommandTask::class, $task1); |
|
116
|
|
|
$this->assertSame('my:command', $task1->getDescription()); |
|
117
|
|
|
$this->assertSame('0 * * * *', (string) $task1->getExpression()); |
|
118
|
|
|
$this->assertCount(1, $task1->getExtensions()); |
|
119
|
|
|
$this->assertSame('Without overlapping', (string) $task1->getExtensions()[0]); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertInstanceOf(ProcessTask::class, $task2); |
|
122
|
|
|
$this->assertSame('/my-script', $task2->getDescription()); |
|
123
|
|
|
$this->assertSame('0 * * * *', (string) $task2->getExpression()); |
|
124
|
|
|
$this->assertCount(1, $task2->getExtensions()); |
|
125
|
|
|
$this->assertSame('Without overlapping', (string) $task2->getExtensions()[0]); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertInstanceOf(PingTask::class, $task3); |
|
128
|
|
|
$this->assertSame('Ping https://example.com', $task3->getDescription()); |
|
129
|
|
|
$this->assertSame('0 * * * *', (string) $task3->getExpression()); |
|
130
|
|
|
$this->assertCount(1, $task3->getExtensions()); |
|
131
|
|
|
$this->assertSame('Without overlapping', (string) $task3->getExtensions()[0]); |
|
132
|
|
|
$this->assertSame('https://example.com', $task3->getUrl()); |
|
133
|
|
|
$this->assertSame('GET', $task3->getMethod()); |
|
134
|
|
|
$this->assertSame([], $task3->getOptions()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @test |
|
139
|
|
|
*/ |
|
140
|
|
|
public function can_configure_compound_task_with_descriptions() |
|
141
|
|
|
{ |
|
142
|
|
|
$schedule = $this->createSchedule([ |
|
143
|
|
|
[ |
|
144
|
|
|
'task' => [ |
|
145
|
|
|
'my command' => 'my:command arg --option=foo', |
|
146
|
|
|
'another command' => 'bash:/my-script', |
|
147
|
|
|
], |
|
148
|
|
|
'frequency' => '0 * * * *', |
|
149
|
|
|
'without_overlapping' => null, |
|
150
|
|
|
], |
|
151
|
|
|
]); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertCount(2, $schedule->all()); |
|
154
|
|
|
|
|
155
|
|
|
[$task1, $task2] = $schedule->all(); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertInstanceOf(CommandTask::class, $task1); |
|
158
|
|
|
$this->assertSame('my command', $task1->getDescription()); |
|
159
|
|
|
$this->assertSame('0 * * * *', (string) $task1->getExpression()); |
|
160
|
|
|
$this->assertCount(1, $task1->getExtensions()); |
|
161
|
|
|
$this->assertSame('Without overlapping', (string) $task1->getExtensions()[0]); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertInstanceOf(ProcessTask::class, $task2); |
|
164
|
|
|
$this->assertSame('another command', $task2->getDescription()); |
|
165
|
|
|
$this->assertSame('0 * * * *', (string) $task2->getExpression()); |
|
166
|
|
|
$this->assertCount(1, $task2->getExtensions()); |
|
167
|
|
|
$this->assertSame('Without overlapping', (string) $task2->getExtensions()[0]); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @test |
|
172
|
|
|
*/ |
|
173
|
|
|
public function can_configure_hashed_frequency_expression() |
|
174
|
|
|
{ |
|
175
|
|
|
$schedule = $this->createSchedule([ |
|
176
|
|
|
[ |
|
177
|
|
|
'task' => 'my:command1', |
|
178
|
|
|
'frequency' => '# * * * *', |
|
179
|
|
|
], |
|
180
|
|
|
[ |
|
181
|
|
|
'task' => 'my:command1', |
|
182
|
|
|
'frequency' => '# * * * *', |
|
183
|
|
|
'description' => 'my description', |
|
184
|
|
|
], |
|
185
|
|
|
[ |
|
186
|
|
|
'task' => 'my:command2', |
|
187
|
|
|
'frequency' => '# #(9-17) * * *', |
|
188
|
|
|
], |
|
189
|
|
|
[ |
|
190
|
|
|
'task' => 'my:command3', |
|
191
|
|
|
'frequency' => '#daily', |
|
192
|
|
|
], |
|
193
|
|
|
[ |
|
194
|
|
|
'task' => 'my:command4', |
|
195
|
|
|
'frequency' => '#midnight', |
|
196
|
|
|
], |
|
197
|
|
|
]); |
|
198
|
|
|
|
|
199
|
|
|
$this->assertCount(5, $schedule->all()); |
|
200
|
|
|
|
|
201
|
|
|
[$task1, $task2, $task3, $task4, $task5] = $schedule->all(); |
|
202
|
|
|
|
|
203
|
|
|
$this->assertSame('16 * * * *', (string) $task1->getExpression()); |
|
204
|
|
|
$this->assertSame('# * * * *', $task1->getExpression()->getRawValue()); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertSame('10 * * * *', (string) $task2->getExpression(), 'Different description changes minute'); |
|
207
|
|
|
$this->assertSame('# * * * *', $task2->getExpression()->getRawValue()); |
|
208
|
|
|
|
|
209
|
|
|
$this->assertSame('9 12 * * *', (string) $task3->getExpression()); |
|
210
|
|
|
$this->assertSame('# #(9-17) * * *', $task3->getExpression()->getRawValue()); |
|
211
|
|
|
|
|
212
|
|
|
$this->assertSame('29 17 * * *', (string) $task4->getExpression()); |
|
213
|
|
|
$this->assertSame('#daily', $task4->getExpression()->getRawValue()); |
|
214
|
|
|
|
|
215
|
|
|
$this->assertSame('11 2 * * *', (string) $task5->getExpression()); |
|
216
|
|
|
$this->assertSame('#midnight', $task5->getExpression()->getRawValue()); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @test |
|
221
|
|
|
*/ |
|
222
|
|
|
public function full_task_configuration() |
|
223
|
|
|
{ |
|
224
|
|
|
$schedule = $this->createSchedule([ |
|
225
|
|
|
[ |
|
226
|
|
|
'task' => 'my:command --option', |
|
227
|
|
|
'frequency' => '0 0 * * *', |
|
228
|
|
|
'description' => 'my description', |
|
229
|
|
|
'timezone' => 'UTC', |
|
230
|
|
|
'without_overlapping' => null, |
|
231
|
|
|
'only_between' => [ |
|
232
|
|
|
'start' => 9, |
|
233
|
|
|
'end' => 17, |
|
234
|
|
|
], |
|
235
|
|
|
'unless_between' => [ |
|
236
|
|
|
'start' => 12, |
|
237
|
|
|
'end' => '13:30', |
|
238
|
|
|
], |
|
239
|
|
|
'ping_before' => [ |
|
240
|
|
|
'url' => 'https://example.com/before', |
|
241
|
|
|
], |
|
242
|
|
|
'ping_after' => [ |
|
243
|
|
|
'url' => 'https://example.com/after', |
|
244
|
|
|
], |
|
245
|
|
|
'ping_on_success' => [ |
|
246
|
|
|
'url' => 'https://example.com/success', |
|
247
|
|
|
], |
|
248
|
|
|
'ping_on_failure' => [ |
|
249
|
|
|
'url' => 'https://example.com/failure', |
|
250
|
|
|
'method' => 'POST', |
|
251
|
|
|
], |
|
252
|
|
|
'email_after' => null, |
|
253
|
|
|
'email_on_failure' => [ |
|
254
|
|
|
'to' => '[email protected]', |
|
255
|
|
|
'subject' => 'my subject', |
|
256
|
|
|
], |
|
257
|
|
|
], |
|
258
|
|
|
]); |
|
259
|
|
|
|
|
260
|
|
|
$task = $schedule->all()[0]; |
|
261
|
|
|
$extensions = $task->getExtensions(); |
|
262
|
|
|
|
|
263
|
|
|
$this->assertSame('my description', $task->getDescription()); |
|
264
|
|
|
$this->assertSame('UTC', $task->getTimezone()->getName()); |
|
265
|
|
|
$this->assertCount(9, $extensions); |
|
266
|
|
|
$this->assertSame('Without overlapping', (string) $extensions[0]); |
|
267
|
|
|
$this->assertSame('Only run between 9:00 and 17:00', (string) $extensions[1]); |
|
268
|
|
|
$this->assertSame('Only run if not between 12:00 and 13:30', (string) $extensions[2]); |
|
269
|
|
|
$this->assertSame('Before Task, ping "https://example.com/before"', (string) $extensions[3]); |
|
270
|
|
|
$this->assertSame('After Task, ping "https://example.com/after"', (string) $extensions[4]); |
|
271
|
|
|
$this->assertSame('On Task Success, ping "https://example.com/success"', (string) $extensions[5]); |
|
272
|
|
|
$this->assertSame('On Task Failure, ping "https://example.com/failure"', (string) $extensions[6]); |
|
273
|
|
|
$this->assertSame('After Task, email output', (string) $extensions[7]); |
|
274
|
|
|
$this->assertSame('On Task Failure, email output to "[email protected]"', (string) $extensions[8]); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
private function createSchedule(array $taskConfig): Schedule |
|
278
|
|
|
{ |
|
279
|
|
|
$processor = new Processor(); |
|
280
|
|
|
$config = $processor->processConfiguration(new Configuration(), [['tasks' => $taskConfig]]); |
|
281
|
|
|
|
|
282
|
|
|
return (new MockScheduleBuilder()) |
|
283
|
|
|
->addSubscriber(new TaskConfigurationSubscriber($config['tasks'])) |
|
284
|
|
|
->getRunner() |
|
285
|
|
|
->buildSchedule() |
|
286
|
|
|
; |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
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