[WAMP] CS, added unit tests for new features, 100% on WAMP
This commit is contained in:
parent
48352fce63
commit
dffb8d2c1f
@ -31,19 +31,19 @@ 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
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
namespace Ratchet\Wamp;
|
||||
use Ratchet\Wamp\ServerProtocol;
|
||||
use Ratchet\Wamp\WampConnection;
|
||||
use Ratchet\Mock\Connection;
|
||||
use Ratchet\Mock\WampComponent as TestComponent;
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace Ratchet\Wamp;
|
||||
use Ratchet\Wamp\TopicManager;
|
||||
|
||||
/**
|
||||
* @covers Ratchet\Wamp\TopicManager
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
namespace Ratchet\Wamp;
|
||||
use Ratchet\Wamp\Topic;
|
||||
use Ratchet\Wamp\WampConnection;
|
||||
|
||||
/**
|
||||
* @covers Ratchet\Wamp\Topic
|
||||
@ -60,6 +58,60 @@ class TopicTest extends \PHPUnit_Framework_TestCase {
|
||||
$topic->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();
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace Ratchet\Wamp;
|
||||
use Ratchet\Wamp\WampConnection;
|
||||
|
||||
/**
|
||||
* @covers Ratchet\Wamp\WampConnection
|
||||
@ -32,6 +31,15 @@ class WampConnectionTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->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';
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace Ratchet\Wamp;
|
||||
use Ratchet\Wamp\WampServer;
|
||||
use Ratchet\AbstractMessageComponentTestCase;
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user