From 04e3e8301a896fab228e989f6323e1c1d6618574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl?= Date: Thu, 18 Jul 2024 11:48:31 +0200 Subject: [PATCH] Make `StreamWrapper::stream_stat()` returns `false` if inner stream's size is `null` (#594) --- src/StreamWrapper.php | 8 ++++++-- tests/StreamWrapperTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 3534729e..77b04d74 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -136,10 +136,14 @@ public function stream_cast(int $cast_as) * ctime: int, * blksize: int, * blocks: int - * } + * }|false */ - public function stream_stat(): array + public function stream_stat() { + if ($this->stream->getSize() === null) { + return false; + } + static $modeMap = [ 'r' => 33060, 'rb' => 33060, diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 153095d8..e30d177b 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -6,6 +6,7 @@ use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\StreamWrapper; +use GuzzleHttp\Psr7\Utils; use PHPUnit\Framework\TestCase; use Psr\Http\Message\StreamInterface; @@ -187,4 +188,15 @@ public function testXmlWriterWithStream(): void $stream->rewind(); self::assertXmlStringEqualsXmlString('', (string) $stream); } + + public function testWrappedNullSizedStreamStaysNullSized(): void + { + $nullSizedStream = new Psr7\PumpStream(function () { return ''; }); + $this->assertNull($nullSizedStream->getSize()); + + $resource = StreamWrapper::getResource($nullSizedStream); + $stream = Utils::streamFor($resource); + + $this->assertNull($stream->getSize()); + } }