Skip to content

Commit

Permalink
PHPUnit: add separate configuration for PHPUnit 10
Browse files Browse the repository at this point in the history
PHPUnit 10 makes significant changes to the configuration file.
While there is a `--migrate-configuration` option available in PHPUnit, that doesn't get us a well enough converted configuration file, so instead use a separate `phpunit10.xml.dist` file for running the tests on PHPUnit 10 with an optimal configuration.

Includes
* Adding separate scripts to the `composer.json` file to make this more obvious for contributors.
* Updating the GH Actions workflows to take the new configuration file into account when appropriate.
* Selectively not using the `--repeat` option, which was [removed without replacement](sebastianbergmann/phpunit#5174) in PHPUnit 10.
  • Loading branch information
jrfnl committed Jun 12, 2023
1 parent 6653cdd commit 1914d5f
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 13 deletions.
22 changes: 20 additions & 2 deletions .github/workflows/quicktest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,32 @@ jobs:
if: matrix.phpcs_version == 'dev-master'
run: composer lint-lt72

- name: Grab PHPUnit version
id: phpunit_version
# yamllint disable-line rule:line-length
run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> $GITHUB_OUTPUT

- name: Determine PHPUnit config file to use
id: phpunit_config
run: |
if [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}" == "true" ]; then
echo 'FILE=phpunit10.xml.dist' >> $GITHUB_OUTPUT
echo 'EXTRA_ARGS=' >> $GITHUB_OUTPUT
else
echo 'FILE=phpunit.xml.dist' >> $GITHUB_OUTPUT
echo 'EXTRA_ARGS= --repeat 2' >> $GITHUB_OUTPUT
fi
- name: Run the unit tests without caching
run: vendor/bin/phpunit --no-coverage
run: vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }} --no-coverage
env:
PHPCS_VERSION: ${{ matrix.phpcs_version }}
PHPCSUTILS_USE_CACHE: false

- name: Run the unit tests with caching
run: vendor/bin/phpunit --testsuite PHPCSUtils --no-coverage --repeat 2
run: >
vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }}
--testsuite PHPCSUtils --no-coverage ${{ steps.phpunit_config.outputs.EXTRA_ARGS }}
env:
PHPCS_VERSION: ${{ matrix.phpcs_version }}
PHPCSUTILS_USE_CACHE: true
41 changes: 30 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,34 @@ jobs:
if: ${{ matrix.phpcs_version == 'lowest' }}
run: composer update squizlabs/php_codesniffer --prefer-lowest --no-scripts --no-interaction

- name: Grab PHPUnit version
id: phpunit_version
# yamllint disable-line rule:line-length
run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> $GITHUB_OUTPUT

- name: Determine PHPUnit config file to use
id: phpunit_config
run: |
if [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}" == "true" ]; then
echo 'FILE=phpunit10.xml.dist' >> $GITHUB_OUTPUT
echo 'EXTRA_ARGS=' >> $GITHUB_OUTPUT
else
echo 'FILE=phpunit.xml.dist' >> $GITHUB_OUTPUT
echo 'EXTRA_ARGS= --repeat 2' >> $GITHUB_OUTPUT
fi
- name: Run the unit tests without caching (non-risky)
if: ${{ matrix.risky == false }}
run: vendor/bin/phpunit --no-coverage
run: vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }} --no-coverage
env:
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.0.x-dev' && '4.0.0' || matrix.phpcs_version }}
PHPCSUTILS_USE_CACHE: false

- name: Run the unit tests with caching (non-risky)
if: ${{ matrix.risky == false }}
run: vendor/bin/phpunit --testsuite PHPCSUtils --no-coverage --repeat 2
run: >
vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }}
--testsuite PHPCSUtils --no-coverage ${{ steps.phpunit_config.outputs.EXTRA_ARGS }}
env:
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.0.x-dev' && '4.0.0' || matrix.phpcs_version }}
PHPCSUTILS_USE_CACHE: true
Expand All @@ -220,15 +238,15 @@ jobs:
- name: Run the unit tests (risky, comparewithPHPCS)
if: ${{ matrix.risky && matrix.phpcs_version == 'dev-master' }}
# "nothing" is excluded to force PHPUnit to ignore the <exclude> settings in phpunit.xml.dist.
run: vendor/bin/phpunit --no-coverage --group compareWithPHPCS --exclude-group nothing
run: vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }} --no-coverage --group compareWithPHPCS --exclude-group nothing
env:
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.0.x-dev' && '4.0.0' || matrix.phpcs_version }}

# Run the "xtra" group against high and low PHPCS as these are tests safeguarding PHPCS itself.
- name: Run the unit tests (risky, xtra)
if: ${{ matrix.risky }}
# "nothing" is excluded to force PHPUnit to ignore the <exclude> settings in phpunit.xml.dist.
run: vendor/bin/phpunit --no-coverage --group xtra --exclude-group nothing
run: vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }} --no-coverage --group xtra --exclude-group nothing
env:
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.0.x-dev' && '4.0.0' || matrix.phpcs_version }}

