Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHPUnit show some close-curly-braces as not being covered?

I'm using PHPUnit 3.6.7, PHP_CodeCoverage 1.1.1, and Xdebug 2.1.2. When I have PHPUnit write my code coverage statistics to a clover-style XML file, it occasionally shows a close-curly-brace as not being covered by tests.

I see a lot of discussion online of when PHPUnit is "reaching" the close curly-braces, but I don't understand the general concept of what's going on. For example, I have zero coverage on one line here:

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 don't have this problem; their close-curly-braces don't show up in the XML file at all, so they are not listed as having zero coverage.

I understand that PHP_CodeCoverage 1.1.2 (not released yet) will let me put a "// @codeCoverageIgnore" comment after a close curly-brace, but until that's available I want to know what's going on, so that I can fix my tests to give me complete coverage. What's the rule-of-thumb to tell me when a curly-brace should be counted as "covered" or "not covered"?

like image 717
Brian Kendig Avatar asked Jan 11 '12 21:01

Brian Kendig


1 Answers

What's the rule-of-thumb to tell me when a curly-brace should be counted as "covered" or "not covered"?

There is a "Edge Cases" section in the phpunit documentation but that apparently isn't complete as I've learned in the last view days :)

What I've personally never seen is your second example failing. I also couldn't reproduce it: I couldn't find a PHP/xDebug/PHPUnit combination where this didn't work out. (Reproduce below)

The same goes for the other case you showed. For all I could test both closing braces where detected as "not executable/reachable" just like one would expect it.

So for both of those cases no //@codeCoverageIgnore or //@codeCoverageIgnore[Start|End] should be needed.

As @Derick suggested in the comments for any further analysis the whole file would be needed.


Reproduce

<?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 the $this->myProperty = '1'; it's closing brace as executable.

like image 97
edorian Avatar answered Oct 24 '22 12:10

edorian