Display build status/test coverage badges for a Laravel project

Suppose we have a Laravel project that includes a specific instruction to display both build status and code coverage report as badges in the readme.md file. The following illustration shows what we will accomplish by the end of this shot.

Setup repo with Travis CI and Coveralls

If you haven’t already, the first thing you need to do is to move over to Travis and log into your GitHub account. Once you’ve logged in, hit the “Add New Repository” button to activate your repo. Repeat this procedure at Coveralls.io.

Configure PHPUnit and Coveralls

Luckily for us, Laravel comes shipped with PHPUnit.

This tutorial assumes you have some tests written already. If you don’t, you can simply check out the Laravel docs for writing tests.

We’re also going to install Coveralls, which we’ll use to host the coverage report. We can simply require it:

composer require php-coveralls/php-coveralls

Or, we can add the following line to the require block of your composer.json file.

"php-coveralls/php-coveralls": "^1.1"

Create a .travis.yml and .coveralls.yml

We will now proceed to create a Travis configuration file (.travis.yml) at the root of our Laravel project to set up Travis CI. We will also create a .coveralls.yml, which will be used to set up the path to our clover.xml.

Here’s what my .travis.yml looks like. I set the language, the version of PHP, and tell it which additional script to run.

language: php
php:
  - 7.3
  - 7.4
os: linux

before_script:
  - rm -rf composer.lock
  - composer install
  - cp .env.example .env
script:
  - mkdir -p build/logs
  - vendor/bin/phpunit -c phpunit.xml
after_success:
  - travis_retry php vendor/bin/coveralls
  # or enable logging
  - travis_retry php vendor/bin/coveralls -v

Here’s what my .coveralls.yml looks like. I assume your phpunit.xml saves its clover.xml at the build/logs path.

coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
service_name: travis-ci

To further clarify, this is what my phpunit.xml and .env.example files look like:

<?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/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <logging>
      <log type="coverage-html" target="tests/coverage" showUncoveredFiles="true" />
      <log type="coverage-clover" target="build/logs/clover.xml"/>
   </logging>
    <php>
        <env name="APP_ENV" value="testing"/>
         <env name="DB_CONNECTION" value="sqlite"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="MAIL_DRIVER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
    </php>
</phpunit>

Deploy code and display badges

Here’s the code for both of the badges I used in my readme.md file. Both Travis and Coveralls will supply embed links for these.

Travis CI build badge

Build Status

Coveralls code coverage badge

Coverage Status

Once we push our code to GitHub with this setup, it should send the build off to Travis, and Travis should send the coverage off to Coveralls.

Attributions:
  1. undefined by undefined
Copyright ©2024 Educative, Inc. All rights reserved