From a0c4a5f6371763bb8a178839a541f407b3cbeb72 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 4 Oct 2021 05:32:41 +0100 Subject: [PATCH] Return null in caching stream size if remote is null (#438) * Return null in caching stream size if remote is null * Added test coverage * Fixed typo * Fixes --- src/CachingStream.php | 8 +++++++- tests/CachingStreamTest.php | 11 ++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/CachingStream.php b/src/CachingStream.php index fe749e98..febade9f 100644 --- a/src/CachingStream.php +++ b/src/CachingStream.php @@ -36,7 +36,13 @@ public function __construct( public function getSize() { - return max($this->stream->getSize(), $this->remoteStream->getSize()); + $remoteSize = $this->remoteStream->getSize(); + + if (null === $remoteSize) { + return null; + } + + return max($this->stream->getSize(), $remoteSize); } public function rewind() diff --git a/tests/CachingStreamTest.php b/tests/CachingStreamTest.php index c760f5db..a1fa04ae 100644 --- a/tests/CachingStreamTest.php +++ b/tests/CachingStreamTest.php @@ -34,13 +34,22 @@ public function tearDownTest() $this->body->close(); } - public function testUsesRemoteSizeIfPossible() + public function testUsesRemoteSizeIfAvailable() { $body = Psr7\Utils::streamFor('test'); $caching = new CachingStream($body); self::assertSame(4, $caching->getSize()); } + public function testUsesRemoteSizeIfNotAvailable() + { + $body = new Psr7\PumpStream(function () { + return 'a'; + }); + $caching = new CachingStream($body); + self::assertNull($caching->getSize()); + } + public function testReadsUntilCachedToByte() { $this->body->seek(5);