From d74a1e1f760d59a88ebfecf6bf8f61567c0cee84 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 12 Sep 2023 14:30:13 +0200 Subject: [PATCH] Backport support for the `AssertObjectProperty` polyfill PHPUnit 10.1.0 introduced two new assertions - `assertObjectHasProperty()` and `assertObjectNotHasProperty()` - to replace the `assertObjectHasAttribute() and `assertObjectNotHasAttribute()` methods, which were deprecated in PHPUnit 9.6.0 and removed in PHPUnit 10.0.0. These two new assertions have since been backported to PHPUnit 9.x in PHPUnit 9.6.11 to allow for getting rid of the deprecation notice and making preparing for PHPUnit 10 more smooth. The PHPUnit Polyfills have followed suit and the 1.1.0 release contains polyfills for the backported `assertObjectHasProperty()` and `assertObjectNotHasProperty()` methods. For the WP Test Utils package, this caused an interesting conundrum: * WP 6.4 - which enforces the use of PHPUnit Polyfills 1.1.0+ - will have the new polyfills available. * WP 5.9 - 6.3 will have the new polyfills available _if the test dependencies are updated and PHPUnit Polyfills 1.1.0+ is used_. * WP 5.2 - 5.9 versions, which include the backports, will **not** have the new polyfills available. * WP 5.2 - 5.9 versions, which **don't** include the backports, will **not** have the new polyfills available. * WP < 5.2 will **not** have the new polyfills available. Previously the WP Test Utils would have two test cases available for integration tests: * One for use with WP 5.9+ and with WP versions < 5.9 which included the backports. * One for use with WP < 5.2 and with WP versions < 5.9 which do not include the backports. To solve the conundrum, this commit: * Updates the minimum version of the PHPUnit Polyfills to 1.1.0. * Add the new polyfill to the test case which is used for WP < 5..2 and for WP versions < 5.9 which do not include the backports. * Introduces a third test case for integration tests against WP 5.2 - 5.8 versions, which _do_ include the backports, but not the new polyfill. While this third polyfill might technically not be needed, I've seen too many issues with traits colliding on older PHP versions to be willing to take that risk. * Updates the autoloader to load the correct test case. This should ensure that the new polyfill is available in all cases. --- composer.json | 2 +- src/WPIntegration/Autoload.php | 12 ++++- src/WPIntegration/TestCase.php | 2 + src/WPIntegration/TestCaseNoPolyfills.php | 8 +--- .../TestCaseOnlyObjectPropertyPolyfill.php | 44 +++++++++++++++++++ 5 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 src/WPIntegration/TestCaseOnlyObjectPropertyPolyfill.php diff --git a/composer.json b/composer.json index 9c12e7d..b15ffa3 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "require": { "php": ">=5.6", "brain/monkey": "^2.6.1", - "yoast/phpunit-polyfills": "^1.0.5" + "yoast/phpunit-polyfills": "^1.1.0" }, "require-dev": { "yoast/yoastcs": "^2.3.1" diff --git a/src/WPIntegration/Autoload.php b/src/WPIntegration/Autoload.php index e61398c..ebb955a 100644 --- a/src/WPIntegration/Autoload.php +++ b/src/WPIntegration/Autoload.php @@ -3,6 +3,7 @@ namespace Yoast\WPTestUtils\WPIntegration; use PHPUnit\Runner\Version as PHPUnit_Version; +use WP_UnitTestCase; /** * Custom autoloader. @@ -105,13 +106,20 @@ private static function load_mockobject_class( $class_name ) { * @return bool */ private static function load_test_case() { - if ( \method_exists( 'WP_UnitTestCase', 'set_up' ) === false ) { + if ( \method_exists( WP_UnitTestCase::class, 'set_up' ) === false ) { // Older WP version from before the test changes. require_once __DIR__ . '/TestCase.php'; return true; } - // WP 5.9 or a WP 5.2 - 5.8 version which includes the Polyfills and the fixture method wrappers. + if ( \method_exists( WP_UnitTestCase::class, 'assertObjectHasProperty' ) === false ) { + // WP 5.2 - 5.8 version which includes the Polyfills and the fixture method wrappers, + // but doesn't include the latest polyfill. + require_once __DIR__ . '/TestCaseOnlyObjectPropertyPolyfill.php'; + return true; + } + + // WP 5.9 or higher which automatically includes all Polyfills and the fixture method wrappers. require_once __DIR__ . '/TestCaseNoPolyfills.php'; return true; } diff --git a/src/WPIntegration/TestCase.php b/src/WPIntegration/TestCase.php index 4a14e0d..2da1174 100644 --- a/src/WPIntegration/TestCase.php +++ b/src/WPIntegration/TestCase.php @@ -10,6 +10,7 @@ use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames; use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType; use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals; +use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectProperty; use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains; use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches; @@ -50,6 +51,7 @@ abstract class TestCase extends WP_UnitTestCase { use AssertionRenames; use AssertIsType; use AssertObjectEquals; + use AssertObjectProperty; use AssertStringContains; use EqualToSpecializations; use ExpectExceptionMessageMatches; diff --git a/src/WPIntegration/TestCaseNoPolyfills.php b/src/WPIntegration/TestCaseNoPolyfills.php index 03e9367..5a2499f 100644 --- a/src/WPIntegration/TestCaseNoPolyfills.php +++ b/src/WPIntegration/TestCaseNoPolyfills.php @@ -15,14 +15,8 @@ * polyfills to allow for using PHPUnit 9.x assertion and expectation syntax. * * This test case is suitable for use with: - * - WP 5.2.13 and higher; - * - WP 5.3.10 and higher; - * - WP 5.4.8 and higher; - * - WP 5.5.7 and higher; - * - WP 5.6.6 and higher; - * - WP 5.7.4 and higher; - * - WP 5.8.1 and higher; * - WP 5.9.0 and higher. + * - WP 6.* and higher. * * The included autoloader will automatically load the correct test case for * the WordPress version the tests are being run on. diff --git a/src/WPIntegration/TestCaseOnlyObjectPropertyPolyfill.php b/src/WPIntegration/TestCaseOnlyObjectPropertyPolyfill.php new file mode 100644 index 0000000..15dbd86 --- /dev/null +++ b/src/WPIntegration/TestCaseOnlyObjectPropertyPolyfill.php @@ -0,0 +1,44 @@ +