2016-07-25 18:19:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WC REST API Unit Test Case (for WP-API Endpoints).
|
|
|
|
*
|
|
|
|
* Provides REST API specific methods and setup/teardown.
|
|
|
|
*
|
2021-03-25 13:31:52 +00:00
|
|
|
* @package WooCommerce\Tests
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2016-07-25 18:19:44 +00:00
|
|
|
*/
|
|
|
|
|
2021-03-25 13:31:52 +00:00
|
|
|
/**
|
|
|
|
* Base class for REST related unit test classes.
|
|
|
|
*/
|
2018-06-13 15:55:39 +00:00
|
|
|
class WC_REST_Unit_Test_Case extends WC_Unit_Test_Case {
|
2016-07-25 18:19:44 +00:00
|
|
|
|
2021-03-25 13:31:52 +00:00
|
|
|
/**
|
|
|
|
* @var WP_REST_Server
|
|
|
|
*/
|
2018-06-13 15:55:39 +00:00
|
|
|
protected $server;
|
2016-07-25 18:19:44 +00:00
|
|
|
|
2018-06-13 15:55:39 +00:00
|
|
|
/**
|
|
|
|
* Setup our test server.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
global $wp_rest_server;
|
|
|
|
$wp_rest_server = new WP_Test_Spy_REST_Server();
|
|
|
|
$this->server = $wp_rest_server;
|
|
|
|
do_action( 'rest_api_init' );
|
2018-08-27 19:05:13 +00:00
|
|
|
|
|
|
|
// Reset payment gateways.
|
2019-05-01 22:05:00 +00:00
|
|
|
$gateways = WC_Payment_Gateways::instance();
|
2018-08-27 19:05:13 +00:00
|
|
|
$gateways->payment_gateways = array();
|
|
|
|
$gateways->init();
|
2018-06-13 15:55:39 +00:00
|
|
|
}
|
2016-07-25 18:19:44 +00:00
|
|
|
|
2018-06-13 15:55:39 +00:00
|
|
|
/**
|
|
|
|
* Unset the server.
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
2018-06-01 12:49:02 +00:00
|
|
|
global $wp_rest_server;
|
|
|
|
unset( $this->server );
|
2018-06-13 15:55:39 +00:00
|
|
|
$wp_rest_server = null;
|
|
|
|
}
|
2021-03-25 13:31:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a REST request.
|
|
|
|
*
|
|
|
|
* @param string $url The endpopint url, if it doesn't start with '/' it'll be prepended with '/wc/v3/'.
|
|
|
|
* @param string $verb HTTP verb for the request, default is GET.
|
|
|
|
* @param array|null $body_params Body parameters for the request, null if none are required.
|
|
|
|
* @param array|null $query_params Query string parameters for the request, null if none are required.
|
|
|
|
* @return array Result from the request.
|
|
|
|
*/
|
|
|
|
public function do_rest_request( $url, $verb = 'GET', $body_params = null, $query_params = null ) {
|
|
|
|
if ( '/' !== $url[0] ) {
|
|
|
|
$url = '/wc/v3/' . $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( $verb, $url );
|
|
|
|
if ( ! is_null( $query_params ) ) {
|
|
|
|
$request->set_query_params( $query_params );
|
|
|
|
}
|
|
|
|
if ( ! is_null( $body_params ) ) {
|
|
|
|
$request->set_body_params( $body_params );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->server->dispatch( $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a GET REST request.
|
|
|
|
*
|
|
|
|
* @param string $url The endpopint url, if it doesn't start with '/' it'll be prepended with '/wc/v3/'.
|
|
|
|
* @param array|null $query_params Query string parameters for the request, null if none are required.
|
|
|
|
* @return WP_REST_Response The response for the request.
|
|
|
|
*/
|
|
|
|
public function do_rest_get_request( $url, $query_params = null ) {
|
|
|
|
return $this->do_rest_request( $url, 'GET', null, $query_params );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a POST REST request.
|
|
|
|
*
|
|
|
|
* @param string $url The endpopint url, if it doesn't start with '/' it'll be prepended with '/wc/v3/'.
|
|
|
|
* @param array|null $body_params Body parameters for the request, null if none are required.
|
|
|
|
* @param array|null $query_params Query string parameters for the request, null if none are required.
|
|
|
|
* @return array Result from the request.
|
|
|
|
*/
|
|
|
|
public function do_rest_post_request( $url, $body_params = null, $query_params = null ) {
|
|
|
|
return $this->do_rest_request( $url, 'POST', $body_params, $query_params );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a PUT REST request.
|
|
|
|
*
|
|
|
|
* @param string $url The endpopint url, if it doesn't start with '/' it'll be prepended with '/wc/v3/'.
|
|
|
|
* @param array|null $body_params Body parameters for the request, null if none are required.
|
|
|
|
* @param array|null $query_params Query string parameters for the request, null if none are required.
|
|
|
|
* @return array Result from the request.
|
|
|
|
*/
|
|
|
|
public function do_rest_put_request( $url, $body_params = null, $query_params = null ) {
|
|
|
|
return $this->do_rest_request( $url, 'PUT', $body_params, $query_params );
|
|
|
|
}
|
2016-07-25 18:19:44 +00:00
|
|
|
}
|