如何捕获“未定义的索引” simpleTest 中出现 E_NOTICE 错误?

发布于 2024-09-10 06:40:16 字数 293 浏览 1 评论 0原文

我想使用 simpleTest 编写一个测试,如果我正在测试的方法导致 PHP E_NOTICE“undefined index : foo”,该测试将会失败。

我尝试了 expectError()expectException() 但没有成功。 simpleTest 网页表明 simpleTest 无法捕获编译时 PHP 错误,但 E_NOTICE 似乎是运行时错误。

有没有办法捕获这样的错误并使我的测试失败(如果是这样)?

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".

I tried expectError() and expectException() without success. The simpleTest webpage indicate that simpleTest isn't able to catch compile time PHP errors, but E_NOTICE seems to be a run time error.

Is there a way to catch such an error and makes my test fail if so ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海夕 2024-09-17 06:40:16

这并不容易,但我终于成功捕获了我想要的 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();
}

这似乎有点过于复杂,必须重新声明错误处理程序来抛出异常才能捕获它。另一个困难的部分是在捕获异常且没有发生错误时正确恢复 error_handler,否则它只会扰乱 SimpleTest 错误处理。

That wasn't really easy but I finally managed to catch the E_NOTICE error I wanted. I needed to override the current error_handler to throw an exception that I will catch in a try{} statement.

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.

攀登最高峰 2024-09-17 06:40:16

实际上没有必要捕获通知错误。人们还可以测试“array_key_exists”的结果,然后从那里继续。

http://www.php.net/manual/en/function .array-key-exists.php

测试 false 并使其失败。

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.

http://www.php.net/manual/en/function.array-key-exists.php

Test for false and have it fail.

尬尬 2024-09-17 06:40:16

你永远不会在 try-catch 块中捕获它,幸运的是我们有 set_error_handler():

<?php
function my_handle(){}
set_error_handler("my_handle");
echo $foo["bar"];
?>

你可以在 my_handle() 函数中做任何你想做的事情,或者只是将其留空以消除通知,尽管不建议这样做。一个正常的处理程序应该是这样的:

function myErrorHandler($errno, $errstr, $errfile, $errline)

You'll never catch it within the try-catch block, luckily we have 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:

function myErrorHandler($errno, $errstr, $errfile, $errline)
宁愿没拥抱 2024-09-17 06:40:16

许多处理 at 符号 E_NOTICE 错误的解决方案都会忽略所有 E_NOTICE 错误。要仅忽略由于使用 at 符号而导致的错误,请在 set_error_handler 回调函数中执行此操作:

if (error_reporting()==0 && $errno==E_NOTICE)
    return; // Ignore notices for at sign

不应忽略的重要 E_NOTICE 示例如下:

$a=$b;

因为 $b 未定义。

Many solutions to handling at sign E_NOTICE errors ignore all E_NOTICE errors. To ignore just errors due to use of at signs, do this in your set_error_handler callback function:

if (error_reporting()==0 && $errno==E_NOTICE)
    return; // Ignore notices for at sign

An example of an important E_NOTICE that should not be ignored is this:

$a=$b;

because $b is undefined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文