From 44bd71161e0dcd11218b0a77a2034fc30703b71d Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 20 Oct 2023 13:37:57 +0200 Subject: [PATCH] Binary: Rewind stream after reading contents --- src/Behavior/Binary.php | 5 ++++- tests/Behavior/BinaryTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Behavior/Binary.php b/src/Behavior/Binary.php index c43082a..602c9c7 100644 --- a/src/Behavior/Binary.php +++ b/src/Behavior/Binary.php @@ -29,7 +29,10 @@ public function fromDb($value, $key, $_) if ($value !== null) { if (is_resource($value)) { - return stream_get_contents($value); + $content = stream_get_contents($value); + rewind($value); + + return $content; } return $value; diff --git a/tests/Behavior/BinaryTest.php b/tests/Behavior/BinaryTest.php index 36c12c4..fc97aa7 100644 --- a/tests/Behavior/BinaryTest.php +++ b/tests/Behavior/BinaryTest.php @@ -37,6 +37,22 @@ public function testRetrievePropertyReturnsStreamContentsIfAdapterIsPostgreSQL() ); } + public function testRetrievePropertyRewindsAStreamIfAdapterIsPostgreSQL(): void + { + $stream = fopen('php://temp', 'r+'); + fputs($stream, static::TEST_BINARY_VALUE); + rewind($stream); + + $this->assertSame( + static::TEST_BINARY_VALUE, + $this->behavior(true)->retrieveProperty($stream, static::TEST_COLUMN) + ); + $this->assertSame( + static::TEST_BINARY_VALUE, + $this->behavior(true)->retrieveProperty($stream, static::TEST_COLUMN) + ); + } + public function testPersistPropertyReturnsVanillaValueIfAdapterIsNotPostgreSQL(): void { $this->assertSame(