Files
wordpress/tests/phpunit/includes/testcase-rest-api.php
Sergey Biryukov 179789fab3 Tests: Add more specific messages 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.

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

See #60426.

git-svn-id: https://develop.svn.wordpress.org/trunk@58051 602fd350-edb4-49c9-b593-d223f7449a82
2024-04-26 15:49:19 +00:00

32 lines
1.2 KiB
PHP

<?php
abstract class WP_Test_REST_TestCase extends WP_UnitTestCase {
/**
* Asserts that the REST API response has the specified error.
*
* @since 4.4.0
* @since 6.6.0 Added the `$message` parameter.
*
* @param string|int $code Expected error code.
* @param WP_REST_Response|WP_Error $response REST API response.
* @param int $status Optional. Status code.
* @param string $message Optional. Message to display when the assertion fails.
*/
protected function assertErrorResponse( $code, $response, $status = null, $message = '' ) {
if ( $response instanceof WP_REST_Response ) {
$response = $response->as_error();
}
$this->assertWPError( $response, $message . ' Passed $response is not a WP_Error object.' );
$this->assertSame( $code, $response->get_error_code(), $message . ' The expected error code does not match.' );
if ( null !== $status ) {
$data = $response->get_error_data();
$this->assertArrayHasKey( 'status', $data, $message . ' Passed $response does not include a status code.' );
$this->assertSame( $status, $data['status'], $message . ' The expected status code does not match.' );
}
}
}