Skip to content

Commit 40d4800

Browse files
authored
Tests: allow for running on PHPUnit 10/11 (#328)
* UnusedFollowedByRequire: fix up test class name PHPUnit 10+ is stricter about naming conventions, so the test class name must now match the file name and the class should end on `Test`. * Tests: allow for running on PHPUnit 10/11 Until now, the tests would run on PHPUnit 9 for PHP 7.3 and higher. PHPUnit 10 (PHP 8.1+) was released in February 2023, PHPUnit 11 (PHP 8.2+) in February 2024 and the update for this test suite is quite painless, so let's allow for running the tests on PHPUnit 10 and 11. This commit actions this. Things to be aware of: Handling of PHPUnit deprecations Prior to PHPUnit 10.5.32 and PHPUnit 11.3.3, if the `failOnDeprecation` attribute was set (previously: `convertDeprecationsToExceptions`), tests runs would not only fail on PHP deprecation notices, but also on PHPUnit native deprecation notices when running on PHPUnit 10/11. This undesireable behaviour has now (finally) been reverted, which makes updating test suites for compatibility with PHPUnit 10/11 a lot more straight-forward. This is the reason that the minimum PHPUnit 10/11 versions in the `composer.json` file are set to such specific versions. As part of this change, PHPUnit 10.5.32 and 11.3.3 introduce two new options for both the configuration file and as CLI arguments: `displayDetailsOnPhpunitDeprecations` and `failOnPhpunitDeprecation`, both of which will default to `false`. These options have been added to the PHPUnit configuration for PHPUnit 10/11 to safeguard them against changes in the default value. Test configuration file changes The configuration file specification has undergone changes in PHPUnit 9.3 and 10.0. Most notably: * In PHPUnit 9.3, the manner of specifying the code coverage configuration has changed. * In PHPUnit 10.0, a significant number of attributes of the `<phpunit>` element were removed or renamed. While the `--migrate-configuration` command can upgrade a configuration for the changes in the format made in PHPUnit 9.3, some of the changes in the configuration format in PHPUnit 10 don't have one-on-one replacements and/or are not taken into account. With that in mind, I deem it more appropriate to have a dedicated PHPUnit configuration file for PHPUnit 10+ to ensure the test run will behave as intended. To that end: * The `phpunit.xml.dist` file has been renamed to `phpunitlte9.xml.dist` (for PHPUnit <= 9) and a new `phpunit.xml.dist` file has been added for PHPUnit 10+. * The renamed file is set to be excluded from release archives via `.gitattributes` and a potential local overload file for it is set to be ignored by default via the `.gitignore` file. * Additional scripts have been added to the `composer.json` file to run the tests either with the new or the old configuration file. * The GH Actions `test` workflow has been updated to select the correct configuration file based on the PHPUnit version on which the tests are being run. Other command/config changes Also note that the `--coverage-cache <dir>` CLI flag was deprecated in PHPUnit 10 and removed in PHPUnit 11. A generic `cacheDirectory` attribute is the recommended replacement (though a CLI option is also available). The new attribute has been added to the PHPUnit 10+ config file, while for PHPUnit 9.3 - 9.5, the flag is still added to the command in CI (as adding the PHPUnit 9.3 equivalent to the config file would make the config incompatible with PHPUnit < 9.3). Composer will not install PHPUnit 11 Psalm 5 has a dependency on PHP-Parser 4, while PHPUnit 11 requires PHP-Parser 5, so Psalm currently effectively blocks the installation of PHPUnit 11 via Composer. Running the tests on PHPUnit 11 will work fine though (tested with a PHAR file). Ref: * https://phpunit.de/announcements/phpunit-11.html * https://phpunit.de/announcements/phpunit-10.html --------- Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
1 parent a15f05d commit 40d4800

File tree

7 files changed

+95
-32
lines changed

7 files changed

+95
-32
lines changed

.gitattributes

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
# https://www.reddit.com/r/PHP/comments/2jzp6k/i_dont_need_your_tests_in_my_production
66
# https://blog.madewithlove.be/post/gitattributes/
77
#
8-
/.gitattributes export-ignore
9-
/.gitignore export-ignore
10-
/phpcs.xml.dist export-ignore
11-
/phpstan.neon.dist export-ignore
12-
/phpunit.xml.dist export-ignore
13-
/.github export-ignore
14-
/Tests/ export-ignore
8+
/.gitattributes export-ignore
9+
/.gitignore export-ignore
10+
/phpcs.xml.dist export-ignore
11+
/phpstan.neon.dist export-ignore
12+
/phpunit.xml.dist export-ignore
13+
/phpunitlte9.xml.dist export-ignore
14+
/.github export-ignore
15+
/Tests/ export-ignore
1516

1617
#
1718
# Auto detect text files and perform LF normalization

.github/workflows/test.yml

+31-9
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,21 @@ jobs:
111111
composer-options: --ignore-platform-req=php
112112
custom-cache-suffix: $(date -u "+%Y-%m")
113113

114+
- name: Grab PHPUnit version
115+
id: phpunit_version
116+
run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> $GITHUB_OUTPUT
117+
118+
- name: Determine PHPUnit config file to use
119+
id: phpunit_config
120+
run: |
121+
if [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) || startsWith( steps.phpunit_version.outputs.VERSION, '11.' ) }}" == "true" ]; then
122+
echo 'FILE=phpunit.xml.dist' >> $GITHUB_OUTPUT
123+
else
124+
echo 'FILE=phpunitlte9.xml.dist' >> $GITHUB_OUTPUT
125+
fi
126+
114127
- name: Run the unit tests
115-
run: composer test
128+
run: vendor/bin/phpunit --no-coverage -c ${{ steps.phpunit_config.outputs.FILE }}
116129

