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


How to make PHPunit return nonzero exit status on warnings

当调用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.

其中一个测试应该失败,但它没有;PHPunit将其标记为"incomplete"。

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

$ echo $?
0

我使用的配置是:

<phpunit
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    strict="true"
    stopOnError="true"
    stopOnFailure="true"
    stopOnIncomplete="true"
    stopOnSkipped="true"
    colors="true"
    bootstrap="bootstrap_phpunit.php"
    >

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

多亏了controllez,我开始研究错误处理程序,并最终找到了解决方案:

set_error_handler(function ($severity, $message, $filepath, $line)
{
    throw new Exception($severity." - ".$message." - ".$filepath." - ".$line);
});

这段代码抛出一个异常,导致PHPunit正确地将test检测为failed而不是incomplete。它应该放在bootstrap_phpunit.php的某个地方(即在文件中,指定为bootstrap文件)。