Skip to content

Commit

Permalink
Tests: Minimize the chances of signature conflicts for `assertEqualsW…
Browse files Browse the repository at this point in the history
…ithDelta()`.

The PHPUnit 7.5+ method `assertEqualsWithDelta()` was polyfilled for PHPUnit < 7.5, but also overloaded for PHPUnit 7.5 itself, which was not necessary and created a higher chance of signature conflicts, especially when the WP test suite is used as a basis for integration tests with plugins/themes.

This change removes the unnecessary overloading for PHPUnit 7.5+ and simplifies the overloaded method for PHPUnit < 7.5, including removing the `IsEqual()` class alias declaration, no longer needed.

Follow-up to [48952].

Props jrf.
See #52625.

git-svn-id: https://develop.svn.wordpress.org/trunk@50986 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov authored and SergeyBiryukov committed May 25, 2021
1 parent 670ea3c commit a66c961
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 35 deletions.
2 changes: 1 addition & 1 deletion tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function wp_tests_options( $value ) {
// Delete any default posts & related data.
_delete_all_posts();

if ( version_compare( tests_get_phpunit_version(), '7.0', '>=' ) ) {
if ( version_compare( tests_get_phpunit_version(), '7.5', '>=' ) ) {
require __DIR__ . '/phpunit7/testcase.php';
} else {
require __DIR__ . '/testcase.php';
Expand Down
1 change: 0 additions & 1 deletion tests/phpunit/includes/phpunit6/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class_alias( 'PHPUnit\Framework\Error\Warning', 'PHPUnit_Framework_Error_Warning
class_alias( 'PHPUnit\Framework\Test', 'PHPUnit_Framework_Test' );
class_alias( 'PHPUnit\Framework\Warning', 'PHPUnit_Framework_Warning' );
class_alias( 'PHPUnit\Framework\AssertionFailedError', 'PHPUnit_Framework_AssertionFailedError' );
class_alias( 'PHPUnit\Framework\Constraint\IsEqual', 'PHPUnit_Framework_Constraint_IsEqual' );
class_alias( 'PHPUnit\Framework\TestSuite', 'PHPUnit_Framework_TestSuite' );
class_alias( 'PHPUnit\Framework\TestListener', 'PHPUnit_Framework_TestListener' );
class_alias( 'PHPUnit\Util\GlobalState', 'PHPUnit_Util_GlobalState' );
Expand Down
28 changes: 1 addition & 27 deletions tests/phpunit/includes/phpunit7/testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,4 @@
*
* All WordPress unit tests should inherit from this class.
*/
class WP_UnitTestCase extends WP_UnitTestCase_Base {

/**
* Asserts that two variables are equal (with delta).
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.6.0
*
* @param mixed $expected First value to compare.
* @param mixed $actual Second value to compare.
* @param float $delta Allowed numerical distance between two values to consider them equal.
* @param string $message Optional. Message to display when the assertion fails.
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public static function assertEqualsWithDelta( $expected, $actual, float $delta, string $message = '' ): void {
$constraint = new PHPUnit\Framework\Constraint\IsEqual(
$expected,
$delta
);

static::assertThat( $actual, $constraint, $message );
}
}
class WP_UnitTestCase extends WP_UnitTestCase_Base {}
7 changes: 1 addition & 6 deletions tests/phpunit/includes/testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ class WP_UnitTestCase extends WP_UnitTestCase_Base {
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public static function assertEqualsWithDelta( $expected, $actual, $delta, $message = '' ) {
$constraint = new PHPUnit_Framework_Constraint_IsEqual(
$expected,
$delta
);

static::assertThat( $actual, $constraint, $message );
static::assertEquals( $expected, $actual, $message, $delta );
}
}

0 comments on commit a66c961

Please sign in to comment.