19declare(strict_types=1);
21use PHPUnit\Framework\TestCase;
29 private DateTimeImmutable
$now;
34 bool $has_flexible_schedule,
35 int $default_schedule_type,
36 ?
int $default_schedule_value,
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;
46 bool $has_flexible_schedule,
47 int $default_schedule_type,
48 ?
int $default_schedule_value,
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;
59 public function getId(): string
64 public function getTitle(): string
69 public function getDescription(): string
74 public function hasAutoActivation(): bool
79 public function hasFlexibleSchedule(): bool
81 return $this->has_flexible_schedule;
84 public function getDefaultScheduleType():
int
86 return $this->default_schedule_type;
89 public function getDefaultScheduleValue(): ?
int
91 return $this->default_schedule_value;
100 $job_instance->setDateTimeProvider(
function (): DateTimeImmutable {
104 return $job_instance;
110 $this->now =
new DateTimeImmutable(
'@' . time());
112 $offset = (((
int) $this->now->format(
'n')) - 1) % 3;
113 $this->this_quarter_start = $this->now->modify(
"first day of -$offset month midnight");
116 'Manual Run is Always Due' => [
124 'Job Without Any Run is Always Due' => [
132 'Daily Schedule / Did not run Today' => [
135 $this->now->modify(
'-1 day'),
140 'Daily Schedule / Did run Today' => [
148 'Weekly Schedule / Did not run this Week' => [
151 $this->now->modify(
'-1 week'),
156 'Weekly Schedule / Did run this Week' => [
159 $this->now->modify(
'monday this week'),
164 'Monthly Schedule / Did not run this Month' => [
167 $this->now->modify(
'last day of last month'),
172 'Monthly Schedule / Did run this Month' => [
175 $this->now->modify(
'first day of this month'),
180 'Yearly Schedule / Did not run this Year' => [
183 $this->now->modify(
'-1 year'),
188 'Yearly Schedule / Did run this Year' => [
191 $this->now->modify(
'first day of January this year'),
196 'Quarterly Schedule / Did not run this Quarter' => [
199 $this->this_quarter_start->modify(
'-1 seconds'),
204 'Quarterly Schedule / Did run this Quarter' => [
207 $this->this_quarter_start->modify(
'+30 seconds'),
212 'Minutely Schedule / Did not run this Minute' => [
215 $this->now->modify(
'-1 minute'),
220 'Minutely Schedule / Did run this Minute' => [
223 $this->now->modify(
'-30 seconds'),
228 'Hourly Schedule / Did not run this Hour' => [
231 $this->now->modify(
'-7 hours'),
236 'Hourly Schedule / Did run this Hour' => [
239 $this->now->modify(
'-7 hours +30 seconds'),
244 'Every 5 Days Schedule / Did not run for 5 Days' => [
247 $this->now->modify(
'-5 days'),
252 'Every 5 Days Schedule / Did run withing the last 5 Days' => [
255 $this->now->modify(
'-4 days'),
260 'Invalid Schedule Type' => [
261 $this->getJob(
true, PHP_INT_MAX, 5, PHP_INT_MAX, 5),
277 ?DateTimeImmutable $last_run_datetime,
279 ?
int $schedule_value,
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')
291 yield
'Different Week' => [
293 function (): DateTimeImmutable {
294 $this->now =
new DateTimeImmutable(
'@1672570104');
296 return $this->now->modify(
'-1 week');
301 yield
'Same Week and Year, but different Month: December (now) and January (Last run)' => [
303 function (): DateTimeImmutable {
304 $this->now =
new DateTimeImmutable(
'@1703669703');
306 return new DateTimeImmutable(
'@1672570104');
311 yield
'Same Week and Year and same Month: January' => [
313 function (): DateTimeImmutable {
314 $this->now =
new DateTimeImmutable(
'@1704188103');
316 return $this->now->modify(
'-1 day');
321 yield
'Same Week (52nd), but Year Difference > 1' => [
323 function (): DateTimeImmutable {
324 $this->now =
new DateTimeImmutable(
'@1672570104');
326 return $this->now->modify(
'tuesday this week')->modify(
'-1 year');
331 yield
'Same Week (52nd) in different Years, but Turn of the Year' => [
333 function (): DateTimeImmutable {
334 $this->now =
new DateTimeImmutable(
'@1672570104');
336 return $this->now->modify(
'monday this week');
341 yield
'Same Week (52nd) in different Years, but not Turn of the Year' => [
343 function (): DateTimeImmutable {
344 $this->now =
new DateTimeImmutable(
'@1703669703');
346 return new DateTimeImmutable(
'@1672012800');
358 callable $last_run_datetime_provider,
361 $last_run_datetime = $last_run_datetime_provider();
365 $job_instance->
isDue(
371 'Last run: ' . $last_run_datetime->format(DATE_ATOM)
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
testWeeklySchedules(ilCronJob $job_instance, callable $last_run_datetime_provider, bool $should_be_due)
@dataProvider weeklyScheduleProvider
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