Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API tests enhancements #156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions endpoint/templates/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
*/
class <%= classname %>_Test extends WP_UnitTestCase {

function setUp() {
parent::setUp();

global $wp_rest_server;
$this->server = $wp_rest_server = new WP_REST_Server;
do_action( 'rest_api_init' );

$this->subscriber = $this->factory->user->create( array( 'role' => 'subscriber' ) );
$this->administrator = $this->factory->user->create( array( 'role' => 'administrator' ) );
}

/**
* Test if our class exists.
*
Expand All @@ -31,6 +42,70 @@ function test_class_access() {
* @since <%= version %>
*/
function test_sample() {
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for such a lengthy delay on looking at this PR.

Can you please remove the commented code and I'll get this merged?

wp_set_current_user( $this->administrator );

$request = new WP_REST_Request( 'POST', '/endpoint' );
$request->set_param( 'key', 'value' );
$response = $this->server->dispatch( $request );
$this->assertResponseStatus( 200, $response );
*/
$this->assertTrue( true );
}

/**
* Test response status for API request.
*
* @since <%= version %>
*/
protected function assertResponseStatus( $status, $response, $error_code = '', $debug = false ) {
if ( $debug ) {
error_log( '$response->get_data(): '. print_r( $response->get_data(), true ) );
}
$this->assertEquals( $status, $response->get_status() );

if ( $error_code ) {
$this->assertResponseErrorCode( $error_code, $response );
}
}

/**
* Test response error code for API request.
*
* @since <%= version %>
*/
protected function assertResponseErrorCode( $error_code, $response ) {
$response_data = $response->get_data();
$this->assertEquals( $error_code, $response_data['code'] );
}

/**
* Test response data for API request.
*
* @since <%= version %>
*/
protected function assertResponseData( $data, $response ) {
$response_data = $response->get_data();
$tested_data = array();
foreach( $data as $key => $value ) {
if ( isset( $response_data[ $key ] ) ) {
$tested_data[ $key ] = $response_data[ $key ];
} else {
$tested_data[ $key ] = null;
}
}
$this->assertEquals( $data, $tested_data );
}

/**
* Turn off Rest server.
*
* @since <%= version %>
*/
public function tearDown() {
parent::tearDown();

global $wp_rest_server;
$wp_rest_server = null;
}
}