diff --git a/docs/testing.rst b/docs/testing.rst index 5ac06691b..6e5077c80 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -47,10 +47,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 ================== @@ -68,9 +77,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); diff --git a/src/Handler/MockHandler.php b/src/Handler/MockHandler.php index d892061c7..15d561980 100644 --- a/src/Handler/MockHandler.php +++ b/src/Handler/MockHandler.php @@ -175,6 +175,11 @@ public function count() return count($this->queue); } + public function reset() + { + $this->queue = []; + } + private function invokeStats( RequestInterface $request, array $options, diff --git a/tests/Handler/MockHandlerTest.php b/tests/Handler/MockHandlerTest.php index 94a5c4a20..22f5e292e 100644 --- a/tests/Handler/MockHandlerTest.php +++ b/tests/Handler/MockHandlerTest.php @@ -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); + } }