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

PHP Test Coverage with Travis CI and Code Climate using Clover

acho_arnold profile image αςнο αяηοℓδ Updated on ・3 min read

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


  - 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

  - 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
  - '7.2'
  - 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
  - "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

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

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

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

My complete PHPUnit file looks like this

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
        <testsuite name="unit">
            <directory suffix="Test.php">./tests</directory>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./src</directory>
        <log type="coverage-clover" target="build/logs/clover.xml"/>

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

Posted on by:

acho_arnold profile

αςнο αяηοℓδ


I'm a full stack web developer who loves writing clean code to solve interesting problems using PHP/Laravel and Vue.js. I ❤ Docker.


Editor guide

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?


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?




Thanks I've fixed it