mirror of
https://github.com/danog/byte-stream.git
synced 2024-11-26 11:54:54 +01:00
Add tests, fix ResourceOutputStream writable check
This commit is contained in:
parent
ad249a8fc8
commit
9e90d1fa09
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
.php_cs.cache
|
||||
build
|
||||
coverage
|
||||
composer.lock
|
||||
phpunit.xml
|
||||
vendor
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
28
test/ResourceInputStreamTest.php
Normal file
28
test/ResourceInputStreamTest.php
Normal 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);
|
||||
}
|
||||
}
|
28
test/ResourceOutputStreamTest.php
Normal file
28
test/ResourceOutputStreamTest.php
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user