smile/httplug-record-and-replay-plugin

dev-master 2019-02-04 19:51 UTC

This package is auto-updated.

Last update: 2024-12-16 23:13:50 UTC


README

Latest Version Software License Build Status Code Coverage Quality Score

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.