~ancarda/psr7-string-stream

415624673042e8093e1d8ff161baa20473bc4d48 — Mark Dain 1 year, 9 months ago bdc2108 1.2
Introduce ReadOnlyStringStream

This commit introduces a Read Only version of StringStream
2 files changed, 56 insertions(+), 0 deletions(-)

A src/ReadOnlyStringStream.php
A tests/ReadOnlyStringStreamTest.php
A src/ReadOnlyStringStream.php => src/ReadOnlyStringStream.php +35 -0
@@ 0,0 1,35 @@
<?php

declare(strict_types=1);

namespace Ancarda\Psr7\StringStream;

use RuntimeException;

/**
 * String based PSR-7 Stream that cannot be written to, only read and seeked
 */
class ReadOnlyStringStream extends StringStream
{
    /**
     * Returns false as this stream is read only
     *
     * @return bool
     */
    public function isWritable(): bool
    {
        return false;
    }

    /**
     * Throws a RuntimeException as this stream is read only
     *
     * @param string $string
     * @throws RuntimeException
     * @return int
     */
    public function write($string): int
    {
        throw new RuntimeException('Cannot write(): Read Only Stream');
    }
}

A tests/ReadOnlyStringStreamTest.php => tests/ReadOnlyStringStreamTest.php +21 -0
@@ 0,0 1,21 @@
<?php

declare(strict_types=1);

namespace Tests;

use Ancarda\Psr7\StringStream\ReadOnlyStringStream;
use PHPUnit\Framework\TestCase;
use RuntimeException;

class ReadOnlyStringStreamTest extends TestCase
{
    public function testReadOnly(): void
    {
        $stream = new ReadOnlyStringStream('read only string');
        static::assertFalse($stream->isWritable());

        $this->expectException(RuntimeException::class);
        $stream->write('Cannot write(): Read Only Stream');
    }
}