Skip to content

Implement default response and exception #21

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
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

### Added

- Default response functionality
- Default exception functionality

## 1.0.1 - 2017-05-02

Expand Down
14 changes: 14 additions & 0 deletions spec/ClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,27 @@ function it_returns_a_response_for_a_request(RequestInterface $request, Response
$this->sendRequest($request)->shouldReturn($response);
}

function it_returns_the_default_response_for_a_request(RequestInterface $request, ResponseInterface $response)
{
$this->setDefaultResponse($response);

$this->sendRequest($request)->shouldReturn($response);
}

function it_throws_an_exception_for_a_request(RequestInterface $request)
{
$this->addException(new \Exception());

$this->shouldThrow('Exception')->duringSendRequest($request);
}

function it_throws_the_default_exception_for_a_request(RequestInterface $request)
{
$this->setDefaultException(new \Exception());

$this->shouldThrow('Exception')->duringSendRequest($request);
}

function it_creates_an_empty_response_when_none_is_added(
RequestInterface $request,
ResponseFactory $responseFactory,
Expand Down
40 changes: 40 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,21 @@ class Client implements HttpClient, HttpAsyncClient
*/
private $responses = [];

/**
* @var ResponseInterface|null
*/
private $defaultResponse;

/**
* @var Exception[]
*/
private $exceptions = [];

/**
* @var Exception|null
*/
private $defaultException;

/**
* @param ResponseFactory|null $responseFactory
*/
Expand All @@ -67,6 +77,14 @@ public function sendRequest(RequestInterface $request)
return array_shift($this->responses);
}

if ($this->defaultException) {
throw $this->defaultException;
}

if ($this->defaultResponse) {
return $this->defaultResponse;
}

// Return success response by default
return $this->responseFactory->createResponse();
}
Expand All @@ -81,6 +99,18 @@ public function addException(\Exception $exception)
$this->exceptions[] = $exception;
}

/**
* Sets the default exception to throw when the list of added exceptions and responses is exhausted.
*
* If both a default exception and a default response are set, the exception will be thrown.
*
* @param \Exception|null $defaultException
*/
public function setDefaultException(\Exception $defaultException = null)
{
$this->defaultException = $defaultException;
}

/**
* Adds a response that will be returned.
*
Expand All @@ -91,6 +121,16 @@ public function addResponse(ResponseInterface $response)
$this->responses[] = $response;
}

/**
* Sets the default response to be returned when the list of added exceptions and responses is exhausted.
*
* @param ResponseInterface|null $defaultResponse
*/
public function setDefaultResponse(ResponseInterface $defaultResponse = null)
{
$this->defaultResponse = $defaultResponse;
}

/**
* Returns requests that were sent.
*
Expand Down