Skip to content

Commit

Permalink
Revert assertThrows* 1.x methods signature (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
boboudreau committed Dec 29, 2021
1 parent 353189a commit 60f6a5a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
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

0 comments on commit 60f6a5a

Please sign in to comment.