ILIAS  release_8 Revision v8.24
CronJobScheduleTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
22
27class CronJobScheduleTest extends TestCase
28{
29 private DateTimeImmutable $now;
30
31 private DateTimeImmutable $this_quarter_start;
32
33 private function getJob(
34 bool $has_flexible_schedule,
35 int $default_schedule_type,
36 ?int $default_schedule_value,
37 int $schedule_type,
38 ?int $schedule_value
39 ): ilCronJob {
40 $job_instance = new class ($has_flexible_schedule, $default_schedule_type, $default_schedule_value, $schedule_type, $schedule_value) extends ilCronJob {
41 private bool $has_flexible_schedule;
42 private int $default_schedule_type;
43 private ?int $default_schedule_value;
44
45 public function __construct(
46 bool $has_flexible_schedule,
47 int $default_schedule_type,
48 ?int $default_schedule_value,
49 int $schedule_type,
50 ?int $schedule_value
51 ) {
52 $this->has_flexible_schedule = $has_flexible_schedule;
53 $this->schedule_type = $schedule_type;
54 $this->schedule_value = $schedule_value;
55 $this->default_schedule_type = $default_schedule_type;
56 $this->default_schedule_value = $default_schedule_value;
57 }
58
59 public function getId(): string
60 {
61 return 'phpunit';
62 }
63
64 public function getTitle(): string
65 {
66 return 'phpunit';
67 }
68
69 public function getDescription(): string
70 {
71 return 'phpunit';
72 }
73
74 public function hasAutoActivation(): bool
75 {
76 return false;
77 }
78
79 public function hasFlexibleSchedule(): bool
80 {
81 return $this->has_flexible_schedule;
82 }
83
84 public function getDefaultScheduleType(): int
85 {
86 return $this->default_schedule_type;
87 }
88
89 public function getDefaultScheduleValue(): ?int
90 {
91 return $this->default_schedule_value;
92 }
93
94 public function run(): ilCronJobResult
95 {
96 return new ilCronJobResult();
97 }
98 };
99
100 $job_instance->setDateTimeProvider(function (): DateTimeImmutable {
101 return $this->now;
102 });
103
104 return $job_instance;
105 }
106
107 public function jobProvider(): array
108 {
109 // Can't be moved to setUp(), because the data provider is executed before the tests are executed
110 $this->now = new DateTimeImmutable('@' . time());
111
112 $offset = (((int) $this->now->format('n')) - 1) % 3;
113 $this->this_quarter_start = $this->now->modify("first day of -$offset month midnight");
114
115 return [
116 'Manual Run is Always Due' => [
117 $this->getJob(true, ilCronJob::SCHEDULE_TYPE_DAILY, null, ilCronJob::SCHEDULE_TYPE_DAILY, null),
118 true,
119 null,
121 null,
122 true
123 ],
124 'Job Without Any Run is Always Due' => [
125 $this->getJob(true, ilCronJob::SCHEDULE_TYPE_DAILY, null, ilCronJob::SCHEDULE_TYPE_DAILY, null),
126 false,
127 null,
129 null,
130 true
131 ],
132 'Daily Schedule / Did not run Today' => [
133 $this->getJob(true, ilCronJob::SCHEDULE_TYPE_DAILY, null, ilCronJob::SCHEDULE_TYPE_DAILY, null),
134 false,
135 $this->now->modify('-1 day'),
137 null,
138 true
139 ],
140 'Daily Schedule / Did run Today' => [
141 $this->getJob(true, ilCronJob::SCHEDULE_TYPE_DAILY, null, ilCronJob::SCHEDULE_TYPE_DAILY, null),
142 false,
143 $this->now,
145 null,
146 false
147 ],
148 'Weekly Schedule / Did not run this Week' => [
150 false,
151 $this->now->modify('-1 week'),
153 null,
154 true
155 ],
156 'Weekly Schedule / Did run this Week' => [
158 false,
159 $this->now->modify('monday this week'),
161 null,
162 false
163 ],
164 'Monthly Schedule / Did not run this Month' => [
166 false,
167 $this->now->modify('last day of last month'),
169 null,
170 true
171 ],
172 'Monthly Schedule / Did run this Month' => [
174 false,
175 $this->now->modify('first day of this month'),
177 null,
178 false
179 ],
180 'Yearly Schedule / Did not run this Year' => [
182 false,
183 $this->now->modify('-1 year'),
185 null,
186 true
187 ],
188 'Yearly Schedule / Did run this Year' => [
190 false,
191 $this->now->modify('first day of January this year'),
193 null,
194 false
195 ],
196 'Quarterly Schedule / Did not run this Quarter' => [
198 false,
199 $this->this_quarter_start->modify('-1 seconds'),
201 null,
202 true
203 ],
204 'Quarterly Schedule / Did run this Quarter' => [
206 false,
207 $this->this_quarter_start->modify('+30 seconds'),
209 null,
210 false
211 ],
212 'Minutely Schedule / Did not run this Minute' => [
214 false,
215 $this->now->modify('-1 minute'),
217 1,
218 true
219 ],
220 'Minutely Schedule / Did run this Minute' => [
222 false,
223 $this->now->modify('-30 seconds'),
225 1,
226 false
227 ],
228 'Hourly Schedule / Did not run this Hour' => [
230 false,
231 $this->now->modify('-7 hours'),
233 7,
234 true
235 ],
236 'Hourly Schedule / Did run this Hour' => [
238 false,
239 $this->now->modify('-7 hours +30 seconds'),
241 7,
242 false
243 ],
244 'Every 5 Days Schedule / Did not run for 5 Days' => [
246 false,
247 $this->now->modify('-5 days'),
249 5,
250 true
251 ],
252 'Every 5 Days Schedule / Did run withing the last 5 Days' => [
254 false,
255 $this->now->modify('-4 days'),
257 5,
258 false
259 ],
260 'Invalid Schedule Type' => [
261 $this->getJob(true, PHP_INT_MAX, 5, PHP_INT_MAX, 5),
262 false,
263 $this->now,
264 PHP_INT_MAX,
265 5,
266 false
267 ]
268 ];
269 }
270
274 public function testSchedule(
275 ilCronJob $job_instance,
276 bool $is_manual_run,
277 ?DateTimeImmutable $last_run_datetime,
278 int $schedule_type,
279 ?int $schedule_value,
280 bool $should_be_due
281 ): void {
282 $this->assertSame(
283 $should_be_due,
284 $job_instance->isDue($last_run_datetime, $schedule_type, $schedule_value, $is_manual_run),
285 'Last run: ' . ($last_run_datetime ? $last_run_datetime->format(DATE_ATOM) : 'never')
286 );
287 }
288
289 public function weeklyScheduleProvider(): Generator
290 {
291 yield 'Different Week' => [
293 function (): DateTimeImmutable {
294 $this->now = new DateTimeImmutable('@1672570104'); // Sun Jan 01 2023 10:48:24 GMT+0000 (year: 2023 / week: 52)
295
296 return $this->now->modify('-1 week'); // Sun Dec 25 2022 10:48:24 GMT+0000 (year: 2022 / week: 51)
297 },
298 true
299 ];
300
301 yield 'Same Week and Year, but different Month: December (now) and January (Last run)' => [
303 function (): DateTimeImmutable {
304 $this->now = new DateTimeImmutable('@1703669703'); // Wed Dec 27 2023 09:35:03 GMT+0000 (year: 2023 / week: 52 / month: 12)
305
306 return new DateTimeImmutable('@1672570104'); // Sun Jan 01 2023 10:48:24 GMT+0000 (year: 2023 / week: 52 / month: 1)
307 },
308 true
309 ];
310
311 yield 'Same Week and Year and same Month: January' => [
313 function (): DateTimeImmutable {
314 $this->now = new DateTimeImmutable('@1704188103'); // Tue Jan 02 2024 09:35:03 GMT+0000 (year: 2024 / week: 1 / month: 1)
315
316 return $this->now->modify('-1 day'); // Mon Jan 01 2024 09:35:03 GMT+0000 (year: 2024 / week: 1 / month: 1)
317 },
318 false
319 ];
320
321 yield 'Same Week (52nd), but Year Difference > 1' => [
323 function (): DateTimeImmutable {
324 $this->now = new DateTimeImmutable('@1672570104'); // Sun Jan 01 2023 10:48:24 GMT+0000 (year: 2023 / week: 52)
325
326 return $this->now->modify('tuesday this week')->modify('-1 year'); // Mon Dec 27 2021 10:48:24 GMT+0000 (year: 2021 / week: 52)
327 },
328 true
329 ];
330
331 yield 'Same Week (52nd) in different Years, but Turn of the Year' => [
333 function (): DateTimeImmutable {
334 $this->now = new DateTimeImmutable('@1672570104'); // Sun Jan 01 2023 10:48:24 GMT+0000 (year: 2023 / week: 52 / month: 1)
335
336 return $this->now->modify('monday this week'); // Mon Dec 26 2022 10:48:24 GMT+0000 (year: 2022 / week: 52 / month: 12)
337 },
338 false
339 ];
340
341 yield 'Same Week (52nd) in different Years, but not Turn of the Year' => [
343 function (): DateTimeImmutable {
344 $this->now = new DateTimeImmutable('@1703669703'); // Wed Dec 27 2023 09:35:03 GMT+0000 (year: 2023 / week: 52 / month: 12)
345
346 return new DateTimeImmutable('@1672012800'); // Mon Dec 26 2022 00:00:00 GMT+0000 (year: 2022 / week: 52 / month: 12)
347 },
348 true
349 ];
350 }
351
356 public function testWeeklySchedules(
357 ilCronJob $job_instance,
358 callable $last_run_datetime_provider,
359 bool $should_be_due
360 ): void {
361 $last_run_datetime = $last_run_datetime_provider();
362
363 $this->assertSame(
364 $should_be_due,
365 $job_instance->isDue(
366 $last_run_datetime,
367 $job_instance->getScheduleType(),
368 $job_instance->getScheduleValue(),
369 false
370 ),
371 'Last run: ' . $last_run_datetime->format(DATE_ATOM)
372 );
373 }
374}
Class CronJobScheduleTest.
getJob(bool $has_flexible_schedule, int $default_schedule_type, ?int $default_schedule_value, int $schedule_type, ?int $schedule_value)
testSchedule(ilCronJob $job_instance, bool $is_manual_run, ?DateTimeImmutable $last_run_datetime, int $schedule_type, ?int $schedule_value, bool $should_be_due)
@dataProvider jobProvider
DateTimeImmutable $this_quarter_start
DateTimeImmutable $now
testWeeklySchedules(ilCronJob $job_instance, callable $last_run_datetime_provider, bool $should_be_due)
@dataProvider weeklyScheduleProvider
return true
getScheduleType()
Get current schedule type (if flexible)
const SCHEDULE_TYPE_IN_DAYS
@depracated This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_IN_HOURS
@depracated This will be replaced with an ENUM in ILIAS 9
isDue(?DateTimeImmutable $last_run, ?int $schedule_type, ?int $schedule_value, bool $is_manually_executed=false)
const SCHEDULE_TYPE_IN_MINUTES
@depracated This will be replaced with an ENUM in ILIAS 9
getScheduleValue()
Get current schedule value (if flexible)
const SCHEDULE_TYPE_WEEKLY
@depracated This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_YEARLY
@depracated This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_DAILY
@depracated This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_QUARTERLY
@depracated This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_MONTHLY
@depracated This will be replaced with an ENUM in ILIAS 9
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc