Skip to content

Add conditional responses to mock client #27

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 1.2.1 - 2019-02-20

### Added

- Conditional mock functionality

## 1.2.0 - 2019-01-19

### Added
Expand Down
53 changes: 52 additions & 1 deletion spec/ClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Message\RequestMatcher;
use Http\Message\ResponseFactory;
use Http\Mock\Client;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -90,7 +91,8 @@ function it_reset(
RequestInterface $request,
ResponseInterface $response,
ResponseInterface $newResponse
) {
)
{
$this->addResponse($response);
$this->setDefaultResponse($response);
$this->addException(new \Exception());
Expand All @@ -106,4 +108,53 @@ function it_reset(

$this->getRequests()->shouldReturn([]);
}
function it_returns_response_if_request_matcher_matches(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $response
) {
$matcher->matches($request)->willReturn(true);
$this->on($matcher, $response);
$this->sendRequest($request)->shouldReturn($response);
}

function it_throws_exception_if_request_matcher_matches(
RequestMatcher $matcher,
RequestInterface $request
) {
$matcher->matches($request)->willReturn(true);
$this->on($matcher, new \Exception());
$this->shouldThrow('Exception')->duringSendRequest($request);
}

function it_skips_conditional_response_if_matcher_returns_false(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $expectedResponse,
ResponseInterface $skippedResponse
)
{
$matcher->matches($request)->willReturn(false);
$this->on($matcher, $skippedResponse);
$this->addResponse($expectedResponse);
$this->sendRequest($request)->shouldReturn($expectedResponse);
}

function it_calls_callable_with_request_as_argument_when_matcher_returns_true(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $response
)
{
$matcher->matches($request)->willReturn(true);

$this->on(
$matcher,
function(RequestInterface $request) use ($response) {
return $response->getWrappedObject();
}
);

$this->sendRequest($request)->shouldReturn($response);
}
}
60 changes: 60 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\RequestMatcher;
use Http\Message\ResponseFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -30,6 +31,11 @@ class Client implements HttpClient, HttpAsyncClient
*/
private $responseFactory;

/**
* @var array
*/
private $conditionalResults = [];

/**
* @var RequestInterface[]
*/
Expand Down Expand Up @@ -67,6 +73,22 @@ public function doSendRequest(RequestInterface $request)
{
$this->requests[] = $request;

foreach ($this->conditionalResults as $result) {
/**
* @var RequestMatcher
*/
$matcher = $result['matcher'];

/**
* @var callable
*/
$callable = $result['callable'];

if ($matcher->matches($request)) {
return $callable($request);
}
}

if (count($this->exceptions) > 0) {
throw array_shift($this->exceptions);
}
Expand All @@ -87,6 +109,44 @@ public function doSendRequest(RequestInterface $request)
return $this->responseFactory->createResponse();
}

/**
* Adds an exception to be thrown or response to be returned if the request matcher matches.
*
* For more complex logic, pass a callable which should take the request and return a response or exception
*
* @param RequestMatcher $requestMatcher
* @param ResponseInterface|\Exception|callable $result
*/
public function on(RequestMatcher $requestMatcher, $result)
{
$callable = null;

switch (true) {
case is_callable($result):
$callable = $result;

break;
case $result instanceof ResponseInterface:
$callable = function () use ($result) {
return $result;
};

break;
case $result instanceof \Exception:
$callable = function () use ($result) {
throw $result;
};

break;
default:
throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable');
}
$this->conditionalResults[] = [
'matcher' => $requestMatcher,
'callable' => $callable,
];
}

/**
* Adds an exception that will be thrown.
*/
Expand Down