ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
CronJobScheduleTest Class Reference

Class CronJobScheduleTest. More...

+ Inheritance diagram for CronJobScheduleTest:
+ Collaboration diagram for CronJobScheduleTest:

Public Member Functions

 jobProvider ()
 
 testSchedule (ilCronJob $job_instance, bool $is_manual_run, ?DateTimeImmutable $last_run_datetime, int $schedule_type, ?int $schedule_value, bool $should_be_due)
 jobProvider More...
 
 weeklyScheduleProvider ()
 
 testWeeklySchedules (ilCronJob $job_instance, callable $last_run_datetime_provider, bool $should_be_due)
 weeklyScheduleProvider More...
 

Private Member Functions

 getJob (bool $has_flexible_schedule, int $default_schedule_type, ?int $default_schedule_value, int $schedule_type, ?int $schedule_value)
 

Private Attributes

DateTimeImmutable $now
 
DateTimeImmutable $this_quarter_start
 

Detailed Description

Class CronJobScheduleTest.

Author
Michael Jansen mjans.nosp@m.en@d.nosp@m.ataba.nosp@m.y.de

Definition at line 27 of file CronJobScheduleTest.php.

Member Function Documentation

◆ getJob()

CronJobScheduleTest::getJob ( bool  $has_flexible_schedule,
int  $default_schedule_type,
?int  $default_schedule_value,
int  $schedule_type,
?int  $schedule_value 
)
private

Definition at line 33 of file CronJobScheduleTest.php.

References $now, ILIAS\GlobalScreen\Provider\__construct(), and ILIAS\Survey\Mode\getId().

Referenced by jobProvider(), and weeklyScheduleProvider().

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  }
DateTimeImmutable $now
__construct(Container $dic, ilPlugin $plugin)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ jobProvider()

CronJobScheduleTest::jobProvider ( )

Definition at line 107 of file CronJobScheduleTest.php.

References $now, getJob(), ILIAS\Repository\int(), ilCronJob\SCHEDULE_TYPE_DAILY, ilCronJob\SCHEDULE_TYPE_IN_DAYS, ilCronJob\SCHEDULE_TYPE_IN_HOURS, ilCronJob\SCHEDULE_TYPE_IN_MINUTES, ilCronJob\SCHEDULE_TYPE_MONTHLY, ilCronJob\SCHEDULE_TYPE_QUARTERLY, ilCronJob\SCHEDULE_TYPE_WEEKLY, and ilCronJob\SCHEDULE_TYPE_YEARLY.

107  : 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' => [
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  }
const SCHEDULE_TYPE_IN_MINUTES
This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_MONTHLY
This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_WEEKLY
This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_IN_DAYS
This will be replaced with an ENUM in ILIAS 9
DateTimeImmutable $now
const SCHEDULE_TYPE_YEARLY
This will be replaced with an ENUM in ILIAS 9
getJob(bool $has_flexible_schedule, int $default_schedule_type, ?int $default_schedule_value, int $schedule_type, ?int $schedule_value)
const SCHEDULE_TYPE_DAILY
This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_QUARTERLY
This will be replaced with an ENUM in ILIAS 9
const SCHEDULE_TYPE_IN_HOURS
This will be replaced with an ENUM in ILIAS 9
+ Here is the call graph for this function:

◆ testSchedule()

CronJobScheduleTest::testSchedule ( ilCronJob  $job_instance,
bool  $is_manual_run,
?DateTimeImmutable  $last_run_datetime,
int  $schedule_type,
?int  $schedule_value,
bool  $should_be_due 
)

jobProvider

Definition at line 274 of file CronJobScheduleTest.php.

References ilCronJob\isDue().

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  }
isDue(?DateTimeImmutable $last_run, ?int $schedule_type, ?int $schedule_value, bool $is_manually_executed=false)
+ Here is the call graph for this function:

◆ testWeeklySchedules()

CronJobScheduleTest::testWeeklySchedules ( ilCronJob  $job_instance,
callable  $last_run_datetime_provider,
bool  $should_be_due 
)

weeklyScheduleProvider

Parameters
callable()DateTimeImmutable $last_run_datetime_provider

Definition at line 356 of file CronJobScheduleTest.php.

References ilCronJob\getScheduleType(), ilCronJob\getScheduleValue(), and ilCronJob\isDue().

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  }
getScheduleValue()
Get current schedule value (if flexible)
getScheduleType()
Get current schedule type (if flexible)
isDue(?DateTimeImmutable $last_run, ?int $schedule_type, ?int $schedule_value, bool $is_manually_executed=false)
+ Here is the call graph for this function:

◆ weeklyScheduleProvider()

CronJobScheduleTest::weeklyScheduleProvider ( )

Definition at line 289 of file CronJobScheduleTest.php.

References getJob(), and ilCronJob\SCHEDULE_TYPE_WEEKLY.

289  : 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  }
const SCHEDULE_TYPE_WEEKLY
This will be replaced with an ENUM in ILIAS 9
getJob(bool $has_flexible_schedule, int $default_schedule_type, ?int $default_schedule_value, int $schedule_type, ?int $schedule_value)
+ Here is the call graph for this function:

Field Documentation

◆ $now

DateTimeImmutable CronJobScheduleTest::$now
private

Definition at line 29 of file CronJobScheduleTest.php.

Referenced by getJob(), and jobProvider().

◆ $this_quarter_start

DateTimeImmutable CronJobScheduleTest::$this_quarter_start
private

Definition at line 31 of file CronJobScheduleTest.php.


The documentation for this class was generated from the following file: