plugin development – Mock a dependency of a WP_REST_Controller class


I have a custom WordPress REST API with a client for an external service being injected in its constructor; that client is going to be used internally in that class, like this:

class Custom_REST_Endpoint extends WP_REST_Controller {
  public function __construct( $client ) {
    $this->client = $client;

Then, I’m registering that REST API, like this:

function custom_api_register_routes() {
    $api = new Custom_REST_Endpoint( new Client() );

add_action( 'rest_api_init', 'custom_api_register_routes' );
  • That said, what’s the best/correct/WordPress-ish approach for mocking that Client object while doing a test?
  • Is there some hook available I can hook in and swap Clients implementation by the mocked one?
  • Do you recommend a different approach for injecting Client into Custom_REST_Endpoint?
Ian Rodrigues 4 months 2022-07-26T13:53:13-05:00 0 Answers 0 views 0

Leave an answer