-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpClient.php
87 lines (72 loc) · 2.77 KB
/
HttpClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
declare(strict_types=1);
namespace Kununu\DataFixtures\Tools;
use Generator;
use ReflectionClass;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\ResponseInterface;
final class HttpClient extends MockHttpClient implements FixturesHttpClientInterface
{
private ?array $responses = null;
private ?array $bodyValidators = null;
public function request(string $method, string $url, array $options = []): ResponseInterface
{
$key = $this->responseKey($method, $url);
$response = $this->responses[$key] ?? null;
$bodyValidator = $this->bodyValidators[$key] ?? null;
if (!$response instanceof MockResponse) {
$response = new MockResponse('', ['http_code' => Response::HTTP_INTERNAL_SERVER_ERROR]);
}
if (is_callable($bodyValidator)) {
$response = $bodyValidator($response, $options);
}
$this->recreateResponseFactory([$response]);
return parent::request($method, $url, $options);
}
public function addResponses(array $responses): void
{
if (null === $this->responses) {
$this->responses = [];
}
if (null === $this->bodyValidators) {
$this->bodyValidators = [];
}
foreach ($responses as $response) {
[$url, $method, $code, $body, $bodyValidator] = $this->extractResponseData($response);
$key = $this->responseKey($method, $url);
if (is_callable($bodyValidator)) {
$this->bodyValidators[$key] = $bodyValidator;
}
$this->responses[$key] = new MockResponse($body, ['http_code' => $code]);
}
}
public function clearResponses(): void
{
$this->responses = [];
$this->bodyValidators = [];
}
private function extractResponseData(array $response): array
{
return [
$response['url'],
$response['method'] ?? Request::METHOD_GET,
(int) ($response['code'] ?? Response::HTTP_OK),
$response['body'] ?? '',
$response['bodyValidator'] ?? null,
];
}
private function responseKey(string $method, string $url): string
{
return sprintf('%s_%s', strtoupper($method), $url);
}
private function recreateResponseFactory(array $responses): void
{
$responseFactory = (static fn(): Generator => yield from $responses)();
$reflectionClass = new ReflectionClass(MockHttpClient::class);
$reflectionProperty = $reflectionClass->getProperty('responseFactory');
$reflectionProperty->setValue($this, $responseFactory);
}
}