Skip to content

Commit f63345b

Browse files
committed
Cron: ilCronJobEntities is more of array than iterator
1 parent a290a96 commit f63345b

3 files changed

Lines changed: 62 additions & 14 deletions

File tree

components/ILIAS/Cron/classes/class.ilCronJobEntities.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,47 @@
2020

2121
class ilCronJobEntities implements ilCronJobCollection
2222
{
23-
private readonly ArrayIterator $jobs;
23+
private array $jobs;
2424

2525
public function __construct(ilCronJobEntity ...$jobs)
2626
{
27-
$this->jobs = new ArrayIterator($jobs);
27+
$this->jobs = $jobs;
2828
}
2929

3030
/**
3131
* @return ArrayIterator|ilCronJobEntity[]
3232
*/
3333
public function getIterator(): ArrayIterator
3434
{
35-
return $this->jobs;
35+
return new ArrayIterator($this->jobs);
3636
}
3737

3838
public function count(): int
3939
{
40-
return iterator_count($this);
40+
return count($this->jobs);
4141
}
4242

4343
public function add(ilCronJobEntity $job): void
4444
{
45-
$this->jobs->append($job);
45+
$this->jobs[] = $job;
4646
}
4747

4848
public function filter(callable $callable): ilCronJobCollection
4949
{
50-
return new static(...array_filter(iterator_to_array($this), $callable));
50+
51+
return new static(...array_filter($this->jobs, $callable));
5152
}
5253

5354
public function slice(int $offset, ?int $length = null): ilCronJobCollection
5455
{
55-
return new static(...array_slice(iterator_to_array($this), $offset, $length, true));
56+
return new static(...array_slice($this->jobs, $offset, $length, true));
5657
}
5758

5859
/**
5960
* @return ilCronJobEntity[]
6061
*/
6162
public function toArray(): array
6263
{
63-
return iterator_to_array($this);
64+
return $this->jobs;
6465
}
6566
}

components/ILIAS/Cron/classes/class.ilCronJobEntity.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
declare(strict_types=1);
2020

2121
use ILIAS\Cron\Schedule\CronJobScheduleType;
22+
use ILIAS\Cron\CronJob;
2223

2324
class ilCronJobEntity
2425
{
@@ -48,7 +49,7 @@ class ilCronJobEntity
4849
* @param array<string, mixed> $record
4950
* @param bool $isPlugin
5051
*/
51-
public function __construct(private readonly ilCronJob $job, array $record, private readonly bool $isPlugin = false)
52+
public function __construct(private readonly CronJob $job, array $record, private readonly bool $isPlugin = false)
5253
{
5354
$job->init();
5455
$this->mapRecord($job, $record);
@@ -57,7 +58,7 @@ public function __construct(private readonly ilCronJob $job, array $record, priv
5758
/**
5859
* @param array<string, mixed> $record
5960
*/
60-
private function mapRecord(ilCronJob $job, array $record): void
61+
private function mapRecord(CronJob $job, array $record): void
6162
{
6263
if (! array_key_exists('job_id', $record)) {
6364
//TODO: remove!

components/ILIAS/Cron/tests/CronJobEntityTest.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use PHPUnit\Framework\TestCase;
2222
use ILIAS\Cron\Schedule\CronJobScheduleType;
23+
use ILIAS\Cron\CronJob;
2324

2425
/**
2526
* Class CronJobEntityTest
@@ -36,7 +37,47 @@ private function getEntity(
3637
int $schedule_value = 5,
3738
bool $is_plugin = false
3839
): ilCronJobEntity {
39-
$job_instance ??= $this->createMock(ilCronJob::class);
40+
$job_instance ??= new class (
41+
$schedule_type,
42+
$schedule_value
43+
) implements CronJob {
44+
public function __construct(
45+
private $schedule_type,
46+
private $schedule_value
47+
) {
48+
}
49+
public function getId(): string
50+
{
51+
return 'phpunit';
52+
}
53+
public function getComponent(): string
54+
{
55+
return 'phpunit';
56+
}
57+
public function getTitle(): string
58+
{
59+
return 'phpunit';
60+
}
61+
public function getDescription(): string
62+
{
63+
return 'phpunit';
64+
}
65+
public function getScheduleType(): ?CronJobScheduleType
66+
{
67+
return $this->schedule_type;
68+
}
69+
public function getScheduleValue(): ?int
70+
{
71+
return $this->schedule_value;
72+
}
73+
public function run(): \ilCronJobResult
74+
{
75+
return new \ilCronJobResult();
76+
}
77+
public function init(): void
78+
{
79+
}
80+
};
4081

4182
if ($schedule_type === null) {
4283
$schedule_type = CronJobScheduleType::SCHEDULE_TYPE_IN_MINUTES->value;
@@ -91,9 +132,14 @@ public function testCollectionCanBeChanged(ilCronJobEntities $entities): ilCronJ
91132
*/
92133
public function testCollectionCanBeFilteredAndSliced(ilCronJobEntities $entities): void
93134
{
94-
$this->assertCount(0, $entities->filter(static function (ilCronJobEntity $entity): bool {
95-
return $entity->getJobId() !== 'phpunit';
96-
}));
135+
$this->assertCount(
136+
0,
137+
$entities->filter(
138+
static function (ilCronJobEntity $entity): bool {
139+
return $entity->getJobId() !== 'phpunit';
140+
}
141+
)
142+
);
97143

98144
$this->assertCount(1, $entities->slice(1, 1));
99145
}

0 commit comments

Comments
 (0)