Created
July 17, 2020 23:55
-
-
Save moon0326/241c86b0acd096060b179df650cc2068 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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