Skip to content

Default response and exception for mock client #212

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

Merged
merged 1 commit into from
Nov 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions clients/mock-client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ To make assertions::
Fake Responses and Exceptions
-----------------------------

By default, the mock client returns an empty response with status 200.
You can set responses and exceptions the mock client should return / throw.
You can set several exceptions and responses, to have the client first throw
each exception once and then each response once on subsequent calls to send().
Additionally you can set a default response or a default exception to be used
instead of the empty response.

Test how your code behaves when the HTTP client throws exceptions or returns
certain responses::

Expand All @@ -63,6 +70,27 @@ certain responses::
}
}

Or set a default response::

use Http\Mock\Client;

class YourTest extends \PHPUnit_Framework_TestCase
{
public function testClientReturnsResponse()
{
$client = new Client();

$response = $this->getMock('Psr\Http\Message\ResponseInterface');
$client->setDefaultResponse($response);

// $firstRequest and $secondRequest are instances of Psr\Http\Message\RequestInterface
$firstReturnedResponse = $client->sendRequest($firstRequest);
$secondReturnedResponse = $client->sendRequest($secondRequest);
$this->assertSame($response, $firstReturnedResponse);
$this->assertSame($response, $secondReturnedResponse);
}
}

To fake an exception being thrown::

use Http\Mock\Client;
Expand All @@ -84,4 +112,31 @@ To fake an exception being thrown::
}
}

Or set a default exception::

use Http\Mock\Client;

class YourTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Exception
*/
public function testClientThrowsException()
{
$client = new Client();

$exception = new \Exception('Whoops!');
$client->setDefaultException($exception);

$response = $this->getMock('Psr\Http\Message\ResponseInterface');
$client->addResponse($response);

// $firstRequest and $secondRequest are instances of Psr\Http\Message\RequestInterface
// The first request will returns the added response.
$firstReturnedResponse = $client->sendRequest($firstRequest);
// There is no more added response, the default exception will be thrown.
$secondReturnedResponse = $client->sendRequest($secondRequest);
}
}

.. include:: includes/further-reading-async.inc