Expand Down Expand Up @@ -312,20 +330,21 @@ jobs:
# As of PHPUnit 9.3.4, a cache warming option is available.
# Using that option prevents issues with PHP-Parser backfilling PHP tokens when PHPCS does not (yet),
# which would otherwise cause tests to fail on tokens being available when they shouldn't be.
# As coverage is only run on high/low PHP, the high PHP version will use PHPUnit 10, so just check for that.
- name: "Warm the PHPUnit cache (PHPUnit 9.3+)"
if: ${{ steps.phpunit_version.outputs.VERSION >= '9.3' }}
run: vendor/bin/phpunit --coverage-cache ./build/phpunit-cache --warm-coverage-cache
if: ${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
run: vendor/bin/phpunit -c phpunit10.xml.dist --coverage-cache ./build/phpunit-cache --warm-coverage-cache

- name: "Run the unit tests without caching with code coverage (PHPUnit < 9.3)"
if: ${{ steps.phpunit_version.outputs.VERSION < '9.3' }}
- name: "Run the unit tests without caching with code coverage (PHPUnit < 10)"
if: ${{ ! startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
run: vendor/bin/phpunit
env:
PHPCS_VERSION: ${{ matrix.phpcs_version }}
PHPCSUTILS_USE_CACHE: false

- name: "Run the unit tests without caching with code coverage (PHPUnit 9.3+)"
if: ${{ steps.phpunit_version.outputs.VERSION >= '9.3' }}
run: vendor/bin/phpunit --coverage-cache ./build/phpunit-cache
- name: "Run the unit tests without caching with code coverage (PHPUnit 10+)"
if: ${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
run: vendor/bin/phpunit -c phpunit10.xml.dist --coverage-cache ./build/phpunit-cache
env:
PHPCS_VERSION: ${{ matrix.phpcs_version }}
PHPCSUTILS_USE_CACHE: false
Expand Down
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,20 @@
"test": [
"@php ./vendor/phpunit/phpunit/phpunit --no-coverage"
],
"test10": [
"@php ./vendor/phpunit/phpunit/phpunit -c phpunit10.xml.dist --no-coverage"
],
"coverage": [
"@php ./vendor/phpunit/phpunit/phpunit"
],
"coverage10": [
"@php ./vendor/phpunit/phpunit/phpunit -c phpunit10.xml.dist"
],
"coverage-local": [
"@php ./vendor/phpunit/phpunit/phpunit --coverage-html ./build/coverage-html"
],
"coverage-local10": [
"@php ./vendor/phpunit/phpunit/phpunit -c phpunit10.xml.dist --coverage-html ./build/coverage-html"
]
},
"config": {
Expand Down
75 changes: 75 additions & 0 deletions phpunit10.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/10.1/phpunit.xsd"
backupGlobals="true"
bootstrap="./Tests/bootstrap.php"
beStrictAboutTestsThatDoNotTestAnything="true"
colors="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
failOnWarning="true"
failOnNotice="true"
failOnDeprecation="true"
requireCoverageMetadata="true"
>
<testsuites>
<testsuite name="RunFirst">
<!--
Run caching tests separately as they will clear the caches.
-->
<file>./Tests/Internal/Cache/GetClearTest.php</file>
<file>./Tests/Internal/Cache/SetTest.php</file>
<file>./Tests/Internal/NoFileCache/GetClearTest.php</file>
<file>./Tests/Internal/NoFileCache/SetTest.php</file>

<!--
A number of tests need process isolation to allow for recording code coverage on
the setting of function local static variables.
However, using process isolation runs into trouble with PHPUnit 4.x/PHPCS 2.6.0.
Executing these specific tests in a separate testsuite, which is run
before the full test suite, will allow for the code coverage for these methods
to be recorded properly, while still allowing the tests to run on all supported
PHP/PHPUnit/PHPCS combinations.
-->
<file>./Tests/Utils/Namespaces/NamespaceTypeTest.php</file>
</testsuite>
<testsuite name="PHPCSUtils">
<directory suffix="Test.php">./Tests/</directory>

<exclude>Tests/Internal/Cache/GetClearTest.php</exclude>
<exclude>Tests/Internal/Cache/SetTest.php</exclude>
<exclude>Tests/Internal/NoFileCache/GetClearTest.php</exclude>
<exclude>Tests/Internal/NoFileCache/SetTest.php</exclude>

<exclude>Tests/Utils/Namespaces/NamespaceTypeTest.php</exclude>
</testsuite>
</testsuites>

<groups>
<exclude>
<group>compareWithPHPCS</group>
<!-- Extra tests are basically testing PHPCS itself, not PHPCSUtils. -->
<group>xtra</group>
</exclude>
</groups>

<source>
<include>
<!-- Not recording coverage for PHPCS23Utils as there is nothing directly testable. -->
<directory suffix=".php">./PHPCSUtils/</directory>
</include>
</source>

<coverage includeUncoveredFiles="true" ignoreDeprecatedCodeUnits="true">
<report>
<clover outputFile="build/logs/clover.xml"/>
<text outputFile="php://stdout" showOnlySummary="true"/>
</report>
</coverage>

</phpunit>

0 comments on commit 1914d5f

Please sign in to comment.