PhpUnit 当前未显示代码中发生的 PHP 错误的堆栈跟踪。
如何配置它以执行此操作?
PHPUnit 使用错误处理程序函数来捕获和显示错误,但从 PHP 错误处理程序手册中,
以下错误类型不能 使用用户定义的函数处理: E_ERROR、E_PARSE、E_CORE_ERROR、 E_CORE_WARNING、E_COMPILE_ERROR、 E_COMPILE_WARNING,以及大部分 E_STRICT在文件中引发的 调用 set_error_handler()。
如果您在单独的进程中运行测试,PHPUnit 将从解释器获得错误和消息,但没有可用的堆栈跟踪。这只是 PHP 解释器的一个限制。致命意味着致命。
这是一种蹩脚但有效的方法,我发现当 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);
}