code coverage of xDebug and PHPUnit says 100%, in fact it is not

断了今生、忘了曾经 提交于 2019-12-06 02:35:25

xDebug's code coverage metrics are statement-based rather than line-based. What this means is that a control structure without a block enclosed in braces is treated as a single statement. To let xDebug see the throw line as separate from the if() test, surround it with braces as you did in the second statement.

if ($p == null)                    // statement 1
    throw new Exception('bla');    // rest of statement 1

vs.

if ($p == null) {                  // statement 1
    throw new Exception('bla');    // statement 2
}

This happens because xDebug can't provide better data, as it is only aware of statements and not of 'lines' and is documented in the PHPUnit documentation under:

Code coverage analysis - Edge Cases:

<?php
// Due to how code coverage works internally these two lines are special.
// This line will show up as non executable
if(false)
    // This line will show up as covered because it is actually the 
    // coverage of the if statement in the line above that gets shown here!
    will_also_show_up_as_coveraged();

// To avoid this it is necessary that braces are used
if(false) {
    this_call_will_never_show_up_as_covered();
}

The same goes for the $x ? $y : $z; construct. The only way to avoid this behavior is to add curly braces.

Its pretty bad when you have to modify your source to overcome flaws of the tools you are using.

Our PHP Test Coverage Tool doesn't have this problem.

In addition, if you place several statements in the same line, ours will track them individually. I believe XDebug will mark the "line" as covered if any part of the first statement in the line is covered. I believe it will do this even for the following:

if (... )  { .... }

So you will get "false" coverage reported for the block controlled by the conditional, even if the conditional is always false.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!