|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 | + |
| 21 | +/** |
| 22 | + * A test-friendly HttpClient that doesn't make actual HTTP requests. |
| 23 | + * |
| 24 | + * @author Nicolas Grekas <p@tchwork.com> |
| 25 | + */ |
| 26 | +class MockHttpClient implements HttpClientInterface |
| 27 | +{ |
| 28 | + use HttpClientTrait; |
| 29 | + |
| 30 | + private $responseFactory; |
| 31 | + private $baseUri; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param callable|ResponseInterface[]|iterable $responseFactory |
| 35 | + */ |
| 36 | + public function __construct($responseFactory, string $baseUri = null) |
| 37 | + { |
| 38 | + if (!\is_callable($responseFactory) && !$responseFactory instanceof \Iterator) { |
| 39 | + $responseFactory = (function () use ($responseFactory) { |
| 40 | + yield from $responseFactory; |
| 41 | + })(); |
| 42 | + } |
| 43 | + |
| 44 | + $this->responseFactory = $responseFactory; |
| 45 | + $this->baseUri = $baseUri; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + */ |
| 51 | + public function request(string $method, string $url, array $options = []): ResponseInterface |
| 52 | + { |
| 53 | + [$url, $options] = $this->prepareRequest($method, $url, $options, ['base_uri' => $this->baseUri], true); |
| 54 | + $url = implode('', $url); |
| 55 | + |
| 56 | + if (\is_callable($this->responseFactory)) { |
| 57 | + $response = ($this->responseFactory)($method, $url, $options); |
| 58 | + } elseif (!$this->responseFactory->valid()) { |
| 59 | + throw new TransportException('The response factory iterator passed to MockHttpClient is empty.'); |
| 60 | + } else { |
| 61 | + $response = $this->responseFactory->current(); |
| 62 | + $this->responseFactory->next(); |
| 63 | + } |
| 64 | + |
| 65 | + return MockResponse::fromRequest($method, $url, $options, $response); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritdoc} |
| 70 | + */ |
| 71 | + public function stream($responses, float $timeout = null): ResponseStreamInterface |
| 72 | + { |
| 73 | + if ($responses instanceof ResponseInterface) { |
| 74 | + $responses = [$responses]; |
| 75 | + } elseif (!\is_iterable($responses)) { |
| 76 | + throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of MockResponse objects, %s given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses))); |
| 77 | + } |
| 78 | + |
| 79 | + return new ResponseStream(MockResponse::stream($responses, $timeout)); |
| 80 | + } |
| 81 | +} |
0 commit comments