Skip to content

Commit d9beaa5

Browse files
committed
update Stream to throw exception on invalid passed to constructor
1 parent acf44b4 commit d9beaa5

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/Stream.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Evenement\EventEmitter;
66
use React\EventLoop\LoopInterface;
7+
use InvalidArgumentException;
78

89
class Stream extends EventEmitter implements DuplexStreamInterface
910
{
@@ -18,10 +19,12 @@ class Stream extends EventEmitter implements DuplexStreamInterface
1819
public function __construct($stream, LoopInterface $loop)
1920
{
2021
$this->stream = $stream;
21-
if (is_resource($this->stream) && get_resource_type($this->stream) === "stream") {
22-
stream_set_blocking($this->stream, 0);
22+
if (!is_resource($this->stream) || get_resource_type($this->stream) !== "stream") {
23+
throw new InvalidArgumentException('Invalid stream resource passed as the first argument to '. __METHOD__);
2324
}
2425

26+
stream_set_blocking($this->stream, 0);
27+
2528
$this->loop = $loop;
2629
$this->buffer = new Buffer($this->stream, $this->loop);
2730

tests/StreamTest.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public function testConstructor()
1717
$conn = new Stream($stream, $loop);
1818
}
1919

20+
/**
21+
* @covers React\Stream\Stream::__construct
22+
*/
23+
public function testConstructorThrowsExceptionOnInvalidStream()
24+
{
25+
$this->setExpectedException('InvalidArgumentException');
26+
$loop = $this->createLoopMock();
27+
$conn = new Stream('breakme', $loop);
28+
}
29+
2030
/**
2131
* @covers React\Stream\Stream::__construct
2232
* @covers React\Stream\Stream::handleData
@@ -55,19 +65,6 @@ public function testWrite()
5565
$this->assertSame("foo\n", fgets($stream));
5666
}
5767

58-
/**
59-
* @covers React\Stream\Stream::write
60-
*/
61-
public function testWriteError()
62-
{
63-
$stream = "Silly developer, you can't write to to a string!";
64-
$loop = $this->createWriteableLoopMock();
65-
66-
$conn = new Stream($stream, $loop);
67-
$conn->on('error', $this->expectCallableOnce());
68-
$conn->write('Attempting to write to a string');
69-
}
70-
7168
/**
7269
* @covers React\Stream\Stream::end
7370
*/

0 commit comments

Comments
 (0)