summaryrefslogtreecommitdiffstats
path: root/tests/Stubs/Taskmanager.php
diff options
context:
space:
mode:
authorSimon Rettberg2025-11-26 10:46:51 +0100
committerSimon Rettberg2025-12-12 15:16:59 +0100
commit7c173411785f959d250d3dfbd7d4cfcb0e20f0e0 (patch)
tree242157791a76afb7af23ec2cd3d22b599e54ce9d /tests/Stubs/Taskmanager.php
parent[exams] Fix incorrect count() clause (diff)
downloadslx-admin-7c173411785f959d250d3dfbd7d4cfcb0e20f0e0.tar.gz
slx-admin-7c173411785f959d250d3dfbd7d4cfcb0e20f0e0.tar.xz
slx-admin-7c173411785f959d250d3dfbd7d4cfcb0e20f0e0.zip
Add tests using PHPUnit
Tests generated by Junie AI. Might not have the best possible quality but at least we got something, and if it turns out to be complete rubbish, we can just throw it out again without any issues, as this is independent of the actual code base.
Diffstat (limited to 'tests/Stubs/Taskmanager.php')
-rw-r--r--tests/Stubs/Taskmanager.php113
1 files changed, 113 insertions, 0 deletions
diff --git a/tests/Stubs/Taskmanager.php b/tests/Stubs/Taskmanager.php
new file mode 100644
index 00000000..a2f662a3
--- /dev/null
+++ b/tests/Stubs/Taskmanager.php
@@ -0,0 +1,113 @@
+<?php
+
+class Taskmanager
+{
+ // Mirror production status codes
+ const NO_SUCH_TASK = 'NO_SUCH_TASK';
+ const TASK_FINISHED = 'TASK_FINISHED';
+ const TASK_ERROR = 'TASK_ERROR';
+ const TASK_WAITING = 'TASK_WAITING';
+ const NO_SUCH_INSTANCE = 'NO_SUCH_INSTANCE';
+ const TASK_PROCESSING = 'TASK_PROCESSING';
+
+ public static array $submissions = []; // list of ['id'=>, 'task'=>, 'data'=>]
+ public static array $statusById = []; // id => ['id'=>, 'statusCode'=>..., 'data'=>[]]
+ private static int $nextId = 1;
+
+ public static function reset(): void
+ {
+ self::$submissions = [];
+ self::$statusById = [];
+ self::$nextId = 1;
+ }
+
+ /**
+ * Stubbed submit compatible with production signature.
+ * @return array{id: string, statusCode: string, data: array}|bool
+ */
+ public static function submit(string $task, ?array $data = null, ?bool $async = false)
+ {
+ $id = (string)self::$nextId++;
+ $record = ['id' => $id, 'task' => $task, 'data' => $data ?? []];
+ self::$submissions[] = $record;
+ // Default initial status: waiting
+ self::$statusById[$id] = ['id' => $id, 'statusCode' => self::TASK_WAITING, 'data' => $record['data']];
+ if ($async) return true;
+ return self::$statusById[$id];
+ }
+
+ /**
+ * @param string|array $task
+ */
+ public static function status($task)
+ {
+ if (is_array($task) && isset($task['id'])) {
+ $task = $task['id'];
+ }
+ if (!is_string($task)) return false;
+ return self::$statusById[$task] ?? ['id' => $task, 'statusCode' => self::NO_SUCH_TASK];
+ }
+
+ public static function isFailed($task): bool
+ {
+ if (!is_array($task) || !isset($task['statusCode']) || !isset($task['id']))
+ return true;
+ // Consider failed if statusCode is none of waiting/processing/finished
+ return !in_array($task['statusCode'], [self::TASK_WAITING, self::TASK_PROCESSING, self::TASK_FINISHED], true);
+ }
+
+ public static function isFinished($task): bool
+ {
+ if (!is_array($task) || !isset($task['statusCode']) || !isset($task['id']))
+ return false;
+ return !in_array($task['statusCode'], [self::TASK_WAITING, self::TASK_PROCESSING], true);
+ }
+
+ public static function isRunning($task): bool
+ {
+ if (!is_array($task) || !isset($task['statusCode']) || !isset($task['id']))
+ return false;
+ return in_array($task['statusCode'], [self::TASK_WAITING, self::TASK_PROCESSING], true);
+ }
+
+ public static function isTask($task): bool
+ {
+ if ($task === false) return false;
+ if (is_string($task)) {
+ $task = self::status($task);
+ }
+ return isset($task['statusCode'])
+ && $task['statusCode'] !== self::NO_SUCH_INSTANCE
+ && $task['statusCode'] !== self::NO_SUCH_TASK;
+ }
+
+ public static function addErrorMessage($task): void
+ {
+ if (!class_exists('Message')) return;
+ if ($task === false) {
+ Message::addError('main.taskmanager-error');
+ return;
+ }
+ if (!isset($task['statusCode'])) {
+ Message::addError('main.taskmanager-format');
+ return;
+ }
+ if (isset($task['data']['error'])) {
+ Message::addError('main.task-error', $task['statusCode'] . ' (' . $task['data']['error'] . ')');
+ return;
+ }
+ Message::addError('main.task-error', $task['statusCode']);
+ }
+
+ /**
+ * Release a task: mark as NO_SUCH_TASK from now on
+ */
+ public static function release($task): void
+ {
+ if (is_array($task) && isset($task['id'])) {
+ $task = $task['id'];
+ }
+ if (!is_string($task)) return;
+ self::$statusById[$task] = ['id' => $task, 'statusCode' => self::NO_SUCH_TASK];
+ }
+}