Mocking - plug-and-pay/sdk-php GitHub Wiki

Mock Client

For performance reasons, rate limit and for stability of your tests, your test should mock external services.

Option 1: Guzzle Client

Below you see an example where we mock the request of deleting an order. It is up to you which response you want to which request.

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Response;
use PlugAndPay\Sdk\Service\Client;
use PlugAndPay\Sdk\Service\OrderService;

$guzzleClient = new GuzzleClient([
    'handler' => function (Request $request) {
        $path   = $request->getUri()->getPath();
        $method = $request->getMethod();

        if ($method === 'DELETE' && $path === '/v2/orders') {
            return new Response(204);
        }

        throw new Exception("No guzzle mock found for path $path and method $method");
    },
]);

$client = new Client(self::KEY, null, $guzzleClient);
$service = new OrderService($client);

// ...

Option 2: Client with predefined responses

The SDK uses predefined responses in the tests. Feel free to use that instead of mocking the Guzzle Client.

use PlugAndPay\Sdk\Tests\Feature\Order\Mock\OrderShowMockClient;
use PlugAndPay\Sdk\Tests\Feature\Order\Mock\OrderDestroyMockClient;
use PlugAndPay\Sdk\Service\OrderService;

$client = new OrderDestroyMockClient(Response::HTTP_NO_CONTENT, []);

(new OrderService($client))->delete(1);

// ...

By using the methods on the client you can get additional responses:

use PlugAndPay\Sdk\Tests\Feature\Order\Mock\OrderShowMockClient;
use PlugAndPay\Sdk\Service\OrderService;

$client = (new OrderShowMockClient(['id' => 1]))->billing();
$order  = (new OrderService($client))->find(1);

$billing = $order->billing();