Skip to content

Commit

Permalink
Merge pull request #129 from mapogolions/fix/non-droppable-cache
Browse files Browse the repository at this point in the history
Detaching a stream drops the cache
  • Loading branch information
l0gicgate committed Aug 29, 2019
2 parents a8c92e7 + c392fcd commit f27d67d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Stream implements StreamInterface
/**
* @var bool
*/
protected $finished;
protected $finished = false;

/**
* @var StreamInterface | null
Expand Down Expand Up @@ -141,6 +141,9 @@ public function detach()
$this->size = null;
$this->isPipe = null;

$this->cache = null;
$this->finished = false;

return $oldResource;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,21 @@ public function testCachedStreamsFillsCacheOnRead()
$this->assertEquals("0", $stream->read(100));
$this->assertEquals("0", $stream->__toString());
}

public function testDetachingStreamDropsCache()
{
$cache = new Stream(fopen('php://temp', 'w+'));
$resource = fopen('data://,foo', 'r');
$stream = new Stream($resource, $cache);

$stream->detach();

$cacheProperty = new ReflectionProperty(Stream::class, 'cache');
$cacheProperty->setAccessible(true);
$finishedProperty = new ReflectionProperty(Stream::class, 'finished');
$finishedProperty->setAccessible(true);

$this->assertNull($cacheProperty->getValue($stream));
$this->assertFalse($finishedProperty->getValue($stream));
}
}

0 comments on commit f27d67d

Please sign in to comment.