Add WordPress unit testing with PHPUnit via wp-env - #1251
Open
utkarshcloudinary wants to merge 1 commit into
Open
Add WordPress unit testing with PHPUnit via wp-env#1251utkarshcloudinary wants to merge 1 commit into
utkarshcloudinary wants to merge 1 commit into
Conversation
Adds a PHPUnit harness that runs against the WordPress core test suite that wp-env already mounts into its containers. wp-env ships PHPUnit 10 globally, but the WP 7.0 core test suite calls PHPUnit\Util\Test::parseTestMethodAnnotations(), which PHPUnit 10 removed. The suite therefore pins PHPUnit 9.6 and the Yoast polyfills as plugin dev dependencies and runs vendor/bin/phpunit in the container. Covers the Utils helpers plus Cloudinary CDN URL generation and image tag attribute rebuilding. The URL path reads only the cloud name, cname and private_cdn credentials, so the tests need no Cloudinary account and the CI job needs no secrets. Run locally with npm run env:start, then npm run test:unit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Approach
Adds a PHPUnit harness that runs against the WordPress core test suite that wp-env already mounts into its containers, so local runs and CI use the same environment as the existing e2e suite.
wp-env ships PHPUnit 10 globally, but the WP 7.0 core test suite calls
PHPUnit\Util\Test::parseTestMethodAnnotations(), which PHPUnit 10 removed. That call runs fromexpectDeprecated()on everyset_up(), so every test would fatal on the global binary. The plugin therefore pins its own PHPUnit 9.6 plus the Yoast polyfills as dev dependencies and runsvendor/bin/phpunitinside the container.phpunit.xml.distandtests/phpunit/bootstrap.php, which reads theWP_TESTS_DIRthat wp-env exports and loads the plugin onmuplugins_loadedphpunit/phpunit ^9.6@stableandyoast/phpunit-polyfills ^1.1@stableinrequire-dev. The@stablesuffix is needed because the repo setsminimum-stability: dev, which otherwise resolves to dev branch buildsUtilshelpers, Cloudinary CDN URL generation, transformation strings, CNAME handling, image tag attribute rebuilding, and thecloudinary_bypass_seo_urlfilterunitCI job, with a~/.wp-envcache so runs do not re-clone roughly 1 GB of WordPress and core PHPUnit sourcestests/phpunit/excluded from PHPCS, matching the existing treatment of the PHPStan stubsThe tests need no Cloudinary account. The URL build path reads only
cloud_name,cnameandprivate_cdn, never the API key or secret, so a stub connection is enough and the CI job needs no secrets. That also means it works on forked PRs. Real account coverage stays in the Playwright suite.QA notes
Run the suite locally:
Expected:
OK (18 tests, 35 assertions).To confirm a failure stops the build, add a failing assertion to any test in
tests/phpunit/tests/and re-run. The command should exit non-zero, which fails theUnit (PHPUnit)CI job. This was verified during development: a deliberate failure gave exit code 1 andTests: 19, Failures: 1.Also worth checking that the
unitjob appears and passes on this PR.Follow-ups, not addressed here
unitneeds adding to that list, otherwise a red job will not block merges.composer lintruns out of memory after a localcomposer phpstanrun, because PHPCS scans the generated.phpstan-cache/directory. It is gitignored but not in the PHPCS exclusions. CI is unaffected since the jobs are separate.composer auditflags CVE-2026-45293 (high) againstwp-coding-standards/wpcs, fixed in 3.4.1.