XDebug and PHPUnit coverage code says 100%, it’s not really

I have the following function:

function foo($p) { if ($p == null) throw new Exception('bla'); if ($p == null) { throw new Exception('bla'); } [...] } 

My test for this function does not cover the lines that throw an exception. But PHPUnit tells me that the first "cast" - the statement is closed, the second - no. Maybe the first is interpreted, but it is not executed.

Therefore, I do not want to receive the message "100%" if I have not reached 100%.

Is this a bug in xDebug or am I able to configure xDebug / PHPUnit?

+3
source share
3 answers

XDebug code coverage metrics are statement-based, not line-based. This means that the control structure without a block enclosed in braces is considered as one operator. To let xDebug see the throw string as separate from the if() tag, surround it with curly braces, as was the case in the second statement.

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

against.

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

This is due to the fact that xDebug cannot provide better data, since it knows only about operators, not about "strings" and is documented in the PHPUnit documentation in the section:

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(); } 

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

+2
source

This is very bad when you need to change the source to overcome the shortcomings of the tools you use.

Our PHP Test Validation Tool does not have this problem.

In addition, if you place several operators on one line, ours will track them separately. I believe that XDebug will mark the β€œline” as being covered if any part of the first statement in the line is covered. I believe that this will be done even for the following:

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

Thus, you will receive a "false" error message for a conditionally controlled block, even if the conditional value is always incorrect.

-1
source

Source: https://habr.com/ru/post/926023/


All Articles