forked from clue/php-sse-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferedChannelTest.php
More file actions
66 lines (50 loc) · 1.95 KB
/
Copy pathBufferedChannelTest.php
File metadata and controls
66 lines (50 loc) · 1.95 KB
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
<?php
namespace Clue\React\Tests\Sse;
use Clue\React\Sse\BufferedChannel;
class BufferedChannelTest extends TestCase
{
public function testNumberOfWritesToStream()
{
$stream = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
$called = 0;
$stream->expects($this->any())->method('write')->will($this->returnCallback(function () use (&$called) {
++$called;
}));
$channel = new BufferedChannel();
// initially nothing written
$channel->connect($stream);
$this->assertEquals(0, $called);
// writing does send
$channel->writeMessage('first');
$this->assertEquals(1, $called);
// writing does send again
$channel->writeMessage('second');
$this->assertEquals(2, $called);
// writing after disconnect does not send
$channel->disconnect($stream);
$channel->writeMessage('third');
$this->assertEquals(2, $called);
// connecting does not send
$channel->connect($stream);
$this->assertEquals(2, $called);
// connecting with offset will send remaining message
$channel->disconnect($stream);
$channel->connect($stream, 2);
$this->assertEquals(3, $called);
}
public function testResultingStreamBuffer()
{
$stream = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
$buffered = '';
$stream->expects($this->any())->method('write')->will($this->returnCallback(function ($data) use (&$buffered) {
$buffered .= $data;
}));
$channel = new BufferedChannel();
// initially nothing
$channel->writeMessage('hello', 'world');
$this->assertEquals('', $buffered);
// connecting will send messages buffered in channel
$channel->connect($stream, 0);
$this->assertEquals("id: 0\nevent: world\ndata: hello\n\n", $buffered);
}
}