PHP code example of lezhnev74 / psr-recording-middleware

1. Go to this page and download the library: Download lezhnev74/psr-recording-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lezhnev74 / psr-recording-middleware example snippets


use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise\Create;
use Lezhnev74\PsrRecordingMiddleware\{HandlerMiddleware, Mode, RecordingClient, RecordingRun, RunConfig};

// The run's default mode seeds every session it opens.
$run = RecordingRun::fromConfig(RunConfig::create('/path/to/store', Mode::Replay));
$session = $run->openSession('example.com');       // inherits Replay

// Guzzle handler stack:
$stack = HandlerStack::create();
$stack->push(HandlerMiddleware::for($session, Create::promiseFor(...)));
$client = new Client(['handler' => $stack]);

// ...or wrap any PSR-18 client:
$client = new RecordingClient($psr18Client, $session);

// Replay (the default): requests are served from the store, network untouched.
// Requests are matched byte-for-byte, in recorded order; a mismatch throws.
$client->get('https://example.com/');

// Mode lives on the session, so one session can be flipped without touching
// the run's default or any sibling session - either seed it when opening:
$recording = $run->openSession('example.com', Mode::Record);
// ...or flip an already-open session (chains off openSession):
$run->openSession('example.com')->setMode(Mode::Record);
// A Record session hits the network and saves every turn to the store.

protected function httpSession(): Session
{
    $run = RecordingRun::fromConfig(RunConfig::create(
        __DIR__ . '/fixtures/http',
        getenv('HTTP_RECORD') !== false ? Mode::Record : Mode::Replay,
        $this->masker(), // see below
    ));

    // One fixture dir per test; session names may not contain "\".
    return $run->openSession(str_replace('\\', '.', static::class) . '.' . $this->name());
}

private function masker(): Masker
{
    return RecordingMasker::create(MaskingConfig::create(
        ['Authorization', 'User-Agent'], // headers
        ['token'],                       // query args
        ['password'],                    // JSON/form body fields
    ));
}