From 3bebac5ea496bce9a0d1c2beef163c7af95aef4e Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 2 Jul 2022 18:05:32 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@53634 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/functions.php | 47 ++++++++++++++++++---------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/tests/phpunit/includes/functions.php b/tests/phpunit/includes/functions.php index 245c37403c..1666185f9e 100644 --- a/tests/phpunit/includes/functions.php +++ b/tests/phpunit/includes/functions.php @@ -152,9 +152,9 @@ function _delete_all_posts() { /** * Handles the WP die handler by outputting the given values as text. * - * @param string $message The message. - * @param string $title The title. - * @param array $args Array with arguments. + * @param string|WP_Error $message Error message or WP_Error object. + * @param string $title Error title. + * @param array $args Arguments passed to wp_die(). */ function _wp_die_handler( $message, $title = '', $args = array() ) { if ( ! $GLOBALS['_wp_die_disabled'] ) { @@ -199,12 +199,14 @@ function _wp_die_handler_filter_exit() { /** * Dies without an exit. * - * @param string $message The message. - * @param string $title The title. - * @param array $args Array with arguments. + * @param string|WP_Error $message Error message or WP_Error object. + * @param string $title Error title. + * @param array $args Arguments passed to wp_die(). */ function _wp_die_handler_txt( $message, $title, $args ) { - echo "\nwp_die called\n"; + list( $message, $title, $args ) = _wp_die_process_input( $message, $title, $args ); + + echo "\nwp_die() called\n"; echo "Message: $message\n"; if ( ! empty( $title ) ) { @@ -212,9 +214,13 @@ function _wp_die_handler_txt( $message, $title, $args ) { } if ( ! empty( $args ) ) { - echo "Args: \n"; - foreach ( $args as $k => $v ) { - echo "\t $k : $v\n"; + echo "Args:\n"; + foreach ( $args as $key => $value ) { + if ( ! is_scalar( $value ) ) { + $value = var_export( $value, true ); + } + + echo "\t$key: $value\n"; } } } @@ -222,12 +228,14 @@ function _wp_die_handler_txt( $message, $title, $args ) { /** * Dies with an exit. * - * @param string $message The message. - * @param string $title The title. - * @param array $args Array with arguments. + * @param string|WP_Error $message Error message or WP_Error object. + * @param string $title Error title. + * @param array $args Arguments passed to wp_die(). */ function _wp_die_handler_exit( $message, $title, $args ) { - echo "\nwp_die called\n"; + list( $message, $title, $args ) = _wp_die_process_input( $message, $title, $args ); + + echo "\nwp_die() called\n"; echo "Message: $message\n"; if ( ! empty( $title ) ) { @@ -235,11 +243,16 @@ function _wp_die_handler_exit( $message, $title, $args ) { } if ( ! empty( $args ) ) { - echo "Args: \n"; - foreach ( $args as $k => $v ) { - echo "\t $k : $v\n"; + echo "Args:\n"; + foreach ( $args as $key => $value ) { + if ( ! is_scalar( $value ) ) { + $value = var_export( $value, true ); + } + + echo "\t$key: $value\n"; } } + exit( 1 ); }