100% code coverage with different PHP versions in Travis-CI

987 views Asked by At

I'm working on my pet-project Pipes and try to keep as close as possible to the 100% code coverage (I'm using Coveralls) for that).

The problem I'm facing is: how to get 100% code coverage with different PHP versions? As of now, my project doesn't contain version-specific code, but this is likely to change very soon.

I'd like to get a 100% code coverage for PHP 5.4 even if the generators related code would not be executed when testing against 5.4.

Can anybody offer a strategy or advice about how to keep my coverage statistics accountable?

Here's my .travis.yml:

language: php

php:
  - 5.6
  - 5.5
  - 5.4
  - hhvm

install:
  - composer require satooshi/php-coveralls:~0.6@stable

before_script:
  - curl -s http://getcomposer.org/installer | php
  - php composer.phar install --dev
  - mkdir -p build/logs

script:
  - phpunit --coverage-clover build/logs/clover.xml

after_success:
  - sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;'

Here's my phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
    backupStaticAttributes="false"
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    processIsolation="false"
    stopOnFailure="false"
    stopOnError="false"
    stopOnIncomplete="false"
    stopOnSkipped="false"
    syntaxCheck="false"
    bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <!-- Add a filter to make sure we don't count venders and Tests in the coverage report -->
    <filter>
        <whitelist>
            <directory suffix="Test.php">./src</directory>
            <exclude>
                <directory>./docs</directory>
                <directory>./vendor</directory>
                <directory>./tests</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

PS: I know that Code Coverage is not a silver bullet.

1

There are 1 answers

0
josephdpurcell On

Using another example I found, I discovered that @requires negatively impacts code coverage. For example:

<?php

require_once('class.php');

class FooTest extends PHPUnit_Framework_TestCase {
    /**
    * @requires PHP 5.5
    * @covers Foo::greater
    */
    public function testGreater() {
        $x = new Foo();
        $this->assertSame(2, $x->greater());
    }

    public function testLesser() {
        $x = new Foo();
        $this->assertSame(1, $x->lesser());
    }
}

<?php
class Foo {
    public function greater() {
        return 2;
    }

    public function lesser() {
        return 1;
    }
}

In my opinion, the @covers should force code coverage to mark the greater method as covered. But, it does not:

enter image description here