Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible history containers #1373

Merged
merged 1 commit into from
Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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