Skip to content

Commit

Permalink
Backport support for the AssertObjectProperty polyfill
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Sep 23, 2023
1 parent 6de43fc commit d74a1e1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions src/WPIntegration/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yoast\WPTestUtils\WPIntegration;

use PHPUnit\Runner\Version as PHPUnit_Version;
use WP_UnitTestCase;

/**
* Custom autoloader.
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/WPIntegration/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +51,7 @@ abstract class TestCase extends WP_UnitTestCase {
use AssertionRenames;
use AssertIsType;
use AssertObjectEquals;
use AssertObjectProperty;
use AssertStringContains;
use EqualToSpecializations;
use ExpectExceptionMessageMatches;
Expand Down
8 changes: 1 addition & 7 deletions src/WPIntegration/TestCaseNoPolyfills.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions src/WPIntegration/TestCaseOnlyObjectPropertyPolyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Yoast\WPTestUtils\WPIntegration;

use WP_UnitTestCase;
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectProperty;
use Yoast\WPTestUtils\Helpers\ExpectOutputHelper;

/**
* Basic test case for use with a WP Integration test test suite.
*
* This test case extends the WordPress native base test case and
* adds a limited set of test helper functions.
*
* The WordPress native base test case will include all relevant
* polyfills to allow for using PHPUnit 9.x assertion and expectation syntax
* with the exception of the PHPUnit 9.6.11+/PHPUnit Polyfills 1.1.0+
* assertObject*() polyfills, as that polyfill was added after the backport.
*
* This test case makes sure the assertObject*() polyfills are still
* available when running tests against WP versions below 5.9, which do
* include the backported changes from WP 5.9, but don't include the
* polyfill which was released after the backports were done.
*
* 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;
*
* The included autoloader will automatically load the correct test case for
* the WordPress version the tests are being run on.
*
* @since 1.2.0
*/
abstract class TestCase extends WP_UnitTestCase {

use AssertObjectProperty;
use ExpectOutputHelper;

}

0 comments on commit d74a1e1

Please sign in to comment.