1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rezzza\RestApiBehatExtension\Rest; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\Discovery\HttpClientDiscovery; |
7
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Tolerance\Operation\Callback; |
11
|
|
|
use Tolerance\Operation\Runner\RetryOperationRunner; |
12
|
|
|
use Tolerance\Operation\Runner\CallbackOperationRunner; |
13
|
|
|
use Tolerance\Waiter\SleepWaiter; |
14
|
|
|
use Tolerance\Waiter\TimeOut; |
15
|
|
|
|
16
|
|
|
class RestApiBrowser |
17
|
|
|
{ |
18
|
|
|
/** @var HttpClient */ |
19
|
|
|
private $httpClient; |
20
|
|
|
|
21
|
|
|
/** @var RequestInterface */ |
22
|
|
|
private $request; |
23
|
|
|
|
24
|
|
|
/** @var ResponseInterface */ |
25
|
|
|
private $response; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
private $requestHeaders = []; |
29
|
|
|
|
30
|
|
|
/** @var ResponseStorage */ |
31
|
|
|
private $responseStorage; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
private $host; |
35
|
|
|
|
36
|
|
|
/** @var MessageFactoryDiscovery */ |
37
|
|
|
private $messageFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $host |
41
|
|
|
*/ |
42
|
|
|
public function __construct($host, HttpClient $httpClient = null) |
43
|
|
|
{ |
44
|
|
|
$this->host = $host; |
45
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
46
|
|
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Allow to override the httpClient to use yours with specific middleware for example. |
51
|
|
|
*/ |
52
|
|
|
public function useHttpClient(HttpClient $httpClient) |
53
|
|
|
{ |
54
|
|
|
$this->httpClient = $httpClient; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ResponseStorage $responseStorage |
59
|
|
|
*/ |
60
|
|
|
public function enableResponseStorage(ResponseStorage $responseStorage) |
61
|
|
|
{ |
62
|
|
|
$this->responseStorage = $responseStorage; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return ResponseInterface |
67
|
|
|
*/ |
68
|
|
|
public function getResponse() |
69
|
|
|
{ |
70
|
|
|
return $this->response; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ResponseInterface $response |
75
|
|
|
*/ |
76
|
|
|
public function setResponse(ResponseInterface $response) |
77
|
|
|
{ |
78
|
|
|
$this->response = $response; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getRequest() |
82
|
|
|
{ |
83
|
|
|
return $this->request; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getRequestHeaders() |
87
|
|
|
{ |
88
|
|
|
return $this->requestHeaders; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $method |
93
|
|
|
* @param string $uri |
94
|
|
|
* @param string|array $body |
95
|
|
|
*/ |
96
|
|
|
public function sendRequest($method, $uri, $body = null) |
97
|
|
|
{ |
98
|
|
|
if (false === $this->hasHost($uri)) { |
99
|
|
|
$uri = rtrim($this->host, '/').'/'.ltrim($uri, '/'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->request = $this->messageFactory->createRequest($method, $uri, $this->requestHeaders, $body); |
|
|
|
|
103
|
|
|
$this->response = $this->httpClient->sendRequest($this->request); |
104
|
|
|
$this->requestHeaders = []; |
105
|
|
|
|
106
|
|
|
if (null !== $this->responseStorage) { |
107
|
|
|
$this->responseStorage->writeRawContent((string) $this->response->getBody()); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function sendRequestUntil($method, $uri, $body, callable $assertion, $maxExecutionTime = 10) |
112
|
|
|
{ |
113
|
|
|
$runner = new RetryOperationRunner( |
114
|
|
|
new CallbackOperationRunner(), |
115
|
|
|
new TimeOut(new SleepWaiter(), $maxExecutionTime) |
116
|
|
|
); |
117
|
|
|
$restApiBrowser = $this; |
118
|
|
|
$runner->run(new Callback(function () use ($restApiBrowser, $method, $uri, $body, $assertion) { |
119
|
|
|
$restApiBrowser->sendRequest($method, $uri, $body); |
120
|
|
|
|
121
|
|
|
return $assertion(); |
122
|
|
|
})); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $name |
127
|
|
|
* @param string $value |
128
|
|
|
*/ |
129
|
|
|
public function setRequestHeader($name, $value) |
130
|
|
|
{ |
131
|
|
|
$this->removeRequestHeader($name); |
132
|
|
|
$this->addRequestHeader($name, $value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $name |
137
|
|
|
* @param string $value |
138
|
|
|
*/ |
139
|
|
|
public function addRequestHeader($name, $value) |
140
|
|
|
{ |
141
|
|
|
if (isset($this->requestHeaders[$name])) { |
142
|
|
|
$this->requestHeaders[$name] .= ', '.$value; |
143
|
|
|
} else { |
144
|
|
|
$this->requestHeaders[$name] = $value; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $headerName |
150
|
|
|
*/ |
151
|
|
|
private function removeRequestHeader($headerName) |
152
|
|
|
{ |
153
|
|
|
if (array_key_exists($headerName, $this->requestHeaders)) { |
154
|
|
|
unset($this->requestHeaders[$headerName]); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $uri |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
private function hasHost($uri) |
164
|
|
|
{ |
165
|
|
|
return strpos($uri, '://') !== false; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..