diff --git a/src/Ratchet/Server/IoServer.php b/src/Ratchet/Server/IoServer.php index 7ed523e..f6726e3 100644 --- a/src/Ratchet/Server/IoServer.php +++ b/src/Ratchet/Server/IoServer.php @@ -71,7 +71,9 @@ class IoServer { throw new \RuntimeException("A React Loop was not provided during instantiation"); } + // @codeCoverageIgnoreStart $this->loop->run(); + // @codeCoverageIgnoreEnd } /** diff --git a/src/Ratchet/Wamp/Topic.php b/src/Ratchet/Wamp/Topic.php index ae75e36..3ca7488 100644 --- a/src/Ratchet/Wamp/Topic.php +++ b/src/Ratchet/Wamp/Topic.php @@ -1,5 +1,6 @@ subscribers->attach($conn); return $this; @@ -55,7 +56,7 @@ class Topic implements \IteratorAggregate, \Countable { * @param WampConnection * @return Topic */ - public function remove(WampConnection $conn) { + public function remove(ConnectionInterface $conn) { if ($this->subscribers->contains($conn)) { $this->subscribers->detach($conn); } diff --git a/src/Ratchet/Wamp/TopicManager.php b/src/Ratchet/Wamp/TopicManager.php index a879a6c..b6916b5 100644 --- a/src/Ratchet/Wamp/TopicManager.php +++ b/src/Ratchet/Wamp/TopicManager.php @@ -39,6 +39,10 @@ class TopicManager implements WsServerInterface, WampServerInterface { public function onSubscribe(ConnectionInterface $conn, $topic) { $topicObj = $this->getTopic($topic); + if ($conn->WAMP->topics->contains($topicObj)) { + return; + } + $conn->WAMP->topics->attach($topicObj); $this->app->onSubscribe($conn, $topicObj); } @@ -50,7 +54,9 @@ class TopicManager implements WsServerInterface, WampServerInterface { $topicObj = $this->getTopic($topic); if ($conn->WAMP->topics->contains($topicObj)) { - $conn->WAMP->topics->remove($topicObj); + $conn->WAMP->topics->detach($topicObj); + } else { + return; } $this->topicLookup[$topic]->remove($conn); diff --git a/tests/Ratchet/Tests/Wamp/ServerProtocolTest.php b/tests/Ratchet/Tests/Wamp/ServerProtocolTest.php index 4dcb28e..cf5d803 100644 --- a/tests/Ratchet/Tests/Wamp/ServerProtocolTest.php +++ b/tests/Ratchet/Tests/Wamp/ServerProtocolTest.php @@ -240,4 +240,11 @@ class ServerProtocolTest extends \PHPUnit_Framework_TestCase { $this->assertGreaterThanOrEqual(3, count($this->_comp->getSubProtocols())); } + + public function testWampOnMessageApp() { + $app = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface'); + $wamp = new ServerProtocol($app); + + $this->assertContains('wamp', $wamp->getSubProtocols()); + } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/Wamp/TopicManagerTest.php b/tests/Ratchet/Tests/Wamp/TopicManagerTest.php index 586ff72..35da9ce 100644 --- a/tests/Ratchet/Tests/Wamp/TopicManagerTest.php +++ b/tests/Ratchet/Tests/Wamp/TopicManagerTest.php @@ -82,7 +82,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase { $this->mngr->onSubscribe($this->conn, 'new topic'); } - public function testTopicIsInConnection() { + public function testTopicIsInConnectionOnSubscribe() { $name = 'New Topic'; $class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager'); @@ -95,4 +95,45 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->conn->WAMP->topics->contains($topic)); } + + public function testDoubleSubscriptionFiresOnce() { + $this->mock->expects($this->exactly(1))->method('onSubscribe'); + + $this->mngr->onSubscribe($this->conn, 'same topic'); + $this->mngr->onSubscribe($this->conn, 'same topic'); + } + + public function testUnsubscribeEvent() { + $name = 'in and out'; + $this->mock->expects($this->once())->method('onUnsubscribe')->with( + $this->conn, $this->isTopic() + ); + + $this->mngr->onSubscribe($this->conn, $name); + $this->mngr->onUnsubscribe($this->conn, $name); + } + + public function testUnsubscribeFiresOnce() { + $name = 'getting sleepy'; + $this->mock->expects($this->exactly(1))->method('onUnsubscribe'); + + $this->mngr->onSubscribe($this->conn, $name); + $this->mngr->onUnsubscribe($this->conn, $name); + $this->mngr->onUnsubscribe($this->conn, $name); + } + + public function testUnsubscribeRemovesTopicFromConnection() { + $name = 'Bye Bye Topic'; + + $class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager'); + $method = $class->getMethod('getTopic'); + $method->setAccessible(true); + + $topic = $method->invokeArgs($this->mngr, array($name)); + + $this->mngr->onSubscribe($this->conn, $name); + $this->mngr->onUnsubscribe($this->conn, $name); + + $this->assertFalse($this->conn->WAMP->topics->contains($topic)); + } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/Wamp/WampConnectionTest.php b/tests/Ratchet/Tests/Wamp/WampConnectionTest.php index 43a1127..9632b03 100644 --- a/tests/Ratchet/Tests/Wamp/WampConnectionTest.php +++ b/tests/Ratchet/Tests/Wamp/WampConnectionTest.php @@ -1,67 +1,69 @@ mock = $this->getMock('\\Ratchet\\ConnectionInterface'); + $this->conn = new WampConnection($this->mock); + } + + public function testCallResult() { $callId = uniqid(); $data = array('hello' => 'world', 'herp' => 'derp'); + $this->mock->expects($this->once())->method('send')->with(json_encode(array(3, $callId, $data))); - $decor->callResult($callId, $data); - $resultString = $conn->last['send']; - - $this->assertEquals(array(3, $callId, $data), json_decode($resultString, true)); + $this->conn->callResult($callId, $data); } public function testCallError() { - $conn = new Connection; - $decor = new WampConnection($conn); - $callId = uniqid(); $uri = 'http://example.com/end/point'; - $decor->callError($callId, $uri); - $resultString = $conn->last['send']; + $this->mock->expects($this->once())->method('send')->with(json_encode(array(4, $callId, $uri, ''))); - $this->assertEquals(array(4, $callId, $uri, ''), json_decode($resultString, true)); + $this->conn->callError($callId, $uri); } public function testDetailedCallError() { - $conn = new Connection; - $decor = new WampConnection($conn); - $callId = uniqid(); $uri = 'http://example.com/end/point'; $desc = 'beep boop beep'; $detail = 'Error: Too much awesome'; - $decor->callError($callId, $uri, $desc, $detail); - $resultString = $conn->last['send']; + $this->mock->expects($this->once())->method('send')->with(json_encode(array(4, $callId, $uri, $desc, $detail))); - $this->assertEquals(array(4, $callId, $uri, $desc, $detail), json_decode($resultString, true)); + $this->conn->callError($callId, $uri, $desc, $detail); } public function testPrefix() { - $conn = new WampConnection(new Connection); - $shortOut = 'outgoing'; $longOut = 'http://example.com/outoing'; - $conn->prefix($shortOut, $longOut); + $this->mock->expects($this->once())->method('send')->with(json_encode(array(1, $shortOut, $longOut))); + + $this->conn->prefix($shortOut, $longOut); } public function testGetUriWhenNoCurieGiven() { - $conn = new WampConnection(new Connection); $uri = 'http://example.com/noshort'; - $this->assertEquals($uri, $conn->getUri($uri)); + $this->assertEquals($uri, $this->conn->getUri($uri)); + } + + public function testClose() { + $mock = $this->getMock('\\Ratchet\\ConnectionInterface'); + $conn = new WampConnection($mock); + + $mock->expects($this->once())->method('close'); + + $conn->close(); } } \ No newline at end of file