php 如何在 simpleTest 中捕获“未定义索引”E_NOTICE 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3261051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch an "undefined index" E_NOTICE error in simpleTest?
提问by pixelastic
I would like to write a test using simpleTest that would fail if the method I'm testing results in a PHP E_NOTICE"undefined index : foo".
我想使用 simpleTest 编写一个测试,如果我正在测试的方法导致 PHP E_NOTICE“未定义索引:foo” ,该测试将失败。
I tried expectError()and expectException()without success. The simpleTest webpage indicate that simpleTest isn't able to catch compile time PHP errors, but E_NOTICEseems to be a run time error.
我试过了expectError(),expectException()但没有成功。simpleTest 网页表明 simpleTest 无法捕获编译时 PHP 错误,但E_NOTICE似乎是运行时错误。
Is there a way to catch such an error and makes my test fail if so ?
有没有办法捕获这样的错误并使我的测试失败?
回答by pixelastic
That wasn't really easy but I finally managed to catch the E_NOTICEerror I wanted. I needed to override the current error_handlerto throw an exception that I will catch in a try{}statement.
这并不容易,但我终于设法抓住了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();
}
This seems a little overly complicated having to redeclare the error handler to throw an exception just to catch it. The other hard part was correctly restoring the error_handler both when an exception was catched and no error occured, otherwise it just messes with SimpleTest error handling.
这似乎有点过于复杂,不得不重新声明错误处理程序以抛出异常只是为了捕获它。另一个困难的部分是在捕获异常并且没有发生错误时正确恢复 error_handler ,否则它只会与 SimpleTest 错误处理混淆。
回答by Robert Brisita
There really isn't a need to catch the notice error. One could also test the outcome of 'array_key_exists' and then proceed from there.
真的没有必要捕捉通知错误。还可以测试“array_key_exists”的结果,然后从那里继续。
http://www.php.net/manual/en/function.array-key-exists.php
http://www.php.net/manual/en/function.array-key-exists.php
Test for false and have it fail.
测试错误并让它失败。
回答by John Lee
You'll never catch it within the try-catch block, luckily we have set_error_handler():
你永远不会在 try-catch 块中捕获它,幸运的是我们有 set_error_handler():
<?php
function my_handle(){}
set_error_handler("my_handle");
echo $foo["bar"];
?>
You can do anything you want inside my_handle() function, or just leave it empty to silence the notice, although, it's not recommended. A normal handler should be like this:
您可以在 my_handle() 函数中做任何您想做的事情,或者只是将其留空以使通知静音,但不建议这样做。一个普通的处理程序应该是这样的:
function myErrorHandler($errno, $errstr, $errfile, $errline)

