Streamlined `wp_send_json_success` handling.
Now testing AJAX is about as simple as it can get.
New method available on the WP_Ajax_UnitTestCase
class:
/**
* Capture and return the "data" key from any call to
* `wp_send_json_success` or `wp_send_json_error`.
*
* Automatically handles:
* 1. Reset $this->_last_response
* 2. Catch the exception
* 3. Return the result that would be sent as `data`.
*
* @author Mat Lipe
*
* @since 1.7.0
*
* @param callable $callable
*
* @return mixed
*/
protected function _getJsonResult( callable $callable ) {
unset( $this->_last_response );
try {
$this->_handleAjaxCustom( $callable );
} catch ( \Exception $exception ) {
return json_decode( $this->_last_response )->data;
}
}
No more boilerplate or try/catch need to test typical results.
Must be using standard wp_send_json_success
or wp_send_json_error
functions to use this. Or at least by following the same wp_die
and data
key patterns.