Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
namespace Ratchet\Wamp;
3
 
4
/**
5
 * @covers Ratchet\Wamp\Topic
6
 */
7
class TopicTest extends \PHPUnit_Framework_TestCase {
8
    public function testGetId() {
9
        $id    = uniqid();
10
        $topic = new Topic($id);
11
 
12
        $this->assertEquals($id, $topic->getId());
13
    }
14
 
15
    public function testAddAndCount() {
16
        $topic = new Topic('merp');
17
 
18
        $topic->add($this->newConn());
19
        $topic->add($this->newConn());
20
        $topic->add($this->newConn());
21
 
22
        $this->assertEquals(3, count($topic));
23
    }
24
 
25
    public function testRemove() {
26
        $topic   = new Topic('boop');
27
        $tracked = $this->newConn();
28
 
29
        $topic->add($this->newConn());
30
        $topic->add($tracked);
31
        $topic->add($this->newConn());
32
 
33
        $topic->remove($tracked);
34
 
35
        $this->assertEquals(2, count($topic));
36
    }
37
 
38
    public function testBroadcast() {
39
        $msg  = 'Hello World!';
40
        $name = 'Batman';
41
        $protocol = json_encode(array(8, $name, $msg));
42
 
43
        $first  = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
44
        $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
45
 
46
        $first->expects($this->once())
47
              ->method('send')
48
              ->with($this->equalTo($protocol));
49
 
50
        $second->expects($this->once())
51
              ->method('send')
52
              ->with($this->equalTo($protocol));
53
 
54
        $topic = new Topic($name);
55
        $topic->add($first);
56
        $topic->add($second);
57
 
58
        $topic->broadcast($msg);
59
    }
60
 
61
    public function testBroadcastWithExclude() {
62
        $msg  = 'Hello odd numbers';
63
        $name = 'Excluding';
64
        $protocol = json_encode(array(8, $name, $msg));
65
 
66
        $first  = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
67
        $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
68
        $third  = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
69
 
70
        $first->expects($this->once())
71
            ->method('send')
72
            ->with($this->equalTo($protocol));
73
 
74
        $second->expects($this->never())->method('send');
75
 
76
        $third->expects($this->once())
77
            ->method('send')
78
            ->with($this->equalTo($protocol));
79
 
80
        $topic = new Topic($name);
81
        $topic->add($first);
82
        $topic->add($second);
83
        $topic->add($third);
84
 
85
        $topic->broadcast($msg, array($second->WAMP->sessionId));
86
    }
87
 
88
    public function testBroadcastWithEligible() {
89
        $msg  = 'Hello white list';
90
        $name = 'Eligible';
91
        $protocol = json_encode(array(8, $name, $msg));
92
 
93
        $first  = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
94
        $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
95
        $third  = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
96
 
97
        $first->expects($this->once())
98
            ->method('send')
99
            ->with($this->equalTo($protocol));
100
 
101
        $second->expects($this->never())->method('send');
102
 
103
        $third->expects($this->once())
104
            ->method('send')
105
            ->with($this->equalTo($protocol));
106
 
107
        $topic = new Topic($name);
108
        $topic->add($first);
109
        $topic->add($second);
110
        $topic->add($third);
111
 
112
        $topic->broadcast($msg, array(), array($first->WAMP->sessionId, $third->WAMP->sessionId));
113
    }
114
 
115
    public function testIterator() {
116
        $first  = $this->newConn();
117
        $second = $this->newConn();
118
        $third  = $this->newConn();
119
 
120
        $topic  = new Topic('Joker');
121
        $topic->add($first)->add($second)->add($third);
122
 
123
        $check = array($first, $second, $third);
124
 
125
        foreach ($topic as $mock) {
126
            $this->assertNotSame(false, array_search($mock, $check));
127
        }
128
    }
129
 
130
    public function testToString() {
131
        $name  = 'Bane';
132
        $topic = new Topic($name);
133
 
134
        $this->assertEquals($name, (string)$topic);
135
    }
136
 
137
    public function testDoesHave() {
138
        $conn  = $this->newConn();
139
        $topic = new Topic('Two Face');
140
        $topic->add($conn);
141
 
142
        $this->assertTrue($topic->has($conn));
143
    }
144
 
145
    public function testDoesNotHave() {
146
        $conn  = $this->newConn();
147
        $topic = new Topic('Alfred');
148
 
149
        $this->assertFalse($topic->has($conn));
150
    }
151
 
152
    public function testDoesNotHaveAfterRemove() {
153
        $conn  = $this->newConn();
154
        $topic = new Topic('Ras');
155
 
156
        $topic->add($conn)->remove($conn);
157
 
158
        $this->assertFalse($topic->has($conn));
159
    }
160
 
161
    protected function newConn() {
162
        return new WampConnection($this->getMock('\\Ratchet\\ConnectionInterface'));
163
    }
164
}