Skip to content

Commit c6a793d

Browse files
committed
Add raises() helper, similar to throws() except for php warnings/notices/deprecated/...
1 parent 55ff6ba commit c6a793d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/utils/tools.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,45 @@ function CLEANUP($uri) {
8282
}
8383
}
8484

85+
function severityToString($type) {
86+
switch($type) {
87+
case E_WARNING:
88+
return "E_WARNING";
89+
default:
90+
return "Some other #_$type";
91+
}
92+
}
93+
function raises(callable $function, $type, $infunction = null) {
94+
$errhandler = function($severity, $message, $file, $line, $errcontext) {
95+
throw new ErrorException($message, 0, $severity, $file, $line);
96+
};
97+
98+
set_error_handler($errhandler, $type);
99+
try {
100+
$function();
101+
} catch(Exception $e) {
102+
if ($e instanceof ErrorException && $e->getSeverity() & $type) {
103+
if ($infunction) {
104+
$function = $e->getTrace()[0]["function"];
105+
if (strcasecmp($function, $infunction) == 0) {
106+
printf("OK: Got %s thrown from %s\n", $exceptionname, $infunction);
107+
} else {
108+
printf("ALMOST: Got %s - but was thrown in %s, not %s\n", $exceptionname, $function, $infunction);
109+
}
110+
restore_error_handler();
111+
return $e->getMessage();
112+
}
113+
printf("OK: Got %s\n", severityToString($type));
114+
} else {
115+
printf("ALMOST: Got %s - expected %s\n", get_class($e), $exceptionname);
116+
}
117+
restore_error_handler();
118+
return $e->getMessage();
119+
}
120+
121+
echo "FAILED: Expected $exceptionname thrown!\n";
122+
restore_error_handler();
123+
}
85124
function throws(callable $function, $exceptionname, $infunction = null) {
86125
try {
87126
$function();

0 commit comments

Comments
 (0)