@@ -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