Skip to content

Conversation

@costdev
Copy link
Member

@costdev costdev commented Jun 10, 2025

This adds an initial setup for PHPUnit tests.

Changes

  • WordPress Core's test suite installer script.
    • ./bin/install-wp-tests.sh
  • A PHPUnit bootstrap file.
    • ./tests/phpunit/bootstrap.php
  • A sample tests configuration file.
    • ./wp-tests-config-sample.php
  • Single-site and Multisite configuration files.
    • ./phpunit.xml.dist and ./tests/phpunit/multisite.xml respectively
  • A sample test file with some test standards/conventions I'm suggesting.
    • ./tests/phpunit/tests/SampleTest.php
  • Composer scripts for running tests, single-site and multisite coverage reports, and merging said reports into one.
  • NPM scripts (using wp-env) for running tests, single-site and multisite coverage reports, and merging said reports into one.

Additional changes to the plugin

  • Bumps the minimum PHP version to 7.4.
    • The existing wp-env configuriation runs PHP 7.4, and there are various features (arrow functions, etc.) that are already in the codebase. PHP 7.2.24 is too early for some of these.
  • Bumps the minimum WordPress version to 5.4.
    • WordPress 5.4 is the version after initial PHP 7.4 support was added.
    • Note: This is likely to be bumped even higher.

Instructions

Setting up the dev environment

Option 1: wp-env

  1. Ensure Docker is installed and running.
  2. Run npm i
  3. Run npm run env start
  4. Run npm run test:php:install-deps
  5. Copy wp-tests-config-sample.php to wp-tests-config.php.

Option 2: Local web server and MySQL database:

  • Run composer install
  • Create a new database using mysql -u<username> -p -> CREATE DATABASE <database_name>; -> exit;
    • Note: Don't use an existing database - the database is reset before every test run by default.
  • Copy wp-tests-config-sample.php to wp-tests-config.php.
  • Ensure that DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST in wp-tests-config.php match your setup.
  • Run ./bin/install-wp-tests.sh with the appropriate credentials.

Running PHPUnit tests

Single Site

npm run test:php

If you're not using wp-env, you can just run composer run test.

Multisite

npm run test:php:multisite

If you're not using wp-env, you can just run composer run test:multisite.

Running PHPUnit tests with line coverage

First, start the environment with xDebug:

npm run env start -- --xdebug=coverage

If you're not using wp-env, make sure the xDebug PHP module is installed and enabled.

Single Site

npm run coverage:php:single

If you're not using wp-env, you can just run composer run coverage:single.

A coverage report will be available at /tests/phpunit/coverage/html/single-site.

Multisite

npm run coverage:php:multisite

If you're not using wp-env, you can just run composer run coverage:multisite.

A coverage report will be available at /tests/phpunit/coverage/html/multisite.

Single Site and Multisite

npm run coverage:php:full

If you're not using wp-env, you can just run composer run coverage:full.

A merged coverage report will be available at /tests/phpunit/coverage/html/full.

This PR only adds a sample test for guidance, which will be ignored when running the test suite. You'll know the above works if PHPUnit runs and "No tests executed!" is displayed.


Fixes #59

costdev added 3 commits June 10, 2025 03:58
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
The wp-env config runs PHP 7.4, and there are various features (arrow functions, etc.) that are already in the codebase. PHP 7.2.24 is too early for some of these.

Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
WordPress 5.4 is the version after initial PHP 7.4 support was added.

Note: This is likely to be bumped even higher.

Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
@joehoyle
Copy link

Does the release action need to exclude tests or is that handled somehow?

@costdev
Copy link
Member Author

costdev commented Jun 10, 2025

I think we're okay - The tests workflow is set to run on push/pull_request.branches.main and workflow_dispatch, whereas the release action runs on push.tags.**.

Was there something you saw in the release action that raised the concern?

@joehoyle
Copy link

@costdev ah, I was specifically talking about the files like tests/phpunit/tests/SampleTest.php getting included in the release zip, rather than the test deps etc.

@costdev
Copy link
Member Author

costdev commented Jun 10, 2025

@joehoyle You know, I had a feeling I had misinterpreted 😂, thanks for clarifying!

Yeah I'll need to update the .distignore file to exclude the tests directory, phpunit.xml.dist, and composer.json. I'll push that change in a bit.

Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
@afragen afragen mentioned this pull request Jun 11, 2025
@afragen afragen self-requested a review June 11, 2025 19:13
costdev added 11 commits June 12, 2025 17:33
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
@costdev
Copy link
Member Author

costdev commented Jun 14, 2025

This PR includes support for three environment types:

  • ✅ CI
    • Confirmed as working by GitHub Actions.
  • ✅ wp-env
  • ✅ Local web server and MySQL database

@norcross
Copy link
Member

confirmed this worked setting up on a local VVV instance

@afragen
Copy link
Contributor

afragen commented Jun 16, 2025

➜  AJF-M1-MBA arm64: ~/Local_Sites/colin-testing/app/public/wp-content/plugins/fair-plugin git:(add_phpunit_test_infrastructure) composer run test
> Composer\Config::disableProcessTimeout
> @php ./vendor/phpunit/phpunit/phpunit
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 9.6.23 by Sebastian Bergmann and contributors.

Warning:       XDEBUG_MODE=coverage (environment variable) or xdebug.mode=coverage (PHP configuration setting) has to be set

No tests executed!

@costdev
Copy link
Member Author

costdev commented Jun 16, 2025

Just need someone to do a wp-env test to confirm.

@afragen
Copy link
Contributor

afragen commented Jun 16, 2025

I have tested in wp-env and it seems to be working.

costdev added 2 commits June 16, 2025 19:49
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
Signed-off-by: costdev <79332690+costdev@users.noreply.github.com>
@costdev costdev merged commit c8b8af7 into main Jun 16, 2025
45 checks passed
@costdev costdev deleted the add_phpunit_test_infrastructure branch June 16, 2025 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create automated testing infrastructure

5 participants