Skip to content

Commit

Permalink
Merge pull request #1373 from guzzle/flexible-history-container
Browse files Browse the repository at this point in the history
More flexible history containers
  • Loading branch information
mtdowling committed Jan 22, 2016
2 parents 789bd23 + 365ca94 commit 5741733
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ function (ResponseInterface $response) use ($request, $handler) {
* @param array $container Container to hold the history (by reference).
*
* @return callable Returns a function that accepts the next handler.
* @throws \InvalidArgumentException if container is not an array or ArrayAccess.
*/
public static function history(array &$container)
public static function history(&$container)
{
if (!is_array($container) && !$container instanceof \ArrayAccess) {
throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess');
}

return function (callable $handler) use (&$container) {
return function ($request, array $options) use ($handler, &$container) {
return $handler($request, $options)->then(
Expand Down
14 changes: 12 additions & 2 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ public function testThrowsExceptionOnHttpServerError()
$this->assertEquals('rejected', $p->getState());
}

public function testTracksHistory()
/**
* @dataProvider getHistoryUseCases
*/
public function testTracksHistory($container)
{
$container = [];
$m = Middleware::history($container);
$h = new MockHandler([new Response(200), new Response(201)]);
$f = $m($h);
Expand All @@ -88,6 +90,14 @@ public function testTracksHistory()
$this->assertEquals('baz', $container[1]['options']['headers']['foo']);
}

public function getHistoryUseCases()
{
return [
[[]], // 1. Container is an array
[new \ArrayObject()] // 2. Container is an ArrayObject
];
}

public function testTracksHistoryForFailures()
{
$container = [];
Expand Down

0 comments on commit 5741733

Please sign in to comment.