-
Notifications
You must be signed in to change notification settings - Fork 56
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd prefer dock to follow best practices and do |
||
*/ | ||
public function testClientThrowsException() | ||
{ | ||
$client = new Client(); | ||
|
||
// $requestMatcher is an instance of Http\Message\RequestMatcher | ||
|
||
$response = $this->createMock('Psr\Http\Message\ResponseInterface'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:: | ||
|
||
|
There was a problem hiding this comment.
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.