【问题标题】:PhpUnit not showing a stack trace for a php fatal errorPhpUnit 未显示 php 致命错误的堆栈跟踪
【发布时间】:2011-08-25 01:58:46
【问题描述】:

PhpUnit 当前未显示代码中发生的 PHP 错误的堆栈跟踪。

我该如何配置它?

【问题讨论】:

  • 您能否提供一些示例代码,因为我不确定我是否理解正确。 (它似乎对我有用,也许我误解了)
  • 如中,我收到一个 PHP 致命错误:达到“100”的最大函数嵌套级别,正在中止!在第 x 行的 file.php 中,所以我想查看它的堆栈跟踪以了解发生了什么。 PhpUnit 显示的是最终错误,而不是完整的跟踪。
  • @edorian:我偶尔会看到当 phpunit 的错误处理程序阻塞递归嵌套对象时会发生这种情况。如果它最终耗尽了输出缓冲区中的内存,您可能会没有任何痕迹(尽管仍然是错误日志中的一个条目)。
  • 根据我的经验,您需要在您的情况下逐步遵循代码。
  • jerry rig 它可以在达到特定条件时停止代码和堆栈跟踪。

标签: php phpunit


【解决方案1】:

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 解释器的一个限制。致命的意思是致命的。

【讨论】:

    【解决方案2】:

    这是一种蹩脚但有效的方法,当 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);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多