Download the PHP package lezhnev74/psr-recording-middleware without Composer
On this page you can find all versions of the php package lezhnev74/psr-recording-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lezhnev74/psr-recording-middleware
More information about lezhnev74/psr-recording-middleware
Files in lezhnev74/psr-recording-middleware
Package psr-recording-middleware
Short Description A recording middleware for any PSR-7 HTTP client that records every request/response exchange and can replay them, turning recorded traffic into a predictable fixture for integration tests. Built on PSR-7, PSR-17 and PSR-3.
License MIT
Informations about the package psr-recording-middleware
PSR-compatible Recording Middleware
A recording middleware for any PSR-7 HTTP client that records every request and response passing through it, and can switch to replay mode to serve the recorded responses back - turning real HTTP traffic into a predictable fixture for integration tests.
It is built on PSR-7 (messages), PSR-17 (message factories) and PSR-3 (logging), so it stays implementation-agnostic: it works with any PSR-7 implementation, none of which is a hard dependency. Guzzle is the primary test target, not a runtime dependency.
Requirements
- PHP 8.1 - 8.5
- Any PSR-7 / PSR-17 implementation installed in your app (e.g.
guzzlehttp/psr7ornyholm/psr7) - discovered automatically via php-http/discovery.
Installation
Usage
Configure a run, open a named session, and attach it to your client - via the Guzzle-style handler middleware or the PSR-18 decorator:
Full working examples live in the tests:
tests/KnownHostRecordReplayTest.php
(end-to-end record/replay with a real Guzzle client),
tests/HandlerMiddlewareTest.php and
tests/RecordingClientTest.php.
PHPUnit integration
Drive the mode from an env var, keep the fixture store in a committed
directory, and give each test its own session. Tests then need no
record/replay branching - run once with HTTP_RECORD=1 against the real
network, commit tests/fixtures/http/, and every ordinary run replays
offline. In your base TestCase:
Attach the session to the client your code under test uses (handler stack or
RecordingClient, as above). For integration tests, do it at the DI seam,
e.g. in Laravel: $this->app->extend(ClientInterface::class, fn ($inner) => new RecordingClient($inner, $this->httpSession())).
To re-prove a single test still works against the live network without
re-recording the whole suite, flip only that test's session:
$this->httpSession()->setMode(Mode::Record).
Masking volatile fields
Replay matches requests byte-for-byte, so any field that changes between runs
(rotating tokens, a default User-Agent embedding PHP/Guzzle versions) breaks
committed fixtures - and secrets must not land in git. Mask them: masking
applies identically when recording and when matching, so a masked field is
stored as ***, never mismatches, and never leaks.
The full runnable pattern - env-driven mode, session per test, masking
proven against changed tokens and User-Agent - is
tests/PhpUnitIntegrationExampleTest.php.
Committing recordings to git
Recordings capture raw request/response bytes, including binary bodies. Tell git
to treat the store as binary so it never normalizes line endings or mangles a
payload, and to keep the noisy diffs out of code review. Add a .gitattributes
next to your fixture store:
binary implies -text -diff, so recordings are stored verbatim and shown as
Binary files differ rather than a line-by-line diff.
Local development
All QA tooling runs inside a PHP 8.5 Docker container (with Xdebug) via the
dev wrapper script - no PHP is needed on the host. Each command tunnels to the
matching Composer script inside the container, so
./dev <script> is just composer <script> run in Docker.
Anything unrecognized is passed to composer, so ./dev require <pkg>,
./dev stan, ./dev rector etc. all work. Use ./dev php ... for a raw PHP
call, ./dev sh for a shell, and ./dev compose ... for raw docker compose.
Contributing
A few conventions, kept light:
- TDD. Every new branch needs a covering test; the recording/replay suite must stay green against both Guzzle and Nyholm PSR-7 impls.
- Conventional Commits (spec) for
messages, e.g.
feat(recording): ...,fix: ...,test: .... - Semantic versioning. Tags are
vMAJOR.MINOR.PATCH; the commit type drives the bump (fix→ patch,feat→ minor, a!/BREAKING CHANGE→ major). - Run
./dev check(style + static analysis + tests) before opening a PR.
License
MIT - see LICENSE.
All versions of psr-recording-middleware with dependencies
lezhnev74/psr-logging-masking-middleware Version ^1.1
php-http/discovery Version ^1.19
psr/http-client Version ^1.0
psr/http-factory Version ^1.0
psr/http-message Version ^1.1 || ^2.0
psr/log Version ^2.0 || ^3.0