WordPress.org

Make WordPress Core

Changeset 51478


Ignore:
Timestamp:
07/22/2021 05:14:44 PM (10 days ago)
Author:
SergeyBiryukov
Message:

Tests: Add a $message parameter for custom assertions in WP_UnitTestCase_Base.

All assertions in PHPUnit have a $message parameter. Setting this parameter allows to distinguish which assertion is failing when a test runs multiple assertions, making debugging of the tests easier.

This optional parameter is now added for the assertion methods in the WP_UnitTestCase_Base class that were missing it.

Props jrf.
See #53363.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r51419 r51478  
    670670     * Asserts that two values are equal, with whitespace differences discarded.
    671671     *
     672     * @since UT (3.7.0)
     673     * @since 5.9.0 Added the `$message` parameter.
     674     *
    672675     * @param string $expected The expected value.
    673676     * @param string $actual   The actual value.
    674      */
    675     public function assertDiscardWhitespace( $expected, $actual ) {
    676         $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ) );
     677     * @param string $message  Optional. Message to display when the assertion fails.
     678     */
     679    public function assertDiscardWhitespace( $expected, $actual, $message = '' ) {
     680        $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual, $message ) );
    677681    }
    678682
     
    682686     * @since 5.6.0
    683687     * @since 5.8.0 Added support for nested arrays.
     688     * @since 5.9.0 Added the `$message` parameter.
    684689     *
    685690     * @param string|array $expected The expected value.
    686691     * @param string|array $actual   The actual value.
    687      */
    688     public function assertSameIgnoreEOL( $expected, $actual ) {
     692     * @param string       $message  Optional. Message to display when the assertion fails.
     693     */
     694    public function assertSameIgnoreEOL( $expected, $actual, $message = '' ) {
    689695        $expected = map_deep(
    690696            $expected,
     
    701707        );
    702708
    703         $this->assertSame( $expected, $actual );
     709        $this->assertSame( $expected, $actual, $message );
    704710    }
    705711
     
    709715     * @since 5.4.0
    710716     * @since 5.6.0 Turned into an alias for `::assertSameIgnoreEOL()`.
     717     * @since 5.9.0 Added the `$message` parameter.
    711718     *
    712719     * @param string $expected The expected value.
    713720     * @param string $actual   The actual value.
    714      */
    715     public function assertEqualsIgnoreEOL( $expected, $actual ) {
    716         $this->assertSameIgnoreEOL( $expected, $actual );
     721     * @param string $message  Optional. Message to display when the assertion fails.
     722     */
     723    public function assertEqualsIgnoreEOL( $expected, $actual, $message = '' ) {
     724        $this->assertSameIgnoreEOL( $expected, $actual, $message );
    717725    }
    718726
     
    721729     *
    722730     * @since 5.6.0
    723      *
    724      * @param array $expected Expected array.
    725      * @param array $actual   Array to check.
    726      */
    727     public function assertSameSets( $expected, $actual ) {
     731     * @since 5.9.0 Added the `$message` parameter.
     732     *
     733     * @param array  $expected Expected array.
     734     * @param array  $actual   Array to check.
     735     * @param string $message  Optional. Message to display when the assertion fails.
     736     */
     737    public function assertSameSets( $expected, $actual, $message = '' ) {
    728738        sort( $expected );
    729739        sort( $actual );
    730         $this->assertSame( $expected, $actual );
     740        $this->assertSame( $expected, $actual, $message );
    731741    }
    732742
     
    735745     *
    736746     * @since 3.5.0
    737      *
    738      * @param array $expected Expected array.
    739      * @param array $actual   Array to check.
    740      */
    741     public function assertEqualSets( $expected, $actual ) {
     747     * @since 5.9.0 Added the `$message` parameter.
     748     *
     749     * @param array  $expected Expected array.
     750     * @param array  $actual   Array to check.
     751     * @param string $message  Optional. Message to display when the assertion fails.
     752     */
     753    public function assertEqualSets( $expected, $actual, $message = '' ) {
    742754        sort( $expected );
    743755        sort( $actual );
    744         $this->assertEquals( $expected, $actual );
     756        $this->assertEquals( $expected, $actual, $message );
    745757    }
    746758
     
    749761     *
    750762     * @since 5.6.0
    751      *
    752      * @param array $expected Expected array.
    753      * @param array $actual   Array to check.
    754      */
    755     public function assertSameSetsWithIndex( $expected, $actual ) {
     763     * @since 5.9.0 Added the `$message` parameter.
     764     *
     765     * @param array  $expected Expected array.
     766     * @param array  $actual   Array to check.
     767     * @param string $message  Optional. Message to display when the assertion fails.
     768     */
     769    public function assertSameSetsWithIndex( $expected, $actual, $message = '' ) {
    756770        ksort( $expected );
    757771        ksort( $actual );
    758         $this->assertSame( $expected, $actual );
     772        $this->assertSame( $expected, $actual, $message );
    759773    }
    760774
     
    763777     *
    764778     * @since 4.1.0
    765      *
    766      * @param array $expected Expected array.
    767      * @param array $actual   Array to check.
    768      */
    769     public function assertEqualSetsWithIndex( $expected, $actual ) {
     779     * @since 5.9.0 Added the `$message` parameter.
     780     *
     781     * @param array  $expected Expected array.
     782     * @param array  $actual   Array to check.
     783     * @param string $message  Optional. Message to display when the assertion fails.
     784     */
     785    public function assertEqualSetsWithIndex( $expected, $actual, $message = '' ) {
    770786        ksort( $expected );
    771787        ksort( $actual );
    772         $this->assertEquals( $expected, $actual );
     788        $this->assertEquals( $expected, $actual, $message );
    773789    }
    774790
     
    777793     *
    778794     * @since 4.8.0
    779      *
    780      * @param array $array Array to check.
    781      */
    782     public function assertNonEmptyMultidimensionalArray( $array ) {
    783         $this->assertIsArray( $array );
    784         $this->assertNotEmpty( $array );
     795     * @since 5.9.0 Added the `$message` parameter.
     796     *
     797     * @param array  $array   Array to check.
     798     * @param string $message Optional. Message to display when the assertion fails.
     799     */
     800    public function assertNonEmptyMultidimensionalArray( $array, $message = '' ) {
     801        $this->assertIsArray( $array, $message . ' Value under test is not an array.' );
     802        $this->assertNotEmpty( $array, $message . ' Array is empty.' );
    785803
    786804        foreach ( $array as $sub_array ) {
    787             $this->assertIsArray( $sub_array );
    788             $this->assertNotEmpty( $sub_array );
     805            $this->assertIsArray( $sub_array, $message . ' Subitem of the array is not an array.' );
     806            $this->assertNotEmpty( $sub_array, $message . ' Subitem of the array is empty.' );
    789807        }
    790808    }
Note: See TracChangeset for help on using the changeset viewer.