それは本当に簡単ではありませんでしたが、最終的に必要なE_NOTICE
エラーをキャッチすることができました. ステートメントerror_handler
でキャッチする例外をスローするには、現在をオーバーライドする必要がありました。try{}
function testGotUndefinedIndex() {
// Overriding the error handler
function errorHandlerCatchUndefinedIndex($errno, $errstr, $errfile, $errline ) {
// We are only interested in one kind of error
if ($errstr=='Undefined index: bar') {
//We throw an exception that will be catched in the test
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
return false;
}
set_error_handler("errorHandlerCatchUndefinedIndex");
try {
// triggering the error
$foo = array();
echo $foo['bar'];
} catch (ErrorException $e) {
// Very important : restoring the previous error handler
restore_error_handler();
// Manually asserting that the test fails
$this->fail();
return;
}
// Very important : restoring the previous error handler
restore_error_handler();
// Manually asserting that the test succeed
$this->pass();
}
これは、例外をキャッチするためだけに例外をスローするようにエラーハンドラーを再宣言する必要があるため、少し複雑すぎるようです。もう 1 つの難しい部分は、例外がキャッチされたときとエラーが発生しなかったときの両方で error_handler を正しく復元することでした。