Skip to content

Commit

Permalink
MockHandler - Add support to reset internal queue
Browse files Browse the repository at this point in the history
  • Loading branch information
ingluisjimenez committed Sep 14, 2018
1 parent c7faf23 commit 9e2dde8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
16 changes: 13 additions & 3 deletions docs/testing.rst
Expand Up @@ -47,10 +47,20 @@ a response or exception by shifting return values off of a queue.
echo $client->request('GET', '/')->getStatusCode();
//> 202
// Reset the queue and queue up a new response
$mock->reset();
$mock->append(new Response(201));
// As the mock was reset, the new response is the 201 CREATED,
// instead of the previously queued RequestException
echo $client->request('GET', '/')->getStatusCode();
//> 201
When no more responses are in the queue and a request is sent, an
``OutOfBoundsException`` is thrown.


History Middleware
==================

Expand All @@ -68,9 +78,9 @@ history of the requests that were sent by a client.
$container = [];
$history = Middleware::history($container);
$handlerStack = HandlerStack::create();
$handlerStack = HandlerStack::create();
// or $handlerStack = HandlerStack::create($mock); if using the Mock handler.
// Add the history middleware to the handler stack.
$handlerStack->push($history);
Expand Down
5 changes: 5 additions & 0 deletions src/Handler/MockHandler.php
Expand Up @@ -175,6 +175,11 @@ public function count()
return count($this->queue);
}

public function reset()
{
$this->queue = [];
}

private function invokeStats(
RequestInterface $request,
array $options,
Expand Down
12 changes: 12 additions & 0 deletions tests/Handler/MockHandlerTest.php
Expand Up @@ -221,4 +221,16 @@ public function testInvokesOnStatsFunctionForError()
$this->assertNull($stats->getResponse());
$this->assertSame($request, $stats->getRequest());
}

public function testResetQueue()
{
$mock = new MockHandler([new Response(200), new Response(204)]);
$this->assertCount(2, $mock);

$mock->reset();
$this->assertEmpty($mock);

$mock->append(new Response(500));
$this->assertCount(1, $mock);
}
}

0 comments on commit 9e2dde8

Please sign in to comment.