Skip to content

Commit

Permalink
GH Actions: start recording code coverage
Browse files Browse the repository at this point in the history
This commit reworks the `test` workflow to start running the tests with code coverage for low/medium/high PHP and upload the generated reports to Coveralls.

* It adds an extra "coverage" job which runs only the unit tests with code coverage against low/medium/high PHP.
    This job does not run the CS check as an integration test.
* Makes minor tweaks to the pre-existing "test" job to prevent duplicate test runs for the exact same PHP/custom ini combinations.
    No need to run the same check twice.

The recorded code coverage reports will be available on: https://coveralls.io/github/PHPCSStandards/PHP_CodeSniffer

Includes adding a code coverage badge to the README.
  • Loading branch information
jrfnl committed Dec 9, 2023
1 parent c49a254 commit 3f29c24
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/quicktest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
run: php bin/phpcs --config-set php_path php

- name: 'PHPUnit: run the tests'
run: vendor/bin/phpunit tests/AllTests.php
run: vendor/bin/phpunit tests/AllTests.php --no-coverage

# Note: The code style check is run as an integration test.
- name: 'PHPCS: check code style without cache, no parallel'
Expand Down
103 changes: 100 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ jobs:
custom_ini: [false]

include:
# Builds running the basic tests with different PHP ini settings.
# Skip test runs on builds which are also run for in the coverage job.
# Note: the tests on PHP 7.2 will still be run as the coverage build is uses custom_ini for that version.
- php: '5.4'
skip_tests: true
- php: '8.3'
skip_tests: true

# Extra builds running only the unit tests with different PHP ini settings.
- php: '5.5'
custom_ini: true
- php: '7.0'
Expand Down Expand Up @@ -137,8 +144,9 @@ jobs:
- name: 'PHPCS: set the path to PHP'
run: php bin/phpcs --config-set php_path php

- name: 'PHPUnit: run the tests'
run: vendor/bin/phpunit tests/AllTests.php
- name: 'PHPUnit: run the tests without code coverage'
if: ${{ matrix.skip_tests != true }}
run: vendor/bin/phpunit tests/AllTests.php --no-coverage

- name: 'PHPCS: check code style without cache, no parallel'
if: ${{ matrix.custom_ini == false && matrix.php != '7.4' }}
Expand All @@ -163,3 +171,92 @@ jobs:
- name: 'PHPCS: check code style using the Phar file'
if: ${{ matrix.custom_ini == false }}
run: php phpcs.phar

coverage:
runs-on: ubuntu-latest

strategy:
matrix:
include:
- php: '5.4'
custom_ini: false
- php: '7.2'
custom_ini: true
- php: '8.3'
custom_ini: false

name: "Coverage: ${{ matrix.php }} ${{ matrix.custom_ini && ' with custom ini settings' || '' }}"

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup ini config
id: set_ini
run: |
# Set the "short_open_tag" ini to make sure specific conditions are tested.
# Also turn on error_reporting to ensure all notices are shown.
if [[ ${{ matrix.custom_ini }} == true && "${{ startsWith( matrix.php, '5.' ) }}" == true ]]; then
echo 'PHP_INI=error_reporting=-1, display_errors=On, date.timezone=Australia/Sydney, short_open_tag=On, asp_tags=On' >> $GITHUB_OUTPUT
elif [[ ${{ matrix.custom_ini }} == true && "${{ startsWith( matrix.php, '7.' ) }}" == true ]]; then
echo 'PHP_INI=error_reporting=-1, display_errors=On, date.timezone=Australia/Sydney, short_open_tag=On' >> $GITHUB_OUTPUT
else
echo 'PHP_INI=error_reporting=-1, display_errors=On' >> $GITHUB_OUTPUT
fi
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
ini-values: ${{ steps.set_ini.outputs.PHP_INI }}
coverage: xdebug

# This action also handles the caching of the dependencies.
- name: Set up node
if: ${{ matrix.custom_ini == false }}
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install external tools used in tests
if: ${{ matrix.custom_ini == false }}
run: >
npm install -g --fund false
csslint
eslint
jshint
# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
- name: Install Composer dependencies
uses: "ramsey/composer-install@v2"
with:
# Bust the cache at least once a month - output format: YYYY-MM.
custom-cache-suffix: $(date -u "+%Y-%m")

- name: 'PHPCS: set the path to PHP'
run: php bin/phpcs --config-set php_path php

- name: 'PHPUnit: run the tests with code coverage'
run: vendor/bin/phpunit tests/AllTests.php

- name: Upload coverage results to Coveralls
if: ${{ success() }}
uses: coverallsapp/github-action@v2
with:
format: clover
file: build/logs/clover.xml
flag-name: php-${{ matrix.php }}-custom-ini-${{ matrix.custom_ini }}
parallel: true

coveralls-finish:
needs: coverage
if: always() && needs.coverage.result == 'success'

runs-on: ubuntu-latest

steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@v2
with:
parallel-finished: true
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PHP_CodeSniffer
[![Latest Stable Version](http://poser.pugx.org/phpcsstandards/php_codesniffer/v)](https://github.com/PHPCSStandards/PHP_CodeSniffer/releases)
[![Validate](https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/workflows/validate.yml/badge.svg?branch=master)](https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/workflows/validate.yml)
[![Test](https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/workflows/test.yml)
[![Coverage Status](https://coveralls.io/repos/github/PHPCSStandards/PHP_CodeSniffer/badge.svg?branch=master)](https://coveralls.io/github/PHPCSStandards/PHP_CodeSniffer?branch=master)
[![License](http://poser.pugx.org/phpcsstandards/php_codesniffer/license)](https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt)

![Minimum PHP Version](https://img.shields.io/packagist/php-v/squizlabs/php_codesniffer.svg?maxAge=3600)
Expand Down

0 comments on commit 3f29c24

Please sign in to comment.