Skip to content

Commit

Permalink
Add tests, fix ResourceOutputStream writable check
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed May 16, 2017
1 parent ad249a8 commit 9e90d1f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.php_cs.cache
build
coverage
composer.lock
phpunit.xml
vendor
5 changes: 4 additions & 1 deletion lib/ResourceOutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public function __construct($stream, int $chunkSize = 8192) {

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

Expand Down
37 changes: 37 additions & 0 deletions test/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -159,4 +161,39 @@ public function testReadAfterCompletion() {
$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 changes: 28 additions & 0 deletions test/ResourceInputStreamTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 28 additions & 0 deletions test/ResourceOutputStreamTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 9e90d1f

Please sign in to comment.