PHPackages                             holgerk/guzzle-replay - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. holgerk/guzzle-replay

ActiveLibrary[Testing &amp; Quality](/categories/testing)

holgerk/guzzle-replay
=====================

A replay middleware for easier testing guzzle clients

v1.1.0(1y ago)03.7k↓45.2%MITPHPCI passing

Since Jul 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/holgerk/guzzle-replay)[ Packagist](https://packagist.org/packages/holgerk/guzzle-replay)[ RSS](/packages/holgerk-guzzle-replay/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (4)Dependencies (12)Versions (5)Used By (0)

guzzle-replay middleware
========================

[](#guzzle-replay-middleware)

[![GitHub Release](https://camo.githubusercontent.com/0262123e829836e59b36d34a316c23fe481809b06a2ab687390d98b03980912e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f686f6c6765726b2f67757a7a6c652d7265706c6179)](https://camo.githubusercontent.com/0262123e829836e59b36d34a316c23fe481809b06a2ab687390d98b03980912e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f686f6c6765726b2f67757a7a6c652d7265706c6179)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/68df37031e1c4524b0a382890fde66107d97f4420624eaf8d768670e37173cd1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f686f6c6765726b2f67757a7a6c652d7265706c61792f74657374732e796d6c)](https://camo.githubusercontent.com/68df37031e1c4524b0a382890fde66107d97f4420624eaf8d768670e37173cd1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f686f6c6765726b2f67757a7a6c652d7265706c61792f74657374732e796d6c)[![Packagist Downloads](https://camo.githubusercontent.com/b3bf24552ce9ee583048fee37e08394814f758da5ac05068aa7da4afba39fe52/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f6c6765726b2f67757a7a6c652d7265706c6179)](https://camo.githubusercontent.com/b3bf24552ce9ee583048fee37e08394814f758da5ac05068aa7da4afba39fe52/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f6c6765726b2f67757a7a6c652d7265706c6179)

Record guzzle requests and have them replayed during next runs. This helps creating tests for http api clients.

Install
-------

[](#install)

```
composer require holgerk/guzzle-replay --dev

```

Usage
-----

[](#usage)

```
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...
```

Example
-------

[](#example)

SimpleApiClient.php```
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'];
    }
}
```

SimpleApiClientTest.php```
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);
    }
}
```

The following file is generated on first recording and updated on following recordings. (for this you need to create the middleware with record mode: `GuzzleReplay::create(GuzzleReplay::MODE_RECORD)`) It contains all responses and requests that happen during the recording. The name of the file is composed of test class name, test method name and the suffix: "\_guzzleRecording.php". If you don't like to have the recordings in separate files you can opt-out (see: Recording to method and not to a file)

SimpleApiClientTest\_testGetUuid\_guzzleRecording.php```
