From 051d6242ed574523dd95f98ea44bb69be8c13213 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 7 Jan 2024 17:06:47 +0000 Subject: [PATCH] [tests] add assertException because PHPUnit doesn't have it --- core/testcase.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/testcase.php b/core/testcase.php index b2e730d2..bd1f04bd 100644 --- a/core/testcase.php +++ b/core/testcase.php @@ -199,6 +199,23 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { $this->assertEquals($results, $ids); } + protected function assertException(string $type, callable $function): \Exception|null + { + $exception = null; + try { + call_user_func($function); + } catch (\Exception $e) { + $exception = $e; + } + + if(is_a($exception, $type)) { + self::assertThat($exception, new \PHPUnit\Framework\Constraint\Exception($type)); + return $exception; + } else { + throw $exception; + } + } + // user things protected static function log_in_as_admin(): void {