From 397b4e45f24aa5817a3197074685c23b380a664c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 20 Jun 2022 17:27:15 +0000 Subject: [PATCH] Tests: Always include the error message in `assertNotWPError()` and `assertNotIXRError()`. Previously, in case of failure, `WP_UnitTestCase_Base::assertNotWPError()` displayed the actual error message from the passed `WP_Error` object, but only if the `$message` parameter was empty. This made the assertion less helpful, as the actual error message was lost in case there was a non-empty `$message` parameter passed to the method, as per the [https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#using-assertions Writing PHP Tests] guidelines: > All PHPUnit assertions, as well as all WordPress custom assertions, allow for a `$message` parameter to be passed. This message will be displayed when the assertion fails and can help immensely when debugging a test. This parameter should always be used if more than one assertion is used in a test method. This commit ensures that the actual error message is always displayed, in addition to the passed `$message` parameter. The same applies to `WP_UnitTestCase_Base::assertNotIXRError()`. Follow-up to [34638], [40417]. Props jrf, SergeyBiryukov. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@53536 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index e7d48b745a..d94498d506 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -654,9 +654,10 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * @param string $message Optional. Message to display when the assertion fails. */ public function assertNotWPError( $actual, $message = '' ) { - if ( '' === $message && is_wp_error( $actual ) ) { - $message = $actual->get_error_message(); + if ( is_wp_error( $actual ) ) { + $message .= ' ' . $actual->get_error_message(); } + $this->assertNotInstanceOf( 'WP_Error', $actual, $message ); } @@ -677,9 +678,10 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * @param string $message Optional. Message to display when the assertion fails. */ public function assertNotIXRError( $actual, $message = '' ) { - if ( '' === $message && $actual instanceof IXR_Error ) { - $message = $actual->message; + if ( $actual instanceof IXR_Error ) { + $message .= ' ' . $actual->message; } + $this->assertNotInstanceOf( 'IXR_Error', $actual, $message ); }