Skip to content

Add cross-version compatibility with WP 5.9 and WP < 5.9#20

Merged
jrfnl merged 2 commits intodevelopfrom
feature/compatibility-with-wp-5.9
Sep 24, 2021
Merged

Add cross-version compatibility with WP 5.9 and WP < 5.9#20
jrfnl merged 2 commits intodevelopfrom
feature/compatibility-with-wp-5.9

Conversation

@jrfnl
Copy link
Copy Markdown
Collaborator

@jrfnl jrfnl commented Sep 24, 2021

Integration tests: update the bootstrap for compatibility with WP 5.9

As of WP 5.9, the PHPUnit Polyfills are also used by WordPress itself and WordPress will try to autoload the Autoloader for the Polyfills.

This will work fine if WordPress is fully installed, but for a partial install, like provided via the WP-CLI scaffold command, this can be problematic.
Similarly, if the PHPUnit Polyfills are available, but in a different location than expected by WP Core, again, this can be problematic.

As the PHPUnit Polyfills is also a requirement of WP Test Utils, we can get prevent any issues with this by loading the autoloader ahead of running the WordPress test bootstrap file.

Integration tests TestCase: double the class for full compat with all supported WP versions

With the introduction of the PHPUnit Polyfills in WP 5.9 and the backport of the availability of the PHPUnit Polyfills + the snake case test wrappers to WP 5.2 - 5.8, integration tests for plugins/themes which test against WP 5.2 - 5.9 (and higher) can start using modern PHPUnit assertions and expectations, as well as switch over to using the snake_case fixture method names.

There is a caveat to this however:

  • Plugins/themes which test against older WP versions (< 5.2) don't have access to either the polyfills or the snake_case fixture method wrappers.
  • Same goes for tests being run against the current WP latest, which is the last 5.8.x release (5.8.1), which doesn't yet include the changes.
  • Same goes for tests being run against all WP 5.2 - 5.8 minors which were released before today.

This commit:

  • Introduces the snake_case fixture method wrappers to the WP integration test TestCase.
  • Introduces a second TestCase for the WP integration tests.
    The original TestCase is suitable for use for older WP versions which don't include the test changes from WP 5.9.
    The new TestCase is suitable for use with WP 5.9 and WP versions which do include the test changes from WP 5.9.
    The new TestCase does not include the Polyfills as those will be inherited from WordPress.
  • Both these test cases have exactly the same name Yoast\WPTestUtils\WPIntegration\TestCase so concrete test classes can extend this class safely.
    Because of this, this class will no longer be loaded via the Composer classmap though.
    Instead, the custom Autoloader, which was already in place for the WP Integration tests, has been adjusted to handle loading the correct version of the class depending on whether or not the WP version against which the tests are being run contains the (backported) changes from WP 5.9 or not.

Includes minor tweak to the PHPCS config to allow for the duplicate class.

Fixes #14

As of WP 5.9, the PHPUnit Polyfills are also used by WordPress itself and WordPress will try to autoload the Autoloader for the Polyfills.

This will work fine if WordPress is fully installed, but for a partial install, like provided via the WP-CLI `scaffold` command, this can be problematic.
Similarly, if the PHPUnit Polyfills are available, but in a different location than expected by WP Core, again, this can be problematic.

As the PHPUnit Polyfills is also a requirement of WP Test Utils, we can get prevent any issues with this by loading the autoloader ahead of running the WordPress test bootstrap file.
… supported WP versions

With the introduction of the PHPUnit Polyfills in WP 5.9 and the backport of the availability of the PHPUnit Polyfills + the snake case test wrappers to WP 5.2 - 5.8, integration tests for plugins/themes which test against WP 5.2 - 5.9 (and higher) can start using modern PHPUnit assertions and expectations, as well as switch over to using the `snake_case` fixture method names.

There is a caveat to this however:
* Plugins/themes which test against older WP versions (< 5.2) don't have access to either the polyfills or the snake_case fixture method wrappers.
* Same goes for tests being run against the current WP `latest`, which is the last 5.8.x release (5.8.1), which doesn't yet include the changes.
* Same goes for tests being run against all WP 5.2 - 5.8 minors which were released before today.

This commit:
* Introduces the snake_case fixture method wrappers to the WP integration test `TestCase`.
* Introduces a second `TestCase` for the WP integration tests.
    The original `TestCase` is suitable for use for older WP versions which don't include the test changes from WP 5.9.
    The new `TestCase` is suitable for use with WP 5.9 and WP versions which *do* include the test changes from WP 5.9.
    The new `TestCase` does not include the Polyfills as those will be inherited from WordPress.
* Both these test cases have exactly the same name `Yoast\WPTestUtils\WPIntegration\TestCase` so concrete test classes can extend this class safely.
    Because of this, this class will no longer be loaded via the Composer classmap though.
    Instead, the custom Autoloader, which was already in place for the WP Integration tests, has been adjusted to handle loading the correct version of the class depending on whether or not the WP version against which the tests are being run contains the (backported) changes from WP 5.9 or not.

Includes minor tweak to the PHPCS config to allow for the duplicate class.

Fixes 14
@jrfnl jrfnl added enhancement New feature or request yoastcs/qa labels Sep 24, 2021
@jrfnl jrfnl added this to the 1.0.0 milestone Sep 24, 2021
@jrfnl jrfnl force-pushed the feature/compatibility-with-wp-5.9 branch from cef7de8 to 18e7e97 Compare September 24, 2021 15:29
@jrfnl jrfnl changed the title Feature/compatibility with wp 5.9 Add cross-version compatibility with WP 5.9 and WP < 5.9 Sep 24, 2021
@jrfnl
Copy link
Copy Markdown
Collaborator Author

jrfnl commented Sep 24, 2021

I've done a test run based on this PR with the WP Importer plugin and all looks good. Merging.
https://github.com/WordPress/wordpress-importer/actions/runs/1270638031

@jrfnl jrfnl merged commit b35d6fb into develop Sep 24, 2021
@jrfnl jrfnl deleted the feature/compatibility-with-wp-5.9 branch September 24, 2021 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request yoastcs/qa

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add wrapper method for the PHPUnit fixture methods if not provided by WordPress

1 participant