DEV Community

Cover image for Display Build Status and Test Coverage Badges for a Laravel Project
Nwachukwu Chibuike
Nwachukwu Chibuike

Posted on

Display Build Status and Test Coverage Badges for a Laravel Project

In search for a new developer role, I found myself building various projects just to scale through multiple interviews. One in particular was a Laravel project that included a specific instruction to display both build status and code coverage report as badges in my readme.md file.

I searched for help online but couldn't find any that gave the full guide to solving this problem, most only provided solutions for build status while ignoring the code coverage aspect. I had to figure this out and time was running out.

This below is what I intended to achieve and I would be showing how I accomplished this

badges

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 login using your GitHub account. Once 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 not, it's as simple as checking out the Laravel docs for writing test.
We're also going to install Coveralls, which we'll use to host the coverage report by simply require it:

composer require php-coveralls/php-coveralls

Or by adding the following line to the require block of your composer.json file.

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

This would add it to our composer.json file as a dependency.

Create a .travis.yml and .coveralls.yml

We would now proceed to create a Travis configuration file(.travis.yml) at the root of our Laravel project to get Travis CI set up. Also we would be creating a .coveralls.yml which would be used to set up the path to our clover.xml

Here's what my .travis.yml looks like: I'm setting the language, the version of php, and telling 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
Enter fullscreen mode Exit fullscreen mode

Here's what my .coveralls.yml looks like: I'm assuming 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
Enter fullscreen mode Exit fullscreen mode

For the benefit of doubt, this is how my phpunit.xml and .env.example files looks 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>
Enter fullscreen mode Exit fullscreen mode
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=sqlite
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=:memory:
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Enter fullscreen mode Exit fullscreen mode

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](https://travis-ci.com/chyke007/credible.svg?branch=master)](https://travis-ci.com/chyke007/credible)

Enter fullscreen mode Exit fullscreen mode

Coveralls Code Coverage Badge

[![Coverage Status](https://coveralls.io/repos/github/chyke007/credible/badge.svg?branch=master)](https://coveralls.io/github/chyke007/credible?branch=master)
Enter fullscreen mode Exit fullscreen mode

Now 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.

That is all, in general it was an exciting adventure, hopefully this serves as a guide to someone, a guide I wish I had!

Top comments (0)