6

In my NetBeans I have set up a project (ZF2 app) with PHPUnit. Everything is fine except when I try to collect the code coverage, it fails to cover certain lines, which I do not understand nor can explain.

The code is like this:

    switch($type) {
        case 'date':
            return date('Y-m-d', strtotime($value));
        case 'numeric':
        default:
            return $value;
    }

Here after the UnitTest tests both cases, the closing bracket of the switch statement is not covered.

Other example:

    foreach ($this->userRoleNames as $role) {
        if (self::$acl->hasResource($moduleName)) {
            if (self::$acl->isAllowed($role, $moduleName)) {
                return true;
            }
/* > */ }

        if (self::$acl->hasResource($privilageName)) {
            if (self::$acl->isAllowed($role, $privilageName)) {
                return true;
            }
/* > */ }

        if (self::$acl->hasResource($privilageNameFull)) {
            if (self::$acl->isAllowed($role, $privilageNameFull)) {
                return true;
            }
/* > */ }
    }

Here all the lines containing /* > */ are also not covered, even the Unit Tests are testing for any possible combinations of conditions.

Just because of these uncovered lines I am not able to reach 100% code coverage.

I am using

  • PHPUnit 3.7.22
  • PHP 5.3.10-1ubuntu3.8 with Suhosin-Patch
  • Netbeans 7.4 (b. 201310111528)

Notice: strange thing is, that in other if, foreach or switch statements, that do not return any value, their closing bracket } is not matched as covered nor as uncovered (covered lines are highlighted green, uncovered red - these are simply white - see attached img) thus have no influence on Code coverage percentage...

Is there any way how can I make the PHPUnit (Code coverage module in NB) or by adjusting the PHPUnit test cover such lines?

Code coverage example from NetBeans

shadyyx
  • 15,825
  • 6
  • 60
  • 95

1 Answers1

3

The } not covered in the lower screenshot is clear - it's never reached because of the return statements in the case clauses. For that reason the switch($type) block is not covered completely which (i guess) results in white background.

Just refactor the switch clause not returning values in the case blocks but assigning a return value, which is returned at the end of the function.

l-x
  • 1,521
  • 9
  • 17
  • Changed the switch to not return values, but assign them, and return afterwards - **no change, still the switch closing bracket is not covered**. But, after I removed **default:** clause, now the code is covered 100% even the switch and it's closing bracket are white (jumped over). Anyway, this does not solve the problem with `if` statements defined in my question... – shadyyx Nov 22 '13 at 10:54
  • And if What You are saying would be true, than any of the closing method brackets could not be covered as well as You have `return ...;` in there... Sounds odd... – shadyyx Nov 22 '13 at 10:58
  • The coverage-analysis is not done by php itself but by xdebug for example. This tools are great but not perfect. So this could simply be a bug in xdebug. I stumbeld over cases where whole methods where declared as dead code while i've seen their output on my terminal. – l-x Nov 22 '13 at 14:28