Automated tests for PHP version compatibility
When preparing for a PHP version update, creating automated tests can be useful for identifying incompatibilities with the updated version of PHP.
There are a wide variety of types of automated tests, but in general this refers to:
- Unit tests: Mocks any WordPress-defined functions or classes, and does not need an instance of a database to be available to run the tests.
- Integration tests: Loads WordPress and uses an actual test database.
Considerations
- The results depend on test suite completeness. Fewer tests will lack the necessary coverage to ensure compatibility.
- Use strict assertions. For instance, using
assertEquals()
does a loose comparison, whereasassertSame()
also checks the type as well. - Use strict code coverage. Add
beStrictAboutCoversAnnotations=”true”
andforceCoversAnnotations=”true”
to the PHPUnit config file, and then use@covers
and@uses
annotations to indicate the code that is intended to be covered with the tests. - Test happy paths (behavior based on expected inputs) and unhappy paths (behavior based on unexpected inputs) to ensure that functions fail in an expected way, as this is where most strictness-related issues occur.
- Consider writing tests before attempting a fix, to be more confident that the compatibility change has been successful.
Last updated: February 29, 2024