Come on, we will use as a starting point that your machine has the version php7.2
with the xdebug
installed and configured. Typing command php -v
the terminal output will be similar to this:
PHP 7.2.11-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 24 2019 18:23:23) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.11-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.8.0, Copyright (c) 2002-2019, by Derick Rethans
Before I even do the Scrutinizer work. You’ll need to make the tests work, then:
- Install the
phpunit
via composer
. See the phpunit version to be used according to your php version.
"require-dev": {
"phpunit/phpunit": "8.0.*",
"mockery/mockery": "0.9.*"
},
- Configure your file
phpunit.xml
.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="false"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">tests</directory>
</testsuite>
</testsuites>
</phpunit>
- Have your bootstrap file configured in your test folder at the project root, for example:
test/bootstrap.php
. If you do not have many settings to be made or no simple file as demonstrated below will be enough
<?php
if (!defined('ROOT_DIR')) {
define('ROOT_DIR', __DIR__);
}
require_once __DIR__ . '/../vendor/autoload.php';
- Create and run your unit tests. None of the steps described here is more important than this rsrsrs
To analyze code coverage some settings are still required.
- In his file
phpunit.xml
add the settings Whitelist and logging.
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="target/coverage.html" lowUpperBound="35" highLowerBound="70" />
<log type="coverage-clover" target="target/coverage.xml"/>
<log type="coverage-text" target="target/coverage.txt"/>
</logging>
Create the folder at the root of the project target/
. This folder will be used to store the files configured in logging in the archive phpunit.xml
generated in code coverage. Don’t forget to add this folder to the file .gitignore
. You don’t have to commit she.
To make your life easier. Create scripts in the file composer.json
not having to remember the command all the time. In this case, we have two scripts. One to run normal tests and the other to run the Coverage.
"scripts": {
"test": "php ./vendor/phpunit/phpunit/phpunit --bootstrap ./tests/bootstrap.php --configuration ./phpunit.xml",
"test-coverage": "php ./vendor/phpunit/phpunit/phpunit --bootstrap ./tests/bootstrap.php --configuration ./phpunit.xml --coverage-clover ./target/coverage.xml",
}
Now we get where we want to go. To set up the Scrutinizer:
build:
environment:
php:
version: 7.2
nodes:
coverage:
tests:
override:
- command: 'php ./vendor/phpunit/phpunit/phpunit --bootstrap ./tests/bootstrap.php --configuration ./phpunit.xml'
coverage:
file: './target/coverage.xml'
format: 'clover'
filter:
paths:
- 'src/*'
excluded_paths:
- 'vendor/*'
- 'tests/*'
In the archive .scrutinizer.yml
pass some settings, as:
- The php version that tests should run
- The command to be rotated
- The archive
./target/coverage.xml
that the Scrutinizer will use to read code coverage information
- The format of the test used
clover
- The briefcase that the Coverage spun
src/*
- And the folders that the Scrutinizer shall not take into account the Coverage
vendor/*
and tests/*
Also follows a sample project to see in practice.