Make WordPress Core


Ignore:
Timestamp:
04/23/2024 06:55:26 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Add a $message parameter for a custom assertion in WP_Test_REST_TestCase.

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 WP_Test_REST_TestCase::assertErrorResponse().

Follow-up to [34928], [51478].

Props mykolashlyakhtun, antonvlasenko, swissspidy, SergeyBiryukov.
Fixes #60426.

File:
1 edited

Legend:

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

    r56352 r58039  
    22
    33abstract class WP_Test_REST_TestCase extends WP_UnitTestCase {
    4         protected function assertErrorResponse( $code, $response, $status = null ) {
     4
     5        /**
     6         * Asserts that the REST API response has the specified error.
     7         *
     8         * @since 4.4.0
     9         * @since 6.6.0 Added the `$message` parameter.
     10         *
     11         * @param string|int                $code     Expected error code.
     12         * @param WP_REST_Response|WP_Error $response REST API response.
     13         * @param int                       $status   Optional. Status code.
     14         * @param string                    $message  Optional. Message to display when the assertion fails.
     15         */
     16        protected function assertErrorResponse( $code, $response, $status = null, $message = '' ) {
    517
    618                if ( $response instanceof WP_REST_Response ) {
     
    820                }
    921
    10                 $this->assertWPError( $response );
    11                 $this->assertSame( $code, $response->get_error_code() );
     22                $this->assertWPError( $response, $message );
     23                $this->assertSame( $code, $response->get_error_code(), $message );
    1224
    1325                if ( null !== $status ) {
    1426                        $data = $response->get_error_data();
    15                         $this->assertArrayHasKey( 'status', $data );
    16                         $this->assertSame( $status, $data['status'] );
     27                        $this->assertArrayHasKey( 'status', $data, $message );
     28                        $this->assertSame( $status, $data['status'], $message );
    1729                }
    1830        }
Note: See TracChangeset for help on using the changeset viewer.