php-http / client-common

Common HTTP Client implementations and tools for HTTPlug
http://httplug.io
MIT License
1.03k stars 54 forks source link

[Plugin] Add a plugin to record and replay responses #172

Closed GaryPEGEOT closed 5 years ago

GaryPEGEOT commented 5 years ago
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets
Documentation TODO
License MIT

What's in this PR?

Add a VCR like plugin to allow storing and replaying response

Why?

Allow user to do functional tests why production-like data and provide same functionality as CsaGuzzleBundle

Example Usage

<?php
//...
/** @var \Http\Client\Common\Plugin\NamingStrategyInterface $strategy */
$strategy = new MyNamingStrategy()
$plugin = new RecordAndReplayPlugin($strategy, 'some/directory/in/vcs');

// Will perform an HTTP request
$res = $plugin->handleRequest($request, $next, $first)->wait();

// Will be fetch from the FS
$mock = $plugin->handleRequest($request, $next, $first)->wait();

assert($res === $mock);

Checklist

To Do

I sadly have no idea how to write PHPSpec tests, so I writed it with PHPUnit, sorry about that...

dbu commented 5 years ago

cool, this is interesting. and nice to see how simple it is. when comparing to https://github.com/php-vcr/php-vcr/, i guess "cassettes" would be the realm of the naming strategy (and could be added later, having a simple naming strategy would be enough to get started imho)

the one thing that i would want to have is to have control over whether recording is ok or not. maybe we could split the plugins into RecordPlugin and ReplayPlugin? or rather have a flag to the plugin to tell it how to behave? separate plugins can be used in own code, and in bundle config the DI could decide which plugin to register.

the problem when you don't have that is that you don't notice when you have missing fixtures and the CI will keep doing actual requests.

GaryPEGEOT commented 5 years ago

Closing as it is duplicated.