diff --git a/src/Ratchet/Wamp/Topic.php b/src/Ratchet/Wamp/Topic.php index 83529a3..9d2c272 100644 --- a/src/Ratchet/Wamp/Topic.php +++ b/src/Ratchet/Wamp/Topic.php @@ -29,25 +29,31 @@ class Topic implements \IteratorAggregate, \Countable { * @param string */ public function broadcast($msg) { - foreach ($thisi->subscribers as $client) { + foreach ($this->subscribers as $client) { $client->event($this->id, $msg); } } /** * @param WampConnection + * @return Topic */ public function add(WampConnection $conn) { $this->subscribers->attach($conn); + + return $this; } /** * @param WampConnection + * @return Topic */ public function remove(WampConnection $conn) { if ($this->subscribers->contains($conn)) { $this->subscribers->detach($conn); } + + return $this; } /** diff --git a/tests/Ratchet/Tests/Wamp/TopicTest.php b/tests/Ratchet/Tests/Wamp/TopicTest.php new file mode 100644 index 0000000..0e5c889 --- /dev/null +++ b/tests/Ratchet/Tests/Wamp/TopicTest.php @@ -0,0 +1,82 @@ +assertEquals($id, $topic->getId()); + } + + public function testAddAndCount() { + $topic = new Topic('merp'); + + $topic->add($this->newConn()); + $topic->add($this->newConn()); + $topic->add($this->newConn()); + + $this->assertEquals(3, count($topic)); + } + + public function testRemove() { + $topic = new Topic('boop'); + $tracked = $this->newConn(); + + $topic->add($this->newConn()); + $topic->add($tracked); + $topic->add($this->newConn()); + + $topic->remove($tracked); + + $this->assertEquals(2, count($topic)); + } + + public function testBroadcast() { + $msg = 'Hello World!'; + $name = 'batman'; + $protocol = json_encode(array(8, $name, $msg)); + + $first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array(new MockConnection)); + $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array(new MockConnection)); + + $first->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $second->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $topic = new Topic($name); + $topic->add($first); + $topic->add($second); + + $topic->broadcast($msg); + } + + public function testIterator() { + $first = $this->newConn(); + $second = $this->newConn(); + $third = $this->newConn(); + + $topic = new Topic('joker'); + $topic->add($first)->add($second)->add($third); + + $check = array($first, $second, $third); + + foreach ($topic as $mock) { + $this->assertNotSame(false, array_search($mock, $check)); + } + } + + protected function newConn() { + return new WampConnection(new MockConnection); + } +} \ No newline at end of file