117130
#### CODE COVERAGE STAGE ####
118131
# N.B.: Coverage is only checked on the lowest and highest stable PHP versions
@@ -178,21 +191,30 @@ jobs:
178191
- name: "DEBUG: Show grabbed version"
179192
run: echo ${{ steps.phpunit_version.outputs.VERSION }}
180193

194+
- name: Determine PHPUnit config file to use
195+
id: phpunit_config
196+
run: |
197+
if [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '9.' ) && steps.phpunit_version.outputs.VERSION >= '9.3' }}" == "true" ]; then
198+
echo 'FILE=phpunitlte9.xml.dist' >> $GITHUB_OUTPUT
199+
echo 'EXTRA_ARGS=--coverage-cache ./build/phpunit-cache' >> $GITHUB_OUTPUT
200+
elif [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) || startsWith( steps.phpunit_version.outputs.VERSION, '11.' ) }}" == "true" ]; then
201+
echo 'FILE=phpunit.xml.dist' >> $GITHUB_OUTPUT
202+
echo 'EXTRA_ARGS=' >> $GITHUB_OUTPUT
203+
else
204+
echo 'FILE=phpunitlte9.xml.dist' >> $GITHUB_OUTPUT
205+
echo 'EXTRA_ARGS=' >> $GITHUB_OUTPUT
206+
fi
207+
181208
# PHPUnit 9.3 started using PHP-Parser for code coverage which can cause interference.
182209
# As of PHPUnit 9.3.4, a cache warming option is available.
183210
# Using that option prevents issues with PHP-Parser backfilling PHP tokens when PHPCS does not (yet),
184211
# which would otherwise cause tests to fail on tokens being available when they shouldn't be.
185212
- name: "Warm the PHPUnit cache (PHPUnit 9.3+)"
186213
if: ${{ steps.phpunit_version.outputs.VERSION >= '9.3' }}
187-
run: vendor/bin/phpunit --coverage-cache ./build/phpunit-cache --warm-coverage-cache
188-
189-
- name: "Run the unit tests with code coverage (PHPUnit < 9.3)"
190-
if: ${{ steps.phpunit_version.outputs.VERSION < '9.3' }}
191-
run: composer coverage
214+
run: vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }} ${{ steps.phpunit_config.outputs.EXTRA_ARGS }} --warm-coverage-cache
192215

193-
- name: "Run the unit tests with code coverage (PHPUnit 9.3+)"
194-
if: ${{ steps.phpunit_version.outputs.VERSION >= '9.3' }}
195-
run: composer coverage -- --coverage-cache ./build/phpunit-cache
216+
- name: "Run the unit tests with code coverage"
217+
run: vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }} ${{ steps.phpunit_config.outputs.EXTRA_ARGS }}
196218

