Skip to content

Instantly share code, notes, and snippets.

@moon0326
Created July 17, 2020 23:55
Show Gist options
  • Save moon0326/241c86b0acd096060b179df650cc2068 to your computer and use it in GitHub Desktop.
Save moon0326/241c86b0acd096060b179df650cc2068 to your computer and use it in GitHub Desktop.
class aController {
private $service;
public function __construct(aService $service) {
$this->service = $service;
}
public function get_items() {
return $this->service->get_items();
}
public function register_routes() {
// register my routes here
}
}
// my test
function test_controller() {
// Create mock
$mock = Mockery::mock(aService::class);
$expected_data = array('hi');
$mock->shouldReceive('get_items')->andReturn($expected_data);
// Override the routes with mock
$setup_controller = function() use ($mock) {
$aController = new aController($mock);
$aController->register_routes();
}
add_action( 'rest_api_init', $setup_controller, 0 );
// test
$request = new WP_REST_Request( 'GET', 'get_items');
$response = rest_get_server()->dispatch( $request );
$response_data = $response->get_data();
$this->assertEquals($expected_data, $response_data);
}
publi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment