Fail a phpunit test if coverage goes below a threshold

Dec 30, 2020 by Thibault Debatty | 4119 views

PHP GitLab DevOps

https://cylab.be/blog/114/fail-a-phpunit-test-if-coverage-goes-below-a-threshold

Tools like maven allow to fail a build pipeline if the coverage of unit tests goes below a given threshold. For phpunit and PHP project, there is no such option. So here is a trick to fail your pipeline if the coverage of your phpunit tests goes below a threshold.

The trick consists of 2 steps:

  1. run phpunit with the option --coverage-xml to create an xml coverage report;
  2. use a custom script (we will call it phpunit-threshold to parse the xml report and fail if the coverage goes below a provided threshold.

phpunit

For the first step, you have to run phpunit with the option --coverage-xml:

./vendor/bin/phpunit --coverage-xml target/coverage

This will create a directory called target/coverage, with a bunch of xml report files, including one file called index.xml

phpunit-threshold

We can now create a script called phpunit-threshold.php that will parse this xml report:

<?php

/**
 * phpunit-threshold.php
 * check if the coverage of phpunit is above specified threshold
 * https://cylab.be/blog/114/fail-a-phpunit-test-if-coverage-goes-below-a-threshold
 */

if ($argc != 3) {
    echo "Usage: " . $argv[0] . " <path/to/index.xml> <threshold>
";
    exit(-1);
}

$file = $argv[1];
$threshold = (double) $argv[2];

$coverage = simplexml_load_file($file);
$ratio = (double) $coverage->project->directory->totals->lines["percent"];

echo "Line coverage: $ratio%
";
echo "Threshold: $threshold%
";

if ($ratio < $threshold) {
    echo "FAILED!
";
    exit(-1);
}

echo "SUCCESS!
";

To use this script, we must indicate that path to index.xml as the first argument, and the desired coverage (in percent) as the second argument:

php phpunit-threshold.php target/coverage/index.xml 90

GitLab

Now you can add this test to your .gitlab-ci.yml:

test:
  image: cylab/php72
  before_script:
    # Install all project dependencies
    - COMPOSER_CACHE_DIR=composer-cache composer install
  script:
    - vendor/bin/phpunit --coverage-text --coverage-xml target/coverage-xml
    - php phpunit-threshold.php target/coverage-xml/index.xml 80
Fully customizable emails using Laravel 9
With the release of Laravel 9, the Swift Mailer (that is no longer maintained) has been replaced by the Symfony Mailer. You can already find some useful information about this change along all the other ones in the Upgrade Guide from Laravel 8.x to 9.0. However this guide does not contain enough information if you want to send fully customized emails. This blog post proposes you a solution coming directly from the Symfony documentation!
SQL injection with SQLMap
Code injection is one of the most critical web application vulnerabilities. Indeed, the consequences of code injection can be dramatic (impact). Moreover, still today a lot of web applications are vulnerable to code injection (frequency). Finally, some tools like SQLMap allow to automatically detect and use these vulnerabilities (exploitation). For this reason, the vulnerability is listed in the top 10 published by the Open Web Application Security Project (OWASP) [1]. In this blog post, we will present one type of code injection, called SQL injection, and we will show how to perform a SQL injection attack with SQLMap.
Filter USB devices with udev (and some PHP code)
USB devices can be a liability : they can be used to exfiltrate data from a computer or server, to plug a hardware keylogger, or to plant a malware. Hence on a managed computer, USB devices should be filtered and whitelisted. In this blog post we show how this can be achieved thanks to udev, and some PHP code.
This website uses cookies. More information about the use of cookies is available in the cookies policy.
Accept