【问题标题】:How to catch an "undefined index" E_NOTICE error in simpleTest?如何在 simpleTest 中捕获“未定义索引”E_NOTICE 错误?
【发布时间】:2011-03-16 17:16:08
【问题描述】:

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

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

有没有办法捕捉到这样的错误并让我的测试失败?

【问题讨论】:

    标签: php exception simpletest


    【解决方案1】:

    这并不容易,但我终于设法捕获了我想要的 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 错误处理相混淆。

    【讨论】:

    • 这是多么糟糕的语言!
    • 如果您将 E_NOTICE 作为第二个参数提供给 set_error_handler,它将只处理通知。 "可用于屏蔽 error_handler 函数的触发,就像 error_reporting ini 设置控制显示哪些错误一样。没有此屏蔽设置,将为每个错误调用 error_handler与 error_reporting 设置无关。”
    【解决方案2】:

    确实不需要捕获通知错误。也可以测试 'array_key_exists' 的结果,然后从那里继续。

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

    测试 false 并让它失败。

    【讨论】:

    • 如果投反对票,请解释原因。
    • 有一种情况是您将一个数组投影到另一个数组中。您不想测试每个单独的地图,您想捕捉任何一个映射失败的情况。如果有例外情况,这会容易得多。
    【解决方案3】:

    你永远不会在 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)
    

    【讨论】:

    • 最佳答案实际上很简单且有效,不确定为什么它没有投票...但我的
    【解决方案4】:

    处理符号 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 未定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 2013-10-27
      • 2021-09-19
      • 2018-09-27
      • 2011-09-25
      • 2021-11-01
      • 2017-11-22
      • 2023-03-20
      相关资源
      最近更新 更多