Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 915 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 liveuser 1
<?php
2
 
3
namespace React\Tests\Stream;
4
 
5
use React\Stream\Buffer;
6
use React\Stream\ReadableStream;
7
use React\Stream\Util;
8
 
9
/**
10
 * @covers React\Stream\Util
11
 */
12
class UtilTest extends TestCase
13
{
14
    public function testPipeShouldEmitEvents()
15
    {
16
        $readable = $this->getMock('React\Stream\ReadableStreamInterface');
17
        $readable
18
            ->expects($this->at(0))
19
            ->method('on')
20
            ->with('data', $this->isInstanceOf('Closure'));
21
        $readable
22
            ->expects($this->at(1))
23
            ->method('on')
24
            ->with('end', $this->isInstanceOf('Closure'));
25
 
26
        $writable = $this->getMock('React\Stream\WritableStreamInterface');
27
        $writable
28
            ->expects($this->at(0))
29
            ->method('emit')
30
            ->with('pipe', array($readable));
31
 
32
        Util::pipe($readable, $writable);
33
    }
34
 
35
    public function testPipeWithEnd()
36
    {
37
        $readable = new Stub\ReadableStreamStub();
38
 
39
        $writable = $this->getMock('React\Stream\WritableStreamInterface');
40
        $writable
41
            ->expects($this->once())
42
            ->method('end');
43
 
44
        Util::pipe($readable, $writable);
45
 
46
        $readable->end();
47
    }
48
 
49
    public function testPipeWithoutEnd()
50
    {
51
        $readable = new Stub\ReadableStreamStub();
52
 
53
        $writable = $this->getMock('React\Stream\WritableStreamInterface');
54
        $writable
55
            ->expects($this->never())
56
            ->method('end');
57
 
58
        Util::pipe($readable, $writable, array('end' => false));
59
 
60
        $readable->end();
61
    }
62
 
63
    public function testPipeWithTooSlowWritableShouldPauseReadable()
64
    {
65
        $readable = new Stub\ReadableStreamStub();
66
 
67
        $writable = $this->getMock('React\Stream\WritableStreamInterface');
68
        $writable
69
            ->expects($this->once())
70
            ->method('write')
71
            ->with('some data')
72
            ->will($this->returnValue(false));
73
 
74
        $readable->pipe($writable);
75
 
76
        $this->assertFalse($readable->paused);
77
        $readable->write('some data');
78
        $this->assertTrue($readable->paused);
79
    }
80
 
81
    public function testPipeWithTooSlowWritableShouldResumeOnDrain()
82
    {
83
        $readable = new Stub\ReadableStreamStub();
84
 
85
        $onDrain = null;
86
 
87
        $writable = $this->getMock('React\Stream\WritableStreamInterface');
88
        $writable
89
            ->expects($this->once())
90
            ->method('on')
91
            ->with('drain', $this->isInstanceOf('Closure'))
92
            ->will($this->returnCallback(function ($name, $callback) use (&$onDrain) {
93
                $onDrain = $callback;
94
            }));
95
 
96
        $readable->pipe($writable);
97
        $readable->pause();
98
 
99
        $this->assertTrue($readable->paused);
100
        $onDrain();
101
        $this->assertFalse($readable->paused);
102
    }
103
 
104
    public function testPipeWithBuffer()
105
    {
106
        $readable = new Stub\ReadableStreamStub();
107
 
108
        $stream = fopen('php://temp', 'r+');
109
        $loop = $this->createWriteableLoopMock();
110
        $buffer = new Buffer($stream, $loop);
111
 
112
        $readable->pipe($buffer);
113
 
114
        $readable->write('hello, I am some ');
115
        $readable->write('random data');
116
 
117
        rewind($stream);
118
        $this->assertSame('hello, I am some random data', stream_get_contents($stream));
119
    }
120
 
121
    /** @test */
122
    public function forwardEventsShouldSetupForwards()
123
    {
124
        $source = new ReadableStream();
125
        $target = new ReadableStream();
126
 
127
        Util::forwardEvents($source, $target, array('data'));
128
        $target->on('data', $this->expectCallableOnce());
129
        $target->on('foo', $this->expectCallableNever());
130
 
131
        $source->emit('data', array('hello'));
132
        $source->emit('foo', array('bar'));
133
    }
134
 
135
    private function createWriteableLoopMock()
136
    {
137
        $loop = $this->createLoopMock();
138
        $loop
139
            ->expects($this->any())
140
            ->method('addWriteStream')
141
            ->will($this->returnCallback(function ($stream, $listener) {
142
                call_user_func($listener, $stream);
143
            }));
144
 
145
        return $loop;
146
    }
147
 
148
    private function createLoopMock()
149
    {
150
        return $this->getMock('React\EventLoop\LoopInterface');
151
    }
152
 
153
    private function notEqualTo($value)
154
    {
155
        return new \PHPUnit_Framework_Constraint_Not($value);
156
    }
157
}