Merge pull request #11525 from woothemes/api-test-base

Add base REST API test case class for testing WP-API based WC endpoints
This commit is contained in:
Justin Shreve 2016-07-26 13:46:12 -07:00 committed by GitHub
commit 48b14e9713
3 changed files with 77 additions and 0 deletions

View File

@ -98,10 +98,12 @@ class WC_Unit_Tests_Bootstrap {
require_once( $this->tests_dir . '/framework/class-wc-mock-session-handler.php' );
require_once( $this->tests_dir . '/framework/class-wc-mock-wc-data.php' );
require_once( $this->tests_dir . '/framework/class-wc-payment-token-stub.php' );
require_once( $this->tests_dir . '/framework/vendor/class-wp-test-spy-rest-server.php' );
// test cases
require_once( $this->tests_dir . '/framework/class-wc-unit-test-case.php' );
require_once( $this->tests_dir . '/framework/class-wc-api-unit-test-case.php' );
require_once( $this->tests_dir . '/framework/class-wc-rest-unit-test-case.php' );
// Helpers
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-product.php' );

View File

@ -0,0 +1,33 @@
<?php
/**
* WC REST API Unit Test Case (for WP-API Endpoints).
*
* Provides REST API specific methods and setup/teardown.
*
* @since 2.7
*/
class WC_REST_Unit_Test_Case extends WC_Unit_Test_Case {
protected $server;
/**
* Setup our test server.
*/
public function setUp() {
parent::setUp();
global $wp_rest_server;
$this->server = $wp_rest_server = new WP_Test_Spy_REST_Server;
do_action( 'rest_api_init' );
}
/**
* Unset the server.
*/
public function tearDown() {
parent::tearDown();
global $wp_rest_server;
$wp_rest_server = null;
}
}

View File

@ -0,0 +1,42 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @version 2.0-beta13.1
*/
class WP_Test_Spy_REST_Server extends WP_REST_Server {
/**
* Get the raw $endpoints data from the server
*
* @return array
*/
public function get_raw_endpoint_data() {
return $this->endpoints;
}
/**
* Allow calling protected methods from tests
*
* @param string $method Method to call
* @param array $args Arguments to pass to the method
* @return mixed
*/
public function __call( $method, $args ) {
return call_user_func_array( array( $this, $method ), $args );
}
/**
* Call dispatch() with the rest_post_dispatch filter
*/
public function dispatch( $request ) {
$result = parent::dispatch( $request );
$result = rest_ensure_response( $result );
if ( is_wp_error( $result ) ) {
$result = $this->error_to_response( $result );
}
return apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $request );
}
}