Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Code coverage - cannot get some lines covered... (Netbeans)

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

like image 271
shadyyx Avatar asked Oct 21 '22 20:10

shadyyx


1 Answers

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.

like image 192
l-x Avatar answered Oct 27 '22 10:10

l-x