PhpUnit没有显示php致命错误的堆栈跟踪

时间:2011-05-13 16:10:04

标签: php phpunit

PhpUnit目前没有显示代码中出现的PHP错误的堆栈跟踪。

如何配置它?

2 个答案:

答案 0 :(得分:8)

PHPUnit使用错误处理函数来捕获和显示错误,但是来自error handlers上的PHP手册,

  

以下错误类型不能   使用用户定义的函数处理:   E_ERROR,E_PARSE,E_CORE_ERROR,   E_CORE_WARNING,E_COMPILE_ERROR,   E_COMPILE_WARNING,以及大部分内容   E_STRICT在文件中引发了   调用set_error_handler()。

如果您在单独的进程中运行测试,PHPUnit将从解释器获取错误和消息,但是没有可用的堆栈跟踪。这只是PHP解释器的限制。致命意味着致命。

答案 1 :(得分:3)

这是一种蹩脚但有效的方法,我发现当php没有提供堆栈转储时。我把它放在一个名为DebugUtil的类中。

        /**
         * This is for use when you have the UBER-LAME...
         * "PHP Fatal error:  Maximum function nesting level of '100' reached,
         * aborting!  in Lame.php(1273)
         * ...which just craps out leaving you without a stack trace.
         * At the line in the file where it finally spazzes out add
         * something like...
         * DebugUtil::dumpStack('/tmp/lame');
         * It will write the stack into that file every time it passes that
         * point and when it eventually blows up (and probably long before) you
         * will be able to see where the problem really is.
         */
        public static function dumpStack($fileName)
        {
            $stack = "";
            foreach (debug_backtrace() as $trace)
            {
                if (isset($trace['file'])  &&
                    isset($trace['line'])  &&
                    isset($trace['class']) &&
                    isset($trace['function']))
                {
                    $stack .= $trace['file']     . '#' .
                              $trace['line']     . ':' .
                              $trace['class']    . '.' .
                              $trace['function'] . "\n";
                }
            }
            file_put_contents($fileName, $stack);
        }
相关问题