Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions AssertThrows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -130,15 +130,15 @@ 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();
$throws = get_class($throws);
}

try {
if ($params !== []) {
if (!empty($params)) {
call_user_func_array($func, $params);
} else {
call_user_func($func);
Expand Down
28 changes: 27 additions & 1 deletion tests/AssertThrowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -69,7 +83,8 @@ public function testAssertThrowsWithParams()
Exception::class,
'foobar',
$func,
['foo', 'bar']
'foo',
'bar'
);
}

Expand All @@ -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 {
Expand Down