From 0b5208507fab01e7e95753a5ae03dd4bb0df4969 Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Sun, 9 Feb 2014 00:44:05 +0400 Subject: [PATCH 1/4] Added subscribers filtering in topic broadcasting for taking into account exclude & eligible data --- src/Ratchet/Wamp/Topic.php | 15 +++++++++++++-- src/Ratchet/Wamp/WampConnection.php | 9 ++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Ratchet/Wamp/Topic.php b/src/Ratchet/Wamp/Topic.php index 688195a..c2f7217 100644 --- a/src/Ratchet/Wamp/Topic.php +++ b/src/Ratchet/Wamp/Topic.php @@ -32,10 +32,21 @@ 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 */ - public function broadcast($msg) { + public function broadcast($msg, array $exclude, array $eligible) { + $useEligible = count($eligible); foreach ($this->subscribers as $client) { + if(in_array($client->getSessionId(), $exclude)) { + continue; + } + + if($useEligible && !in_array($client->getSessionId(), $eligible)) { + continue; + } + $client->event($this->id, $msg); } @@ -85,4 +96,4 @@ class Topic implements \IteratorAggregate, \Countable { public function count() { return $this->subscribers->count(); } -} \ No newline at end of file +} diff --git a/src/Ratchet/Wamp/WampConnection.php b/src/Ratchet/Wamp/WampConnection.php index de13844..4004843 100644 --- a/src/Ratchet/Wamp/WampConnection.php +++ b/src/Ratchet/Wamp/WampConnection.php @@ -99,4 +99,11 @@ class WampConnection extends AbstractConnectionDecorator { public function close($opt = null) { $this->getConnection()->close($opt); } -} \ No newline at end of file + + /** + * Get session ID + */ + public function getSessionId() { + return $this->WAMP->sessionId; + } +} From 646c2e263abf33c5c75f79d107b20fe0a6089dba Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Sun, 9 Feb 2014 14:50:07 +0400 Subject: [PATCH 2/4] Slightly modified TopicTest to support new broadcast params --- tests/unit/Wamp/TopicTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/Wamp/TopicTest.php b/tests/unit/Wamp/TopicTest.php index 0a2102b..02bdd2d 100644 --- a/tests/unit/Wamp/TopicTest.php +++ b/tests/unit/Wamp/TopicTest.php @@ -57,7 +57,7 @@ class TopicTest extends \PHPUnit_Framework_TestCase { $topic->add($first); $topic->add($second); - $topic->broadcast($msg); + $topic->broadcast($msg, array(), array()); } public function testIterator() { @@ -109,4 +109,4 @@ class TopicTest extends \PHPUnit_Framework_TestCase { protected function newConn() { return new WampConnection($this->getMock('\\Ratchet\\ConnectionInterface')); } -} \ No newline at end of file +} From 48352fce637c02bb49624719ac766214a6f038fd Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Tue, 11 Feb 2014 11:16:25 +0400 Subject: [PATCH 3/4] Code fix for pull request 165 --- src/Ratchet/Wamp/Topic.php | 6 +++--- src/Ratchet/Wamp/WampConnection.php | 6 ------ tests/unit/Wamp/TopicTest.php | 2 +- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Ratchet/Wamp/Topic.php b/src/Ratchet/Wamp/Topic.php index c2f7217..5e0cc24 100644 --- a/src/Ratchet/Wamp/Topic.php +++ b/src/Ratchet/Wamp/Topic.php @@ -36,14 +36,14 @@ class Topic implements \IteratorAggregate, \Countable { * @param array $eligible * @return Topic */ - public function broadcast($msg, array $exclude, array $eligible) { + public function broadcast($msg, array $exclude = array(), array $eligible = array()) { $useEligible = count($eligible); foreach ($this->subscribers as $client) { - if(in_array($client->getSessionId(), $exclude)) { + if(in_array($client->WAMP->sessionId, $exclude)) { continue; } - if($useEligible && !in_array($client->getSessionId(), $eligible)) { + if($useEligible && !in_array($client->WAMP->sessionId, $eligible)) { continue; } diff --git a/src/Ratchet/Wamp/WampConnection.php b/src/Ratchet/Wamp/WampConnection.php index 4004843..95e1969 100644 --- a/src/Ratchet/Wamp/WampConnection.php +++ b/src/Ratchet/Wamp/WampConnection.php @@ -100,10 +100,4 @@ class WampConnection extends AbstractConnectionDecorator { $this->getConnection()->close($opt); } - /** - * Get session ID - */ - public function getSessionId() { - return $this->WAMP->sessionId; - } } diff --git a/tests/unit/Wamp/TopicTest.php b/tests/unit/Wamp/TopicTest.php index 02bdd2d..1f00d15 100644 --- a/tests/unit/Wamp/TopicTest.php +++ b/tests/unit/Wamp/TopicTest.php @@ -57,7 +57,7 @@ class TopicTest extends \PHPUnit_Framework_TestCase { $topic->add($first); $topic->add($second); - $topic->broadcast($msg, array(), array()); + $topic->broadcast($msg); } public function testIterator() { From dffb8d2c1fef7264a43af68d2782a11cc8e334fb Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 16 Feb 2014 19:09:54 -0500 Subject: [PATCH 4/4] [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 @@