ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
CronJobEntityTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
28 class CronJobEntityTest extends TestCase
29 {
33  private function getEntity(
34  ilCronJob $job_instance = null,
35  int $schedule_type = null,
36  int $schedule_value = 5,
37  bool $is_plugin = false
38  ): ilCronJobEntity {
39  $job_instance ??= $this->createMock(ilCronJob::class);
40 
41  if ($schedule_type === null) {
42  $schedule_type = CronJobScheduleType::SCHEDULE_TYPE_IN_MINUTES->value;
43  }
44 
45  return new ilCronJobEntity($job_instance, [
46  'job_id' => 'phpunit',
47  'component' => 'phpunit',
48  'schedule_type' => $schedule_type,
49  'schedule_value' => $schedule_value,
50  'job_status' => 1,
51  'job_status_user_id' => 6,
52  'job_status_type' => 1,
53  'job_status_ts' => time(),
54  'job_result_status' => ilCronJobResult::STATUS_OK,
55  'job_result_user_id' => 6,
56  'job_result_code' => ilCronJobResult::CODE_NO_RESULT,
57  'job_result_message' => 'msg',
58  'job_result_type' => 1,
59  'job_result_ts' => time(),
60  'class' => 'Job',
61  'path' => '/',
62  'running_ts' => time(),
63  'job_result_dur' => time(),
64  'alive_ts' => time(),
65  ], $is_plugin);
66  }
67 
69  {
70  $entities = new ilCronJobEntities($this->getEntity(), $this->getEntity());
71 
72  $this->assertCount(2, $entities->toArray());
73 
74  return $entities;
75  }
76 
81  {
82  $entities->add($this->getEntity());
83 
84  $this->assertCount(3, $entities->toArray());
85 
86  return $entities;
87  }
88 
93  {
94  $this->assertCount(0, $entities->filter(static function (ilCronJobEntity $entity): bool {
95  return $entity->getJobId() !== 'phpunit';
96  }));
97 
98  $this->assertCount(1, $entities->slice(1, 1));
99  }
100 
101  public function testEffectiveScheduleCanBeDetermined(): void
102  {
103  $job_instance = $this->createMock(ilCronJob::class);
104  $job_instance->method('hasFlexibleSchedule')->willReturn(true);
105 
106  $entity = $this->getEntity($job_instance);
107  $this->assertSame(CronJobScheduleType::SCHEDULE_TYPE_IN_MINUTES, $entity->getEffectiveScheduleType());
108  $this->assertSame(5, $entity->getEffectiveScheduleValue());
109 
110  $another_job_instance = $this->createMock(ilCronJob::class);
111  $another_job_instance->method('hasFlexibleSchedule')->willReturn(false);
112  $another_job_instance->method('getDefaultScheduleType')->willReturn(CronJobScheduleType::SCHEDULE_TYPE_IN_HOURS);
113  $another_job_instance->method('getDefaultScheduleValue')->willReturn(5);
114 
115  $another_entity = $this->getEntity($another_job_instance, CronJobScheduleType::SCHEDULE_TYPE_DAILY->value);
116  $this->assertSame(CronJobScheduleType::SCHEDULE_TYPE_IN_HOURS, $another_entity->getEffectiveScheduleType());
117  $this->assertSame(5, $another_entity->getEffectiveScheduleValue());
118 
119  $yet_another_job_instance = $this->createMock(ilCronJob::class);
120  $yet_another_job_instance->method('hasFlexibleSchedule')->willReturn(true);
121  $yet_another_job_instance->method('getDefaultScheduleType')->willReturn(CronJobScheduleType::SCHEDULE_TYPE_IN_HOURS);
122  $yet_another_job_instance->method('getDefaultScheduleValue')->willReturn(5);
123 
124  $yet_another_entity = $this->getEntity($yet_another_job_instance, 0);
125  $this->assertSame(CronJobScheduleType::SCHEDULE_TYPE_IN_HOURS, $yet_another_entity->getEffectiveScheduleType());
126  $this->assertSame(5, $yet_another_entity->getEffectiveScheduleValue());
127  }
128 }
slice(int $offset, ?int $length=null)
Extracts a slice of $length elements starting at position $offset from the Collection.
Class CronJobEntityTest.
testCollectionCanBeFilteredAndSliced(ilCronJobEntities $entities)
testCollectionCanBeChanged
getEntity(ilCronJob $job_instance=null, int $schedule_type=null, int $schedule_value=5, bool $is_plugin=false)
testCollectionCanBeChanged(ilCronJobEntities $entities)
testEntityCollectionCanBeCreatedWithItems
filter(callable $callable)
Returns all the elements of this collection that satisfy the predicate $callable. ...
add(ilCronJobEntity $job)