Skip to content

Commit 5edc24d

Browse files
Merge pull request #21 from Soullivaneuh/default-response-exception
Implement default response and exception
2 parents 7b47440 + 9490607 commit 5edc24d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
### Added
4+
5+
- Default response functionality
6+
- Default exception functionality
37

48
## 1.0.1 - 2017-05-02
59

spec/ClientSpec.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,27 @@ function it_returns_a_response_for_a_request(RequestInterface $request, Response
3636
$this->sendRequest($request)->shouldReturn($response);
3737
}
3838

39+
function it_returns_the_default_response_for_a_request(RequestInterface $request, ResponseInterface $response)
40+
{
41+
$this->setDefaultResponse($response);
42+
43+
$this->sendRequest($request)->shouldReturn($response);
44+
}
45+
3946
function it_throws_an_exception_for_a_request(RequestInterface $request)
4047
{
4148
$this->addException(new \Exception());
4249

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

53+
function it_throws_the_default_exception_for_a_request(RequestInterface $request)
54+
{
55+
$this->setDefaultException(new \Exception());
56+
57+
$this->shouldThrow('Exception')->duringSendRequest($request);
58+
}
59+
4660
function it_creates_an_empty_response_when_none_is_added(
4761
RequestInterface $request,
4862
ResponseFactory $responseFactory,

src/Client.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ class Client implements HttpClient, HttpAsyncClient
3939
*/
4040
private $responses = [];
4141

42+
/**
43+
* @var ResponseInterface|null
44+
*/
45+
private $defaultResponse;
46+
4247
/**
4348
* @var Exception[]
4449
*/
4550
private $exceptions = [];
4651

52+
/**
53+
* @var Exception|null
54+
*/
55+
private $defaultException;
56+
4757
/**
4858
* @param ResponseFactory|null $responseFactory
4959
*/
@@ -67,6 +77,14 @@ public function sendRequest(RequestInterface $request)
6777
return array_shift($this->responses);
6878
}
6979

80+
if ($this->defaultException) {
81+
throw $this->defaultException;
82+
}
83+
84+
if ($this->defaultResponse) {
85+
return $this->defaultResponse;
86+
}
87+
7088
// Return success response by default
7189
return $this->responseFactory->createResponse();
7290
}
@@ -81,6 +99,18 @@ public function addException(\Exception $exception)
8199
$this->exceptions[] = $exception;
82100
}
83101

102+
/**
103+
* Sets the default exception to throw when the list of added exceptions and responses is exhausted.
104+
*
105+
* If both a default exception and a default response are set, the exception will be thrown.
106+
*
107+
* @param \Exception|null $defaultException
108+
*/
109+
public function setDefaultException(\Exception $defaultException = null)
110+
{
111+
$this->defaultException = $defaultException;
112+
}
113+
84114
/**
85115
* Adds a response that will be returned.
86116
*
@@ -91,6 +121,16 @@ public function addResponse(ResponseInterface $response)
91121
$this->responses[] = $response;
92122
}
93123

124+
/**
125+
* Sets the default response to be returned when the list of added exceptions and responses is exhausted.
126+
*
127+
* @param ResponseInterface|null $defaultResponse
128+
*/
129+
public function setDefaultResponse(ResponseInterface $defaultResponse = null)
130+
{
131+
$this->defaultResponse = $defaultResponse;
132+
}
133+
94134
/**
95135
* Returns requests that were sent.
96136
*

0 commit comments

Comments
 (0)