WordPress.org

Make WordPress Core

Ticket #53363: 53363-05-custom-assertions-should-have-msg-param.patch

File 53363-05-custom-assertions-should-have-msg-param.patch, 6.6 KB (added by jrf, 4 months ago)

Build/Tests: custom assertions should always have a $message parameter All assertions in PHPUnit have a $message parameter. Setting this parameter allows to distinguish which assertion is failing when a test runs multiple assertion, making debugging of the tests easier. The WP custom assertions which are included in the WP_UnitTestCase_Base class should also allow for this $message parameter. This patch adds this - optional - parameter for those assertions which were missing it.

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

    From b28c9eebdcccc682f9ff00358a5a78a7faf253ed Mon Sep 17 00:00:00 2001
    From: jrfnl <jrfnl@users.noreply.github.com>
    Date: Wed, 21 Jul 2021 16:33:30 +0200
    Subject: [PATCH] Build/Tests: custom assertions should always have a $message
     parameter
    
    All assertions in PHPUnit have a `$message` parameter.
    Setting this parameter allows to distinguish which assertion is failing when a test runs multiple assertion, making debugging of the tests easier.
    
    The WP custom assertions which are included in the `WP_UnitTestCase_Base` class should also allow for this `$message` parameter.
    
    This patch adds this - optional - parameter for those assertions which were missing it.
    ---
     tests/phpunit/includes/abstract-testcase.php | 64 +++++++++++---------
     1 file changed, 36 insertions(+), 28 deletions(-)
    
    diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php
    index ab984160e6..3d65410a67 100644
    a b abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { 
    671671         *
    672672         * @param string $expected The expected value.
    673673         * @param string $actual   The actual value.
     674         * @param string $message  Optional. Message to display when the assertion fails.
    674675         */
    675         public function assertDiscardWhitespace( $expected, $actual ) {
    676                 $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ) );
     676        public function assertDiscardWhitespace( $expected, $actual, $message = '' ) {
     677                $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual, $message ) );
    677678        }
    678679
    679680        /**
    abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { 
    684685         *
    685686         * @param string|array $expected The expected value.
    686687         * @param string|array $actual   The actual value.
     688         * @param string       $message  Optional. Message to display when the assertion fails.
    687689         */
    688         public function assertSameIgnoreEOL( $expected, $actual ) {
     690        public function assertSameIgnoreEOL( $expected, $actual, $message = '' ) {
    689691                $expected = map_deep(
    690692                        $expected,
    691693                        function ( $value ) {
    abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { 
    700702                        }
    701703                );
    702704
    703                 $this->assertSame( $expected, $actual );
     705                $this->assertSame( $expected, $actual, $message );
    704706        }
    705707
    706708        /**
    abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { 
    711713         *
    712714         * @param string $expected The expected value.
    713715         * @param string $actual   The actual value.
     716         * @param string $message  Optional. Message to display when the assertion fails.
    714717         */
    715         public function assertEqualsIgnoreEOL( $expected, $actual ) {
    716                 $this->assertSameIgnoreEOL( $expected, $actual );
     718        public function assertEqualsIgnoreEOL( $expected, $actual, $message = '' ) {
     719                $this->assertSameIgnoreEOL( $expected, $actual, $message );
    717720        }
    718721
    719722        /**
    abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { 
    721724         *
    722725         * @since 5.6.0
    723726         *
    724          * @param array $expected Expected array.
    725          * @param array $actual   Array to check.
     727         * @param array  $expected Expected array.
     728         * @param array  $actual   Array to check.
     729         * @param string $message  Optional. Message to display when the assertion fails.
    726730         */
    727         public function assertSameSets( $expected, $actual ) {
     731        public function assertSameSets( $expected, $actual, $message = '' ) {
    728732                sort( $expected );
    729733                sort( $actual );
    730                 $this->assertSame( $expected, $actual );
     734                $this->assertSame( $expected, $actual, $message );
    731735        }
    732736
    733737        /**
    abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { 
    735739         *
    736740         * @since 3.5.0
    737741         *
    738          * @param array $expected Expected array.
    739          * @param array $actual   Array to check.
     742         * @param array  $expected Expected array.
     743         * @param array  $actual   Array to check.
     744         * @param string $message  Optional. Message to display when the assertion fails.
    740745         */
    741         public function assertEqualSets( $expected, $actual ) {
     746        public function assertEqualSets( $expected, $actual, $message = '' ) {
    742747                sort( $expected );
    743748                sort( $actual );
    744                 $this->assertEquals( $expected, $actual );
     749                $this->assertEquals( $expected, $actual, $message );
    745750        }
    746751
    747752        /**
    abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { 
    749754         *
    750755         * @since 5.6.0
    751756         *
    752          * @param array $expected Expected array.
    753          * @param array $actual   Array to check.
     757         * @param array  $expected Expected array.
     758         * @param array  $actual   Array to check.
     759         * @param string $message  Optional. Message to display when the assertion fails.
    754760         */
    755         public function assertSameSetsWithIndex( $expected, $actual ) {
     761        public function assertSameSetsWithIndex( $expected, $actual, $message = '' ) {
    756762                ksort( $expected );
    757763                ksort( $actual );
    758                 $this->assertSame( $expected, $actual );
     764                $this->assertSame( $expected, $actual, $message );
    759765        }
    760766
    761767        /**
    abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { 
    763769         *
    764770         * @since 4.1.0
    765771         *
    766          * @param array $expected Expected array.
    767          * @param array $actual   Array to check.
     772         * @param array  $expected Expected array.
     773         * @param array  $actual   Array to check.
     774         * @param string $message  Optional. Message to display when the assertion fails.
    768775         */
    769         public function assertEqualSetsWithIndex( $expected, $actual ) {
     776        public function assertEqualSetsWithIndex( $expected, $actual, $message = '' ) {
    770777                ksort( $expected );
    771778                ksort( $actual );
    772                 $this->assertEquals( $expected, $actual );
     779                $this->assertEquals( $expected, $actual, $message );
    773780        }
    774781
    775782        /**
    abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { 
    777784         *
    778785         * @since 4.8.0
    779786         *
    780          * @param array $array Array to check.
     787         * @param array  $array   Array to check.
     788         * @param string $message Optional. Message to display when the assertion fails.
    781789         */
    782         public function assertNonEmptyMultidimensionalArray( $array ) {
    783                 $this->assertIsArray( $array );
    784                 $this->assertNotEmpty( $array );
     790        public function assertNonEmptyMultidimensionalArray( $array, $message = '' ) {
     791                $this->assertIsArray( $array, $message . ' Value under test is not an array.' );
     792                $this->assertNotEmpty( $array, $message . ' Array is empty' );
    785793
    786794                foreach ( $array as $sub_array ) {
    787                         $this->assertIsArray( $sub_array );
    788                         $this->assertNotEmpty( $sub_array );
     795                        $this->assertIsArray( $sub_array, $message . ' Subitem of the array is not an array' );
     796                        $this->assertNotEmpty( $sub_array, $message . ' Subitem of the array is empty' );
    789797                }
    790798        }
    791799