Skip to content

Commit

Permalink
add optional exception message patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasmus Schultz committed May 9, 2016
1 parent a15310f commit 272516f
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,37 @@ function eq($value, $expected, $why = null)
/**
* Check for an expected exception, which must be thrown.
*
* @param string $exception_type Exception type name (use `ClassName::class` syntax where possible)
* @param string $why description of assertion
* @param callable $function function expected to cause the exception
* @param string $exception_type Exception type name (use `ClassName::class` syntax where possible)
* @param string $why reason for making this assertion
* @param callable $function function expected to cause the exception
* @param string|string[] $patterns regular expression pattern(s) to test against the Exception message
*
* @void
*/
function expect($exception_type, $why, $function)
function expect($exception_type, $why, $function, $patterns = array())
{
try {
call_user_func($function);
} catch (Exception $e) {
if ($e instanceof $exception_type) {
foreach ((array) $patterns as $pattern) {
if (preg_match($pattern, $e->getMessage()) !== 1) {
ok(false, "$why (expected {$exception_type} message did not match pattern: {$pattern})", $e);
return;
}
}

ok(true, $why, $e);
} else {
$actual_type = get_class($e);

configure()->driver->printResult(false, "$why (expected {$exception_type} but {$actual_type} was thrown)");
ok(false, "$why (expected {$exception_type} but {$actual_type} was thrown)");
}

return;
}

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

/**
Expand Down

0 comments on commit 272516f

Please sign in to comment.