diff options
Diffstat (limited to 'tests/lib/BackgroundJob/DummyJobList.php')
-rw-r--r-- | tests/lib/BackgroundJob/DummyJobList.php | 108 |
1 files changed, 73 insertions, 35 deletions
diff --git a/tests/lib/BackgroundJob/DummyJobList.php b/tests/lib/BackgroundJob/DummyJobList.php index 0751409f62c..717db52715f 100644 --- a/tests/lib/BackgroundJob/DummyJobList.php +++ b/tests/lib/BackgroundJob/DummyJobList.php @@ -1,54 +1,80 @@ <?php + /** - * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\BackgroundJob; +use OC\BackgroundJob\JobList; use OCP\BackgroundJob\IJob; +use OCP\BackgroundJob\Job; +use OCP\Server; /** * Class DummyJobList * * in memory job list for testing purposes */ -class DummyJobList extends \OC\BackgroundJob\JobList { +class DummyJobList extends JobList { /** * @var IJob[] */ - private $jobs = []; + private array $jobs = []; + + /** + * @var bool[] + */ + private array $reserved = []; - private $last = 0; + private int $last = 0; + private int $lastId = 0; public function __construct() { } /** - * @param IJob|string $job + * @param IJob|class-string<IJob> $job * @param mixed $argument */ - public function add($job, $argument = null) { + public function add($job, $argument = null, ?int $firstCheck = null): void { if (is_string($job)) { - /** @var \OC\BackgroundJob\Job $job */ - $job = new $job; + /** @var IJob $job */ + $job = Server::get($job); } $job->setArgument($argument); + $job->setId($this->lastId); + $this->lastId++; if (!$this->has($job, null)) { $this->jobs[] = $job; } } + public function scheduleAfter(string $job, int $runAfter, $argument = null): void { + $this->add($job, $argument, $runAfter); + } + /** * @param IJob|string $job * @param mixed $argument */ - public function remove($job, $argument = null) { - $index = array_search($job, $this->jobs); - if ($index !== false) { - unset($this->jobs[$index]); + public function remove($job, $argument = null): void { + foreach ($this->jobs as $index => $listJob) { + if (get_class($job) === get_class($listJob) && $job->getArgument() == $listJob->getArgument()) { + unset($this->jobs[$index]); + return; + } + } + } + + public function removeById(int $id): void { + foreach ($this->jobs as $index => $listJob) { + if ($listJob->getId() === $id) { + unset($this->jobs[$index]); + return; + } } } @@ -59,7 +85,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList { * @param mixed $argument * @return bool */ - public function has($job, $argument) { + public function has($job, $argument): bool { return array_search($job, $this->jobs) !== false; } @@ -68,17 +94,30 @@ class DummyJobList extends \OC\BackgroundJob\JobList { * * @return IJob[] */ - public function getAll() { + public function getAll(): array { return $this->jobs; } + public function getJobsIterator($job, ?int $limit, int $offset): array { + if ($job instanceof IJob) { + $jobClass = get_class($job); + } else { + $jobClass = $job; + } + return array_slice( + array_filter( + $this->jobs, + fn ($job) => ($jobClass === null) || (get_class($job) == $jobClass) + ), + $offset, + $limit + ); + } + /** * get the next job in the list - * - * @param bool $onlyTimeSensitive - * @return IJob|null */ - public function getNext(bool $onlyTimeSensitive = false): ?IJob { + public function getNext(bool $onlyTimeSensitive = false, ?array $jobClasses = null): ?IJob { if (count($this->jobs) > 0) { if ($this->last < (count($this->jobs) - 1)) { $i = $this->last + 1; @@ -94,9 +133,9 @@ class DummyJobList extends \OC\BackgroundJob\JobList { /** * set the job that was last ran * - * @param \OC\BackgroundJob\Job $job + * @param Job $job */ - public function setLastJob(IJob $job) { + public function setLastJob(IJob $job): void { $i = array_search($job, $this->jobs); if ($i !== false) { $this->last = $i; @@ -105,11 +144,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList { } } - /** - * @param int $id - * @return IJob - */ - public function getById($id) { + public function getById(int $id): ?IJob { foreach ($this->jobs as $job) { if ($job->getId() === $id) { return $job; @@ -122,16 +157,19 @@ class DummyJobList extends \OC\BackgroundJob\JobList { return null; } - /** - * set the lastRun of $job to now - * - * @param IJob $job - */ - public function setLastRun(IJob $job) { + public function setLastRun(IJob $job): void { $job->setLastRun(time()); } - public function setExecutionTime(IJob $job, $timeTaken) { + public function hasReservedJob(?string $className = null): bool { + return isset($this->reserved[$className ?? '']) && $this->reserved[$className ?? '']; + } + + public function setHasReservedJob(?string $className, bool $hasReserved): void { + $this->reserved[$className ?? ''] = $hasReserved; + } + + public function setExecutionTime(IJob $job, $timeTaken): void { } public function resetBackgroundJob(IJob $job): void { |