From b8a677a6bb7517b217682354f401615e2671438c Mon Sep 17 00:00:00 2001
From: Chris Boden <cboden@gmail.com>
Date: Tue, 17 Jul 2012 23:59:35 -0400
Subject: [PATCH] [WAMP] Topic tests and fix

Fixed a bug in Topic
Created unit tests for Topic
---
 src/Ratchet/Wamp/Topic.php             |  8 ++-
 tests/Ratchet/Tests/Wamp/TopicTest.php | 82 ++++++++++++++++++++++++++
 2 files changed, 89 insertions(+), 1 deletion(-)
 create mode 100644 tests/Ratchet/Tests/Wamp/TopicTest.php

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 @@
+<?php
+namespace Ratchet\Tests\Wamp;
+use Ratchet\Wamp\Topic;
+use Ratchet\Wamp\WampConnection;
+use Ratchet\Tests\Mock\Connection as MockConnection;
+
+/**
+ * @covers Ratchet\Wamp\Topic
+ */
+class TopicTest extends \PHPUnit_Framework_TestCase {
+    public function testGetId() {
+        $id    = uniqid();
+        $topic = new Topic($id);
+
+        $this->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