TomorrowIdeas / plaid-sdk-php

PHP bindings for the Plaid API
MIT License
111 stars 42 forks source link

Make Plaid class not final #15

Closed psychob closed 4 years ago

psychob commented 4 years ago

Currently Plaid class is marked as final, there is no real benefits to this, and for the downside it makes testing hard as i can't just mock this object.

brentscheffler commented 4 years ago

You can also easily mock this class by providing your own instance of a ClientInterface via the included Shuttle library and setting a MockHandler to return mocked API responses.

For example:

$httpClient = new Shuttle([
    "handler" => new MockHandler([
         new Response(200, \json_encode(["foo" => "bar"]))
    ])
]);

$plaid = new Plaid("my-client-id", "my-secret-id", "my-public-key");
$plaid->setHttpClient($httpClient);

$item = $plaid->getItem("itm_1234");

The above code will use the provided ClientInterface instance and with each API call pop-off a response from the MockHandler instance. The MockHandler instance can take either an array of ResponseInterface instances or a closure that accepts a ServerRequestInterface instance and returns a ResponseInterface instance, or a mix of both. We use this pattern extensively in the unit tests.

Hope this helps.