Yoast / yoast-acf-analysis

WordPress plugin that adds the content of all ACF fields to the Yoast SEO score analysis.
https://wordpress.org/plugins/acf-content-analysis-for-yoast-seo/
GNU General Public License v3.0
61 stars 20 forks source link

Tests: make the testsuite compatible with PHPUnit 9 and test against PHP 8 #278

Closed jrfnl closed 3 years ago

jrfnl commented 3 years ago

Summary

This PR can be summarized in the following changelog entry:

Relevant technical choices:

Composer: update the dependencies of the test dependencies

Update the composer.lock file with the latest dependencies related to the test suite.

composer update --dev brain/monkey phpunit/phpunit --with-dependencies

Note: the update is still based on platform: php 5.6, but some of the more recent sub-dependencies have widened their PHP version constraints, which makes getting the tests running on PHP 8 easier.

Tests: use annotations instead of fixtures

In PHPUnit 8.x, the setUpBeforeClass(), tearDownAfterClass(), setUp() and tearDown() methods have received type declarations,with a return type void, making it neigh impossible to implement this in a cross-version manner as the void return type is not available until PHP 7.1.

However, PHPUnit also supports @beforeClass, @afterClass, @before and @after annotations to run arbitrary methods for setting things up/tearing things down. Those annotations have been around forever and are supported all the way back to PHPUnit 4.x.

So instead of doing convoluted things with extending different classes for different PHPUnit versions or creating classes on the fly for this, by renaming the fixture related methods and using the PHPUnit annotations, the unit test suite becomes compatible with PHPUnit 8.x without much effort.

Tests: implement work-around for remaining PHPUnit cross-version issue

PHPUnit 9 removed the assertInternalType() method in favour of more specific methods, which were introduced in PHPUnit 8.

As this test suite only uses this assertion in one test file, a simple helper method with an if/else does the trick.

Composer: allow installation of PHPUnit 8 and 9

Now the tests have been made cross-version compatible with PHPUnit 8 and 9, allow installation of those versions by widening the version restraints in the composer.json file.

PHPUnit 8 introduces a form of caching to PHPUnit, so adding this cache file to the .gitignore file.

Travis: run the tests against PHP 8/nightly

Now the tests are fully compatible with PHPUnit 9, the test suite can be run on PHP 8.

As not all dependencies of PHPUnit have tagged a new version which allows for PHP 8 yet, we do need to ignore-platform-reqs for the time being.

Includes adding travis_retry to composer install-like commands to get round builds stalling on the connection with Packagist, especially on older PHP versions.

Test instructions

This PR can be tested by following these steps:

To test locally, it will be simplest to:

  1. Download phars of PHPUnit 7/8/9 and save them somewhere in the system path or in the root directory of this project (an exclude them from Git).
  2. On a recent PHP 7.4.x version, run the tests using each of the downloaded phars:
    phpunit7.phar
    phpunit8.phar
    phpunit9.phar

    The tests on each version should run without any warnings and pass. This confirms the PHPUnit cross-version compatibility.

  3. On a recent PHP 8 pre-release, run the tests again, though now only using the phpunit9.phar. The tests should pass, which confirms PHP 8 compatibility for the code covered by tests.
jrfnl commented 3 years ago

I'm closing this PR for now as it needs to be updated to use the PHPUnit Polyfills and WP Test Tests. Will re-open once updated.