Skip to content

Commit

Permalink
Binary: Rewind stream after reading contents
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Oct 20, 2023
1 parent fab2c91 commit 44bd711
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Behavior/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions tests/Behavior/BinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 44bd711

Please sign in to comment.