@@ -108,29 +108,37 @@ function eq($value, $expected, $why = null)
108
108
/**
109
109
* Check for an expected exception, which must be thrown.
110
110
*
111
- * @param string $exception_type Exception type name (use `ClassName::class` syntax where possible)
112
- * @param string $why description of assertion
113
- * @param callable $function function expected to cause the exception
111
+ * @param string $exception_type Exception type name (use `ClassName::class` syntax where possible)
112
+ * @param string $why reason for making this assertion
113
+ * @param callable $function function expected to cause the exception
114
+ * @param string|string[] $patterns regular expression pattern(s) to test against the Exception message
114
115
*
115
116
* @void
116
117
*/
117
- function expect ($ exception_type , $ why , $ function )
118
+ function expect ($ exception_type , $ why , $ function, $ patterns = array () )
118
119
{
119
120
try {
120
121
call_user_func ($ function );
121
122
} catch (Exception $ e ) {
122
123
if ($ e instanceof $ exception_type ) {
124
+ foreach ((array ) $ patterns as $ pattern ) {
125
+ if (preg_match ($ pattern , $ e ->getMessage ()) !== 1 ) {
126
+ ok (false , "$ why (expected {$ exception_type } message did not match pattern: {$ pattern }) " , $ e );
127
+ return ;
128
+ }
129
+ }
130
+
123
131
ok (true , $ why , $ e );
124
132
} else {
125
133
$ actual_type = get_class ($ e );
126
134
127
- configure ()-> driver -> printResult (false , "$ why (expected {$ exception_type } but {$ actual_type } was thrown) " );
135
+ ok (false , "$ why (expected {$ exception_type } but {$ actual_type } was thrown) " );
128
136
}
129
137
130
138
return ;
131
139
}
132
140
133
- configure ()-> driver -> printResult (false , "{$ why } (expected exception {$ exception_type } was NOT thrown) " );
141
+ ok (false , "{$ why } (expected exception {$ exception_type } was NOT thrown) " );
134
142
}
135
143
136
144
/**
0 commit comments