1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-11-30 04:19:23 +01:00

Add tests, fix ResourceOutputStream writable check

This commit is contained in:
Niklas Keller 2017-05-16 23:27:48 +02:00
parent ad249a8fc8
commit 9e90d1fa09
5 changed files with 98 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.php_cs.cache
build
coverage
composer.lock
phpunit.xml
vendor

View File

@ -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");
}

View File

@ -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();
});
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Amp\ByteStream\Test;
use Amp\ByteStream\ResourceInputStream;
use PHPUnit\Framework\TestCase;
class ResourceInputStreamTest extends TestCase {
public function testGetResource() {
$stream = new ResourceInputStream(\STDIN);
$this->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);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Amp\ByteStream\Test;
use Amp\ByteStream\ResourceOutputStream;
use PHPUnit\Framework\TestCase;
class ResourceOutputStreamTest extends TestCase {
public function testGetResource() {
$stream = new ResourceOutputStream(\STDOUT);
$this->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);
}
}