Skip to content

Commit db313d6

Browse files
author
Jerome Thayananthajothy
committed
Refactor FetchTest and ResponseTest to improve structure and coverage
- Updated FetchTest to utilize Guzzle's MockHandler for better request simulation. - Added tests for various HTTP methods, including GET, POST, PUT, DELETE, and PATCH. - Implemented checks for handling query parameters, form parameters, and custom headers. - Enhanced error handling tests for request exceptions and response validation. - Refactored ResponseTest to use PHPUnit's TestCase for improved organization. - Added comprehensive tests for JSON and XML decoding, status code checkers, and header accessors. - Improved array access and blob handling in Response class tests.
1 parent 8cdf3f9 commit db313d6

File tree

9 files changed

+1700
-676
lines changed

9 files changed

+1700
-676
lines changed

src/Fetch/Http/ClientHandler.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ClientHandler implements ClientHandlerInterface
3636
* Default options for the request.
3737
*/
3838
protected static array $defaultOptions = [
39-
'method' => 'GET',
39+
'method' => 'GET',
4040
'headers' => [],
4141
'timeout' => self::DEFAULT_TIMEOUT,
4242
];
@@ -186,14 +186,6 @@ protected function isRetryableError(RequestException $e): bool
186186
$e->getPrevious() instanceof \GuzzleHttp\Exception\ConnectException;
187187
}
188188

189-
/**
190-
* Determine if an error is retryable.
191-
*/
192-
protected function isRetryableError(RequestException $e): bool
193-
{
194-
return in_array($e->getCode(), [500, 502, 503, 504]);
195-
}
196-
197189
/**
198190
* Get the full URI for the request.
199191
*/
@@ -209,7 +201,7 @@ protected function getFullUri(): string
209201
$parsedUrl = parse_url($uri);
210202
$separator = ! empty($parsedUrl['query']) ? '&' : '?';
211203

212-
return $uri . $separator . http_build_query($this->options['query']);
204+
return $uri.$separator.http_build_query($this->options['query']);
213205
}
214206

215207
return $uri;
@@ -225,13 +217,13 @@ protected function getFullUri(): string
225217
}
226218

227219
// Concatenate base URI and URI ensuring no double slashes
228-
$result = rtrim($baseUri, '/') . '/' . ltrim($uri, '/');
220+
$result = rtrim($baseUri, '/').'/'.ltrim($uri, '/');
229221
}
230222

231223
// If query parameters exist, append them to the URL
232224
if (! empty($this->options['query'])) {
233225
$separator = strpos($result, '?') !== false ? '&' : '?';
234-
$result .= $separator . http_build_query($this->options['query']);
226+
$result .= $separator.http_build_query($this->options['query']);
235227
}
236228

237229
return $result;
@@ -291,6 +283,26 @@ public function baseUri(string $baseUri): self
291283
return $this;
292284
}
293285

286+
/**
287+
* Set multiple options for the request.
288+
*/
289+
public function withOptions(array $options): self
290+
{
291+
$this->options = array_merge($this->options, $options);
292+
293+
return $this;
294+
}
295+
296+
/**
297+
* Set a single option for the request.
298+
*/
299+
public function withOption(string $key, mixed $value): self
300+
{
301+
$this->options[$key] = $value;
302+
303+
return $this;
304+
}
305+
294306
/**
295307
* Set the query parameters for the request.
296308
*/
@@ -316,7 +328,7 @@ public function withMultipart(array $multipart): self
316328
*/
317329
public function withToken(string $token): self
318330
{
319-
$this->options['headers']['Authorization'] = 'Bearer ' . $token;
331+
$this->options['headers']['Authorization'] = 'Bearer '.$token;
320332

321333
return $this;
322334
}
@@ -633,8 +645,8 @@ public function hasOption(string $option): bool
633645
public function debug(): array
634646
{
635647
return [
636-
'uri' => $this->getFullUri(),
637-
'method' => $this->options['method'] ?? 'GET',
648+
'uri' => $this->getFullUri(),
649+
'method' => $this->options['method'] ?? 'GET',
638650
'headers' => $this->getHeaders(),
639651
'options' => array_diff_key($this->options, ['headers' => true]),
640652
];

0 commit comments

Comments
 (0)