Skip to content

Added documentation for conditional mocks #256

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 2 commits into from
Mar 2, 2019
Merged
Changes from 1 commit
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
82 changes: 82 additions & 0 deletions clients/mock-client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,88 @@ Or set a default exception::
}
}

Mocking request-dependent responses
-----------------------------------

You can mock responses and exceptions which are only used in certain requests
or act differently depending on the request.

To conditionally return a response when a request is matched::

use Http\Mock\Client;

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

// $requestMatcher is an instance of Http\Message\RequestMatcher

$response = $this->createMock('Psr\Http\Message\ResponseInterface');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use use statements and the ::class pseudo constant (for the code, the comment about $requestMatcher is fine.

$client->on($requestMatcher, $response);

// $request is an instance of Psr\Http\Message\RequestInterface
$returnedResponse = $client->sendRequest($request);
}
}

Or for an exception::

use Http\Mock\Client;

class YourTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Exception
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd prefer dock to follow best practices and do $this->expectException(\Exception::class); in the code, on the line before sending the request.

*/
public function testClientThrowsException()
{
$client = new Client();

// $requestMatcher is an instance of Http\Message\RequestMatcher

$response = $this->createMock('Psr\Http\Message\ResponseInterface');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this not be an exception for the test to behave as expected?

$client->on($requestMatcher, $response);

// $request is an instance of Psr\Http\Message\RequestInterface
$returnedResponse = $client->sendRequest($request);
}
}

Or pass a callable, and return a response or exception based on the request::

use Http\Mock\Client;
use Psr\Http\Message\RequestInterface;

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

// $requestMatcher is an instance of Http\Message\RequestMatcher

$client->on($requestMatcher, function (RequestInterface $request) {

// return a response
return $this->createMock('Psr\Http\Message\ResponseInterface');

// or an exception
return new \Exception('Whoops!');
});

// $request is an instance of Psr\Http\Message\RequestInterface
$returnedResponse = $client->sendRequest($request);
}
}


.. hint::

Expand Down