Skip to content

Commit f663761

Browse files
authored
Fix message for catching wrong exception in raises() (#1252)
* Fix message for catching wrong exception in raises() The original implementation of raises() in c6a793d apparently referenced an undeclared $exceptionname variable several times. That was later addressed 804f383 but that commit neglected to fix this reference. * Refactor raises() and throws() utility functions
1 parent bd10948 commit f663761

File tree

1 file changed

+97
-61
lines changed

1 file changed

+97
-61
lines changed

tests/utils/tools.php

Lines changed: 97 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -568,84 +568,120 @@ function destroyTemporaryMongoInstance($id = NULL)
568568
$json = file_get_contents(MONGO_ORCHESTRATION_URI . "/servers/$id", false, $ctx);
569569
}
570570

571-
function severityToString($type) {
572-
switch($type) {
573-
case E_DEPRECATED:
574-
return "E_DEPRECATED";
575-
case E_RECOVERABLE_ERROR:
576-
return "E_RECOVERABLE_ERROR";
577-
case E_WARNING:
578-
return "E_WARNING";
579-
case E_NOTICE:
580-
return "E_NOTICE";
581-
default:
582-
return "Some other #_$type";
583-
}
584-
}
585-
function raises($function, $type, $infunction = null) {
586-
$errhandler = function($severity, $message, $file, $line) {
571+
/**
572+
* Converts an error level (constant or bitmask) to a string description.
573+
*/
574+
function severityToString(int $severity): string {
575+
static $constants = [
576+
'E_ERROR' => E_ERROR,
577+
'E_WARNING' => E_WARNING,
578+
'E_PARSE' => E_PARSE,
579+
'E_NOTICE' => E_NOTICE,
580+
'E_CORE_ERROR' => E_CORE_ERROR,
581+
'E_CORE_WARNING' => E_CORE_WARNING,
582+
'E_COMPILE_ERROR' => E_COMPILE_ERROR,
583+
'E_COMPILE_WARNING' => E_COMPILE_WARNING,
584+
'E_USER_ERROR' => E_USER_ERROR,
585+
'E_USER_WARNING' => E_USER_WARNING,
586+
'E_USER_NOTICE' => E_USER_NOTICE,
587+
'E_STRICT' => E_STRICT,
588+
'E_RECOVERABLE_ERROR' => E_RECOVERABLE_ERROR,
589+
'E_DEPRECATED' => E_DEPRECATED,
590+
'E_USER_DEPRECATED' => E_USER_DEPRECATED,
591+
// E_ALL is handled separately
592+
];
593+
594+
if ($severity === E_ALL) {
595+
return 'E_ALL';
596+
}
597+
598+
foreach ($constants as $constant => $value) {
599+
if ($severity & $value) {
600+
$matches[] = $constant;
601+
}
602+
}
603+
604+
return empty($matches) ? 'UNKNOWN' : implode('|', $matches);
605+
}
606+
607+
/**
608+
* Expects the callable to raise an error matching the expected severity, which
609+
* may be a constant or bitmask. May optionally expect the error to be raised
610+
* from a particular function. Returns the message from the raised error or
611+
* exception, or an empty string if neither was thrown.
612+
*/
613+
function raises(callable $callable, int $expectedSeverity, string $expectedFromFunction = null): string
614+
{
615+
set_error_handler(function(int $severity, string $message, string $file, int $line) {
587616
throw new ErrorException($message, 0, $severity, $file, $line);
588-
};
617+
});
589618

590-
set_error_handler($errhandler, $type);
591619
try {
592-
$function();
593-
} catch(Exception $e) {
594-
$exceptionname = get_class($e);
595-
596-
if ($e instanceof ErrorException && $e->getSeverity() & $type) {
597-
if ($infunction) {
598-
$trace = $e->getTrace();
599-
$function = $trace[0]["function"];
600-
if (strcasecmp($function, $infunction) == 0) {
601-
printf("OK: Got %s thrown from %s\n", $exceptionname, $infunction);
602-
} else {
603-
printf("ALMOST: Got %s - but was thrown in %s, not %s\n", $exceptionname, $function, $infunction);
604-
}
605-
restore_error_handler();
606-
return $e->getMessage();
607-
}
620+
call_user_func($callable);
621+
} catch (ErrorException $e) {
622+
if (!($e->getSeverity() & $expectedSeverity)) {
623+
printf("ALMOST: Got %s - expected %s\n", severityToString($e->getSeverity()), severityToString($expectedSeverity));
624+
return $e->getMessage();
625+
}
626+
627+
if ($expectedFromFunction === null) {
608628
printf("OK: Got %s\n", severityToString($e->getSeverity()));
609-
} else {
610-
printf("ALMOST: Got %s - expected %s\n", get_class($e), $exceptionname);
629+
return $e->getMessage();
611630
}
612-
restore_error_handler();
631+
632+
$fromFunction = $e->getTrace()[0]['function'];
633+
634+
if (strcasecmp($fromFunction, $expectedFromFunction) !== 0) {
635+
printf("ALMOST: Got %s - but was raised from %s, not %s\n", errorLevelToString($e->getSeverity()), $fromFunction, $expectedFromFunction);
636+
return $e->getMessage();
637+
}
638+
639+
printf("OK: Got %s raised from %s\n", severityToString($e->getSeverity()), $fromFunction);
640+
return $e->getMessage();
641+
} catch (Throwable $e) {
642+
printf("ALMOST: Got %s - expected %s\n", get_class($e), ErrorException::class);
613643
return $e->getMessage();
644+
} finally {
645+
restore_error_handler();
614646
}
615647

616-
printf("FAILED: Expected %s thrown!\n", ErrorException::class);
617-
restore_error_handler();
648+
printf("FAILED: Expected %s, but no error raised!\n", ErrorException::class);
649+
return '';
618650
}
619-
function throws($function, $exceptionname, $infunction = null) {
651+
652+
/**
653+
* Expects the callable to throw an expected exception. May optionally expect
654+
* the exception to be thrown from a particular function. Returns the message
655+
* from the thrown exception, or an empty string if one was not thrown.
656+
*/
657+
function throws(callable $callable, string $expectedException, string $expectedFromFunction = null): string
658+
{
620659
try {
621-
$function();
660+
call_user_func($callable);
622661
} catch (Throwable $e) {
623-
} catch (Exception $e) {
624-
}
662+
if (!($e instanceof $expectedException)) {
663+
printf("ALMOST: Got %s - expected %s\n", get_class($e), $expectedException);
664+
return $e->getMessage();
665+
}
625666

626-
if (!isset($e)) {
627-
echo "FAILED: Expected $exceptionname thrown, but no exception thrown!\n";
628-
return;
629-
}
667+
if ($expectedFromFunction === null) {
668+
printf("OK: Got %s\n", $expectedException);
669+
return $e->getMessage();
670+
}
671+
672+
$fromFunction = $e->getTrace()[0]['function'];
630673

631-
$message = str_replace(array("\n", "\r"), ' ', $e->getMessage());
632-
if ($e instanceof $exceptionname) {
633-
if ($infunction) {
634-
$trace = $e->getTrace();
635-
$function = $trace[0]["function"];
636-
if (strcasecmp($function, $infunction) == 0) {
637-
printf("OK: Got %s thrown from %s\n", $exceptionname, $infunction);
638-
} else {
639-
printf("ALMOST: Got %s - but was thrown in %s, not %s (%s)\n", $exceptionname, $function, $infunction, $message);
640-
}
674+
if (strcasecmp($fromFunction, $expectedFromFunction) !== 0) {
675+
printf("ALMOST: Got %s - but was thrown from %s, not %s\n", $expectedException, $fromFunction, $expectedFromFunction);
641676
return $e->getMessage();
642677
}
643-
printf("OK: Got %s\n", $exceptionname);
644-
} else {
645-
printf("ALMOST: Got %s (%s) - expected %s\n", get_class($e), $message, $exceptionname);
678+
679+
printf("OK: Got %s thrown from %s\n", $expectedException, $fromFunction);
680+
return $e->getMessage();
646681
}
647682

648-
return $e->getMessage();
683+
printf("FAILED: Expected %s, but no exception thrown!\n", $expectedException);
684+
return '';
649685
}
650686

651687
function printServer(Server $server)

0 commit comments

Comments
 (0)