diff --git a/CHANGELOG.md b/CHANGELOG.md index fbf5d8c..fdf3742 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +### Added + +- Default response functionality +- Default exception functionality ## 1.0.1 - 2017-05-02 diff --git a/spec/ClientSpec.php b/spec/ClientSpec.php index a6ad71d..77c6390 100644 --- a/spec/ClientSpec.php +++ b/spec/ClientSpec.php @@ -36,6 +36,13 @@ 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()); @@ -43,6 +50,13 @@ function it_throws_an_exception_for_a_request(RequestInterface $request) $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, diff --git a/src/Client.php b/src/Client.php index 090e2bc..57309f5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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 */ @@ -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(); } @@ -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. * @@ -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. *