From 30f9d4e136056d2ebea1efd4fb67a33d1c3a4bc5 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 30 Aug 2021 20:40:05 +0000 Subject: [PATCH] Tests: Do whitespace replacement in `assertDiscardWhitespace()` only when string. The `assertDiscardWhitespace()` method uses `assertEquals()` under the hood, meaning that in reality any type of actual/expected value should be accepted by the function. Fixed the documentation to reflect that. At the same time, only strings can contain whitespace differences. So the whitespace replacement should only be done when string values are passed. This change (a) prevents potential `passing null to non-nullable` errors on PHP 8.1, if either of the inputs would turn out to be `null` and (b) increases tests stability. Follow-up to [35003], [44902]. Props jrf. See #53363. git-svn-id: https://develop.svn.wordpress.org/trunk@51698 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 4225f82496..cbd8c31f30 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -701,12 +701,20 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * @since UT (3.7.0) * @since 5.9.0 Added the `$message` parameter. * - * @param string $expected The expected value. - * @param string $actual The actual value. + * @param mixed $expected The expected value. + * @param mixed $actual The actual value. * @param string $message Optional. Message to display when the assertion fails. */ public function assertDiscardWhitespace( $expected, $actual, $message = '' ) { - $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ), $message ); + if ( is_string( $expected ) ) { + $expected = preg_replace( '/\s*/', '', $expected ); + } + + if ( is_string( $actual ) ) { + $actual = preg_replace( '/\s*/', '', $actual ); + } + + $this->assertEquals( $expected, $actual, $message ); } /**