Skip to content

Commit 249f20b

Browse files
committed
GH Actions: warm PHPUnit cache on PHPUnit 9.3+
As of PHPUnit 9.3, PHPUnit started using PHP-Parser as a basis to calculate code coverage. PHP Parser also polyfills tokens and that can interfere with the sniffs, but only in code coverage runs. In PHPUnit 9.3.4 a new feature was added to warm the code cache ahead of running tests recording code coverage. Using that feature should prevent the token interference. So far, I've not seen problems with this for this codebase, but better safe than sorry, so I'm going to enable cache warming now anyway. Notes regarding cache-warming: * The `--coverage-cache` and `--warm-coverage-cache` options are available since PHPUnit 9.3.4 and if these are used on older PHPUnit versions, PHPUnit will error out with an "unrecognized CLI argument" error. * In other words: these options can only be used with PHPUnit 9.3+, which is why the PHPUnit version is checked and remembered and subsequently used in the conditions. * Also: running PHPUnit with the `--warm-coverage-cache` option _does not run the tests_. It literally only warms the cache, which is why this is implemented as a separate step in the workflow. * The cache directory can be configured in the `phpunit.xml[.dist]` file, but only when using the PHPUnit 9.3+ `coverage` XML tag. As the PHPUnit config used needs to stay cross-version compatible with older PHPUnit versions, the CLI option is used for now. Refs: * sebastianbergmann/php-code-coverage#798 (comment) * sebastianbergmann/phpunit@9.3.3...9.3.4 Also note that PHP-Parser 5.0 was released a couple of days ago and when used for code coverage runs on PHP 8.0 (as used for the 4.0 branch), this causes problems. When I merge this commit up to 4.0, I will adjust the GH Actions script for the 4.0 branch to use PHP 8.1 instead of PHP 8.0 to avoid this issue. The issue has been reported upstream: sebastianbergmann/php-code-coverage#1025
1 parent d68210c commit 249f20b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

.github/workflows/test.yml

+23-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ jobs:
162162
include:
163163
- php: '7.2'
164164
custom_ini: false
165-
- php: '8.0'
165+
- php: '8.1'
166166
custom_ini: true
167167
- php: '8.3'
168168
custom_ini: false
@@ -199,12 +199,33 @@ jobs:
199199
# Bust the cache at least once a month - output format: YYYY-MM.
200200
custom-cache-suffix: $(date -u "+%Y-%m")
201201

202+
- name: Grab PHPUnit version
203+
id: phpunit_version
204+
# yamllint disable-line rule:line-length
205+
run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> $GITHUB_OUTPUT
206+
207+
- name: "DEBUG: Show grabbed version"
208+
run: echo ${{ steps.phpunit_version.outputs.VERSION }}
209+
202210
- name: 'PHPCS: set the path to PHP'
203211
run: php bin/phpcs --config-set php_path php
204212

205-
- name: 'PHPUnit: run the tests with code coverage'
213+
# PHPUnit 9.3 started using PHP-Parser for code coverage, which can cause issues due to Parser
214+
# also polyfilling PHP tokens.
215+
# As of PHPUnit 9.3.4, a cache warming option is available.
216+
# Using that option prevents issues with PHP-Parser backfilling PHP tokens during our test runs.
217+
- name: "Warm the PHPUnit cache (PHPUnit 9.3+)"
218+
if: ${{ steps.phpunit_version.outputs.VERSION >= '9.3' }}
219+
run: vendor/bin/phpunit --coverage-cache ./build/phpunit-cache --warm-coverage-cache
220+
221+
- name: "Run the unit tests with code coverage (PHPUnit < 9.3)"
222+
if: ${{ steps.phpunit_version.outputs.VERSION < '9.3' }}
206223
run: vendor/bin/phpunit
207224

225+
- name: "Run the unit tests with code coverage (PHPUnit 9.3+)"
226+
if: ${{ steps.phpunit_version.outputs.VERSION >= '9.3' }}
227+
run: vendor/bin/phpunit --coverage-cache ./build/phpunit-cache
228+
208229
- name: Upload coverage results to Coveralls
209230
if: ${{ success() }}
210231
uses: coverallsapp/github-action@v2

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"ext-xmlwriter": "*"
3737
},
3838
"require-dev": {
39-
"phpunit/phpunit": "^8.0 || ^9.0"
39+
"phpunit/phpunit": "^8.0 || ^9.3.4"
4040
},
4141
"bin": [
4242
"bin/phpcbf",

0 commit comments

Comments
 (0)