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"?
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.
<?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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With