From dffb8d2c1fef7264a43af68d2782a11cc8e334fb Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 16 Feb 2014 19:09:54 -0500 Subject: [PATCH] [WAMP] CS, added unit tests for new features, 100% on WAMP --- src/Ratchet/Wamp/Topic.php | 18 ++++----- tests/unit/Wamp/ServerProtocolTest.php | 2 - tests/unit/Wamp/TopicManagerTest.php | 1 - tests/unit/Wamp/TopicTest.php | 56 +++++++++++++++++++++++++- tests/unit/Wamp/WampConnectionTest.php | 10 ++++- tests/unit/Wamp/WampServerTest.php | 1 - 6 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/Ratchet/Wamp/Topic.php b/src/Ratchet/Wamp/Topic.php index 5e0cc24..f1bd68a 100644 --- a/src/Ratchet/Wamp/Topic.php +++ b/src/Ratchet/Wamp/Topic.php @@ -30,20 +30,20 @@ class Topic implements \IteratorAggregate, \Countable { } /** - * Send a message to all the connections in this topic - * @param string $msg - * @param array $exclude - * @param array $eligible - * @return Topic - */ + * Send a message to all the connections in this topic + * @param string $msg Payload to publish + * @param array $exclude A list of session IDs the message should be excluded from (blacklist) + * @param array $eligible A list of session Ids the message should be send to (whitelist) + * @return Topic The same Topic object to chain + */ public function broadcast($msg, array $exclude = array(), array $eligible = array()) { - $useEligible = count($eligible); + $useEligible = (bool)count($eligible); foreach ($this->subscribers as $client) { - if(in_array($client->WAMP->sessionId, $exclude)) { + if (in_array($client->WAMP->sessionId, $exclude)) { continue; } - if($useEligible && !in_array($client->WAMP->sessionId, $eligible)) { + if ($useEligible && !in_array($client->WAMP->sessionId, $eligible)) { continue; } diff --git a/tests/unit/Wamp/ServerProtocolTest.php b/tests/unit/Wamp/ServerProtocolTest.php index b4f57a4..14a8b1e 100644 --- a/tests/unit/Wamp/ServerProtocolTest.php +++ b/tests/unit/Wamp/ServerProtocolTest.php @@ -1,7 +1,5 @@ broadcast($msg); } + public function testBroadcastWithExclude() { + $msg = 'Hello odd numbers'; + $name = 'Excluding'; + $protocol = json_encode(array(8, $name, $msg)); + + $first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + $third = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + + $first->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $second->expects($this->never())->method('send'); + + $third->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $topic = new Topic($name); + $topic->add($first); + $topic->add($second); + $topic->add($third); + + $topic->broadcast($msg, array($second->WAMP->sessionId)); + } + + public function testBroadcastWithEligible() { + $msg = 'Hello white list'; + $name = 'Eligible'; + $protocol = json_encode(array(8, $name, $msg)); + + $first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + $third = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + + $first->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $second->expects($this->never())->method('send'); + + $third->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $topic = new Topic($name); + $topic->add($first); + $topic->add($second); + $topic->add($third); + + $topic->broadcast($msg, array(), array($first->WAMP->sessionId, $third->WAMP->sessionId)); + } + public function testIterator() { $first = $this->newConn(); $second = $this->newConn(); diff --git a/tests/unit/Wamp/WampConnectionTest.php b/tests/unit/Wamp/WampConnectionTest.php index 7f82f98..e33c166 100644 --- a/tests/unit/Wamp/WampConnectionTest.php +++ b/tests/unit/Wamp/WampConnectionTest.php @@ -1,6 +1,5 @@ conn->callError($callId, $uri); } + public function testCallErrorWithTopic() { + $callId = uniqid(); + $uri = 'http://example.com/end/point'; + + $this->mock->expects($this->once())->method('send')->with(json_encode(array(4, $callId, $uri, ''))); + + $this->conn->callError($callId, new Topic($uri)); + } + public function testDetailedCallError() { $callId = uniqid(); $uri = 'http://example.com/end/point'; diff --git a/tests/unit/Wamp/WampServerTest.php b/tests/unit/Wamp/WampServerTest.php index e76d693..02c81a6 100644 --- a/tests/unit/Wamp/WampServerTest.php +++ b/tests/unit/Wamp/WampServerTest.php @@ -1,6 +1,5 @@