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\TopicManager
6
 */
7
class TopicManagerTest extends \PHPUnit_Framework_TestCase {
8
    private $mock;
9
 
10
    /**
11
     * @var \Ratchet\Wamp\TopicManager
12
     */
13
    private $mngr;
14
 
15
    /**
16
     * @var \Ratchet\ConnectionInterface
17
     */
18
    private $conn;
19
 
20
    public function setUp() {
21
        $this->conn = $this->getMock('\Ratchet\ConnectionInterface');
22
        $this->mock = $this->getMock('\Ratchet\Wamp\WampServerInterface');
23
        $this->mngr = new TopicManager($this->mock);
24
 
25
        $this->conn->WAMP = new \StdClass;
26
        $this->mngr->onOpen($this->conn);
27
    }
28
 
29
    public function testGetTopicReturnsTopicObject() {
30
        $class  = new \ReflectionClass('Ratchet\Wamp\TopicManager');
31
        $method = $class->getMethod('getTopic');
32
        $method->setAccessible(true);
33
 
34
        $topic = $method->invokeArgs($this->mngr, array('The Topic'));
35
 
36
        $this->assertInstanceOf('Ratchet\Wamp\Topic', $topic);
37
    }
38
 
39
    public function testGetTopicCreatesTopicWithSameName() {
40
        $name = 'The Topic';
41
 
42
        $class  = new \ReflectionClass('Ratchet\Wamp\TopicManager');
43
        $method = $class->getMethod('getTopic');
44
        $method->setAccessible(true);
45
 
46
        $topic = $method->invokeArgs($this->mngr, array($name));
47
 
48
        $this->assertEquals($name, $topic->getId());
49
    }
50
 
51
    public function testGetTopicReturnsSameObject() {
52
        $class  = new \ReflectionClass('Ratchet\Wamp\TopicManager');
53
        $method = $class->getMethod('getTopic');
54
        $method->setAccessible(true);
55
 
56
        $topic = $method->invokeArgs($this->mngr, array('No copy'));
57
        $again = $method->invokeArgs($this->mngr, array('No copy'));
58
 
59
        $this->assertSame($topic, $again);
60
    }
61
 
62
    public function testOnOpen() {
63
        $this->mock->expects($this->once())->method('onOpen');
64
        $this->mngr->onOpen($this->conn);
65
    }
66
 
67
    public function testOnCall() {
68
        $id = uniqid();
69
 
70
        $this->mock->expects($this->once())->method('onCall')->with(
71
            $this->conn
72
          , $id
73
          , $this->isInstanceOf('Ratchet\Wamp\Topic')
74
          , array()
75
        );
76
 
77
        $this->mngr->onCall($this->conn, $id, 'new topic', array());
78
    }
79
 
80
    public function testOnSubscribeCreatesTopicObject() {
81
        $this->mock->expects($this->once())->method('onSubscribe')->with(
82
            $this->conn, $this->isInstanceOf('Ratchet\Wamp\Topic')
83
        );
84
 
85
        $this->mngr->onSubscribe($this->conn, 'new topic');
86
    }
87
 
88
    public function testTopicIsInConnectionOnSubscribe() {
89
        $name = 'New Topic';
90
 
91
        $class  = new \ReflectionClass('Ratchet\Wamp\TopicManager');
92
        $method = $class->getMethod('getTopic');
93
        $method->setAccessible(true);
94
 
95
        $topic = $method->invokeArgs($this->mngr, array($name));
96
 
97
        $this->mngr->onSubscribe($this->conn, $name);
98
 
99
        $this->assertTrue($this->conn->WAMP->subscriptions->contains($topic));
100
    }
101
 
102
    public function testDoubleSubscriptionFiresOnce() {
103
        $this->mock->expects($this->exactly(1))->method('onSubscribe');
104
 
105
        $this->mngr->onSubscribe($this->conn, 'same topic');
106
        $this->mngr->onSubscribe($this->conn, 'same topic');
107
    }
108
 
109
    public function testUnsubscribeEvent() {
110
        $name = 'in and out';
111
        $this->mock->expects($this->once())->method('onUnsubscribe')->with(
112
            $this->conn, $this->isInstanceOf('Ratchet\Wamp\Topic')
113
        );
114
 
115
        $this->mngr->onSubscribe($this->conn, $name);
116
        $this->mngr->onUnsubscribe($this->conn, $name);
117
    }
118
 
119
    public function testUnsubscribeFiresOnce() {
120
        $name = 'getting sleepy';
121
        $this->mock->expects($this->exactly(1))->method('onUnsubscribe');
122
 
123
        $this->mngr->onSubscribe($this->conn,   $name);
124
        $this->mngr->onUnsubscribe($this->conn, $name);
125
        $this->mngr->onUnsubscribe($this->conn, $name);
126
    }
127
 
128
    public function testUnsubscribeRemovesTopicFromConnection() {
129
        $name = 'Bye Bye Topic';
130
 
131
        $class  = new \ReflectionClass('Ratchet\Wamp\TopicManager');
132
        $method = $class->getMethod('getTopic');
133
        $method->setAccessible(true);
134
 
135
        $topic = $method->invokeArgs($this->mngr, array($name));
136
 
137
        $this->mngr->onSubscribe($this->conn, $name);
138
        $this->mngr->onUnsubscribe($this->conn, $name);
139
 
140
        $this->assertFalse($this->conn->WAMP->subscriptions->contains($topic));
141
    }
142
 
143
    public function testOnPublishBubbles() {
144
        $msg = 'Cover all the code!';
145
 
146
        $this->mock->expects($this->once())->method('onPublish')->with(
147
            $this->conn
148
          , $this->isInstanceOf('Ratchet\Wamp\Topic')
149
          , $msg
150
          , $this->isType('array')
151
          , $this->isType('array')
152
        );
153
 
154
        $this->mngr->onPublish($this->conn, 'topic coverage', $msg, array(), array());
155
    }
156
 
157
    public function testOnCloseBubbles() {
158
        $this->mock->expects($this->once())->method('onClose')->with($this->conn);
159
        $this->mngr->onClose($this->conn);
160
    }
161
 
162
    protected function topicProvider($name) {
163
        $class  = new \ReflectionClass('Ratchet\Wamp\TopicManager');
164
        $method = $class->getMethod('getTopic');
165
        $method->setAccessible(true);
166
 
167
        $attribute = $class->getProperty('topicLookup');
168
        $attribute->setAccessible(true);
169
 
170
        $topic = $method->invokeArgs($this->mngr, array($name));
171
 
172
        return array($topic, $attribute);
173
    }
174
 
175
    public function testConnIsRemovedFromTopicOnClose() {
176
        $name = 'State Testing';
177
        list($topic, $attribute) = $this->topicProvider($name);
178
 
179
        $this->assertCount(1, $attribute->getValue($this->mngr));
180
 
181
        $this->mngr->onSubscribe($this->conn, $name);
182
        $this->mngr->onClose($this->conn);
183
 
184
        $this->assertFalse($topic->has($this->conn));
185
    }
186
 
187
    public static function topicConnExpectationProvider() {
188
        return [
189
            [ 'onClose', 0]
190
          , ['onUnsubscribe', 0]
191
        ];
192
    }
193
 
194
    /**
195
     * @dataProvider topicConnExpectationProvider
196
     */
197
    public function testTopicRetentionFromLeavingConnections($methodCall, $expectation) {
198
        $topicName = 'checkTopic';
199
        list($topic, $attribute) = $this->topicProvider($topicName);
200
 
201
        $this->mngr->onSubscribe($this->conn, $topicName);
202
        call_user_func_array(array($this->mngr, $methodCall), array($this->conn, $topicName));
203
 
204
        $this->assertCount($expectation, $attribute->getValue($this->mngr));
205
    }
206
 
207
    public function testOnErrorBubbles() {
208
        $e = new \Exception('All work and no play makes Chris a dull boy');
209
        $this->mock->expects($this->once())->method('onError')->with($this->conn, $e);
210
 
211
        $this->mngr->onError($this->conn, $e);
212
    }
213
 
214
    public function testGetSubProtocolsReturnsArray() {
215
        $this->assertInternalType('array', $this->mngr->getSubProtocols());
216
    }
217
 
218
    public function testGetSubProtocolsBubbles() {
219
        $subs = array('hello', 'world');
220
        $app  = $this->getMock('Ratchet\Wamp\Stub\WsWampServerInterface');
221
        $app->expects($this->once())->method('getSubProtocols')->will($this->returnValue($subs));
222
        $mngr = new TopicManager($app);
223
 
224
        $this->assertEquals($subs, $mngr->getSubProtocols());
225
    }
226
}