diff --git a/AssertThrows.php b/AssertThrows.php index e2fc16e..5f5bcfc 100644 --- a/AssertThrows.php +++ b/AssertThrows.php @@ -35,9 +35,9 @@ private function checkException(?string $message, Throwable $exception) * @param mixed ...$params * @throws Throwable */ - public function assertThrows($throws, callable $func, array $params = []) + public function assertThrows($throws, callable $func, ...$params) { - $this->assertThrowsWithMessage($throws, null, $func, $params); + $this->assertThrowsWithMessage($throws, null, $func, ...$params); } /** @@ -49,7 +49,7 @@ public function assertThrows($throws, callable $func, array $params = []) * @param mixed ...$params * @throws Throwable */ - public function assertThrowsWithMessage($throws, ?string $message, callable $func, array $params = []) + public function assertThrowsWithMessage($throws, ?string $message, callable $func, ...$params) { if ($throws instanceof Throwable) { $message = $throws->getMessage(); @@ -61,7 +61,7 @@ public function assertThrowsWithMessage($throws, ?string $message, callable $fun } try { - if ($params !== []) { + if (!empty($params)) { call_user_func_array($func, $params); } else { call_user_func($func); @@ -117,9 +117,9 @@ public function assertThrowsWithMessage($throws, ?string $message, callable $fun * @param callable $func * @param mixed ...$params */ - public function assertDoesNotThrow($throws, callable $func, array $params = []) + public function assertDoesNotThrow($throws, callable $func, ...$params) { - $this->assertDoesNotThrowWithMessage($throws, null, $func, $params); + $this->assertDoesNotThrowWithMessage($throws, null, $func, ...$params); } /** @@ -130,7 +130,7 @@ public function assertDoesNotThrow($throws, callable $func, array $params = []) * @param callable $func * @param mixed ...$params */ - public function assertDoesNotThrowWithMessage($throws, ?string $message, callable $func, array $params = []) + public function assertDoesNotThrowWithMessage($throws, ?string $message, callable $func, ...$params) { if ($throws instanceof Throwable) { $message = $throws->getMessage(); @@ -138,7 +138,7 @@ public function assertDoesNotThrowWithMessage($throws, ?string $message, callabl } try { - if ($params !== []) { + if (!empty($params)) { call_user_func_array($func, $params); } else { call_user_func($func); diff --git a/tests/AssertThrowsTest.php b/tests/AssertThrowsTest.php index 51d655d..32e0e99 100644 --- a/tests/AssertThrowsTest.php +++ b/tests/AssertThrowsTest.php @@ -60,6 +60,20 @@ function() { } public function testAssertThrowsWithParams() + { + $func = function (string $foo, string $bar): void { + throw new MyException($foo.$bar); + }; + + $this->assertThrows( + MyException::class, + $func, + 'foo', + 'bar' + ); + } + + public function testAssertThrowsWithMessageWithParams() { $func = function (string $foo, string $bar): void { throw new Exception($foo.$bar); @@ -69,7 +83,8 @@ public function testAssertThrowsWithParams() Exception::class, 'foobar', $func, - ['foo', 'bar'] + 'foo', + 'bar' ); } @@ -88,6 +103,17 @@ public function testAssertDoesNotThrow() $this->assertDoesNotThrowWithMessage(Exception::class, 'bar', $func); $this->assertDoesNotThrow(new Exception('bar'), $func); } + + public function testAssertDoesNotThrowWithParams() + { + $func = function (string $foo, string $bar): void { + throw new Exception($foo.$bar); + }; + + $this->assertDoesNotThrowWithMessage(Exception::class, 'bar', $func, 'bar'); + $this->assertDoesNotThrowWithMessage(Exception::class, 'foobar', $func, 'bar', 'foo'); + $this->assertDoesNotThrow(RuntimeException::class, $func, 'bar', 'foo'); + } } final class MyException extends Exception {