Make WordPress Core

Ticket #53363: 53363-11-custom-assertions-improve-assertDiscardWhitespace.patch

File 53363-11-custom-assertions-improve-assertDiscardWhitespace.patch, 2.0 KB (added by jrf, 2 years ago)

WP_UnitTestCase_Base::assertDiscardWhitespace(): stabilize the custom assertion 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 will prevent potential passing null to non-nullable errors on PHP 8.1, if either of the inputs would turn out to be null.

  • tests/phpunit/includes/abstract-testcase.php

    From 23cc1a956291e45cf619782f2ba97a3740dbf375 Mon Sep 17 00:00:00 2001
    From: jrfnl <jrfnl@users.noreply.github.com>
    Date: Sun, 29 Aug 2021 01:49:21 +0200
    Subject: [PATCH] WP_UnitTestCase_Base::assertDiscardWhitespace(): stabilize
     the custom assertion
    
    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 will prevent potential `passing null to non-nullable` errors on PHP 8.1, if either of the inputs would turn out to be `null`.
    ---
     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 b abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { 
    701701         * @since UT (3.7.0)
    702702         * @since 5.9.0 Added the `$message` parameter.
    703703         *
    704          * @param string $expected The expected value.
    705          * @param string $actual   The actual value.
     704         * @param mixed $expected The expected value.
     705         * @param mixed $actual   The actual value.
    706706         * @param string $message  Optional. Message to display when the assertion fails.
    707707         */
    708708        public function assertDiscardWhitespace( $expected, $actual, $message = '' ) {
    709                 $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ), $message );
     709                if ( is_string( $expected ) ) {
     710                        $expected = preg_replace( '/\s*/', '', $expected );
     711                }
     712
     713                if ( is_string( $actual ) ) {
     714                        $actual = preg_replace( '/\s*/', '', $actual );
     715                }
     716
     717                $this->assertEquals( $expected, $actual, $message );
    710718        }
    711719
    712720        /**