如何使PHPunit在警告时返回非零退出状态

joh*_*odo 5 php phpunit

在一些失败并带有警告的测试中调用PHPunit时,我得到:

$ phpunit -c phpunit.xml --group app
Warning - MongoCollection::insert(): expects parameter 1 to be an array or object, null given in ...
    <more output>

OK, but incomplete or skipped tests!
Tests: 17, Assertions: 81, Incomplete: 1.
Run Code Online (Sandbox Code Playgroud)

其中一项测试应该失败,但事实并非如此; PHPunit将其标记为"不完整".

我们来检查最后退出状态:

$ echo $?
0
Run Code Online (Sandbox Code Playgroud)

我使用的配置是:

<phpunit
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    strict="true"
    stopOnError="true"
    stopOnFailure="true"
    stopOnIncomplete="true"
    stopOnSkipped="true"

    colors="true"
    bootstrap="bootstrap_phpunit.php"
    >
Run Code Online (Sandbox Code Playgroud)

任何想法如何强制PHPunit在"不完整"测试的情况下发出非零退出状态?

joh*_*odo 1

感谢 gontrollez,我开始研究错误处理程序并最终找到了解决方案:

set_error_handler(function ($severity, $message, $filepath, $line)
{
    throw new Exception($severity." - ".$message." - ".$filepath." - ".$line);
});
Run Code Online (Sandbox Code Playgroud)

此代码引发异常,导致 PHPunit 正确检测 test 作为而failed不是incomplete. 它应该放在某处bootstrap_phpunit.php(即在文件中,指定为bootstrap文件)。