DEV Community

Cover image for PHP Test Coverage with Travis CI and Code Climate using Clover
Arnold Acho
Arnold Acho

Posted on • Updated on • Originally published at acho.arnold.cf

PHP Test Coverage with Travis CI and Code Climate using Clover

This article is a guide on how to set-up a PHP based repository on code climate such that it reports the test coverage. When you push a commit to your repository on GitHub, the Travis CI pipelines will run and generate the test coverage report which will then be sent to the code climate platform.

1. Get your code climate Test Reporter ID

Navigate to "Repo Settings -> Test Coverage" as seen on the image below
Code Climate Image

2. Configure your .travis.yml file

Grab a sample .travis.yml file for a PHP project from this repository https://git.io/fxu0q

NOTE: Copy the .travis.yml file from Example 2 since it uses Clover for test coverage.

In the global section of the .travis.yml file, replace the CC_TEST_REPORTER_ID variable with the ID which you copied from step 1 above.

Change the php version under the php block to your desired version.

Also, under the before_install section, add the script composer install --no-dev such that your composer dependencies are installed before tests are run.

Finally, Change the last line from

 - if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT; fi

to

  - if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build  -t clover --exit-code $TRAVIS_TEST_RESULT; fi

Your .travis.yml file would look like this after the above changes

env:
  global:
  - CC_TEST_REPORTER_ID={put your reporter id here}
  - GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct; else git log -1 --skip 1 --pretty=format:%ct; fi)
language: php
php:
  - '7.2'
before_script:
  - composer install --no-dev
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
  - chmod +x ./cc-test-reporter
  - ./cc-test-reporter before-build
script:
  - "phpunit --coverage-text --coverage-clover build/logs/clover.xml"
  - if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build  -t clover --exit-code $TRAVIS_TEST_RESULT; fi

You can get this .travis.yml file on github.

3. Configure phpunit.xml

It is mandatory to configure a whitelist for telling PHPUnit which source code files to include in the code coverage report. If you don't already have a whitelist filter in your phpunit.xml file, add one as such

<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./src</directory> 
        <exclude>
           <directory>./vendor</directory>
        </exclude>
    </whitelist>
</filter>

In the example above, ./src is the path to the directory where your source files are found and ./vendor is the path to a directory which you want to ignore.

Also, tell PHPUnit where to log the test coverage output by adding a logging block in the phpunit.xml file

<logging>
    <log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

My complete PHPUnit file looks like this

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="unit">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./src</directory>
            <exclude>
                <directory>./vendor</directory>
                <directory>./doc</directory>
                <directory>./SampleApp_RoadPlanner/</directory>
            </exclude>>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-clover" target="build/logs/clover.xml"/>
    </logging>
</phpunit>

You can see this code on GitHub

That Is All!

Next time when your push a build on Travis CI, it will run your tests and send the coverage report to Code Climate.

Sample Test Coverage Report

Top comments (7)

Collapse
 
david_j_eddy profile image
David J Eddy

Nice article about adding coverage reports to test execution. I love this stuff. Getting metrics and telemetry from application trends.

Could you add an image at the end of what the output of the example project looks like?

Collapse
 
achoarnold profile image
Arnold Acho

I think the header image shows how the output looks on code climate? Or would you like to see the actual test coverage report which is generated by clover?

Collapse
 
david_j_eddy profile image
David J Eddy

A link to the report would be nice.

Thread Thread
 
achoarnold profile image
Arnold Acho • Edited
Thread Thread
 
david_j_eddy profile image
David J Eddy

Bingo, thank you.

Collapse
 
rokumatsumoto profile image
Samet Gunaydin

Nice typo CC_TEST_REPOTER_ID

Collapse
 
achoarnold profile image
Arnold Acho

Thanks I've fixed it