From 94d3c3e8a33b9010c6f8b846c91113d1c4716c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BB=C3=B3=C5=82tak?= Date: Wed, 18 Dec 2024 11:56:34 +0100 Subject: [PATCH] RunTestsTrait::runTests(): $throwException parameter added --- src/acdhOeaw/arche/doorkeeper/RunTestsTrait.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/acdhOeaw/arche/doorkeeper/RunTestsTrait.php b/src/acdhOeaw/arche/doorkeeper/RunTestsTrait.php index 96eab30..29b48dd 100644 --- a/src/acdhOeaw/arche/doorkeeper/RunTestsTrait.php +++ b/src/acdhOeaw/arche/doorkeeper/RunTestsTrait.php @@ -36,8 +36,16 @@ */ trait RunTestsTrait { + /** + * + * @param bool $throwException if `true`, the method throws a DoorkeeperException + * on errors or null otherwise. If `false`, method returns a (possibly empty) + * array of DoorkeeperExceptions. + * @return array|null + */ public function runTests(string $attribute = CheckAttribute::class, - int $filter = ReflectionMethod::IS_PUBLIC): void { + int $filter = ReflectionMethod::IS_PUBLIC, + bool $throwException = true): array | null { $rc = new ReflectionClass(static::class); $closures = []; foreach ($rc->getMethods($filter) as $method) { @@ -52,11 +60,12 @@ public function runTests(string $attribute = CheckAttribute::class, try { $i(); } catch (DoorkeeperException $ex) { - $errors[] = $ex->getMessage(); + $errors[] = $ex; } } - if (count($errors) > 0) { - throw new DoorkeeperException(implode("\n", $errors)); + if (count($errors) > 0 && $throwException) { + throw new DoorkeeperException(implode("\n", array_map(fn($x) => $x->getMessage(), $errors))); } + return $throwException ? null : $errors; } }