forked from clue/reactphp-zlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformStreamTest.php
198 lines (151 loc) · 5.08 KB
/
TransformStreamTest.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Clue\Tests\React\Zlib;
use Clue\React\Zlib\TransformStream;
use React\Stream\ThroughStream;
class TransformStreamTest extends TestCase
{
public function testForwardsDataAndEndClosesStream()
{
$stream = new TransformStream();
$stream->on('data', function ($chunk) use (&$buffered) {
$buffered .= $chunk;
});
$stream->on('end', $this->expectCallableOnce());
$stream->on('close', $this->expectCallableOnce());
$stream->on('error', $this->expectCallableNever());
$stream->write('hello');
$stream->end('world');
$this->assertEquals('helloworld', $buffered);
}
public function testEndWithNoDataEmitsEndAndClose()
{
$stream = new TransformStream();
$stream->on('end', $this->expectCallableOnce());
$stream->on('close', $this->expectCallableOnce());
$stream->on('data', $this->expectCallableNever());
$stream->on('error', $this->expectCallableNever());
$stream->end();
return $stream;
}
/**
* @depends testEndWithNoDataEmitsEndAndClose
* @param TransformStream $stream
*/
public function testDoesNotEmitIfAlreadyEnded(TransformStream $stream)
{
$stream->on('data', $this->expectCallableNever());
$stream->on('error', $this->expectCallableNever());
$stream->on('end', $this->expectCallableNever());
$stream->on('close', $this->expectCallableNever());
$stream->write('hello');
$stream->end();
$stream->close();
}
public function testCloseRemovesListeners()
{
$stream = new TransformStream();
$this->assertCount(0, $stream->listeners('close'));
$stream->on('close', $this->expectCallableOnce());
$this->assertCount(1, $stream->listeners('close'));
$stream->close();
$this->assertCount(0, $stream->listeners('close'));
}
public function testCloseOnlyEmitsCloses()
{
$stream = new TransformStream();
$stream->on('close', $this->expectCallableOnce());
$stream->on('data', $this->expectCallableNever());
$stream->on('end', $this->expectCallableNever());
$stream->on('error', $this->expectCallableNever());
$stream->close();
return $stream;
}
/**
* @depends testCloseOnlyEmitsCloses
* @param TransformStream $stream
*/
public function testDoesNotEmitIfAlreadyClosed(TransformStream $stream)
{
$stream->on('data', $this->expectCallableNever());
$stream->on('error', $this->expectCallableNever());
$stream->on('end', $this->expectCallableNever());
$stream->on('close', $this->expectCallableNever());
$stream->write('hello');
$stream->end();
$stream->close();
}
public function testWriteReturnsTrueNormally()
{
$stream = new TransformStream();
$ret = $stream->write('hello');
$this->assertTrue($ret);
}
public function testWriteEmptyStringReturnsTrueNormally()
{
$stream = new TransformStream();
$ret = $stream->write('');
$this->assertTrue($ret);
}
public function testWriteReturnsFalseWhenClosed()
{
$stream = new TransformStream();
$stream->close();
$ret = $stream->write('hello');
$this->assertFalse($ret);
}
public function testWriteEmptyStringReturnsFalseWhenClosed()
{
$stream = new TransformStream();
$stream->close();
$ret = $stream->write('');
$this->assertFalse($ret);
}
public function testWriteReturnsFalseWhenPaused()
{
$stream = new TransformStream();
$stream->pause();
$ret = $stream->write('hello');
$this->assertFalse($ret);
}
public function testWriteReturnsTrueWhenResumedAgain()
{
$stream = new TransformStream();
$stream->pause();
$stream->resume();
$ret = $stream->write('hello');
$this->assertTrue($ret);
}
public function testResumeEmitsDrainEventWhenPreviousWriteReturnedFalse()
{
$stream = new TransformStream();
$stream->pause();
$stream->write('hello');
$stream->on('drain', $this->expectCallableOnce());
$stream->resume();
}
public function testResumeDoesNotEmitDrainEventWhenNoPreviousWriteReturnedFalse()
{
$stream = new TransformStream();
$stream->pause();
$stream->on('drain', $this->expectCallableNever());
$stream->resume();
}
public function testPauseAndResumeIsNoOpWhenClosed()
{
$stream = new TransformStream();
$stream->close();
$stream->on('drain', $this->expectCallableNever());
$stream->pause();
$stream->resume();
}
public function testSupportsBackPressureInPipeChain()
{
$source = new ThroughStream();
$dest = new ThroughStream();
$dest->pause();
$stream = new TransformStream();
$source->pipe($stream)->pipe($dest);
$ret = $source->write('hello');
$this->assertFalse($ret);
}
}