php - Reaching 100% Code Coverage with PHPUnit -



php - Reaching 100% Code Coverage with PHPUnit -

i've been in process of creating test suite project, , while realize getting 100% coverage isn't the metric 1 should strive to, there unusual bit in code coverage study clarification.

see screenshot:

because lastly line of method beingness tested return, final line (which closing bracket) shows never beingness executed, , consequence whole method flagged not executed in overview. (either that, or i'm not reading study correctly.)

the finish method:

static public function &getdomain($domain = null) { $domain = $domain ?: self::domain(); if (! array_key_exists($domain, self::$domains)) { self::$domains[$domain] = new config(); } homecoming self::$domains[$domain]; }

is there reason this, or glitch?

(yes, read through how 100% code coverage phpunit, different case although similar.)

edit:

trudging on through report, noticed same true switch statement elsewhere in code. behaviour @ to the lowest degree extent consistent, baffling me none less.

edit2:

i'm running on: phpunit 3.6.7, php 5.4.0rc5, xdebug 2.2.0-dev on os x

first off: 100% code coverage great metric strive for. it's not achievable sane amount of effort , it's not of import :)

the issue comes xdebug telling phpunit line executable not covered.

for simple cases xdebug can tell line not reachable 100% code coverage there.

see simple example below.

2nd update

the issue fixed xdebug bugtracker building new version of xdebug solve issues :)

update (see below issues php 5.3.x)

since running php 5.4 , dev version of xdebug i've installed , tested it. run same issues same output you've commented on.

i'm not 100% sure if issue comes php-code-coverage (the phpunit module) xdebug. might issue xdebug dev.

i've filed bug php-code-coverage , we'll figure out issue comes from.

for php 5.3.x issues:

for more complex cases can fail.

for code showed can "it works me" (complex sample below).

maybe update xdebug , phpunit versions , seek again.

i've seen fail current versions depends on how whole class looks sometimes.

removing ?: operators , other single-line multi-statement things might help out.

there ongoing refactoring in xdebug avoid more of cases far i'm aware. xdebug 1 time wants able provide "statement coverage" , should prepare lot of cases. there not much 1 can here

while //@codecoverageignorestart , //@codecoverageignoreend line "covered" looks ugly , doing more bad good.

for case happens see question , answers from:

what-to-do-when-project-coding-standards-conflicts-with-unit-test-code-coverage

simple example: <?php class footest extends phpunit_framework_testcase { public function testbar() { $x = new foo(); $this->assertsame(1, $x->bar()); } } <?php class foo { public function bar() { homecoming 1; } }

produces:

phpunit --coverage-text mep.php phpunit 3.6.7 sebastian bergmann. . time: 0 seconds, memory: 3.50mb ok (1 test, 1 assertion) generating textual code coverage report, may take moment. code coverage study 2012-01-10 15:54:56 summary: classes: 100.00% (2/2) methods: 100.00% (1/1) lines: 100.00% (1/1) foo methods: 100.00% ( 1/ 1) lines: 100.00% ( 1/ 1) complex example: <?php require __dir__ . '/foo.php'; class footest extends phpunit_framework_testcase { public function testbar() { $this->assertsame('b', foo::getdomain('a')); $this->assertinstanceof('config', foo::getdomain('foo')); } } <?php class foo { static $domains = array('a' => 'b'); static public function &getdomain($domain = null) { $domain = $domain ?: self::domain(); if (! array_key_exists($domain, self::$domains)) { self::$domains[$domain] = new config(); } homecoming self::$domains[$domain]; } } class config {}

produces:

phpunit 3.6.7 sebastian bergmann. . time: 0 seconds, memory: 3.50mb ok (1 test, 2 assertions) generating textual code coverage report, may take moment. code coverage study 2012-01-10 15:55:55 summary: classes: 100.00% (2/2) methods: 100.00% (1/1) lines: 100.00% (5/5) foo methods: 100.00% ( 1/ 1) lines: 100.00% ( 5/ 5)

php testing phpunit code-coverage

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -