| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace React\Tests\Stream;
|
|
|
4 |
|
|
|
5 |
use React\Stream\CompositeStream;
|
|
|
6 |
use React\Stream\ReadableStream;
|
|
|
7 |
use React\Stream\WritableStream;
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* @covers React\Stream\CompositeStream
|
|
|
11 |
*/
|
|
|
12 |
class CompositeStreamTest extends TestCase
|
|
|
13 |
{
|
|
|
14 |
/** @test */
|
|
|
15 |
public function itShouldForwardWritableCallsToWritableStream()
|
|
|
16 |
{
|
|
|
17 |
$readable = $this->getMock('React\Stream\ReadableStreamInterface');
|
|
|
18 |
$writable = $this->getMock('React\Stream\WritableStreamInterface');
|
|
|
19 |
$writable
|
|
|
20 |
->expects($this->once())
|
|
|
21 |
->method('write')
|
|
|
22 |
->with('foo');
|
|
|
23 |
$writable
|
|
|
24 |
->expects($this->once())
|
|
|
25 |
->method('isWritable');
|
|
|
26 |
|
|
|
27 |
$composite = new CompositeStream($readable, $writable);
|
|
|
28 |
$composite->write('foo');
|
|
|
29 |
$composite->isWritable();
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
/** @test */
|
|
|
33 |
public function itShouldForwardReadableCallsToReadableStream()
|
|
|
34 |
{
|
|
|
35 |
$readable = $this->getMock('React\Stream\ReadableStreamInterface');
|
|
|
36 |
$readable
|
|
|
37 |
->expects($this->once())
|
|
|
38 |
->method('isReadable');
|
|
|
39 |
$readable
|
|
|
40 |
->expects($this->once())
|
|
|
41 |
->method('pause');
|
|
|
42 |
$readable
|
|
|
43 |
->expects($this->once())
|
|
|
44 |
->method('resume');
|
|
|
45 |
$writable = $this->getMock('React\Stream\WritableStreamInterface');
|
|
|
46 |
|
|
|
47 |
$composite = new CompositeStream($readable, $writable);
|
|
|
48 |
$composite->isReadable();
|
|
|
49 |
$composite->pause();
|
|
|
50 |
$composite->resume();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/** @test */
|
|
|
54 |
public function endShouldDelegateToWritableWithData()
|
|
|
55 |
{
|
|
|
56 |
$readable = $this->getMock('React\Stream\ReadableStreamInterface');
|
|
|
57 |
$writable = $this->getMock('React\Stream\WritableStreamInterface');
|
|
|
58 |
$writable
|
|
|
59 |
->expects($this->once())
|
|
|
60 |
->method('end')
|
|
|
61 |
->with('foo');
|
|
|
62 |
|
|
|
63 |
$composite = new CompositeStream($readable, $writable);
|
|
|
64 |
$composite->end('foo');
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/** @test */
|
|
|
68 |
public function closeShouldCloseBothStreams()
|
|
|
69 |
{
|
|
|
70 |
$readable = $this->getMock('React\Stream\ReadableStreamInterface');
|
|
|
71 |
$readable
|
|
|
72 |
->expects($this->once())
|
|
|
73 |
->method('close');
|
|
|
74 |
$writable = $this->getMock('React\Stream\WritableStreamInterface');
|
|
|
75 |
$writable
|
|
|
76 |
->expects($this->once())
|
|
|
77 |
->method('close');
|
|
|
78 |
|
|
|
79 |
$composite = new CompositeStream($readable, $writable);
|
|
|
80 |
$composite->close();
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/** @test */
|
|
|
84 |
public function itShouldReceiveForwardedEvents()
|
|
|
85 |
{
|
|
|
86 |
$readable = new ReadableStream();
|
|
|
87 |
$writable = new WritableStream();
|
|
|
88 |
|
|
|
89 |
$composite = new CompositeStream($readable, $writable);
|
|
|
90 |
$composite->on('data', $this->expectCallableOnce());
|
|
|
91 |
$composite->on('drain', $this->expectCallableOnce());
|
|
|
92 |
|
|
|
93 |
$readable->emit('data', array('foo'));
|
|
|
94 |
$writable->emit('drain');
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/** @test */
|
|
|
98 |
public function itShouldHandlePipingCorrectly()
|
|
|
99 |
{
|
|
|
100 |
$readable = $this->getMock('React\Stream\ReadableStreamInterface');
|
|
|
101 |
$writable = $this->getMock('React\Stream\WritableStreamInterface');
|
|
|
102 |
$writable
|
|
|
103 |
->expects($this->once())
|
|
|
104 |
->method('write')
|
|
|
105 |
->with('foo');
|
|
|
106 |
|
|
|
107 |
$composite = new CompositeStream($readable, $writable);
|
|
|
108 |
|
|
|
109 |
$input = new ReadableStream();
|
|
|
110 |
$input->pipe($composite);
|
|
|
111 |
$input->emit('data', array('foo'));
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/** @test */
|
|
|
115 |
public function itShouldForwardPauseAndResumeUpstreamWhenPipedTo()
|
|
|
116 |
{
|
|
|
117 |
$readable = $this->getMock('React\Stream\ReadableStreamInterface');
|
|
|
118 |
$writable = $this->getMock('React\Stream\WritableStream', array('write'));
|
|
|
119 |
$writable
|
|
|
120 |
->expects($this->once())
|
|
|
121 |
->method('write')
|
|
|
122 |
->will($this->returnValue(false));
|
|
|
123 |
|
|
|
124 |
$composite = new CompositeStream($readable, $writable);
|
|
|
125 |
|
|
|
126 |
$input = $this->getMock('React\Stream\ReadableStream', array('pause', 'resume'));
|
|
|
127 |
$input
|
|
|
128 |
->expects($this->once())
|
|
|
129 |
->method('pause');
|
|
|
130 |
$input
|
|
|
131 |
->expects($this->once())
|
|
|
132 |
->method('resume');
|
|
|
133 |
|
|
|
134 |
$input->pipe($composite);
|
|
|
135 |
$input->emit('data', array('foo'));
|
|
|
136 |
$writable->emit('drain');
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/** @test */
|
|
|
140 |
public function itShouldForwardPipeCallsToReadableStream()
|
|
|
141 |
{
|
|
|
142 |
$readable = new ReadableStream();
|
|
|
143 |
$writable = $this->getMock('React\Stream\WritableStreamInterface');
|
|
|
144 |
$composite = new CompositeStream($readable, $writable);
|
|
|
145 |
|
|
|
146 |
$output = $this->getMock('React\Stream\WritableStreamInterface');
|
|
|
147 |
$output
|
|
|
148 |
->expects($this->once())
|
|
|
149 |
->method('write')
|
|
|
150 |
->with('foo');
|
|
|
151 |
|
|
|
152 |
$composite->pipe($output);
|
|
|
153 |
$readable->emit('data', array('foo'));
|
|
|
154 |
}
|
|
|
155 |
}
|