diff --git a/.gitignore b/.gitignore index fb6c963..b93909d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .php_cs.cache build +coverage composer.lock phpunit.xml vendor diff --git a/lib/ResourceOutputStream.php b/lib/ResourceOutputStream.php index dab9dd1..5bb2d90 100644 --- a/lib/ResourceOutputStream.php +++ b/lib/ResourceOutputStream.php @@ -28,7 +28,10 @@ class ResourceOutputStream implements OutputStream { $meta = \stream_get_meta_data($stream); - if (isset($meta["mode"]) && $meta["mode"] === "r") { + if (isset($meta["mode"]) + && \strpos($meta["mode"], "r") !== false + && \strpos($meta["mode"], "+") === false + ) { throw new \Error("Expected a writable stream"); } diff --git a/test/MessageTest.php b/test/MessageTest.php index 04de2df..6aa650e 100644 --- a/test/MessageTest.php +++ b/test/MessageTest.php @@ -2,8 +2,10 @@ namespace Amp\ByteStream\Test; +use Amp\ByteStream\InMemoryStream; use Amp\ByteStream\IteratorStream; use Amp\ByteStream\Message; +use Amp\ByteStream\PendingReadError; use Amp\Emitter; use Amp\Loop; use Amp\PHPUnit\TestCase; @@ -159,4 +161,39 @@ class MessageTest extends TestCase { $this->assertNull(yield $stream->read()); }); } + + public function testGetInputStream() { + Loop::run(function () { + $inputStream = new InMemoryStream(""); + $message = new Message($inputStream); + + $this->assertSame($inputStream, $message->getInputStream()); + $this->assertSame("", yield $message->getInputStream()->read()); + }); + } + + public function testPendingRead() { + Loop::run(function () { + $emitter = new Emitter; + $stream = new Message(new IteratorStream($emitter->iterate())); + + Loop::delay(0, function () use ($emitter) { + $emitter->emit("test"); + }); + + $this->assertSame("test", yield $stream->read()); + }); + } + + public function testPendingReadError() { + Loop::run(function () { + $emitter = new Emitter; + $stream = new Message(new IteratorStream($emitter->iterate())); + $stream->read(); + + $this->expectException(PendingReadError::class); + + $stream->read(); + }); + } } diff --git a/test/ResourceInputStreamTest.php b/test/ResourceInputStreamTest.php new file mode 100644 index 0000000..9f3ab2e --- /dev/null +++ b/test/ResourceInputStreamTest.php @@ -0,0 +1,28 @@ +assertSame(\STDIN, $stream->getResource()); + } + + public function testNonStream() { + $this->expectException(\Error::class); + $this->expectExceptionMessage("Expected a valid stream"); + + new ResourceInputStream(42); + } + + public function testNotReadable() { + $this->expectException(\Error::class); + $this->expectExceptionMessage("Expected a readable stream"); + + new ResourceInputStream(\STDOUT); + } +} diff --git a/test/ResourceOutputStreamTest.php b/test/ResourceOutputStreamTest.php new file mode 100644 index 0000000..3949fe5 --- /dev/null +++ b/test/ResourceOutputStreamTest.php @@ -0,0 +1,28 @@ +assertSame(\STDOUT, $stream->getResource()); + } + + public function testNonStream() { + $this->expectException(\Error::class); + $this->expectExceptionMessage("Expected a valid stream"); + + new ResourceOutputStream(42); + } + + public function testNotWritable() { + $this->expectException(\Error::class); + $this->expectExceptionMessage("Expected a writable stream"); + + new ResourceOutputStream(\STDIN); + } +}