smile / httplug-record-and-replay-plugin
Taping plugin for HTTPlug
Requires
- php: ^7.0
- php-http/cache-plugin: ^1.6
- php-http/discovery: ^1.6
- php-http/httplug: ^2.0
- php-http/message: ^1.7
- psr/http-factory: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- cache/array-adapter: ^1.0
- http-interop/http-factory-guzzle: ^1.0
- php-http/client-common: 2.0.0-RC1
- php-http/mock-client: ^1.2
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-10-16 22:48:56 UTC
README
Achieve isolated test-suites and predictable HTTP communications.
Install
Via Composer
$ composer require --prefer-stable smile/httplug-record-and-replay-plugin
Usage
Vanilla PHP
Run the following lines with the environment variable HTTPLUG_RECORDING=1
:
/** @var Http\Client\HttpClient $client */ $client = new Client(); /** * You have to provide concrete instances for the following parameters : * @var Psr\SimpleCache\CacheInterface $cachePool * @var Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator $cacheKeyGenerator * @var Psr\Http\Message\StreamInterface\StreamFactory $streamFactory */ $plugin = new RecordAndReplayPlugin( $cachePool, $cacheKeyGenerator, $streamFactory, getenv('HTTPLUG_RECORDING') ); /** @var Http\Client\Common\PluginClient $client */ $client = new PluginClient($client, [$plugin]); $request = Psr17FactoryDiscovery::findRequestFactory()->createRequest('GET', 'https://api.somewhere/some_endpoint'); $client->sendRequest($request);
If you then run this lines again, but with HTTPLUG_RECORDING=0
, the plugin will replay the recorded communications without actually calling the remote service.
HTTPlug Bundle for Symfony
Declare the plugin (and its wanted PSR-16 storage) :
# config/services.yaml services: _defaults: autowire: true public: false Smile\HTTPlugRecordAndReplayPlugin\RecordAndReplayPlugin: arguments: $cachePool: '@app.simple_cache.httplug_records' $isRecording: true app.simple_cache.httplug_records: class: Symfony\Component\Cache\Simple\FilesystemCache arguments: $directory: '%kernel.project_dir%/tests/httplug_records'
Plug it to your client(s) :
# config/packages/test/httplug.yaml httplug: clients: default: plugins: - 'Smile\HTTPlugRecordAndReplayPlugin\RecordAndReplayPlugin'
You can now run your test-suite and you should see the tests/httplug_records
folder being filled with cache files representing your records.
Once the test-suite is green, you can remove the $isRecording
line from your service definition and commit all the records along with the updated configuration and composer files.
Later on, when adding other behaviors based on third-party requests, you can switch back to the record mode (by putting back the $isRecording: true
in the plugin service definition) and run only the new tests in order to avoid rewriting all your records.
Autowiring troubleshooting
Depending on the dependencies versions used on your application, you may have to declare some additional services :
# config/services.yaml services: _defaults: autowire: true public: false # PSR-17 and PSR-18 autowiring compat Psr\Http\Client\ClientInterface: '@Http\Client\HttpClient' Psr\Http\Message\RequestFactoryInterface: '@Http\Factory\Guzzle\RequestFactory' Http\Factory\Guzzle\RequestFactory: ~ # HTTPlug record/replay plugin & records storage Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator: class: Http\Client\Common\Plugin\Cache\Generator\SimpleGenerator
Contributing and testing
$ composer update --prefer-lowest $ ./vendor/bin/phpunit
Please maintain the test-suite : if you add a feature, prove the new behavior; if you fix a bug, ensure the non-regression.
License
The MIT License (MIT). Please see License File for more information.