197219
- name: Upload coverage results to Coveralls
198220
if: ${{ success() }}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/vendor/
33
/build/
44
phpunit.xml
5+
phpunitlte9.xml
56
.phpunit.result.cache
67
.phpcs.xml
78
phpcs.xml

Tests/VariableAnalysisSniff/UnusedFollowedByRequireTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use VariableAnalysis\Tests\BaseTestCase;
66

7-
class UnusedFollowedByRequire extends BaseTestCase
7+
class UnusedFollowedByRequireTest extends BaseTestCase
88
{
99
public function testUnusedFollowedByRequireWarnsByDefault()
1010
{

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
"scripts": {
4242
"test": "./vendor/bin/phpunit --no-coverage",
4343
"coverage": "./vendor/bin/phpunit",
44+
"test-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist --no-coverage",
45+
"coverage-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist",
4446
"lint": "./vendor/bin/phpcs",
4547
"fix": "./vendor/bin/phpcbf",
4648
"phpstan": "./vendor/bin/phpstan analyse",
@@ -52,7 +54,7 @@
5254
"squizlabs/php_codesniffer": "^3.5.6"
5355
},
5456
"require-dev": {
55-
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.5 || ^7.0 || ^8.0 || ^9.0",
57+
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.5 || ^7.0 || ^8.0 || ^9.0 || ^10.5.32 || ^11.3.3",
5658
"sirbrillig/phpcs-import-detection": "^1.1",
5759
"phpcsstandards/phpcsdevcs": "^1.1",
5860
"phpstan/phpstan": "^1.7",

phpunit.xml.dist

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.0/phpunit.xsd"
5-
bootstrap="Tests/bootstrap.php"
6-
convertErrorsToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertDeprecationsToExceptions="true"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/10.5/phpunit.xsd"
5+
bootstrap="Tests/bootstrap.php"
6+
cacheDirectory="build/.phpunit-cache"
7+
displayDetailsOnTestsThatTriggerErrors="true"
8+
displayDetailsOnTestsThatTriggerWarnings="true"
9+
displayDetailsOnTestsThatTriggerNotices="true"
10+
displayDetailsOnTestsThatTriggerDeprecations="true"
11+
displayDetailsOnIncompleteTests="true"
12+
displayDetailsOnSkippedTests="true"
13+
displayDetailsOnPhpunitDeprecations="false"
14+
failOnWarning="true"
15+
failOnNotice="true"
16+
failOnDeprecation="true"
17+
failOnPhpunitDeprecation="false"
1018
>
1119
<testsuites>
1220
<testsuite name="VariableAnalysis">
1321
<directory>Tests</directory>
1422
</testsuite>
1523
</testsuites>
1624

17-
<filter>
18-
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="false">
25+
<source>
26+
<include>
1927
<directory>./VariableAnalysis/</directory>
20-
</whitelist>
21-
</filter>
28+
</include>
29+
</source>
2230

23-
<logging>
24-
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/>
25-
<log type="coverage-clover" target="build/logs/clover.xml"/>
26-
</logging>
31+
<coverage includeUncoveredFiles="true" ignoreDeprecatedCodeUnits="true">
32+
<report>
33+
<clover outputFile="build/logs/clover.xml"/>
34+
<text outputFile="php://stdout" showOnlySummary="true"/>
35+
</report>
36+
</coverage>
2737
</phpunit>

phpunitlte9.xml.dist

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.0/phpunit.xsd"
5+
bootstrap="Tests/bootstrap.php"
6+
convertErrorsToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertDeprecationsToExceptions="true"
10+
>
11+
<testsuites>
12+
<testsuite name="VariableAnalysis">
13+
<directory>Tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
17+
<filter>
18+
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="false">
19+
<directory>./VariableAnalysis/</directory>
20+
</whitelist>
21+
</filter>
22+
23+
<logging>
24+
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/>
25+
<log type="coverage-clover" target="build/logs/clover.xml"/>
26+
</logging>
27+
</phpunit>

0 commit comments

Comments
 (0)