Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions .github/workflows/reusable-phpunit-tests-v3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ jobs:
with:
custom-cache-suffix: $(/bin/date -u --date='last Mon' "+%F")

# Release branches run this workflow from trunk, but older branches lack this file.
- name: Select PHPUnit configuration
if: ${{ hashFiles('tests/phpunit/phpunit-config.php') != '' }}
run: echo "PHPUNIT_CONFIG=$(php ./tests/phpunit/phpunit-config.php "${PHPUNIT_CONFIG}")" >> "$GITHUB_ENV"

- name: Install npm dependencies
run: npm ci

Expand Down Expand Up @@ -293,20 +298,6 @@ jobs:
- name: Install WordPress
run: npm run env:install

- name: Select PHPUnit configuration
run: |
PHPUNIT_VERSION=$(node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit --version | grep -oE '\b[0-9]+\.[0-9]+' | head -n 1)
echo "Detected PHPUnit version: ${PHPUNIT_VERSION}"
MAJOR_VERSION=$(echo "${PHPUNIT_VERSION}" | cut -d. -f1)
if [ "${MAJOR_VERSION}" -ge 10 ]; then
if [ "${PHPUNIT_CONFIG}" = "tests/phpunit/multisite.xml" ]; then
NEW_CONFIG="tests/phpunit/multisite10.xml"
else
NEW_CONFIG="phpunit10.xml.dist"
fi
echo "Using PHPUnit 10+ configuration: ${NEW_CONFIG}"
echo "PHPUNIT_CONFIG=${NEW_CONFIG}" >> "$GITHUB_ENV"
fi
- name: Run external HTTP tests
if: ${{ ! inputs.multisite && ! inputs.phpunit-test-groups && ! inputs.coverage-report }}
continue-on-error: ${{ inputs.allow-errors }}
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ wp-tests-config.php
.phpunit.result.cache
/phpunit.xml
/phpunit10.xml
/phpunit-10.xml
/.phpcs.xml
/phpcs.xml
.cache/*
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source",
"lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source",
"lint:errors": "@lint -n",
"test": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit" ],
"test10": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit -c phpunit10.xml.dist" ]
"test": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit -c \"$(php ./tests/phpunit/phpunit-config.php)\"" ]
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@
"env:logs": "node ./tools/local-env/scripts/docker.js logs",
"env:pull": "node ./tools/local-env/scripts/docker.js pull",
"test:performance": "wp-scripts test-playwright --config tests/performance/playwright.config.js",
"test:php": "node ./tools/local-env/scripts/docker.js run --rm php ./vendor/bin/phpunit",
"test:php10": "node ./tools/local-env/scripts/docker.js run --rm php ./vendor/bin/phpunit -c phpunit10.xml.dist",
"test:php": "node ./tools/local-env/scripts/docker.js run --rm php composer test --",
"test:coverage": "npm run test:php -- --coverage-html ./coverage/html/ --coverage-php ./coverage/php/report.php --coverage-text=./coverage/text/report.txt",
"test:e2e": "wp-scripts test-playwright --config tests/e2e/playwright.config.js",
"test:visual": "wp-scripts test-playwright --config tests/visual-regression/playwright.config.js",
Expand Down
16 changes: 8 additions & 8 deletions tests/phpunit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ For more information, please review the relevant Core Handbook pages:

## Configuration Files

WordPress core maintains separate configuration files to support both PHPUnit 9 and PHPUnit 10.1+:
WordPress core has separate configuration files for PHPUnit 9 and PHPUnit 10:

- `phpunit.xml.dist` and `tests/phpunit/multisite.xml`: Used for PHPUnit 9 and earlier.
- `phpunit10.xml.dist` and `tests/phpunit/multisite10.xml`: Used for PHPUnit 10.1 and later.
- `phpunit10.xml.dist` and `tests/phpunit/multisite10.xml`: Used for PHPUnit 10 and later. These use the `<source>` element, so PHPUnit 10.0 is not supported.

### Running Tests Locally

When running tests via Composer:
- `composer test`: Runs tests using the default configuration (PHPUnit &lt; 10).
- `composer test10`: Runs tests using `phpunit10.xml.dist` (PHPUnit 10+).
`composer test` and `npm run test:php` use the single site configuration file that matches the installed version of PHPUnit. A local `phpunit.xml` file is used instead if one exists, so base it on the file that matches your version of PHPUnit.

When running tests via npm and Docker:
- `npm run test:php`: Runs tests using the default configuration.
- `npm run test:php10`: Runs tests using `phpunit10.xml.dist`.
`tests/phpunit/phpunit-config.php` prints the matching configuration file. Pass it either configuration file name to get the one for the installed version, for example to run the multisite tests:

```
./vendor/bin/phpunit -c "$(php tests/phpunit/phpunit-config.php tests/phpunit/multisite.xml)"
```

## Run the full matrix on a pull request

Expand Down
51 changes: 51 additions & 0 deletions tests/phpunit/phpunit-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Prints the PHPUnit configuration file that matches the installed version of PHPUnit.
*
* PHPUnit 10 changed the configuration file format, so PHPUnit 9 and earlier use
* phpunit.xml.dist and tests/phpunit/multisite.xml, while PHPUnit 10 and later use
* phpunit10.xml.dist and tests/phpunit/multisite10.xml.
*
* Pass one of those files to get its counterpart for the installed version. Any other file
* is printed unchanged. With no argument, a local phpunit.xml is printed if one exists,
* otherwise the single site configuration file for the installed version.
*
* The printed path is relative to the root of the repository.
*
* Usage: ./vendor/bin/phpunit -c "$(php tests/phpunit/phpunit-config.php [file])"
*/

$root = dirname( __DIR__, 2 );

$installed = require $root . '/vendor/composer/installed.php';
$is_phpunit_10 = version_compare( $installed['versions']['phpunit/phpunit']['version'], '10', '>=' );

$swaps = array(
'phpunit.xml.dist' => 'phpunit10.xml.dist',
'tests/phpunit/multisite.xml' => 'tests/phpunit/multisite10.xml',
);

if ( ! $is_phpunit_10 ) {
$swaps = array_flip( $swaps );
}

if ( ! isset( $argv[1] ) ) {
if ( file_exists( $root . '/phpunit.xml' ) ) {
echo 'phpunit.xml';
} else {
echo $is_phpunit_10 ? 'phpunit10.xml.dist' : 'phpunit.xml.dist';
}
exit;
}

$config = $argv[1];
$real = realpath( $config );

foreach ( $swaps as $from => $to ) {
if ( false !== $real && realpath( $root . '/' . $from ) === $real ) {
$config = $to;
break;
}
}

echo $config;