Skip to content

Commit 5ff102f

Browse files
committed
Implement default response and exception
1 parent 7b47440 commit 5ff102f

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-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: 38 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,16 @@ public function addException(\Exception $exception)
8199
$this->exceptions[] = $exception;
82100
}
83101

102+
/**
103+
* Sets the default exception to throw if none added.
104+
*
105+
* @param \Exception|null $defaultException
106+
*/
107+
public function setDefaultException(\Exception $defaultException = null)
108+
{
109+
$this->defaultException = $defaultException;
110+
}
111+
84112
/**
85113
* Adds a response that will be returned.
86114
*
@@ -91,6 +119,16 @@ public function addResponse(ResponseInterface $response)
91119
$this->responses[] = $response;
92120
}
93121

122+
/**
123+
* Sets the default response to be returned if none added.
124+
*
125+
* @param ResponseInterface|null $defaultResponse
126+
*/
127+
public function setDefaultResponse(ResponseInterface $defaultResponse = null)
128+
{
129+
$this->defaultResponse = $defaultResponse;
130+
}
131+
94132
/**
95133
* Returns requests that were sent.
96134
*

0 commit comments

Comments
 (0)