-
Notifications
You must be signed in to change notification settings - Fork 10
/
AssertThrows.php
176 lines (147 loc) · 4.76 KB
/
AssertThrows.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Codeception;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\AssertionFailedError;
use Throwable;
trait AssertThrows
{
private function checkException(?string $message, Throwable $exception)
{
$actualMessage = strtolower($exception->getMessage());
if (!$message || $message === $actualMessage) {
return;
}
throw new AssertionFailedError(
sprintf(
"Exception message '%s' was expected, but '%s' was received",
$message,
$actualMessage
)
);
}
/**
* Asserts that callback throws an exception
*
* @param string|Throwable $throws
* @param callable $func
* @param mixed ...$params
* @throws Throwable
*/
public function assertThrows($throws, callable $func, ...$params)
{
$this->assertThrowsWithMessage($throws, null, $func, ...$params);
}
/**
* Asserts that callback throws an exception with a message
*
* @param string|Throwable $throws
* @param string|null $message
* @param callable $func
* @param mixed ...$params
* @throws Throwable
*/
public function assertThrowsWithMessage($throws, ?string $message, callable $func, ...$params)
{
if ($throws instanceof Throwable) {
$message = $throws->getMessage();
$throws = get_class($throws);
}
if ($message) {
$message = strtolower($message);
}
try {
if (!empty($params)) {
call_user_func_array($func, $params);
} else {
call_user_func($func);
}
} catch (AssertionFailedError $exception) {
if ($throws !== get_class($exception)) {
throw $exception;
}
$this->checkException($message, $exception);
} catch (Throwable $exception) {
if (!$throws) {
throw $exception;
}
$actualThrows = get_class($exception);
if ($throws !== $actualThrows) {
throw new AssertionFailedError(
sprintf(
"Exception '%s' was expected, but '%s' was thrown with message '%s'",
$throws,
get_class($exception),
$exception->getMessage()
)
);
}
$this->checkException($message, $exception);
}
if (!$throws) {
return;
}
if (isset($exception)) {
Assert::assertTrue(true, 'Exception handled');
return;
}
throw new AssertionFailedError(
sprintf("Exception '%s' was not thrown as expected", $throws)
);
}
/**
* Asserts that callback does not throws an exception
*
* @param null|string|Throwable $throws
* @param callable $func
* @param mixed ...$params
*/
public function assertDoesNotThrow($throws, callable $func, ...$params)
{
$this->assertDoesNotThrowWithMessage($throws, null, $func, ...$params);
}
/**
* Asserts that callback does not throws an exception with a message
*
* @param null|string|Throwable $throws
* @param string|null $message
* @param callable $func
* @param mixed ...$params
*/
public function assertDoesNotThrowWithMessage($throws, ?string $message, callable $func, ...$params)
{
if ($throws instanceof Throwable) {
$message = $throws->getMessage();
$throws = get_class($throws);
}
try {
if (!empty($params)) {
call_user_func_array($func, $params);
} else {
call_user_func($func);
}
} catch (Throwable $exception) {
if (!$throws) {
throw new AssertionFailedError('Exception was not expected to be thrown');
}
$actualThrows = get_class($exception);
if ($throws != $actualThrows) {
Assert::assertNotSame($throws, $actualThrows);
return;
}
if (!$message) {
throw new AssertionFailedError(
sprintf("Exception '%s' was not expected to be thrown", $throws)
);
}
$actualMessage = $exception->getMessage();
if ($message !== $actualMessage) {
Assert::assertNotSame($message, $actualMessage);
return;
}
throw new AssertionFailedError(
sprintf("Exception '%s' with message '%s' was not expected to be thrown", $throws, $message)
);
}
}
}