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
This commit is contained in:
Sergey Biryukov 2022-06-20 17:27:15 +00:00
parent a649d58128
commit 397b4e45f2

View File

@ -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 );
}