Skip to content

Commit

Permalink
MockHandler - Add support to reset internal queue (#2143)
Browse files Browse the repository at this point in the history
  • Loading branch information
ingluisjimenez authored and Nyholm committed Oct 23, 2019
1 parent 33bee79 commit 6a94646
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
15 changes: 12 additions & 3 deletions docs/testing.rst
Expand Up @@ -50,10 +50,19 @@ 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 @@ -71,9 +80,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 @@ -235,4 +235,16 @@ public function testTransferTime()
$mock($request, [ 'on_stats' => $onStats, 'transfer_time' => 0.4 ])->wait(false);
$this->assertEquals(0.4, $stats->getTransferTime());
}

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 6a94646

Please sign in to comment.