ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CronJobEntityTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
26use PHPUnit\Framework\Attributes\Depends;
27
28class CronJobEntityTest extends TestCase
29{
30 private function getEntity(
31 ?CronJob $job_instance = null,
32 ?int $schedule_type = null,
33 int $schedule_value = 5,
34 bool $is_plugin = false
35 ): JobEntity {
36 $job_instance ??= $this->createMock(CronJob::class);
37
38 if ($schedule_type === null) {
39 $schedule_type = JobScheduleType::IN_MINUTES->value;
40 }
41
42 return new JobEntity($job_instance, [
43 'job_id' => 'phpunit',
44 'component' => 'phpunit',
45 'schedule_type' => $schedule_type,
46 'schedule_value' => $schedule_value,
47 'job_status' => 1,
48 'job_status_user_id' => 6,
49 'job_status_type' => 1,
50 'job_status_ts' => time(),
51 'job_result_status' => JobResult::STATUS_OK,
52 'job_result_user_id' => 6,
53 'job_result_code' => JobResult::CODE_NO_RESULT,
54 'job_result_message' => 'msg',
55 'job_result_type' => 1,
56 'job_result_ts' => time(),
57 'class' => 'Job',
58 'path' => '/',
59 'running_ts' => time(),
60 'job_result_dur' => time(),
61 'alive_ts' => time(),
62 ], $is_plugin);
63 }
64
65 public function testEntityCollectionCanBeCreatedWithItems(): \ILIAS\Cron\Job\Collection\JobEntities
66 {
67 $entities = new \ILIAS\Cron\Job\Collection\JobEntities($this->getEntity(), $this->getEntity());
68
69 $this->assertCount(2, $entities->toArray());
70
71 return $entities;
72 }
73
74 #[Depends('testEntityCollectionCanBeCreatedWithItems')]
76 \ILIAS\Cron\Job\Collection\JobEntities $entities
77 ): \ILIAS\Cron\Job\Collection\JobEntities {
78 $entities->add($this->getEntity());
79
80 $this->assertCount(3, $entities->toArray());
81
82 return $entities;
83 }
84
85 #[Depends('testCollectionCanBeChanged')]
86 public function testCollectionCanBeFilteredAndSliced(\ILIAS\Cron\Job\Collection\JobEntities $entities): void
87 {
88 $this->assertCount(0, $entities->filter(static function (JobEntity $entity): bool {
89 return $entity->getJobId() !== 'phpunit';
90 }));
91
92 $this->assertCount(1, $entities->slice(1, 1));
93 }
94
96 {
97 $job_instance = $this->createMock(CronJob::class);
98 $job_instance->method('hasFlexibleSchedule')->willReturn(true);
99
100 $entity = $this->getEntity($job_instance);
101 $this->assertSame(JobScheduleType::IN_MINUTES, $entity->getEffectiveScheduleType());
102 $this->assertSame(5, $entity->getEffectiveScheduleValue());
103
104 $another_job_instance = $this->createMock(CronJob::class);
105 $another_job_instance->method('hasFlexibleSchedule')->willReturn(false);
106 $another_job_instance->method('getDefaultScheduleType')->willReturn(
107 JobScheduleType::IN_HOURS
108 );
109 $another_job_instance->method('getDefaultScheduleValue')->willReturn(5);
110
111 $another_entity = $this->getEntity($another_job_instance, JobScheduleType::DAILY->value);
112 $this->assertSame(JobScheduleType::IN_HOURS, $another_entity->getEffectiveScheduleType());
113 $this->assertSame(5, $another_entity->getEffectiveScheduleValue());
114
115 $yet_another_job_instance = $this->createMock(CronJob::class);
116 $yet_another_job_instance->method('hasFlexibleSchedule')->willReturn(true);
117 $yet_another_job_instance->method('getDefaultScheduleType')->willReturn(
118 JobScheduleType::IN_HOURS
119 );
120 $yet_another_job_instance->method('getDefaultScheduleValue')->willReturn(5);
121
122 $yet_another_entity = $this->getEntity($yet_another_job_instance, 0);
123 $this->assertSame(JobScheduleType::IN_HOURS, $yet_another_entity->getEffectiveScheduleType());
124 $this->assertSame(5, $yet_another_entity->getEffectiveScheduleValue());
125 }
126}
testCollectionCanBeFilteredAndSliced(\ILIAS\Cron\Job\Collection\JobEntities $entities)
testCollectionCanBeChanged(\ILIAS\Cron\Job\Collection\JobEntities $entities)
getEntity(?CronJob $job_instance=null, ?int $schedule_type=null, int $schedule_value=5, bool $is_plugin=false)
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.