| 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 $responseFactory; |
| 32 |
private $requestsCount = 0; |
| 33 |
private $defaultOptions = []; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param callable|callable[]|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory |
| 37 |
*/ |
| 38 |
public function __construct($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 = $responseFactory; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* {@inheritdoc} |
| 64 |
*/ |
| 65 |
public function request(string $method, string $url, array $options = []): ResponseInterface |
| 66 |
{ |
| 67 |
[$url, $options] = $this->prepareRequest($method, $url, $options, $this->defaultOptions, true); |
| 68 |
$url = implode('', $url); |
| 69 |
|
| 70 |
if (null === $this->responseFactory) { |
| 71 |
$response = new MockResponse(); |
| 72 |
} elseif (\is_callable($this->responseFactory)) { |
| 73 |
$response = ($this->responseFactory)($method, $url, $options); |
| 74 |
} elseif (!$this->responseFactory->valid()) { |
| 75 |
throw new TransportException('The response factory iterator passed to MockHttpClient is empty.'); |
| 76 |
} else { |
| 77 |
$responseFactory = $this->responseFactory->current(); |
| 78 |
$response = \is_callable($responseFactory) ? $responseFactory($method, $url, $options) : $responseFactory; |
| 79 |
$this->responseFactory->next(); |
| 80 |
} |
| 81 |
++$this->requestsCount; |
| 82 |
|
| 83 |
if (!$response instanceof ResponseInterface) { |
| 84 |
throw new TransportException(sprintf('The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "%s" given.', \is_object($response) ? \get_class($response) : \gettype($response))); |
| 85 |
} |
| 86 |
|
| 87 |
return MockResponse::fromRequest($method, $url, $options, $response); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* {@inheritdoc} |
| 92 |
*/ |
| 93 |
public function stream($responses, float $timeout = null): ResponseStreamInterface |
| 94 |
{ |
| 95 |
if ($responses instanceof ResponseInterface) { |
| 96 |
$responses = [$responses]; |
| 97 |
} elseif (!is_iterable($responses)) { |
| 98 |
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of MockResponse objects, "%s" given.', __METHOD__, get_debug_type($responses))); |
| 99 |
} |
| 100 |
|
| 101 |
return new ResponseStream(MockResponse::stream($responses, $timeout)); |
| 102 |
} |
| 103 |
|
| 104 |
public function getRequestsCount(): int |
| 105 |
{ |
| 106 |
return $this->requestsCount; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* {@inheritdoc} |
| 111 |
*/ |
| 112 |
public function withOptions(array $options): self |
| 113 |
{ |
| 114 |
$clone = clone $this; |
| 115 |
$clone->defaultOptions = self::mergeDefaultOptions($options, $this->defaultOptions, true); |
| 116 |
|
| 117 |
return $clone; |
| 118 |
} |
| 119 |
|
| 120 |
public function reset() |
| 121 |
{ |
| 122 |
$this->requestsCount = 0; |
| 123 |
} |
| 124 |
} |
| 125 |
|