@@ -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,16 @@ public function addException(\Exception $exception)
81
99
$ this ->exceptions [] = $ exception ;
82
100
}
83
101
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
+
84
112
/**
85
113
* Adds a response that will be returned.
86
114
*
@@ -91,6 +119,16 @@ public function addResponse(ResponseInterface $response)
91
119
$ this ->responses [] = $ response ;
92
120
}
93
121
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
+
94
132
/**
95
133
* Returns requests that were sent.
96
134
*
0 commit comments