From 4078a360a85f1d7e8dcd232bccea35cdc95b7751 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Thu, 19 Jul 2012 00:14:36 -0400 Subject: [PATCH] [WAMP] Topic testing Fixed bugs failing on Travis More unit test coverage --- src/Ratchet/Wamp/TopicManager.php | 2 +- src/Ratchet/Wamp/WampServer.php | 5 +++ tests/Ratchet/Tests/Wamp/TopicManagerTest.php | 1 + tests/Ratchet/Tests/Wamp/WampServerTest.php | 36 +++++++++++++++++-- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Ratchet/Wamp/TopicManager.php b/src/Ratchet/Wamp/TopicManager.php index 5263f7a..a879a6c 100644 --- a/src/Ratchet/Wamp/TopicManager.php +++ b/src/Ratchet/Wamp/TopicManager.php @@ -49,7 +49,7 @@ class TopicManager implements WsServerInterface, WampServerInterface { public function onUnsubscribe(ConnectionInterface $conn, $topic) { $topicObj = $this->getTopic($topic); - if ($conn->WAMP->topics->contains($topicobj)) { + if ($conn->WAMP->topics->contains($topicObj)) { $conn->WAMP->topics->remove($topicObj); } diff --git a/src/Ratchet/Wamp/WampServer.php b/src/Ratchet/Wamp/WampServer.php index dd45a27..6c6c04e 100644 --- a/src/Ratchet/Wamp/WampServer.php +++ b/src/Ratchet/Wamp/WampServer.php @@ -4,6 +4,11 @@ use Ratchet\MessageComponentInterface; use Ratchet\WebSocket\WsServerInterface; use Ratchet\ConnectionInterface; +/** + * This class just makes it 1 step easier to use Topic objects in WAMP + * If you're looking at the source code, look in the __construct of this + * class and use that to make your application instead of using this + */ class WampServer implements MessageComponentInterface, WsServerInterface { /** * @var ServerProtocol diff --git a/tests/Ratchet/Tests/Wamp/TopicManagerTest.php b/tests/Ratchet/Tests/Wamp/TopicManagerTest.php index 6ee2f6d..586ff72 100644 --- a/tests/Ratchet/Tests/Wamp/TopicManagerTest.php +++ b/tests/Ratchet/Tests/Wamp/TopicManagerTest.php @@ -15,6 +15,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase { $this->mock = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface'); $this->mngr = new TopicManager($this->mock); + $this->conn->WAMP = new \StdClass; $this->mngr->onOpen($this->conn); } diff --git a/tests/Ratchet/Tests/Wamp/WampServerTest.php b/tests/Ratchet/Tests/Wamp/WampServerTest.php index 344bbcb..6a92d57 100644 --- a/tests/Ratchet/Tests/Wamp/WampServerTest.php +++ b/tests/Ratchet/Tests/Wamp/WampServerTest.php @@ -14,6 +14,8 @@ class WampServerTest extends \PHPUnit_Framework_TestCase { $this->mock = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface'); $this->serv = new WampServer($this->mock); $this->conn = $this->getMock('\\Ratchet\\ConnectionInterface'); + + $this->serv->onOpen($this->conn); } public function isWampConn() { @@ -22,6 +24,36 @@ class WampServerTest extends \PHPUnit_Framework_TestCase { public function testOpen() { $this->mock->expects($this->once())->method('onOpen')->with($this->isWampConn()); - $this->serv->onOpen($this->conn); + $this->serv->onOpen($this->getMock('\\Ratchet\\ConnectionInterface')); } -} + + public function testOnClose() { + $this->mock->expects($this->once())->method('onClose')->with($this->isWampConn()); + $this->serv->onClose($this->conn); + } + + public function testOnError() { + $e = new \Exception('hurr hurr'); + $this->mock->expects($this->once())->method('onError')->with($this->isWampConn(), $e); + $this->serv->onError($this->conn, $e); + } + + public function testOnMessageToEvent() { + $published = 'Client published this message'; + + $this->mock->expects($this->once())->method('onPublish')->with( + $this->isWampConn() + , new \PHPUnit_Framework_Constraint_IsInstanceOf('\\Ratchet\\Wamp\\Topic') + , $published + , array() + , array() + ); + + $this->serv->onMessage($this->conn, json_encode(array(7, 'topic', $published))); + } + + public function testGetSubProtocols() { + // todo: could expand on this + $this->assertInternalType('array', $this->serv->getSubProtocols()); + } +} \ No newline at end of file