How to enable code coverage output in job list for PHP project on gitlab.com

Well, in my case I had to add a pair of parentheses (capturing group) in my coverage regex. This is an extract from my .gitlab-ci.yml file:

phpunit:
  image: php:8.0
  stage: qa
  before_script:
    - pecl install xdebug
    - docker-php-ext-enable xdebug
  script: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text
  coverage: /\s+Lines:\s+(\d+\.\d+%)/

Also I recommend to move all PHPUnit configuration to phpunit.xml.


For people who are searching why PHPUnit is not outputting coverage report when run in CI there are some changes required.

In my case it was with PHP 7.3 CLI version and phpunit 9.4.3 coverage report was missing when run vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-text --colors=never, in log file it was outputted Warning: xdebug.mode=coverage has to be set in php.ini.

Solved it by adding xdebug.ini file with xdebug attribute xdebug.mode=coverage to PHP conf.d folder. To find where is your php.ini file is located run php -i |grep php\.ini


The problem was the missing Xdebug installation in the docker image. I could not install a proper version using apt-get, so I had to add a pecl install xdebug call in the before_script section:

image: php:7.1.1

cache:
  paths:
  - vendor/

before_script:
# Install git, the php image doesn't have installed
- apt-get update -yqq
- apt-get install git -yqq

# Install Xdebug
- pecl install xdebug
- docker-php-ext-enable xdebug

# Install composer
- curl -sS https://getcomposer.org/installer | php

# Install all project dependencies
- php composer.phar install

# Run our tests
test:
    only:
        - master
    script:
        - vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never