|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\HttpClient\Testing; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Assert as PHPUnit; |
| 8 | +use Psr\Http\Client\ClientInterface; |
| 9 | +use Psr\Http\Message\RequestInterface; |
| 10 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 11 | +use Psr\Http\Message\ResponseInterface; |
| 12 | +use Psr\Http\Message\StreamFactoryInterface; |
| 13 | +use PsrDiscovery\Discover; |
| 14 | +use RuntimeException; |
| 15 | + |
| 16 | +/** |
| 17 | + * PSR-18 compliant HTTP testing client. |
| 18 | + */ |
| 19 | +final class MockClient implements ClientInterface |
| 20 | +{ |
| 21 | + private readonly ResponseFactoryInterface $responseFactory; |
| 22 | + |
| 23 | + private readonly StreamFactoryInterface $streamFactory; |
| 24 | + |
| 25 | + /** @var array<array-key,RequestInterface> */ |
| 26 | + private array $requests = []; |
| 27 | + |
| 28 | + private RequestInterface $lastRequest; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var array<string,ResponseInterface|ResponseBag> |
| 32 | + */ |
| 33 | + private array $fakedResponses = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array<string, ResponseInterface|ResponseBag> |
| 37 | + */ |
| 38 | + private array $fakedWildcardResponses = []; |
| 39 | + |
| 40 | + public function __construct(?ResponseFactoryInterface $responseFactory = null, ?StreamFactoryInterface $streamFactory = null) |
| 41 | + { |
| 42 | + $this->responseFactory = $responseFactory ?? $this->initializeResponseFactory(); |
| 43 | + $this->streamFactory = $streamFactory ?? $this->initializeStreamFactory(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @param array<string,ResponseInterface|ResponseBag> $map |
| 48 | + */ |
| 49 | + public static function fake(array $map = []): self |
| 50 | + { |
| 51 | + return new self()->setResponses($map); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param array<array-key,ResponseInterface> $responses |
| 56 | + */ |
| 57 | + public static function sequence(array $responses = []): ResponseBag |
| 58 | + { |
| 59 | + return new ResponseBag($responses); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param array<array-key,ResponseInterface> $responses |
| 64 | + */ |
| 65 | + public static function random(array $responses = []): ResponseBag |
| 66 | + { |
| 67 | + return self::sequence($responses)->randomize(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param null|string|array<mixed,mixed> $body |
| 72 | + * @param array<string,string> $headers |
| 73 | + */ |
| 74 | + public static function response(null|string|array $body = null, int $code = 200, array $headers = []): ResponseInterface |
| 75 | + { |
| 76 | + $client = new self(); |
| 77 | + $response = $client->responseFactory->createResponse($code); |
| 78 | + $body = is_array($body) ? json_encode($body) : $body; |
| 79 | + |
| 80 | + if ($body) { |
| 81 | + $stream = is_file($body) |
| 82 | + ? $client->streamFactory->createStreamFromFile($body) |
| 83 | + : $client->streamFactory->createStream($body); |
| 84 | + |
| 85 | + $response = $response->withBody($stream); |
| 86 | + } |
| 87 | + |
| 88 | + foreach ($headers as $header => $value) { |
| 89 | + $response = $response->withHeader($header, $value); |
| 90 | + } |
| 91 | + |
| 92 | + return $response; |
| 93 | + } |
| 94 | + |
| 95 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 96 | + { |
| 97 | + $this->lastRequest = $request; |
| 98 | + $this->requests[] = $request; |
| 99 | + |
| 100 | + if ($response = $this->resolveFakeResponse($request)) { |
| 101 | + return $response; |
| 102 | + } |
| 103 | + |
| 104 | + if ($response = $this->resolveWildcardFakeResponse($request)) { |
| 105 | + return $response; |
| 106 | + } |
| 107 | + |
| 108 | + return $this->responseFactory->createResponse(); |
| 109 | + } |
| 110 | + |
| 111 | + public function assertUri(string $uri): self |
| 112 | + { |
| 113 | + $this->assertRequestsWereMade(); |
| 114 | + |
| 115 | + PHPUnit::assertSame( |
| 116 | + strtolower($uri), |
| 117 | + strtolower($this->lastRequest->getUri()->__toString()), |
| 118 | + ); |
| 119 | + |
| 120 | + return $this; |
| 121 | + } |
| 122 | + |
| 123 | + public function assertMethod(string $method): self |
| 124 | + { |
| 125 | + $this->assertRequestsWereMade(); |
| 126 | + |
| 127 | + PHPUnit::assertSame( |
| 128 | + strtoupper($method), |
| 129 | + strtoupper($this->lastRequest->getMethod()), |
| 130 | + ); |
| 131 | + |
| 132 | + return $this; |
| 133 | + } |
| 134 | + |
| 135 | + public function assertHeaderEquals(string $header, mixed $value): self |
| 136 | + { |
| 137 | + $this->assertRequestsWereMade(); |
| 138 | + |
| 139 | + PHPUnit::assertSame( |
| 140 | + $value, |
| 141 | + $this->lastRequest->getHeaderLine($header), |
| 142 | + ); |
| 143 | + |
| 144 | + return $this; |
| 145 | + } |
| 146 | + |
| 147 | + public function assertBodyIs(string $content): self |
| 148 | + { |
| 149 | + $this->assertRequestsWereMade(); |
| 150 | + |
| 151 | + PHPUnit::assertSame( |
| 152 | + $content, |
| 153 | + $this->lastRequest->getBody()->getContents(), |
| 154 | + ); |
| 155 | + |
| 156 | + return $this; |
| 157 | + } |
| 158 | + |
| 159 | + public function assertBodyIsEmpty(): self |
| 160 | + { |
| 161 | + return $this->assertBodyIs(''); |
| 162 | + } |
| 163 | + |
| 164 | + public function assertBodyContains(string $content): self |
| 165 | + { |
| 166 | + $this->assertRequestsWereMade(); |
| 167 | + |
| 168 | + PHPUnit::assertStringContainsString( |
| 169 | + $content, |
| 170 | + $this->lastRequest->getBody()->getContents(), |
| 171 | + ); |
| 172 | + |
| 173 | + return $this; |
| 174 | + } |
| 175 | + |
| 176 | + public function assertRequestsWereMade(?int $count = null): self |
| 177 | + { |
| 178 | + if ($count) { |
| 179 | + PHPUnit::assertCount($count, $this->requests); |
| 180 | + } else { |
| 181 | + PHPUnit::assertNotEmpty($this->requests); |
| 182 | + } |
| 183 | + |
| 184 | + return $this; |
| 185 | + } |
| 186 | + |
| 187 | + public function assertNoRequestsWereMade(): self |
| 188 | + { |
| 189 | + PHPUnit::assertEmpty($this->requests); |
| 190 | + |
| 191 | + return $this; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * @param array<string,ResponseInterface|ResponseBag> $responses |
| 196 | + */ |
| 197 | + private function setResponses(array $responses): self |
| 198 | + { |
| 199 | + foreach ($responses as $uri => $response) { |
| 200 | + $this->setResponse($uri, $response); |
| 201 | + } |
| 202 | + |
| 203 | + return $this; |
| 204 | + } |
| 205 | + |
| 206 | + private function setResponse(string $uri, ResponseInterface|ResponseBag $response): self |
| 207 | + { |
| 208 | + if (str_contains($uri, '*')) { |
| 209 | + $this->fakedWildcardResponses[$uri] = $response; |
| 210 | + } else { |
| 211 | + $this->fakedResponses[$uri] = $response; |
| 212 | + } |
| 213 | + |
| 214 | + return $this; |
| 215 | + } |
| 216 | + |
| 217 | + private function resolveFakeResponse(RequestInterface $request): ?ResponseInterface |
| 218 | + { |
| 219 | + foreach ($this->fakedResponses as $uri => $fakedResponse) { |
| 220 | + if (strtolower($uri) !== strtolower($request->getUri()->__toString())) { |
| 221 | + continue; |
| 222 | + } |
| 223 | + |
| 224 | + return ($fakedResponse instanceof ResponseBag) |
| 225 | + ? $fakedResponse->getNextResponse() |
| 226 | + : $fakedResponse; |
| 227 | + } |
| 228 | + |
| 229 | + return null; |
| 230 | + } |
| 231 | + |
| 232 | + private function resolveWildcardFakeResponse(RequestInterface $request): ?ResponseInterface |
| 233 | + { |
| 234 | + foreach ($this->fakedWildcardResponses as $url => $fakedWildcardResponse) { |
| 235 | + $url = str_replace('\*', '.*', preg_quote($url, '/')); |
| 236 | + |
| 237 | + if (! preg_match("/{$url}/i", $request->getUri()->__toString())) { |
| 238 | + continue; |
| 239 | + } |
| 240 | + |
| 241 | + return ($fakedWildcardResponse instanceof ResponseBag) |
| 242 | + ? $fakedWildcardResponse->getNextResponse() |
| 243 | + : $fakedWildcardResponse; |
| 244 | + } |
| 245 | + |
| 246 | + return null; |
| 247 | + } |
| 248 | + |
| 249 | + private function initializeResponseFactory(): ResponseFactoryInterface |
| 250 | + { |
| 251 | + return Discover::httpResponseFactory() ?? throw new RuntimeException( |
| 252 | + 'The PSR request factory cannot be null. Please ensure that it is properly initialized.', |
| 253 | + ); |
| 254 | + } |
| 255 | + |
| 256 | + private function initializeStreamFactory(): StreamFactoryInterface |
| 257 | + { |
| 258 | + return Discover::httpStreamFactory() ?? throw new RuntimeException( |
| 259 | + 'The PSR stream factory cannot be null. Please ensure that it is properly initialized.', |
| 260 | + ); |
| 261 | + } |
| 262 | +} |
0 commit comments