How to configure phpunit with Scrutinizer?

Asked

Viewed 37 times

0

Following the document of Scrutinizer I realized the creation of the archive .scrutinizer.yml in the project in PHP. Analyzing other sites got to the final result of the file demonstrated below:

build:
  environment:
    php:
      version: 7.2
  nodes:
    coverage:
      tests:
        before:
          - "cp .env.example .env"
          - "echo 'END_POINT =your_webhook' > .env"
        override:
          - command: 'php ./vendor/bin/phpunit'
            coverage:
              file: 'phpunit.xml'
              format: 'php-clover'

filter:
  paths:
    - 'src/*'
  excluded_paths:
    - 'vendor/*'
    - 'tests/*'

This is my first experience with the Scrutinizer. It means that the final result of this file is more for trial and error of a conscious action for each set file configuration line.

As you can see I performed some specifications in the file, such as:

  1. Specify the php version used in the project.
  2. The need to exist a file . env and creation of a local variable because I use it in tests
  3. The command to run the test php ./vendor/bin/phpunit
  4. The phpunit configuration file phpunit.xml - I don’t know why I actually have that.
  5. And the format of the test php-clover. I don’t know what that’s for.

In the course of running Scrutinizer all tests are run successfully, but the option covarage aparece not enable.

What’s left to do to make it work? Note: I put all the information I considered useful, but if you need more details I will add as you ask.

1 answer

0


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:

  1. 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.*"
  },
  1. 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>
  1. 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';
  1. 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.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.