@@ -39,11 +39,21 @@ class Client implements HttpClient, HttpAsyncClient
39
39
*/
40
40
private $ responses = [];
41
41
42
+ /**
43
+ * @var ResponseInterface|null
44
+ */
45
+ private $ defaultResponse ;
46
+
42
47
/**
43
48
* @var Exception[]
44
49
*/
45
50
private $ exceptions = [];
46
51
52
+ /**
53
+ * @var Exception|null
54
+ */
55
+ private $ defaultException ;
56
+
47
57
/**
48
58
* @param ResponseFactory|null $responseFactory
49
59
*/
@@ -67,6 +77,14 @@ public function sendRequest(RequestInterface $request)
67
77
return array_shift ($ this ->responses );
68
78
}
69
79
80
+ if ($ this ->defaultException ) {
81
+ throw $ this ->defaultException ;
82
+ }
83
+
84
+ if ($ this ->defaultResponse ) {
85
+ return $ this ->defaultResponse ;
86
+ }
87
+
70
88
// Return success response by default
71
89
return $ this ->responseFactory ->createResponse ();
72
90
}
@@ -81,6 +99,18 @@ public function addException(\Exception $exception)
81
99
$ this ->exceptions [] = $ exception ;
82
100
}
83
101
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
+
84
114
/**
85
115
* Adds a response that will be returned.
86
116
*
@@ -91,6 +121,16 @@ public function addResponse(ResponseInterface $response)
91
121
$ this ->responses [] = $ response ;
92
122
}
93
123
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
+
94
134
/**
95
135
* Returns requests that were sent.
96
136
*
0 commit comments