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\ReadableStream;
6
use React\Stream\ThroughStream;
7
 
8
/**
9
 * @covers React\Stream\ThroughStream
10
 */
11
class ThroughStreamTest extends TestCase
12
{
13
    /** @test */
14
    public function itShouldEmitAnyDataWrittenToIt()
15
    {
16
        $through = new ThroughStream();
17
        $through->on('data', $this->expectCallableOnceWith('foo'));
18
        $through->write('foo');
19
    }
20
 
21
    /** @test */
22
    public function pipingStuffIntoItShouldWork()
23
    {
24
        $readable = new ReadableStream();
25
 
26
        $through = new ThroughStream();
27
        $through->on('data', $this->expectCallableOnceWith('foo'));
28
 
29
        $readable->pipe($through);
30
        $readable->emit('data', array('foo'));
31
    }
32
 
33
    /** @test */
34
    public function endShouldCloseTheStream()
35
    {
36
        $through = new ThroughStream();
37
        $through->on('data', $this->expectCallableNever());
38
        $through->end();
39
 
40
        $this->assertFalse($through->isReadable());
41
        $this->assertFalse($through->isWritable());
42
    }
43
 
44
    /** @test */
45
    public function endShouldWriteDataBeforeClosing()
46
    {
47
        $through = new ThroughStream();
48
        $through->on('data', $this->expectCallableOnceWith('foo'));
49
        $through->end('foo');
50
 
51
        $this->assertFalse($through->isReadable());
52
        $this->assertFalse($through->isWritable());
53
    }
54
 
55
    /** @test */
56
    public function itShouldBeReadableByDefault()
57
    {
58
        $through = new ThroughStream();
59
        $this->assertTrue($through->isReadable());
60
    }
61
 
62
    /** @test */
63
    public function itShouldBeWritableByDefault()
64
    {
65
        $through = new ThroughStream();
66
        $this->assertTrue($through->isWritable());
67
    }
68
 
69
    /** @test */
70
    public function pauseShouldDelegateToPipeSource()
71
    {
72
        $input = $this->getMock('React\Stream\ReadableStream', array('pause'));
73
        $input
74
            ->expects($this->once())
75
            ->method('pause');
76
 
77
        $through = new ThroughStream();
78
        $input->pipe($through);
79
 
80
        $through->pause();
81
    }
82
 
83
    /** @test */
84
    public function resumeShouldDelegateToPipeSource()
85
    {
86
        $input = $this->getMock('React\Stream\ReadableStream', array('resume'));
87
        $input
88
            ->expects($this->once())
89
            ->method('resume');
90
 
91
        $through = new ThroughStream();
92
        $input->pipe($through);
93
 
94
        $through->resume();
95
    }
96
 
97
    /** @test */
98
    public function closeShouldClose()
99
    {
100
        $through = new ThroughStream();
101
        $through->close();
102
 
103
        $this->assertFalse($through->isReadable());
104
        $this->assertFalse($through->isWritable());
105
    }
106
 
107
    /** @test */
108
    public function doubleCloseShouldWork()
109
    {
110
        $through = new ThroughStream();
111
        $through->close();
112
        $through->close();
113
 
114
        $this->assertFalse($through->isReadable());
115
        $this->assertFalse($through->isWritable());
116
    }
117
 
118
    /** @test */
119
    public function pipeShouldPipeCorrectly()
120
    {
121
        $output = $this->getMock('React\Stream\WritableStreamInterface');
122
        $output
123
            ->expects($this->once())
124
            ->method('write')
125
            ->with('foo');
126
 
127
        $through = new ThroughStream();
128
        $through->pipe($output);
129
        $through->write('foo');
130
    }
131
 
132
    protected function expectCallableOnceWith($arg)
133
    {
134
        $mock = $this->createCallableMock();
135
        $mock
136
            ->expects($this->once())
137
            ->method('__invoke')
138
            ->with($arg);
139
 
140
        return $mock;
141
    }
142
}