|
8 | 8 | use SaintSystems\OData\HttpRequestMessage; |
9 | 9 | use SaintSystems\OData\HttpMethod; |
10 | 10 | use SaintSystems\OData\IHttpProvider; |
| 11 | +use Psr\Http\Message\RequestInterface; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
11 | 13 |
|
12 | 14 | class MockHttpProvider implements IHttpProvider |
13 | 15 | { |
14 | 16 | public $lastRequest = null; |
15 | 17 |
|
16 | | - public function send(HttpRequestMessage $request) |
| 18 | + public function send(HttpRequestMessage $request): ResponseInterface |
17 | 19 | { |
18 | 20 | $this->lastRequest = $request; |
19 | 21 |
|
20 | | - // Return a mock response |
21 | | - return new class { |
22 | | - public function getBody() { |
23 | | - return '{"value": []}'; |
| 22 | + // Return a mock response that implements ResponseInterface |
| 23 | + return new class implements ResponseInterface { |
| 24 | + public function getProtocolVersion(): string { |
| 25 | + return '1.1'; |
24 | 26 | } |
25 | | - public function getStatusCode() { |
26 | | - return 200; |
| 27 | + public function withProtocolVersion(string $version): ResponseInterface { |
| 28 | + return $this; |
27 | 29 | } |
28 | | - public function getHeaders() { |
| 30 | + public function getHeaders(): array { |
29 | 31 | return []; |
30 | 32 | } |
| 33 | + public function hasHeader(string $name): bool { |
| 34 | + return false; |
| 35 | + } |
| 36 | + public function getHeader(string $name): array { |
| 37 | + return []; |
| 38 | + } |
| 39 | + public function getHeaderLine(string $name): string { |
| 40 | + return ''; |
| 41 | + } |
| 42 | + public function withHeader(string $name, $value): ResponseInterface { |
| 43 | + return $this; |
| 44 | + } |
| 45 | + public function withAddedHeader(string $name, $value): ResponseInterface { |
| 46 | + return $this; |
| 47 | + } |
| 48 | + public function withoutHeader(string $name): ResponseInterface { |
| 49 | + return $this; |
| 50 | + } |
| 51 | + public function getBody(): \Psr\Http\Message\StreamInterface { |
| 52 | + return new class implements \Psr\Http\Message\StreamInterface { |
| 53 | + private $content = '{"value": []}'; |
| 54 | + |
| 55 | + public function __toString(): string { |
| 56 | + return $this->content; |
| 57 | + } |
| 58 | + public function close(): void {} |
| 59 | + public function detach() { |
| 60 | + return null; |
| 61 | + } |
| 62 | + public function getSize(): ?int { |
| 63 | + return strlen($this->content); |
| 64 | + } |
| 65 | + public function tell(): int { |
| 66 | + return 0; |
| 67 | + } |
| 68 | + public function eof(): bool { |
| 69 | + return false; |
| 70 | + } |
| 71 | + public function isSeekable(): bool { |
| 72 | + return true; |
| 73 | + } |
| 74 | + public function seek(int $offset, int $whence = SEEK_SET): void {} |
| 75 | + public function rewind(): void {} |
| 76 | + public function isWritable(): bool { |
| 77 | + return false; |
| 78 | + } |
| 79 | + public function write(string $string): int { |
| 80 | + return 0; |
| 81 | + } |
| 82 | + public function isReadable(): bool { |
| 83 | + return true; |
| 84 | + } |
| 85 | + public function read(int $length): string { |
| 86 | + return substr($this->content, 0, $length); |
| 87 | + } |
| 88 | + public function getContents(): string { |
| 89 | + return $this->content; |
| 90 | + } |
| 91 | + public function getMetadata(?string $key = null) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + }; |
| 95 | + } |
| 96 | + public function withBody(\Psr\Http\Message\StreamInterface $body): ResponseInterface { |
| 97 | + return $this; |
| 98 | + } |
| 99 | + public function getStatusCode(): int { |
| 100 | + return 200; |
| 101 | + } |
| 102 | + public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface { |
| 103 | + return $this; |
| 104 | + } |
| 105 | + public function getReasonPhrase(): string { |
| 106 | + return 'OK'; |
| 107 | + } |
31 | 108 | }; |
32 | 109 | } |
| 110 | + |
| 111 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 112 | + { |
| 113 | + // For PSR-18 compatibility |
| 114 | + $httpMessage = new HttpRequestMessage(); |
| 115 | + $httpMessage->method = $request->getMethod(); |
| 116 | + $httpMessage->requestUri = (string)$request->getUri(); |
| 117 | + $httpMessage->headers = []; |
| 118 | + foreach ($request->getHeaders() as $name => $values) { |
| 119 | + $httpMessage->headers[$name] = implode(', ', $values); |
| 120 | + } |
| 121 | + $httpMessage->body = $request->getBody()->getContents(); |
| 122 | + |
| 123 | + return $this->send($httpMessage); |
| 124 | + } |
33 | 125 | } |
34 | 126 |
|
35 | 127 | class CustomHeadersTest extends TestCase |
|
0 commit comments