From d9371730dc3e2718f542207d884722807f402b6d Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Mon, 10 May 2021 22:00:20 +0200 Subject: [PATCH] rewind in-memory stream to have expected behaviour (#177) --- CHANGELOG.md | 6 ++++++ src/Stream.php | 1 + tests/StreamTest.php | 7 +++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 759bdbf..2a761d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## Unreleased + +### Fixed + +- `Stream::create` with a string needs to rewind the created memory stream. + ## 1.4.0 ### Removed diff --git a/src/Stream.php b/src/Stream.php index bcc59ba..fee0c63 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -71,6 +71,7 @@ public static function create($body = ''): StreamInterface if (\is_string($body)) { $resource = \fopen('php://temp', 'rw+'); \fwrite($resource, $body); + \rewind($resource); $body = $resource; } diff --git a/tests/StreamTest.php b/tests/StreamTest.php index 74898e9..d3ac5aa 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -44,6 +44,13 @@ public function testConvertsToString() $stream->close(); } + public function testBuildFromString() + { + $stream = Stream::create('data'); + $this->assertEquals('data', $stream->getContents()); + $stream->close(); + } + public function testGetsContents() { $handle = fopen('php://temp', 'w+');