forked from reactphp/reactphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamTest.php
More file actions
137 lines (109 loc) · 3.33 KB
/
Copy pathStreamTest.php
File metadata and controls
137 lines (109 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace React\Tests\Stream;
use React\Stream\Stream;
use React\Tests\Socket\TestCase;
class StreamTest extends TestCase
{
/**
* @covers React\Stream\Stream::__construct
*/
public function testConstructor()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createLoopMock();
$conn = new Stream($stream, $loop);
}
/**
* @covers React\Stream\Stream::__construct
* @covers React\Stream\Stream::handleData
*/
public function testDataEvent()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createLoopMock();
$capturedData = null;
$conn = new Stream($stream, $loop);
$conn->on('data', function ($data) use (&$capturedData) {
$capturedData = $data;
});
fwrite($stream, "foobar\n");
rewind($stream);
$conn->handleData($stream);
$this->assertSame("foobar\n", $capturedData);
}
/**
* @covers React\Stream\Stream::write
*/
public function testWrite()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createWriteableLoopMock();
$conn = new Stream($stream, $loop);
$conn->write("foo\n");
rewind($stream);
$this->assertSame("foo\n", fgets($stream));
}
/**
* @covers React\Stream\Stream::write
*/
public function testWriteError()
{
$stream = "Silly developer, you can't write to to a string!";
$loop = $this->createWriteableLoopMock();
$conn = new Stream($stream, $loop);
$conn->on('error', $this->expectCallableOnce());
$conn->write('Attempting to write to a string');
}
/**
* @covers React\Stream\Stream::end
*/
public function testEnd()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createLoopMock();
$conn = new Stream($stream, $loop);
$conn->end();
$this->assertFalse(is_resource($stream));
}
public function testBufferEventsShouldBubbleUp()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createLoopMock();
$conn = new Stream($stream, $loop);
$conn->on('drain', $this->expectCallableOnce());
$conn->on('error', $this->expectCallableOnce());
$buffer = $conn->getBuffer();
$buffer->emit('drain');
$buffer->emit('error', array(new \RuntimeException('Whoops')));
}
/**
* @covers React\Stream\Stream::handleData
*/
public function testClosingStreamInDataEventShouldNotTriggerError()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createLoopMock();
$conn = new Stream($stream, $loop);
$conn->on('data', function ($data, $stream) {
$stream->close();
});
fwrite($stream, "foobar\n");
rewind($stream);
$conn->handleData($stream);
}
private function createWriteableLoopMock()
{
$loop = $this->createLoopMock();
$loop
->expects($this->once())
->method('addWriteStream')
->will($this->returnCallback(function ($stream, $listener) {
call_user_func($listener, $stream);
}));
return $loop;
}
private function createLoopMock()
{
return $this->getMock('React\EventLoop\LoopInterface');
}
}