执行 phpunit 的测试覆盖率统计,结果却报错了
Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable
先说结论,是因为代码中使用 CR
作为换行符, phpunit coverage 使用 \n
来判断文件有多少行,所以导致文件被判断为 0 行。
Laravel 在 bootsrap/app.php
中才对 ExceptionHandler
进行绑定,然而 coverage 没有执行绑定,导致处理异常时出现了如题所示的错误,导致真正的异常被覆盖掉,无法得到展示。真正的异常应该是
SebastianBergmann\LinesOfCode\NegativeValueException: $nonCommentLinesOfCode must not be negative
最终经过代码定位,找到了如下的代码
/**
* @throws ParserException
*/
private function analyse(string $filename): void
{
if (isset($this->classes[$filename])) {
return;
}
$source = file_get_contents($filename);
//就是这一行
$linesOfCode = substr_count($source, "\n");
if ($linesOfCode === 0 && !empty($source)) {
$linesOfCode = 1;
}
$parser = (new ParserFactory)->create(
ParserFactory::PREFER_PHP7,
new Lexer
);