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