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

MockHandler - Add support to reset internal queue #2143

Merged
merged 1 commit into from Oct 23, 2019
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
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);
}
}