From 7b9fb37fd655202969077504c3bce9731910ae8e Mon Sep 17 00:00:00 2001 From: Guillaume Loulier Date: Tue, 20 Jun 2023 09:06:35 +0200 Subject: [PATCH] ref --- src/Task/HttpTask.php | 10 ++++++++-- src/Test/Constraint/Probe/ProbeExecutedTask.php | 4 ++++ src/Test/Constraint/Probe/ProbeFailedTask.php | 4 ++++ src/Test/Constraint/Probe/ProbeScheduledTask.php | 4 ++++ src/Test/Constraint/Probe/ProbeState.php | 6 ++++++ src/Test/Constraint/Scheduler/SchedulerDueTask.php | 10 +++++++++- src/Test/Constraint/TaskExecuted.php | 8 +++++++- src/Test/Constraint/TaskFailed.php | 7 +++++-- src/Test/Constraint/TaskQueued.php | 8 +++++++- src/Test/Constraint/TaskScheduled.php | 6 +++++- src/Test/Constraint/TaskUnscheduled.php | 6 +++++- src/Worker/Worker.php | 5 ++++- 12 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/Task/HttpTask.php b/src/Task/HttpTask.php index dae554f2..31e4dea8 100644 --- a/src/Task/HttpTask.php +++ b/src/Task/HttpTask.php @@ -56,7 +56,10 @@ public function setUrl(string $url): self public function getMethod(): string { - return $this->options['method'] ?? 'GET'; + return is_string(value: $this->options['method']) + ? $this->options['method'] + : 'GET' + ; } public function setMethod(string $method): self @@ -71,7 +74,10 @@ public function setMethod(string $method): self */ public function getClientOptions(): array { - return is_array(value: $this->options['client_options']) ? $this->options['client_options'] : []; + return is_array(value: $this->options['client_options']) + ? $this->options['client_options'] + : [] + ; } /** diff --git a/src/Test/Constraint/Probe/ProbeExecutedTask.php b/src/Test/Constraint/Probe/ProbeExecutedTask.php index f6ea2144..698af81b 100644 --- a/src/Test/Constraint/Probe/ProbeExecutedTask.php +++ b/src/Test/Constraint/Probe/ProbeExecutedTask.php @@ -31,6 +31,10 @@ public function toString(): string */ protected function matches($other): bool { + if (!$other instanceof ProbeInterface) { + return false; + } + return $this->expectedCount === $other->getExecutedTasks(); } } diff --git a/src/Test/Constraint/Probe/ProbeFailedTask.php b/src/Test/Constraint/Probe/ProbeFailedTask.php index 2c0fbff6..3504fd63 100644 --- a/src/Test/Constraint/Probe/ProbeFailedTask.php +++ b/src/Test/Constraint/Probe/ProbeFailedTask.php @@ -31,6 +31,10 @@ public function toString(): string */ protected function matches($other): bool { + if (!$other instanceof ProbeInterface) { + return false; + } + return $this->expectedCount === $other->getFailedTasks(); } } diff --git a/src/Test/Constraint/Probe/ProbeScheduledTask.php b/src/Test/Constraint/Probe/ProbeScheduledTask.php index 05515788..16df1931 100644 --- a/src/Test/Constraint/Probe/ProbeScheduledTask.php +++ b/src/Test/Constraint/Probe/ProbeScheduledTask.php @@ -31,6 +31,10 @@ public function toString(): string */ protected function matches($other): bool { + if (!$other instanceof ProbeInterface) { + return false; + } + return $this->expectedCount === $other->getScheduledTasks(); } } diff --git a/src/Test/Constraint/Probe/ProbeState.php b/src/Test/Constraint/Probe/ProbeState.php index 1f41091e..c87a7176 100644 --- a/src/Test/Constraint/Probe/ProbeState.php +++ b/src/Test/Constraint/Probe/ProbeState.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\Constraint\Constraint; use SchedulerBundle\Probe\ProbeInterface; +use Throwable; use function json_encode; use function sprintf; @@ -32,9 +33,14 @@ public function toString(): string /** * @param mixed|ProbeInterface $other + * @throws Throwable {@see ProbeInterface::getScheduledTasks()} */ protected function matches($other): bool { + if (!$other instanceof ProbeInterface) { + return false; + } + return $this->expectedState === [ 'executedTasks' => $other->getExecutedTasks(), 'failedTasks' => $other->getFailedTasks(), diff --git a/src/Test/Constraint/Scheduler/SchedulerDueTask.php b/src/Test/Constraint/Scheduler/SchedulerDueTask.php index 700f5943..5d6ef77f 100644 --- a/src/Test/Constraint/Scheduler/SchedulerDueTask.php +++ b/src/Test/Constraint/Scheduler/SchedulerDueTask.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\Constraint\Constraint; use SchedulerBundle\SchedulerInterface; +use Throwable; use function sprintf; /** @@ -28,9 +29,16 @@ public function toString(): string /** * @param mixed|SchedulerInterface $other + * @throws Throwable {@see SchedulerInterface::getDueTasks()} */ protected function matches($other): bool { - return $this->expectedCount === $other->getDueTasks()->count(); + if (!$other instanceof SchedulerInterface) { + return false; + } + + $dueTasks = $other->getDueTasks(); + + return $this->expectedCount === $dueTasks->count(); } } diff --git a/src/Test/Constraint/TaskExecuted.php b/src/Test/Constraint/TaskExecuted.php index 743e9324..eb5666ea 100644 --- a/src/Test/Constraint/TaskExecuted.php +++ b/src/Test/Constraint/TaskExecuted.php @@ -33,6 +33,10 @@ public function toString(): string */ protected function matches($other): bool { + if (!$other instanceof TaskEventList) { + return false; + } + return $this->expectedCount === $this->countExecutedTasks($other); } @@ -44,7 +48,9 @@ private function countExecutedTasks(TaskEventList $taskEventList): int continue; } - if (TaskInterface::SUCCEED !== $taskEvent->getTask()->getExecutionState()) { + $task = $taskEvent->getTask(); + + if (TaskInterface::SUCCEED !== $task->getExecutionState()) { continue; } diff --git a/src/Test/Constraint/TaskFailed.php b/src/Test/Constraint/TaskFailed.php index c4c4b3f4..5fac5515 100644 --- a/src/Test/Constraint/TaskFailed.php +++ b/src/Test/Constraint/TaskFailed.php @@ -8,7 +8,6 @@ use SchedulerBundle\Event\TaskEventList; use function count; -use function is_countable; use function sprintf; /** @@ -33,6 +32,10 @@ public function toString(): string */ protected function matches($other): bool { - return $this->expectedCount === (is_countable($other->getFailedTaskEvents()) ? count($other->getFailedTaskEvents()) : 0); + if (!$other instanceof TaskEventList) { + return false; + } + + return $this->expectedCount === count($other->getFailedTaskEvents()); } } diff --git a/src/Test/Constraint/TaskQueued.php b/src/Test/Constraint/TaskQueued.php index 443eece6..295ef7e8 100644 --- a/src/Test/Constraint/TaskQueued.php +++ b/src/Test/Constraint/TaskQueued.php @@ -32,6 +32,10 @@ public function toString(): string */ protected function matches($other): bool { + if (!$other instanceof TaskEventList) { + return false; + } + return $this->expectedCount === $this->countQueuedTasks($other); } @@ -43,7 +47,9 @@ private function countQueuedTasks(TaskEventList $taskEventList): int continue; } - if (!$taskEvent->getTask()->isQueued()) { + $task = $taskEvent->getTask(); + + if (!$task->isQueued()) { continue; } diff --git a/src/Test/Constraint/TaskScheduled.php b/src/Test/Constraint/TaskScheduled.php index 3e31603a..3620e724 100644 --- a/src/Test/Constraint/TaskScheduled.php +++ b/src/Test/Constraint/TaskScheduled.php @@ -32,6 +32,10 @@ public function toString(): string */ protected function matches($other): bool { - return $this->expectedCount === (is_countable($other->getScheduledTaskEvents()) ? count($other->getScheduledTaskEvents()) : 0); + if (!$other instanceof TaskEventList) { + return false; + } + + return $this->expectedCount === count($other->getScheduledTaskEvents()); } } diff --git a/src/Test/Constraint/TaskUnscheduled.php b/src/Test/Constraint/TaskUnscheduled.php index 1711fc9f..94936c22 100644 --- a/src/Test/Constraint/TaskUnscheduled.php +++ b/src/Test/Constraint/TaskUnscheduled.php @@ -32,6 +32,10 @@ public function toString(): string */ protected function matches($other): bool { - return $this->expectedCount === (is_countable($other->getUnscheduledTaskEvents()) ? count($other->getUnscheduledTaskEvents()) : 0); + if (!$other instanceof TaskEventList) { + return false; + } + + return $this->expectedCount === count($other->getUnscheduledTaskEvents()); } } diff --git a/src/Worker/Worker.php b/src/Worker/Worker.php index 06b64bac..cbfc0c3f 100644 --- a/src/Worker/Worker.php +++ b/src/Worker/Worker.php @@ -118,7 +118,10 @@ public function execute(WorkerConfiguration $configuration, TaskInterface ...$ta */ public function preempt(TaskListInterface $preemptTaskList, TaskListInterface $toPreemptTasksList): void { - $nonExecutedTasks = $toPreemptTasksList->slice(...$preemptTaskList->map(func: static fn (TaskInterface $task): string => $task->getName(), keepKeys: false)); + /** @var string[] $taskNameList */ + $taskNameList = $preemptTaskList->map(func: static fn (TaskInterface $task): string => $task->getName(), keepKeys: false); + + $nonExecutedTasks = $toPreemptTasksList->slice(...$taskNameList); $nonExecutedTasks->walk(func: function (TaskInterface $task): void { $accessLockBag = $task->getAccessLockBag();