Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace React\Tests\Stream;
4
 
5
use React\Stream\Stream;
6
 
7
class StreamTest extends TestCase
8
{
9
    /**
10
     * @covers React\Stream\Stream::__construct
11
     */
12
    public function testConstructor()
13
    {
14
        $stream = fopen('php://temp', 'r+');
15
        $loop = $this->createLoopMock();
16
 
17
        $conn = new Stream($stream, $loop);
18
    }
19
 
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
 
30
    /**
31
     * @covers React\Stream\Stream::__construct
32
     * @covers React\Stream\Stream::handleData
33
     */
34
    public function testDataEvent()
35
    {
36
        $stream = fopen('php://temp', 'r+');
37
        $loop = $this->createLoopMock();
38
 
39
        $capturedData = null;
40
 
41
        $conn = new Stream($stream, $loop);
42
        $conn->on('data', function ($data) use (&$capturedData) {
43
            $capturedData = $data;
44
        });
45
 
46
        fwrite($stream, "foobar\n");
47
        rewind($stream);
48
 
49
        $conn->handleData($stream);
50
        $this->assertSame("foobar\n", $capturedData);
51
    }
52
 
53
    /**
54
     * @covers React\Stream\Stream::write
55
     */
56
    public function testWrite()
57
    {
58
        $stream = fopen('php://temp', 'r+');
59
        $loop = $this->createWriteableLoopMock();
60
 
61
        $conn = new Stream($stream, $loop);
62
        $conn->write("foo\n");
63
 
64
        rewind($stream);
65
        $this->assertSame("foo\n", fgets($stream));
66
    }
67
 
68
    /**
69
     * @covers React\Stream\Stream::end
70
     */
71
    public function testEnd()
72
    {
73
        $stream = fopen('php://temp', 'r+');
74
        $loop = $this->createLoopMock();
75
 
76
        $conn = new Stream($stream, $loop);
77
        $conn->end();
78
 
79
        $this->assertFalse(is_resource($stream));
80
    }
81
 
82
    public function testBufferEventsShouldBubbleUp()
83
    {
84
        $stream = fopen('php://temp', 'r+');
85
        $loop = $this->createLoopMock();
86
 
87
        $conn = new Stream($stream, $loop);
88
 
89
        $conn->on('drain', $this->expectCallableOnce());
90
        $conn->on('error', $this->expectCallableOnce());
91
 
92
        $buffer = $conn->getBuffer();
93
        $buffer->emit('drain');
94
        $buffer->emit('error', array(new \RuntimeException('Whoops')));
95
    }
96
 
97
    /**
98
     * @covers React\Stream\Stream::handleData
99
     */
100
    public function testClosingStreamInDataEventShouldNotTriggerError()
101
    {
102
        $stream = fopen('php://temp', 'r+');
103
        $loop = $this->createLoopMock();
104
 
105
        $conn = new Stream($stream, $loop);
106
        $conn->on('data', function ($data, $stream) {
107
            $stream->close();
108
        });
109
 
110
        fwrite($stream, "foobar\n");
111
        rewind($stream);
112
 
113
        $conn->handleData($stream);
114
    }
115
 
116
    private function createWriteableLoopMock()
117
    {
118
        $loop = $this->createLoopMock();
119
        $loop
120
            ->expects($this->once())
121
            ->method('addWriteStream')
122
            ->will($this->returnCallback(function ($stream, $listener) {
123
                call_user_func($listener, $stream);
124
            }));
125
 
126
        return $loop;
127
    }
128
 
129
    private function createLoopMock()
130
    {
131
        return $this->getMock('React\EventLoop\LoopInterface');
132
    }
133
}