PHP code example of holgerk / guzzle-replay

1. Go to this page and download the library: Download holgerk/guzzle-replay library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

holgerk / guzzle-replay example snippets


use GuzzleHttp\Client;
use Holgerk\GuzzleReplay\Mode;
use Holgerk\GuzzleReplay\GuzzleReplay;

$guzzleClient = new Client();
// create middleware either in recording or in replay mode
//$middleware = GuzzleReplay::create(Mode::Replay);
$middleware = GuzzleReplay::create(Mode::Record);
// inject middleware into guzzle client
$middleware->inject($guzzleClient);
// inject guzzle client into to your api client that you want to test
$apiClient = new GithubApiClient($guzzleClient);
// do your tests with the api client...

use GuzzleHttp\Client;

class SimpleApiClient
{
    public function __construct(private Client $client) {}

    public function getUuid(): string
    {
        $content = $this->client
            ->get('https://httpbin.org/uuid')
            ->getBody()
            ->getContents();
        return json_decode($content, true)['uuid'];
    }
}

use GuzzleHttp\Client;
use Holgerk\GuzzleReplay\GuzzleReplay;
use PHPUnit\Framework\TestCase;
use function PHPUnit\Framework\assertEquals;

class SimpleApiClientTest extends TestCase
{
    public function testGetUuid(): void
    {
        // GIVEN
        $guzzleClient = new Client();
        //$middleware = GuzzleReplay::create(GuzzleReplay::MODE_RECORD);
        $middleware = GuzzleReplay::create(GuzzleReplay::MODE_REPLAY);
        $middleware->inject($guzzleClient);

        // WHEN
        $apiClient = new SimpleApiClient($guzzleClient);
        $firstUuid = $apiClient->getUuid();
        $secondUuid = $apiClient->getUuid();

        // THEN
        assertEquals('bdd37445-2455-44d3-a00d-18d6220ff565', $firstUuid);
        assertEquals('ea175c37-df2a-42aa-b9b8-c3eac8dfb80b', $secondUuid);
    }
}



// GENERATED - DO NOT EDIT
return \Holgerk\GuzzleReplay\Recording::fromArray(
    [
        'records' => [
            [
                'requestModel' => [
                    'method' => 'GET',
                    'uri' => 'https://httpbin.org/uuid',
                    'headers' => [
                        'User-Agent' => [
                            'GuzzleHttp/7',
                        ],
                        'Host' => [
                            'httpbin.org',
                        ],
                    ],
                    'body' => '',
                    'version' => '1.1',
                ],
                'responseModel' => [
                    'status' => 200,
                    'headers' => [
                        'Date' => [
                            'Wed, 07 May 2025 09:16:13 GMT',
                        ],
                        'Content-Type' => [
                            'application/json',
                        ],
                        'Content-Length' => [
                            '53',
                        ],
                        'Connection' => [
                            'keep-alive',
                        ],
                        'Server' => [
                            'gunicorn/19.9.0',
                        ],
                        'Access-Control-Allow-Origin' => [
                            '*',
                        ],
                        'Access-Control-Allow-Credentials' => [
                            'true',
                        ],
                    ],
                    'body' => '{'."\n"
                        .'  "uuid": "bdd37445-2455-44d3-a00d-18d6220ff565"'."\n"
                        .'}'."\n",
                    'version' => '1.1',
                    'reason' => 'OK',
                    'decodedBody' => [
                        'uuid' => 'bdd37445-2455-44d3-a00d-18d6220ff565',
                    ],
                ],
            ],
            [
                'requestModel' => [
                    'method' => 'GET',
                    'uri' => 'https://httpbin.org/uuid',
                    'headers' => [
                        'User-Agent' => [
                            'GuzzleHttp/7',
                        ],
                        'Host' => [
                            'httpbin.org',
                        ],
                    ],
                    'body' => '',
                    'version' => '1.1',
                ],
                'responseModel' => [
                    'status' => 200,
                    'headers' => [
                        'Date' => [
                            'Wed, 07 May 2025 09:16:13 GMT',
                        ],
                        'Content-Type' => [
                            'application/json',
                        ],
                        'Content-Length' => [
                            '53',
                        ],
                        'Connection' => [
                            'keep-alive',
                        ],
                        'Server' => [
                            'gunicorn/19.9.0',
                        ],
                        'Access-Control-Allow-Origin' => [
                            '*',
                        ],
                        'Access-Control-Allow-Credentials' => [
                            'true',
                        ],
                    ],
                    'body' => '{'."\n"
                        .'  "uuid": "ea175c37-df2a-42aa-b9b8-c3eac8dfb80b"'."\n"
                        .'}'."\n",
                    'version' => '1.1',
                    'reason' => 'OK',
                    'decodedBody' => [
                        'uuid' => 'ea175c37-df2a-42aa-b9b8-c3eac8dfb80b',
                    ],
                ],
            ],
        ],
    ]
);

use Illuminate\Support\Facades\Http;
use Holgerk\GuzzleReplay\GuzzleReplay;
use Holgerk\GuzzleReplay\Mode;
Http::globalMiddleware(GuzzleReplay::create(Mode::Replay));

use Holgerk\GuzzleReplay\MethodRecorder;
use Holgerk\GuzzleReplay\Options;
Options::$globalRecorderFactory = fn() => new MethodRecorder();

use Holgerk\GuzzleReplay\GuzzleReplay;
use Holgerk\GuzzleReplay\MethodRecorder;
use Holgerk\GuzzleReplay\Options;
$middleware = GuzzleReplay::create(
    Mode::Record, 
    Options::create()->setRecorder(new MethodRecorder())
);

$middleware = GuzzleReplay::create(Mode::Replay, Options::create()
    ->setRequestTransformer(static function (RequestModel $requestModel) {
        // mask authorization token, to not leak sensitive data
        $requestModel->replaceString($_ENV['GITHUB_TOKEN'], 'XXX');
        // or you can unset the header 
        //unset($requestModel->headers['Authorization']);
        //$requestModel->removeHeader('content-length');
    })
);

public static function dataProviderTestGetStatusCode(): array
{
    return [
        'data-set-1' => ['givenStatusCode' => 201],
        'data-set-2' => ['givenStatusCode' => 400],
    ];
}

/**
 * @dataProvider dataProviderTestGetStatusCode
 */
public function testGetStatusCode(int $givenStatusCode): void
{
    // GIVEN
     
    // append status code to testMethodName so we get distinct 
    // recordings foreach data-set.
    $options = Options::create();
    $options->recordName->testMethodName .= $givenStatusCode;
    
    $guzzleClient = new Client();
    $middleware = GuzzleReplay::create(GuzzleReplay::MODE_REPLAY, $options);
    $middleware->inject($guzzleClient);

    // WHEN
    $apiClient = new SimpleApiClient($guzzleClient);
    $responseStatusCode = $apiClient->getStatusCode($givenStatusCode);

    // THEN
    assertEquals($givenStatusCode, $responseStatusCode);
}