Skip to content

Commit

Permalink
Merge pull request #64 from Yoast/JRF/update-for-phpunit-polyfills-1.1.0
Browse files Browse the repository at this point in the history
Backport support for the `AssertObjectProperty` polyfill
  • Loading branch information
jrfnl committed Sep 27, 2023
2 parents 9484959 + b0a3817 commit b65370b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Requirements
* PHP 5.6 or higher.

The following packages will be automatically required via Composer:
* [PHPUnit Polyfills] 1.0.4 or higher.
* [PHPUnit Polyfills] 1.1.0 or higher.
* [PHPUnit] 5.7 - 9.x.
* [BrainMonkey] 2.6.1 or higher.

Expand Down
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
43 changes: 43 additions & 0 deletions src/WPIntegration/TestCaseOnlyObjectPropertyPolyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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 b65370b

Please sign in to comment.