Make WordPress Core

Changeset 53634


Ignore:
Timestamp:
07/02/2022 06:05:32 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Build/Test Tools: Add support for WP_Error in the test suite's wp_die() handlers.

This brings parity with WordPress core wp_die() handlers and ensures that if a WP_Error object is passed as the $message argument to wp_die(), the PHPUnit test suite displays the error message correctly.

Previously, this would cause a silent fatal error: Object of class WP_Error could not be converted to string, leading to just displaying wp_die called without any further details.

Follow-up to [28797], [41966], [44666], [45160], [47882].

See #55652.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/functions.php

    r53633 r53634  
    153153 * Handles the WP die handler by outputting the given values as text.
    154154 *
    155  * @param string $message The message.
    156  * @param string $title   The title.
    157  * @param array  $args    Array with arguments.
     155 * @param string|WP_Error $message Error message or WP_Error object.
     156 * @param string          $title   Error title.
     157 * @param array           $args    Arguments passed to wp_die().
    158158 */
    159159function _wp_die_handler( $message, $title = '', $args = array() ) {
     
    200200 * Dies without an exit.
    201201 *
    202  * @param string $message The message.
    203  * @param string $title   The title.
    204  * @param array  $args    Array with arguments.
     202 * @param string|WP_Error $message Error message or WP_Error object.
     203 * @param string          $title   Error title.
     204 * @param array           $args    Arguments passed to wp_die().
    205205 */
    206206function _wp_die_handler_txt( $message, $title, $args ) {
    207     echo "\nwp_die called\n";
     207    list( $message, $title, $args ) = _wp_die_process_input( $message, $title, $args );
     208
     209    echo "\nwp_die() called\n";
    208210    echo "Message: $message\n";
    209211
     
    213215
    214216    if ( ! empty( $args ) ) {
    215         echo "Args: \n";
    216         foreach ( $args as $k => $v ) {
    217             echo "\t $k : $v\n";
     217        echo "Args:\n";
     218        foreach ( $args as $key => $value ) {
     219            if ( ! is_scalar( $value ) ) {
     220                $value = var_export( $value, true );
     221            }
     222
     223            echo "\t$key: $value\n";
    218224        }
    219225    }
     
    223229 * Dies with an exit.
    224230 *
    225  * @param string $message The message.
    226  * @param string $title   The title.
    227  * @param array  $args    Array with arguments.
     231 * @param string|WP_Error $message Error message or WP_Error object.
     232 * @param string          $title   Error title.
     233 * @param array           $args    Arguments passed to wp_die().
    228234 */
    229235function _wp_die_handler_exit( $message, $title, $args ) {
    230     echo "\nwp_die called\n";
     236    list( $message, $title, $args ) = _wp_die_process_input( $message, $title, $args );
     237
     238    echo "\nwp_die() called\n";
    231239    echo "Message: $message\n";
    232240
     
    236244
    237245    if ( ! empty( $args ) ) {
    238         echo "Args: \n";
    239         foreach ( $args as $k => $v ) {
    240             echo "\t $k : $v\n";
     246        echo "Args:\n";
     247        foreach ( $args as $key => $value ) {
     248            if ( ! is_scalar( $value ) ) {
     249                $value = var_export( $value, true );
     250            }
     251
     252            echo "\t$key: $value\n";
    241253        }
    242254    }
     255
    243256    exit( 1 );
    244257}
Note: See TracChangeset for help on using the changeset viewer.