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

我们两清 提交于 2019-12-10 13:07:39

问题


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"?


回答1:


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.



来源:https://stackoverflow.com/questions/8827132/why-does-phpunit-show-some-close-curly-braces-as-not-being-covered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!