12
12
use Psr \Http \Message \ResponseInterface ;
13
13
14
14
/**
15
- * HTTP client mock .
15
+ * An implementation of the HTTP client that is useful for automated tests .
16
16
*
17
- * This mock is most useful in tests. It does not send requests but stores them
18
- * for later retrieval. Additionally, you can set an exception to test
19
- * exception handling.
17
+ * This mock does not send requests but stores them for later retrieval.
18
+ * You can configure the mock with responses to return and/or exceptions to throw.
20
19
*
21
20
* @author David de Boer <[email protected] >
22
21
*/
@@ -54,9 +53,6 @@ class Client implements HttpClient, HttpAsyncClient
54
53
*/
55
54
private $ defaultException ;
56
55
57
- /**
58
- * @param ResponseFactory|null $responseFactory
59
- */
60
56
public function __construct (ResponseFactory $ responseFactory = null )
61
57
{
62
58
$ this ->responseFactory = $ responseFactory ?: MessageFactoryDiscovery::find ();
@@ -65,7 +61,7 @@ public function __construct(ResponseFactory $responseFactory = null)
65
61
/**
66
62
* {@inheritdoc}
67
63
*/
68
- public function sendRequest (RequestInterface $ request )
64
+ public function sendRequest (RequestInterface $ request ): ResponseInterface
69
65
{
70
66
$ this ->requests [] = $ request ;
71
67
@@ -91,8 +87,6 @@ public function sendRequest(RequestInterface $request)
91
87
92
88
/**
93
89
* Adds an exception that will be thrown.
94
- *
95
- * @param \Exception $exception
96
90
*/
97
91
public function addException (\Exception $ exception )
98
92
{
@@ -103,18 +97,14 @@ public function addException(\Exception $exception)
103
97
* Sets the default exception to throw when the list of added exceptions and responses is exhausted.
104
98
*
105
99
* If both a default exception and a default response are set, the exception will be thrown.
106
- *
107
- * @param \Exception|null $defaultException
108
100
*/
109
- public function setDefaultException (\Exception $ defaultException = null )
101
+ public function setDefaultException (? \Exception $ defaultException )
110
102
{
111
103
$ this ->defaultException = $ defaultException ;
112
104
}
113
105
114
106
/**
115
- * Adds a response that will be returned.
116
- *
117
- * @param ResponseInterface $response
107
+ * Adds a response that will be returned in first in first out order.
118
108
*/
119
109
public function addResponse (ResponseInterface $ response )
120
110
{
@@ -123,10 +113,8 @@ public function addResponse(ResponseInterface $response)
123
113
124
114
/**
125
115
* Sets the default response to be returned when the list of added exceptions and responses is exhausted.
126
- *
127
- * @param ResponseInterface|null $defaultResponse
128
116
*/
129
- public function setDefaultResponse (ResponseInterface $ defaultResponse = null )
117
+ public function setDefaultResponse (? ResponseInterface $ defaultResponse )
130
118
{
131
119
$ this ->defaultResponse = $ defaultResponse ;
132
120
}
@@ -136,16 +124,13 @@ public function setDefaultResponse(ResponseInterface $defaultResponse = null)
136
124
*
137
125
* @return RequestInterface[]
138
126
*/
139
- public function getRequests ()
127
+ public function getRequests (): array
140
128
{
141
129
return $ this ->requests ;
142
130
}
143
131
144
- /**
145
- * @return RequestInterface|false
146
- */
147
- public function getLastRequest ()
132
+ public function getLastRequest (): ?RequestInterface
148
133
{
149
- return end ($ this ->requests );
134
+ return end ($ this ->requests ) ?: null ;
150
135
}
151
136
}
0 commit comments