webignition / http-history-container
A container for Guzzle history middleware
Installs: 30 390
Dependents: 9
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^8.2
- ext-json: *
- guzzlehttp/psr7: ^2.1
- psr/http-message: ^1.0 || ^2.0
- psr/log: ^1.1 || ^2.0 || ^3.0
Requires (Dev)
- mockery/mockery: ^1.4
- monolog/monolog: ^2
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-mockery: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.1
- squizlabs/php_codesniffer: ^3.6
- webignition/object-reflector: ^1.0
README
Helping you more easily test what your Guzzle HTTP client has been up to.
A container for Guzzle history middleware offering smashingly-nice access to:
- collections of HTTP transactions (requests plus responses)
- requests made
- responses received
- request URLs
Oh also logging. Really useful when your Guzzle client under test is not executing within the same thread as your tests.
Usage
Basic Usage
<?php use GuzzleHttp\Client as HttpClient; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use webignition\HttpHistoryContainer\Container; $historyContainer = new Container(); $historyHandler = Middleware::history($historyContainer); $handlerStack = HandlerStack::create($historyHandler); $httpClient = new HttpClient(['handler' => $handlerStack]); /// ... things happen ... $historyContainer->getTransactions(); // an array of HttpTransaction $historyContainer->getRequests(); // a Collection\RequestCollection $historyContainer->getResponses(); // a Collection\ResponseCollection foreach ($historyContainer as $transaction) { // $transaction is a HttpTransaction $request = $transaction->getRequest(); // $request is a RequestInterface $response = $transaction->getResponse(); // $response is a ResponseInterface }
Details on Requests Sent
<?php use webignition\HttpHistoryContainer\Container; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; $historyContainer = new Container(); // ... create and use Guzzle client ... $transactions = $historyContainer->getTransactions(); // $transactions is a Collection\TransactionCollection $requests = $transactions->getRequests(); // $requests is a Collection\RequestCollection // RequestCollection is iterable foreach ($requests as $request) { // $request is a RequestInterface } $urls = $requests->getUrls(); // $urls is a Collection\UrlCollection // UrlCollection is iterable foreach ($urls as $url) { // $url is a UriInterface } foreach ($urls->getAsStrings() as $urlString) { // convenient access to requested URLs as strings }
Details on Responses Received
<?php use webignition\HttpHistoryContainer\Container; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; $historyContainer = new Container(); // ... create and use Guzzle client ... $transactions = $historyContainer->getTransactions(); // $transactions is a Collection\TransactionCollection $responses = $transactions->getResponses(); // $responses is a Collection\ResponseCollection // ResponseCollection is iterable foreach ($responses as $response) { // $response is a ResponseInterface }
Logging!
You may not be able to access your Guzzle history if the client under test is executing in a different thread from your tests.
A LoggableContainer
allows you to record transactions as they happen to whatever Psr\Log\LoggerInterface
instance you like.
The logging container wraps each HttpTransaction
in a LoggableTransaction
object which is
serialized to JSON and output as a debug message.
LoggableTransaction::fromJson()
lets you (in a somewhat slightly lossy manner) re-create
transactions object from logged messages.`
Logging Example
<?php use Monolog\Formatter\LineFormatter; use Monolog\Handler\StreamHandler; use Monolog\Logger; use webignition\HttpHistoryContainer\LoggableContainer; use webignition\HttpHistoryContainer\Transaction\LoggableTransaction; // Create the PSR logger $path = '/path/to/log'; $stream = fopen($path, 'w+'); $logger = new Logger(''); $logHandler = new StreamHandler($stream); $logHandler ->setFormatter(new LineFormatter('%message%' . "\n")); $logger->pushHandler($logHandler); // Create LoggableContainer $container = new LoggableContainer($logger); // ... create and use Guzzle client ... $logContent = file_get_contents($path); $logLines = array_filter(explode("\n", $logContent)); $loggedTransactions = []; foreach ($logLines as $logLine) { $loggedTransactions[] = LoggableTransaction::fromJson($logLine); } // $loggedTransactions is now an array of LoggableTransaction foreach ($loggedTransactions as $loggedTransaction) { $transaction = $loggedTransaction->getTransaction(); // $transaction is a HttpTransaction $request = $transaction->getRequest(); // $request is a RequestInterface $response = $transaction->getResponse(); // $response is a ResponseInterface }