aporat / store-receipt-validator

PHP receipt validator for Apple iTunes, Google Play and Amazon App Store
Apache License 2.0
633 stars 153 forks source link

Swappable guzzle client #121

Closed andresayej closed 4 years ago

andresayej commented 4 years ago

This PR introduces a way to swap the guzzle client.

This can be particularly useful if, for example, a dev wants to use a different class that extends the GuzzleHttp\Client.

My personal use-case in my Laravel application was that I wanted to swap the client when running Tests with a custom class that extends the GuzzleHttp\Client and has some special snapshotting functionality in it.

Example from the codebase located in the app/Providers/AppServiceProvider.php

$this->app->bind(iTunesValidator::class, function () {
    if ($this->app->runningUnitTests()) {
        return new iTunesValidator(
            iTunesValidator::ENDPOINT_SANDBOX,
            new GuzzleSnapshotClient()
        );
    }

    return new iTunesValidator(
        iTunesValidator::ENDPOINT_PRODUCTION,
    );
});

This is NOT a breaking change because the newly introduced __construct argument has a default fallback value - null if the client is not provided when iTunesValidator is instantiated.

Stafox commented 4 years ago

To change the client in your test, I guess you are talking about functional tests , you can extend base class and override ‘getClient()’ method. No needs to modify constructor to do that.

Moreover, you can write plugins to guzzle, and attach them to client (it can help to solve your issue)