PHPackages                             ikwattro/guzzle-stereo - 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. ikwattro/guzzle-stereo

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

ikwattro/guzzle-stereo
======================

Guzzle Recorder for recording Request/Responses and replay them back in a Mock

1.4.0(9y ago)782.6k91MITPHPPHP &gt;=5.6

Since Jul 27Pushed 9y ago6 watchersCompare

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

READMEChangelogDependencies (6)Versions (11)Used By (1)

Guzzle Stereo
=============

[](#guzzle-stereo)

**Record** and **Replay** HTTP Responses easily.

[![Build Status](https://camo.githubusercontent.com/42d0eb014a4a5c3f1407a0c0e42b2a3f4b0a3e9014d89eca5bf2df45750e5e2b/68747470733a2f2f7472617669732d63692e6f72672f696b77617474726f2f67757a7a6c652d73746572656f2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ikwattro/guzzle-stereo)

### Requirements

[](#requirements)

- PHP 5.6+
- Guzzle 6

Installation
------------

[](#installation)

Require the composer package :

```
composer require ikwattro/guzzle-stereo
```

NB: If you're using the Symfony Framework, you can take a look at the [GuzzleStereoBundle](https://github.com/estahn/guzzle-stereo-bundle) from @estahn.

Basics
------

[](#basics)

### Recorder

[](#recorder)

A recorder is the main object that knows everything about tapes and how to store them afterwards.

### Tapes

[](#tapes)

A tape will record `Response` objects. It has a name and can have `filters`. A filter can tell for example that only Responses with a 200 status code will be recorded in that tape.

Defining tapes and filters is done through a `yaml` file definition :

```
tapes:
	my_tape:
		filters:
			status_code: 200
```

This tape will record only success responses.

### Filters

[](#filters)

A filter is a `rule` telling if the `Response` object should be included or not. There is a set of built-in filters available with the library or you can create your own and register them.

NB: If you feel that your filter can be generic enough, do not hesitate to open a PullRequest

### Store directory

[](#store-directory)

In order to be replayed later (see the Replay section below), all tapes will then be dumped in json files to disk. You need to provide a writable directory.

### Player

[](#player)

A player is able to replay dumped json files as `Response` objects. A nice use case is to replay them with a `Mock Handler` in your test suites.

Usage
-----

[](#usage)

### Recording

[](#recording)

Instantiate the recorder by providing a writable store directory and the location of your tapes definitions file :

```
require_once(__DIR__.'/vendor/autoload.php');

use Ikwattro\GuzzleStereo\Recorder;

$recorder = new Recorder(__DIR__.'/var/records', __DIR__.'/stereo.yml');
```

Next, when creating your Guzzle client, you need to make him aware of the recorder. An easy way to do this is by using a Middleware available with the library :

```
$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(\Ikwattro\GuzzleStereo\RecorderMiddleware::record($recorder));

$client = new \GuzzleHttp\Client(['handler' => $stack]);
```

You can now make http requests with the Client as you would usually do, for e.g. here we'll call the Github events API 10 times :

```
for ($i = 0; $i < 10; $i++) {
	try {
		$client->get('https://api.github.com/events');
	} catch (RequestException $e) {
		// Do what you want
	}
}
```

Finally, you'll need to tell the recorder to dump the tapes to the disk :

```
$recorder->dump();
```

A file named `record_ + {tape_name}` will be created in the provided store directory containing the responses passing the filters :

### Replaying

[](#replaying)

In order to replay the recorded tapes, you can use the `Player`. The player is in fact creating a Mock Handler and will return you a `GuzzleHttp\Client` instance created with the MockHandler containing the responses from the tape file.

```
use Ikwattro\GuzzleStereo\Player;

$player = Player::replayFromTape('/path/to/tape.json');

$player->get('/');
// will return you the first response record present in the tape
```

---

Filters reference
-----------------

[](#filters-reference)

### StatusCode

[](#statuscode)

Include the `Response` only if it has the corresponding status code.

```
tapes:
	my_tape:
		filters:
			status_code: 200
```

### Non Empty Body

[](#non-empty-body)

Include the `Response` only if the body is not empty.

```
tapes:
	my_tape:
		filters:
			non_empty_body: ~
```

### Has Header

[](#has-header)

Include the `Response` only if she contains a header with the specified key.

```
tapes:
	my_tape:
		filters:
			has_header: "Content-Type"
```

Creating your own filters
-------------------------

[](#creating-your-own-filters)

Creating a filter is really easy. You need to create a filter class implementing `Ikwattro\GuzzleStereo\Filter\FilterInterface` and declaring the two methods :

- `public static function getName()`
- `public function isIncluded(ResponseInterface $response)`

The `getName` static function is responsible for defining the name of the filters in your tapes.

The `isIncluded` function will contain the logic determining if the received `Response` should be included or not in the Tape.

The following example filter will receive a response from the Github events api, and will record it only if the body contains an event done by the Github users you will pass as arguments when associating your filter to a tape.

```
