|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Eventjet\TestDouble; |
| 6 | + |
| 7 | +use Override; |
| 8 | +use Psr\Http\Client\ClientInterface; |
| 9 | +use Psr\Http\Message\RequestInterface; |
| 10 | +use Psr\Http\Message\ResponseInterface; |
| 11 | +use RuntimeException; |
| 12 | + |
| 13 | +use function array_key_first; |
| 14 | +use function array_keys; |
| 15 | +use function array_splice; |
| 16 | +use function count; |
| 17 | +use function explode; |
| 18 | +use function implode; |
| 19 | +use function sprintf; |
| 20 | + |
| 21 | +/** |
| 22 | + * @phpstan-type RequestMatcher callable(RequestInterface): (true | string) |
| 23 | + */ |
| 24 | +final class TestHttpClient implements ClientInterface |
| 25 | +{ |
| 26 | + /** @var list<array{RequestMatcher, ResponseInterface}> */ |
| 27 | + private $mapping = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param RequestMatcher ...$matchers |
| 31 | + * @return RequestMatcher |
| 32 | + */ |
| 33 | + public static function and(callable ...$matchers): callable |
| 34 | + { |
| 35 | + return static function (RequestInterface $request) use ($matchers): true|string { |
| 36 | + $results = []; |
| 37 | + $issues = false; |
| 38 | + $i = 0; |
| 39 | + foreach ($matchers as $matcher) { |
| 40 | + $result = $matcher($request); |
| 41 | + $results[$i++] = $result; |
| 42 | + if ($result === true) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + $issues = true; |
| 46 | + } |
| 47 | + if (!$issues) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + $matcherResults = []; |
| 51 | + foreach ($results as $index => $result) { |
| 52 | + $matcherResults[] = sprintf('%d: %s', $index, $result === true ? 'Matched' : $result); |
| 53 | + } |
| 54 | + $matcherResults = implode("\n", $matcherResults); |
| 55 | + return sprintf("Some matchers did not match:\n%s", self::indent($matcherResults)); |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return RequestMatcher |
| 61 | + */ |
| 62 | + public static function method(string $expected): callable |
| 63 | + { |
| 64 | + return static function (RequestInterface $request) use ($expected): true|string { |
| 65 | + $actual = $request->getMethod(); |
| 66 | + return $actual === $expected ? true : sprintf('Expected method "%s", but got "%s".', $expected, $actual); |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return RequestMatcher |
| 72 | + */ |
| 73 | + public static function uri(string $expected): callable |
| 74 | + { |
| 75 | + return static function (RequestInterface $request) use ($expected): true|string { |
| 76 | + $actual = (string)$request->getUri(); |
| 77 | + return $actual === $expected ? true : sprintf('Expected URI "%s", but got "%s".', $expected, $actual); |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return RequestMatcher |
| 83 | + */ |
| 84 | + public static function path(string $expected): callable |
| 85 | + { |
| 86 | + return static function (RequestInterface $request) use ($expected): true|string { |
| 87 | + $actual = $request->getUri()->getPath(); |
| 88 | + return $actual === $expected ? true : sprintf('Expected path "%s", but got "%s".', $expected, $actual); |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + private static function indent(string $string): string |
| 93 | + { |
| 94 | + $lines = explode("\n", $string); |
| 95 | + foreach ($lines as &$line) { |
| 96 | + $line = sprintf(' %s', $line); |
| 97 | + } |
| 98 | + return implode("\n", $lines); |
| 99 | + } |
| 100 | + |
| 101 | + private static function requestName(RequestInterface $request): string |
| 102 | + { |
| 103 | + return sprintf('%s %s', $request->getMethod(), $request->getUri()); |
| 104 | + } |
| 105 | + |
| 106 | + private static function noMatchersLeft(RequestInterface $request): never |
| 107 | + { |
| 108 | + throw new RuntimeException(sprintf( |
| 109 | + 'Got a request for %s, but there are no matchers left.', |
| 110 | + self::requestName($request), |
| 111 | + )); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @param list<string> $issues |
| 116 | + */ |
| 117 | + private static function noMatches(RequestInterface $request, array $issues): never |
| 118 | + { |
| 119 | + $text = sprintf('There are no matches for request %s.', self::requestName($request)); |
| 120 | + $matcherTexts = []; |
| 121 | + foreach ($issues as $index => $issue) { |
| 122 | + $matcherTexts[] = sprintf("Matcher #%d:\n%s", $index, self::indent($issue)); |
| 123 | + } |
| 124 | + $text .= "\n\n" . implode("\n\n", $matcherTexts); |
| 125 | + throw new RuntimeException($text); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param array<int, ResponseInterface> $matches |
| 130 | + */ |
| 131 | + private static function multipleMatches(RequestInterface $request, array $matches): never |
| 132 | + { |
| 133 | + $message = sprintf('There are multiple matches for request %s: %s', self::requestName($request), implode(', ', array_keys($matches))); |
| 134 | + throw new RuntimeException($message); |
| 135 | + } |
| 136 | + |
| 137 | + #[Override] |
| 138 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 139 | + { |
| 140 | + if (count($this->mapping) === 0) { |
| 141 | + self::noMatchersLeft($request); |
| 142 | + } |
| 143 | + $matches = []; |
| 144 | + $issues = []; |
| 145 | + foreach ($this->mapping as $index => [$matcher, $response]) { |
| 146 | + $result = $matcher($request); |
| 147 | + if ($result === true) { |
| 148 | + $matches[$index] = $response; |
| 149 | + } else { |
| 150 | + $issues[] = $result; |
| 151 | + } |
| 152 | + } |
| 153 | + if (count($matches) === 0) { |
| 154 | + self::noMatches($request, $issues); |
| 155 | + } |
| 156 | + if (count($matches) > 1) { |
| 157 | + self::multipleMatches($request, $matches); |
| 158 | + } |
| 159 | + $matchIndex = array_key_first($matches); |
| 160 | + array_splice($this->mapping, $matchIndex, 1); |
| 161 | + return $matches[$matchIndex]; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param RequestMatcher $matcher |
| 166 | + */ |
| 167 | + public function map(callable $matcher, ResponseInterface $response): void |
| 168 | + { |
| 169 | + $this->mapping[] = [$matcher, $response]; |
| 170 | + } |
| 171 | +} |
0 commit comments