Debian Codegraph @

lib/3rdParty/composer/symfony/http-client/MockHttpClient.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpClient;
13
14 use Symfony\Component\HttpClient\Exception\TransportException;
15 use Symfony\Component\HttpClient\Response\MockResponse;
16 use Symfony\Component\HttpClient\Response\ResponseStream;
17 use Symfony\Contracts\HttpClient\HttpClientInterface;
18 use Symfony\Contracts\HttpClient\ResponseInterface;
19 use Symfony\Contracts\HttpClient\ResponseStreamInterface;
20 use Symfony\Contracts\Service\ResetInterface;
21
22 /**
23 * A test-friendly HttpClient that doesn't make actual HTTP requests.
24 *
25 * @author Nicolas Grekas <[email protected]>
26 */
27 class MockHttpClient implements HttpClientInterface, ResetInterface
28 {
29 use HttpClientTrait;
30
31 private ResponseInterface|\Closure|iterable|null $responseFactory;
32 private int $requestsCount = 0;
33 private array $defaultOptions = [];
34
35 /**
36 * @param callable|callable[]|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory
37 */
38 public function __construct(callable|iterable|ResponseInterface|null $responseFactory = null, ?string $baseUri = 'https://example.com')
39 {
40 $this->setResponseFactory($responseFactory);
41 $this->defaultOptions['base_uri'] = $baseUri;
42 }
43
44 /**
45 * @param callable|callable[]|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory
46 */
47 public function setResponseFactory($responseFactory): void
48 {
49 if ($responseFactory instanceof ResponseInterface) {
50 $responseFactory = [$responseFactory];
51 }
52
53 if (!$responseFactory instanceof \Iterator && null !== $responseFactory && !\is_callable($responseFactory)) {
54 $responseFactory = (static function () use ($responseFactory) {
55 yield from $responseFactory;
56 })();
57 }
58
59 $this->responseFactory = !\is_callable($responseFactory) ? $responseFactory : $responseFactory(...);
60 }
61
62 public function request(string $method, string $url, array $options = []): ResponseInterface
63 {
64 [$url, $options] = $this->prepareRequest($method, $url, $options, $this->defaultOptions, true);
65 $url = implode('', $url);
66
67 if (null === $this->responseFactory) {
68 $response = new MockResponse();
69 } elseif (\is_callable($this->responseFactory)) {
70 $response = ($this->responseFactory)($method, $url, $options);
71 } elseif (!$this->responseFactory->valid()) {
72 throw new TransportException($this->requestsCount ? 'No more response left in the response factory iterator passed to MockHttpClient: the number of requests exceeds the number of responses.' : 'The response factory iterator passed to MockHttpClient is empty.');
73 } else {
74 $responseFactory = $this->responseFactory->current();
75 $response = \is_callable($responseFactory) ? $responseFactory($method, $url, $options) : $responseFactory;
76 $this->responseFactory->next();
77 }
78 ++$this->requestsCount;
79
80 if (!$response instanceof ResponseInterface) {
81 throw new TransportException(\sprintf('The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "%s" given.', get_debug_type($response)));
82 }
83
84 return MockResponse::fromRequest($method, $url, $options, $response);
85 }
86
87 public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface
88 {
89 if ($responses instanceof ResponseInterface) {
90 $responses = [$responses];
91 }
92
93 return new ResponseStream(MockResponse::stream($responses, $timeout));
94 }
95
96 public function getRequestsCount(): int
97 {
98 return $this->requestsCount;
99 }
100
101 public function withOptions(array $options): static
102 {
103 $clone = clone $this;
104 $clone->defaultOptions = self::mergeDefaultOptions($options, $this->defaultOptions, true);
105
106 return $clone;
107 }
108
109 public function reset(): void
110 {
111 $this->requestsCount = 0;
112 }
113 }