Skip to content

Commit 60f6a5a

Browse files
authored
Revert assertThrows* 1.x methods signature (#16)
1 parent 353189a commit 60f6a5a

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

AssertThrows.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ private function checkException(?string $message, Throwable $exception)
3535
* @param mixed ...$params
3636
* @throws Throwable
3737
*/
38-
public function assertThrows($throws, callable $func, array $params = [])
38+
public function assertThrows($throws, callable $func, ...$params)
3939
{
40-
$this->assertThrowsWithMessage($throws, null, $func, $params);
40+
$this->assertThrowsWithMessage($throws, null, $func, ...$params);
4141
}
4242

4343
/**
@@ -49,7 +49,7 @@ public function assertThrows($throws, callable $func, array $params = [])
4949
* @param mixed ...$params
5050
* @throws Throwable
5151
*/
52-
public function assertThrowsWithMessage($throws, ?string $message, callable $func, array $params = [])
52+
public function assertThrowsWithMessage($throws, ?string $message, callable $func, ...$params)
5353
{
5454
if ($throws instanceof Throwable) {
5555
$message = $throws->getMessage();
@@ -61,7 +61,7 @@ public function assertThrowsWithMessage($throws, ?string $message, callable $fun
6161
}
6262

6363
try {
64-
if ($params !== []) {
64+
if (!empty($params)) {
6565
call_user_func_array($func, $params);
6666
} else {
6767
call_user_func($func);
@@ -117,9 +117,9 @@ public function assertThrowsWithMessage($throws, ?string $message, callable $fun
117117
* @param callable $func
118118
* @param mixed ...$params
119119
*/
120-
public function assertDoesNotThrow($throws, callable $func, array $params = [])
120+
public function assertDoesNotThrow($throws, callable $func, ...$params)
121121
{
122-
$this->assertDoesNotThrowWithMessage($throws, null, $func, $params);
122+
$this->assertDoesNotThrowWithMessage($throws, null, $func, ...$params);
123123
}
124124

125125
/**
@@ -130,15 +130,15 @@ public function assertDoesNotThrow($throws, callable $func, array $params = [])
130130
* @param callable $func
131131
* @param mixed ...$params
132132
*/
133-
public function assertDoesNotThrowWithMessage($throws, ?string $message, callable $func, array $params = [])
133+
public function assertDoesNotThrowWithMessage($throws, ?string $message, callable $func, ...$params)
134134
{
135135
if ($throws instanceof Throwable) {
136136
$message = $throws->getMessage();
137137
$throws = get_class($throws);
138138
}
139139

140140
try {
141-
if ($params !== []) {
141+
if (!empty($params)) {
142142
call_user_func_array($func, $params);
143143
} else {
144144
call_user_func($func);

tests/AssertThrowsTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ function() {
6060
}
6161

6262
public function testAssertThrowsWithParams()
63+
{
64+
$func = function (string $foo, string $bar): void {
65+
throw new MyException($foo.$bar);
66+
};
67+
68+
$this->assertThrows(
69+
MyException::class,
70+
$func,
71+
'foo',
72+
'bar'
73+
);
74+
}
75+
76+
public function testAssertThrowsWithMessageWithParams()
6377
{
6478
$func = function (string $foo, string $bar): void {
6579
throw new Exception($foo.$bar);
@@ -69,7 +83,8 @@ public function testAssertThrowsWithParams()
6983
Exception::class,
7084
'foobar',
7185
$func,
72-
['foo', 'bar']
86+
'foo',
87+
'bar'
7388
);
7489
}
7590

@@ -88,6 +103,17 @@ public function testAssertDoesNotThrow()
88103
$this->assertDoesNotThrowWithMessage(Exception::class, 'bar', $func);
89104
$this->assertDoesNotThrow(new Exception('bar'), $func);
90105
}
106+
107+
public function testAssertDoesNotThrowWithParams()
108+
{
109+
$func = function (string $foo, string $bar): void {
110+
throw new Exception($foo.$bar);
111+
};
112+
113+
$this->assertDoesNotThrowWithMessage(Exception::class, 'bar', $func, 'bar');
114+
$this->assertDoesNotThrowWithMessage(Exception::class, 'foobar', $func, 'bar', 'foo');
115+
$this->assertDoesNotThrow(RuntimeException::class, $func, 'bar', 'foo');
116+
}
91117
}
92118

93119
final class MyException extends Exception {

0 commit comments

Comments
 (0)