webignition / http-history-container
A container for Guzzle history middleware
Installs: 81 516
Dependents: 9
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
pkg:composer/webignition/http-history-container
Requires
- php: ^8.4
- ext-json: *
- guzzlehttp/promises: ^2.3
- guzzlehttp/psr7: ^2.1
- psr/http-message: ^1.0 || ^2.0
- psr/log: ^1.1 || ^2.0 || ^3.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.10
- mockery/mockery: ^1.4
- monolog/monolog: ^3.9
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-mockery: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^12
- squizlabs/php_codesniffer: ^4.0
- 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.
LoggableTransactionFactory::createFromJson() 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\Factory\LoggableTransactionFactory;
// 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[] = LoggableTransactionFactory::createFromJson($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
}