PHPStorm 2020.2 - PHPUnit Code Coverage Incorrect

I recently upgraded from PHPStorm 2019.1.4 to 2020.2. I also upgraded from PHPUnit 7.4 to PHPUnit 9.3.7 just yesterday. After doing so, the coverage report generated by PHPStorm is severely inaccurate. When I generate an HTML coverage report with PHPUnit, I get 100% coverage across all files. PHPStorm is reporting 80-97% coverage. When I open the files in PHPStorm that are indicated as having less than the expected 100%, there are no uncovered lines being highlighted. All coded lines are indicated as being covered. The only thing that I can assume is that PHPStorm is counting empty lines as non-covered (including lines between methods/properties).

1
6 comments

Tried to reproduce that, but no luck: https://recordit.co/YhEDRu2rEW
Does it happen to any projects for you? Maybe you can build some kind of a project stub that is enough for reproduction?

0
Avatar
Permanently deleted user

Yes, it is happening for me. I updated to PHPUnit 9.3.8 (php-code-coverage 9.1.6), just make sure I was on the latest versions. I am still seeing a discrepancy between coverage statistics. I can't attach a screen recording due the proprietary nature of the code in question, but I do have screenshots showing the differences being reported.

0

This second screenshot is a screenshot of the generated HTML, isn't it? If so, it seems that it's not PhpStorm's fault - the report generated by PHPUnit also shows the same numbers.
You can just run the test in the terminal, outside PhpStorm, to confirm that. Something like vendor/bin/phpunit tests/MyClassTest.php --coverage-text=coverage.txt.

0
Avatar
Permanently deleted user

It doesn't show the same number, that's the problem. It appears that PHPStorm is treating a class with no executable lines as not being covered, even if it is covered. There are 4 screenshots. The first screenshot is from PHPStorm showing 50% files 93% lines. The second screenshot is from the HTML report showing 100% files and 100% lines. The last 2 screenshots were to show the contents of the Email namespace. All executable lines in the files have been covered.

It appears as though PHPStorm is not counting classes with no executable lines. i.e:

namespace App\Exception\Email;

use Exception;

class EmailSendException extends Exception
{
}

Even though this class is used in the code (thrown in source code and expected in tests), PHPStorm is not including it because there are no lines in the class. I have other classes that use traits with no other functionality in the class, but are counted as covered. i.e.:

namespace App;

class MyClass
{
use TraitA, TraitB;
}

The issue is that PHPStorm is processing the TeamCity XML output and marking files as not covered that are covered.

0
Avatar
Permanently deleted user

For added clarification, here is the Team City XML output for the 3 files I showed above:

<file name="/path/to/my/app/src/Exception/Email/EmailRecipientsFailedException.php">
<class name="App\Exception\Email\EmailRecipientsFailedException" namespace="global">
<metrics complexity="5" methods="3" coveredmethods="3" conditionals="0" coveredconditionals="0" statements="11" coveredstatements="11" elements="14" coveredelements="14"/>
</class>
<line num="11" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="2"/>
<line num="13" type="stmt" count="2"/>
<line num="14" type="stmt" count="2"/>
<line num="16" type="stmt" count="2"/>
<line num="17" type="stmt" count="2"/>
<line num="19" type="method" name="getRecipientEmailAddresses" visibility="public" complexity="1" crap="1" count="1"/>
<line num="21" type="stmt" count="1"/>
<line num="24" type="method" name="getRecipientEmailAddressesAsString" visibility="public" complexity="3" crap="3" count="2"/>
<line num="26" type="stmt" count="2"/>
<line num="27" type="stmt" count="2"/>
<line num="28" type="stmt" count="2"/>
<line num="29" type="stmt" count="2"/>
<line num="31" type="stmt" count="1"/>
<line num="35" type="stmt" count="2"/>
<metrics loc="37" ncloc="37" classes="1" methods="3" coveredmethods="3" conditionals="0" coveredconditionals="0" statements="11" coveredstatements="11" elements="14" coveredelements="14"/>
</file>
<file name="/path/to/my/app/src/Exception/Email/EmailSendException.php">
<class name="App\Exception\Email\EmailSendException" namespace="global">
<metrics complexity="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</class>
<line num="10" type="stmt" count="0"/>
<metrics loc="9" ncloc="9" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</file>
<file name="/path/to/my/app/src/Exception/Email/MessageTemplateNotFoundException.php">
<class name="App\Exception\Email\MessageTemplateNotFoundException" namespace="global">
<metrics complexity="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</class>
<metrics loc="8" ncloc="8" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</file>

There are no code lines, but PHPStorm is treating it like the file was not processed during the test at all. When there are 0 methods, the reported 0 covered methods shouldn't be saying the file was not included in the test, right?

0

Please sign in to leave a comment.