From e8d1e3c8c446f134a856df6a6b4a92a7be2be8dc Mon Sep 17 00:00:00 2001 From: Adam Quaile Date: Thu, 21 Feb 2019 21:49:51 +0000 Subject: [PATCH 1/2] Added documentation for conditional mocks --- clients/mock-client.rst | 82 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/clients/mock-client.rst b/clients/mock-client.rst index fb8e956..7f0253d 100644 --- a/clients/mock-client.rst +++ b/clients/mock-client.rst @@ -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'); + $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 + */ + public function testClientThrowsException() + { + $client = new Client(); + + // $requestMatcher is an instance of Http\Message\RequestMatcher + + $response = $this->createMock('Psr\Http\Message\ResponseInterface'); + $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:: From dfc8094008c834ef7063fea788158575e0e7ee1c Mon Sep 17 00:00:00 2001 From: Adam Quaile Date: Fri, 1 Mar 2019 19:31:38 +0000 Subject: [PATCH 2/2] Updated documentation in response to feedback --- clients/mock-client.rst | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/clients/mock-client.rst b/clients/mock-client.rst index 7f0253d..c4b9faf 100644 --- a/clients/mock-client.rst +++ b/clients/mock-client.rst @@ -149,6 +149,7 @@ or act differently depending on the request. To conditionally return a response when a request is matched:: use Http\Mock\Client; + use Psr\Http\Message\ResponseInterface; class YourTest extends \PHPUnit_Framework_TestCase { @@ -161,7 +162,7 @@ To conditionally return a response when a request is matched:: // $requestMatcher is an instance of Http\Message\RequestMatcher - $response = $this->createMock('Psr\Http\Message\ResponseInterface'); + $response = $this->createMock(ResponseInterface:class); $client->on($requestMatcher, $response); // $request is an instance of Psr\Http\Message\RequestInterface @@ -172,22 +173,22 @@ To conditionally return a response when a request is matched:: Or for an exception:: use Http\Mock\Client; + use Exception; 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'); - $client->on($requestMatcher, $response); + $exception = new \Exception('Whoops!'); + $client->on($requestMatcher, $exception); // $request is an instance of Psr\Http\Message\RequestInterface + + $this->expectException(\Exception::class); $returnedResponse = $client->sendRequest($request); } }