Skip to content

Commit 272516f

Browse files
author
Rasmus Schultz
committed
add optional exception message patterns
1 parent a15310f commit 272516f

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

header.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,37 @@ function eq($value, $expected, $why = null)
108108
/**
109109
* Check for an expected exception, which must be thrown.
110110
*
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
114115
*
115116
* @void
116117
*/
117-
function expect($exception_type, $why, $function)
118+
function expect($exception_type, $why, $function, $patterns = array())
118119
{
119120
try {
120121
call_user_func($function);
121122
} catch (Exception $e) {
122123
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+
123131
ok(true, $why, $e);
124132
} else {
125133
$actual_type = get_class($e);
126134

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)");
128136
}
129137

130138
return;
131139
}
132140

133-
configure()->driver->printResult(false, "{$why} (expected exception {$exception_type} was NOT thrown)");
141+
ok(false, "{$why} (expected exception {$exception_type} was NOT thrown)");
134142
}
135143

136144
/**

0 commit comments

Comments
 (0)