Why does PHPUnit show some closed curly braces that are not covered?

I am using PHPUnit 3.6.7, PHP_CodeCoverage 1.1.1 and Xdebug 2.1.2. When I have PHPUnit, write my statistics on code coverage in a clover style XML file, it sometimes shows a tight figure that is not covered by tests.

I see a lot of discussion on the web when PHPUnit “reaches” closed curly braces, but I don’t understand the general concept of what is happening. For example, I have zero coverage on one line:

if (is_array($foo)) { foreach ($foo as $bar) { if (property_exists($bar, 'baz')) { return $bar; } } } // this line has 0 coverage return null; 

And here:

 class Foo { public $myProperty; public function myMethod() { $this->myProperty = '1'; } } // this line has 0 coverage 

Other classes in my project do not have this problem; their close braces are not displayed at all in the XML file, therefore they are not listed as having zero coverage.

I understand that PHP_CodeCoverage 1.1.2 (not yet released) will allow me to add a comment "// @ codeCoverageIgnore" after closing the curly brace, but while this is not available, I want to know what is happening so that I can correct my tests to give me full coverage. What is the rule of the thumb to tell me when a brace should be considered “covered” or “not covered”?

+4
source share
1 answer

As a general rule, to tell me when a brace should be considered “covered” or “not covered”?

There is a "Edge Cases" section in the phpunit documentation , but this seems to be not complete, as I found out in the last browse days :)

What I personally have never seen is your second example of failure. I also could not reproduce it: I could not find a combination of PHP / xDebug / PHPUnit where this did not work. (Play below)

The same can be said of another case that you showed. For everyone, I could test both closing curly braces that were detected as “not feasible / reachable,” as you would expect.

So, for both of these cases, // @codeCoverageIgnore or // @ codeCoverageIgnore is not required [Home | The end].

As @Derick explained in the comments for further analysis, you will need the whole file.


Play

 <?php class Foo { public $myProperty; public function myMethod() { $this->myProperty = '1'; } } <?php require __DIR__ . '/closingBrace.php'; class FooTest extends PHPUnit_Framework_TestCase { public function testMyMethod() { $x = new Foo(); $x->myMethod(); } } 

Running phpunit --coverage-text fooTest.php

 Code Coverage Report 2012-01-12 10:17:32 Summary: Classes: 100.00% (1/1) Methods: 100.00% (1/1) Lines: 100.00% (2/2) 

which only marks $this->myProperty = '1'; closing bracket as executable file.

+3
source

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


All Articles