Skip to content

Commit 1ed4303

Browse files
committedSep 29, 2022
Improve CS in FPM Tester Response
1 parent f3dba7e commit 1ed4303

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed
 

‎sapi/fpm/tests/response.inc

+55-34
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,26 @@ class Response
4343

4444
/**
4545
* @param string|array|null $data
46-
* @param bool $expectInvalid
46+
* @param bool $expectInvalid
4747
*/
4848
public function __construct($data = null, $expectInvalid = false)
4949
{
50-
if (!is_array($data)) {
50+
if ( ! is_array($data)) {
5151
$data = [
52-
'response' => $data,
52+
'response' => $data,
5353
'err_response' => null,
5454
'out_response' => $data,
5555
];
5656
}
5757

58-
$this->data = $data;
58+
$this->data = $data;
5959
$this->expectInvalid = $expectInvalid;
6060
}
6161

6262
/**
63-
* @param mixed $body
63+
* @param mixed $body
6464
* @param string $contentType
65+
*
6566
* @return Response
6667
*/
6768
public function expectBody($body, $contentType = 'text/html')
@@ -99,22 +100,28 @@ class Response
99100
}
100101

101102
/**
102-
* @param string $name
103-
* @param string $value
103+
* Expect header in the response.
104+
*
105+
* @param string $name Header name.
106+
* @param string $value Header value.
107+
*
104108
* @return Response
105109
*/
106-
public function expectHeader($name, $value)
110+
public function expectHeader($name, $value): Response
107111
{
108112
$this->checkHeader($name, $value);
109113

110114
return $this;
111115
}
112116

113117
/**
114-
* @param string|null $errorMessage
118+
* Expect error in the response.
119+
*
120+
* @param string|null $errorMessage Expected error message.
121+
*
115122
* @return Response
116123
*/
117-
public function expectError($errorMessage)
124+
public function expectError($errorMessage): Response
118125
{
119126
$errorData = $this->getErrorData();
120127
if ($errorData !== $errorMessage) {
@@ -128,19 +135,23 @@ class Response
128135
}
129136

130137
/**
131-
* @param string $errorMessage
138+
* Expect no error in the response.
139+
*
132140
* @return Response
133141
*/
134-
public function expectNoError()
142+
public function expectNoError(): Response
135143
{
136144
return $this->expectError(null);
137145
}
138146

139147
/**
140-
* @param string $contentType
148+
* Get response body.
149+
*
150+
* @param string $contentType Expect body to have specified content type.
151+
*
141152
* @return string|null
142153
*/
143-
public function getBody($contentType = 'text/html')
154+
public function getBody(string $contentType = 'text/html'): ?string
144155
{
145156
if ($this->checkIfValid() && $this->checkDefaultHeaders($contentType)) {
146157
return $this->rawBody;
@@ -150,15 +161,15 @@ class Response
150161
}
151162

152163
/**
153-
* Print raw body
164+
* Print raw body.
154165
*/
155166
public function dumpBody()
156167
{
157168
var_dump($this->getBody());
158169
}
159170

160171
/**
161-
* Print raw body
172+
* Print raw body.
162173
*/
163174
public function printBody()
164175
{
@@ -181,7 +192,7 @@ class Response
181192
/**
182193
* @return string|null
183194
*/
184-
public function getErrorData()
195+
public function getErrorData(): ?string
185196
{
186197
return $this->data['err_response'];
187198
}
@@ -191,24 +202,27 @@ class Response
191202
*
192203
* @return bool
193204
*/
194-
private function checkIfValid()
205+
private function checkIfValid(): bool
195206
{
196207
if ($this->isValid()) {
197208
return true;
198209
}
199210

200-
if (!$this->expectInvalid) {
211+
if ( ! $this->expectInvalid) {
201212
$this->error("The response is invalid: $this->rawData");
202213
}
203214

204215
return false;
205216
}
206217

207218
/**
219+
* Check default headers that should be present.
220+
*
208221
* @param string $contentType
222+
*
209223
* @return bool
210224
*/
211-
private function checkDefaultHeaders($contentType)
225+
private function checkDefaultHeaders($contentType): bool
212226
{
213227
// check default headers
214228
return (
@@ -218,40 +232,46 @@ class Response
218232
}
219233

220234
/**
221-
* @param string $name
222-
* @param string $value
223-
* @param bool $useRegex
235+
* Check a specified header.
236+
*
237+
* @param string $name Header name.
238+
* @param string $value Header value.
239+
* @param bool $useRegex Whether value is regular expression.
240+
*
224241
* @return bool
225242
*/
226-
private function checkHeader(string $name, string $value, $useRegex = false)
243+
private function checkHeader(string $name, string $value, $useRegex = false): bool
227244
{
228-
$lcName = strtolower($name);
245+
$lcName = strtolower($name);
229246
$headers = $this->getHeaders();
230-
if (!isset($headers[$lcName])) {
247+
if ( ! isset($headers[$lcName])) {
231248
return $this->error("The header $name is not present");
232249
}
233250
$header = $headers[$lcName];
234251

235-
if (!$useRegex) {
252+
if ( ! $useRegex) {
236253
if ($header === $value) {
237254
return true;
238255
}
256+
239257
return $this->error("The header $name value '$header' is not the same as '$value'");
240258
}
241259

242-
if (!preg_match($value, $header)) {
260+
if ( ! preg_match($value, $header)) {
243261
return $this->error("The header $name value '$header' does not match RegExp '$value'");
244262
}
245263

246264
return true;
247265
}
248266

249267
/**
268+
* Get all headers.
269+
*
250270
* @return array|null
251271
*/
252-
private function getHeaders()
272+
private function getHeaders(): ?array
253273
{
254-
if (!$this->isValid()) {
274+
if ( ! $this->isValid()) {
255275
return null;
256276
}
257277

@@ -260,7 +280,7 @@ class Response
260280
}
261281

262282
$headerRows = explode("\r\n", $this->rawHeaders);
263-
$headers = [];
283+
$headers = [];
264284
foreach ($headerRows as $headerRow) {
265285
$colonPosition = strpos($headerRow, ':');
266286
if ($colonPosition === false) {
@@ -292,8 +312,8 @@ class Response
292312
private function processData()
293313
{
294314
$this->rawData = $this->data['out_response'];
295-
$this->valid = (
296-
!is_null($this->rawData) &&
315+
$this->valid = (
316+
! is_null($this->rawData) &&
297317
strpos($this->rawData, self::HEADER_SEPARATOR)
298318
);
299319
if ($this->valid) {
@@ -308,9 +328,10 @@ class Response
308328
* Emit error message
309329
*
310330
* @param string $message
331+
*
311332
* @return bool
312333
*/
313-
private function error($message)
334+
private function error($message): bool
314335
{
315336
echo "ERROR: $message\n";
316337

0 commit comments

Comments
 (0)