Testing custom API endpoint with class dependency

Sorry I feel like really stuck here.

I have a plugin introducing a new Rest API controller (WP_REST_Controller) with basically a single endpoint which uses a separate class as a client to fetch some data. Let's say:

#my_plugin.php

function register_items_routes() {
    if ( ! class_exists( 'WP_REST_My_Controller' ) ) {
        require_once __DIR__ . '/class-wp-my-controller.php';
    }
    if ( ! class_exists( 'My_Class' ) ) {
        require_once __DIR__ . '/class-wp-my-class.php';
    }

    $controller = new WP_REST_My_Controller(new My_Class);
    $controller-register_routes();
}
add_action( 'rest_api_init', 'register_items_routes' );

_

#class-wp-my-controller.php

class WP_REST_My_Controller extends WP_REST_Controller {

    protected $client;

    public function __construct( $client ) {
        $this-client = $client;
    }

    /**
    * Registers the routes.
    */
    public function register_routes() {
        $namespace = 'my/namespace';
        $path      = 'get-items';

        register_rest_route( $namespace, '/' . $path, [
            array(
                'methods'             = 'GET',
                'callback'            = array( $this, 'get_items' ),
                'permission_callback' = array( $this, 'get_items_permissions_check' )
            ),
        ] );
    }

    public function get_items_permissions_check( $request ) {
            return true;
    }

    /**
    * Get items from My_Class and return them.
    *
    * @param WP_REST_Request $request The incoming HTTP request.
    *
    * @return WP_REST_Response|WP_Error The response containing the items in JSON, WP_Error in case of error.
    */
    public function get_items( $request ) {

        try {
            $items = $this-client-fetch_some_items();
        } catch ( Exception $e ) {
            return new WP_Error(
            'some-client-error',
            $e-getMessage()
        );

        // Code to be tested. - Do some stuff with items and return.

        return new WP_REST_Response( $items );
    }

How am I supposed to stub the My_Class dependency from PhpUnit in order to return a predefined set of items which I could test with?

#test-items-api.php

class Test__Items_REST_Endpoint extends WP_UnitTestCase {

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

        // re-hook the routes here somehow with a mocked client?

    }

    public function test_get_items() {
        $request  = new WP_REST_Request( 'GET', '/my/namespace/get-items' );
        $data = rest_get_server()-dispatch( $request );

        $expected_items = [
                'some_key1' = 'some_value1',
                'some_key2' = 'some_value2',
        ];
    
        $this-assertTrue( count($data['items']) == count($expected_items) );
    }
}

Topic unit-tests rest-api Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.