This reusable package bootstraps our PHPUnit unit and integration tests. It includes:
- bootstrapping for both Unit and Integration tests
phpunit.xml.distfor each test suiteTestCasefor each test suite
This means your repo only needs its tests. w00t!
In your repo's composer.json file, add the following "require-dev" dependency:
"wp-media/phpunit": "^2.0"Sometimes you need a custom bootstrapping solution in your repo, such as loading a factory, handling licensing, etc. Here are the steps to get you rolling:
- Add a
bootstrap.phpfile inUnitorIntegrationdirectory. - In your
Tests/Integration/bootstrap.phpfile, add the following code to it:
tests_add_filter(
'muplugins_loaded',
function() {
// Do your bootstrapping work here.
}
);When you need to customize the test case, extend off of the base test cases in this package:
- For a custom integration, extend off of
WPMedia\PHPUnit\Integration\TestCase. - For a custom unit, extend off of
WPMedia\PHPUnit\Unit\TestCase.
Composer symlinks this package's runner to vendor/bin/wpmedia-phpunit. Point your repo's own
Composer scripts at it, passing the suite to run:
"scripts": {
"test-unit": "wpmedia-phpunit unit",
"test-integration": "wpmedia-phpunit integration"
}The runner (WPMedia\PHPUnit\BootstrapManager) resolves where your tests and configuration live,
then hands off to PHPUnit. It accepts a few optional arguments:
WPMEDIA_PHPUNIT_ROOT_DIR=<path>— the root of the repo under test. Defaults to four levels up from the package'ssrc/(i.e. your project root when this package is installed undervendor/). Pass.to target this package itself.path=<dir>— the test directory. Defaults toTests/UnitorTests/Integrationdepending on the suite.--group <name>— the standard PHPUnit group selector. Two groups are special during integration bootstrapping:AdminOnlydefinesWP_ADMIN(sois_admin()istrue) andMultisitedefinesMULTISITE.
Any other arguments (e.g. --filter) are forwarded to PHPUnit unchanged. If your repo ships its
own phpunit.xml.dist in the test directory, it is used; otherwise the bundled default applies.
The default, supported way to run this package's own unit and integration tests locally is wp-env. It spins up a disposable, Dockerized WordPress + MySQL environment with Composer, PHPUnit, and WP-CLI preinstalled, and exposes the WordPress PHPUnit test suite (WP_TESTS_DIR) automatically — no manual database or test-suite install required.
npm install # installs @wordpress/env
npm run env:start # boots WordPress + MySQL (first run downloads images)
npm run env:install # installs Composer deps inside the containernpm run test:php # unit + integration + admin integration
npm run test:php:unit # unit only
npm run test:php:integration # integration only
npm run test:php:integration-admin # AdminOnly integration groupStop the environment with npm run env:stop (or npm run env:destroy to remove it entirely).
The environment defaults to the latest WordPress on PHP 8.3 (see .wp-env.json). To try another PHP version, override it per-run, e.g. WP_ENV_PHP_VERSION=8.1 npm run env:start, or add a .wp-env.override.json.
Unit tests are fully mocked and need no WordPress, so they can also be run directly on the host with
composer test-unit. Integration tests require a WordPress install and are best run throughwp-env.