Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/stream/src/DecoratorStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class DecoratorStream implements StreamInterface

public function __construct(?StreamInterface $target = null)
{
if ($target) {
$this->stream = $target;
}
$this->stream = $target ?? new Stream();
}

public function __toString(): string
Expand Down
2 changes: 1 addition & 1 deletion packages/stream/src/PhpInputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function __construct(?StreamInterface $cache = null)

public static function getInstance(?StreamInterface $cache = null): self
{
return self::$instance ?? new PhpInputStream($cache);
return self::$instance ??= new self($cache);
}

public static function getInstanceAndCached(?StreamInterface $cache = null): self
Expand Down
7 changes: 7 additions & 0 deletions packages/stream/src/StringStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ public function isWritable(): bool
*/
public function write($string): int
{
if (!$this->isWritable()) {
throw new RuntimeException('Stream is not writable');
}

$length = strlen($string);

$start = substr($this->resource, 0, $this->pointer);
Expand Down Expand Up @@ -326,6 +330,9 @@ public function getContents(): string
return '';
}

$this->pointer += strlen($result);
$this->readPosition = $this->pointer;

return $result;
}

Expand Down
8 changes: 8 additions & 0 deletions packages/stream/test/DecoratorStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public function constructAcceptsInnerStream(): void
$this->assertSame($inner, new DecoratorStream($inner)->getInnerStream());
}

#[Test]
public function constructWithoutTargetCreatesEmptyStream(): void
{
$decorator = new DecoratorStream();

$this->assertSame('', $decorator->getContents());
}

#[Test]
public function setInnerStreamReplacesTarget(): void
{
Expand Down
5 changes: 4 additions & 1 deletion packages/stream/test/PhpInputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function getInstanceAndCachedReturnsRewoundStreamWithInputContents(): voi
#[Test]
public function getInstanceCreatesStreamWithGivenCache(): void
{
$this->assertInstanceOf(PhpInputStream::class, PhpInputStream::getInstance());
$stream = PhpInputStream::getInstance();

$this->assertInstanceOf(PhpInputStream::class, $stream);
$this->assertSame($stream, PhpInputStream::getInstance());
}
}
14 changes: 14 additions & 0 deletions packages/stream/test/StringStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ public function getContentsReturnsRemainingStringFromPosition(): void
$stream->seek(2);

$this->assertEquals('O BAR', $stream->getContents());
$this->assertSame(7, $stream->tell());
$this->assertSame('', $stream->getContents());
}

#[Test]
Expand Down Expand Up @@ -384,4 +386,16 @@ public function writableControlsIsWritable(): void

$this->assertTrue($stream->isWritable());
}

#[Test]
public function writableFalseBlocksWrite(): void
{
$stream = new StringStream('FOO', READ_WRITE_FROM_BEGIN);
$stream->writable(false);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessageIs('Stream is not writable');

$stream->write('BAR');
}
}