|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Saloon\Tests\Fixtures\Mocking; |
| 6 | + |
| 7 | +use Saloon\Http\Faking\MockResponse; |
| 8 | +use Psr\Http\Message\StreamInterface; |
| 9 | +use Psr\Http\Message\ResponseInterface; |
| 10 | +use Psr\Http\Message\StreamFactoryInterface; |
| 11 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * A MockResponse that uses an unseekable body stream so we can test |
| 15 | + * that the response debugger buffers the body and shows it correctly. |
| 16 | + */ |
| 17 | +class UnseekableBodyMockResponse extends MockResponse |
| 18 | +{ |
| 19 | + public function createPsrResponse(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory): ResponseInterface |
| 20 | + { |
| 21 | + $response = parent::createPsrResponse($responseFactory, $streamFactory); |
| 22 | + $body = (string) $response->getBody(); |
| 23 | + |
| 24 | + return $response->withBody(new UnseekableStream($body)); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Stream that reports isSeekable() === false (e.g. like a network stream). |
| 30 | + */ |
| 31 | +final class UnseekableStream implements StreamInterface |
| 32 | +{ |
| 33 | + private string $contents; |
| 34 | + |
| 35 | + private int $position = 0; |
| 36 | + |
| 37 | + private bool $closed = false; |
| 38 | + |
| 39 | + public function __construct(string $contents) |
| 40 | + { |
| 41 | + $this->contents = $contents; |
| 42 | + } |
| 43 | + |
| 44 | + public function __toString(): string |
| 45 | + { |
| 46 | + return $this->contents; |
| 47 | + } |
| 48 | + |
| 49 | + public function close(): void |
| 50 | + { |
| 51 | + $this->closed = true; |
| 52 | + } |
| 53 | + |
| 54 | + public function detach() |
| 55 | + { |
| 56 | + $this->closed = true; |
| 57 | + |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + public function getSize(): ?int |
| 62 | + { |
| 63 | + return mb_strlen($this->contents); |
| 64 | + } |
| 65 | + |
| 66 | + public function tell(): int |
| 67 | + { |
| 68 | + return $this->position; |
| 69 | + } |
| 70 | + |
| 71 | + public function eof(): bool |
| 72 | + { |
| 73 | + return $this->position >= mb_strlen($this->contents); |
| 74 | + } |
| 75 | + |
| 76 | + public function isSeekable(): bool |
| 77 | + { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + public function seek(int $offset, int $whence = SEEK_SET): void |
| 82 | + { |
| 83 | + throw new \RuntimeException('Stream is not seekable'); |
| 84 | + } |
| 85 | + |
| 86 | + public function rewind(): void |
| 87 | + { |
| 88 | + throw new \RuntimeException('Stream is not seekable'); |
| 89 | + } |
| 90 | + |
| 91 | + public function isWritable(): bool |
| 92 | + { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + public function write(string $string): int |
| 97 | + { |
| 98 | + throw new \RuntimeException('Stream is not writable'); |
| 99 | + } |
| 100 | + |
| 101 | + public function isReadable(): bool |
| 102 | + { |
| 103 | + return ! $this->closed; |
| 104 | + } |
| 105 | + |
| 106 | + public function read(int $length): string |
| 107 | + { |
| 108 | + $chunk = mb_substr($this->contents, $this->position, $length); |
| 109 | + $this->position += mb_strlen($chunk); |
| 110 | + |
| 111 | + return $chunk; |
| 112 | + } |
| 113 | + |
| 114 | + public function getContents(): string |
| 115 | + { |
| 116 | + $remaining = mb_substr($this->contents, $this->position); |
| 117 | + $this->position = mb_strlen($this->contents); |
| 118 | + |
| 119 | + return $remaining; |
| 120 | + } |
| 121 | + |
| 122 | + public function getMetadata(?string $key = null) |
| 123 | + { |
| 124 | + return $key === null ? [] : null; |
| 125 | + } |
| 126 | +} |
0 commit comments