emanueleminotto/guzzle-snapshot

Guzzle Snapshots Middleware

1.1.0 2020-11-26 10:23 UTC

This package is auto-updated.

Last update: 2024-10-26 18:38:55 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Guzzle 6 & 7 middleware to store responses and requests and replay them in tests.

Structure

If any of the following are applicable to your project, then the directory structure should follow industry best practices by being named the following.

bin/        
config/
src/
tests/
vendor/

Install

Via Composer

$ composer require emanueleminotto/guzzle-snapshot

Usage

use EmanueleMinotto\GuzzleSnapshot\Strategy\ReadableFileStrategy;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
// use Doctrine\Common\Cache\ArrayCache;
// use EmanueleMinotto\GuzzleSnapshot\Strategy\DoctrineCacheStrategy;

// $strategy = new DoctrineCacheStrategy(new ArrayCache());
$strategy = new ReadableFileStrategy(__DIR__.'/__snapshots__');
$middleware = new SnapshotMiddleware($strategy);
// $middleware->setRequestStorage(true); // this will save requests too

$stack = HandlerStack::create();
$stack->push($middleware);

$client = new Client([
    'handler' => $this->stack,
]);

// if the response file was already stored in
// the directory __snapshots___, this request wont
// be sent to the remote endpoint
$client->get('http://www.example.com/');

// this request wont be sent in any case
$client->get('http://www.example.com/');

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email minottoemanuele@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.