[WAMP]
Lots of unit tests for new WAMP classes Fixed parse error in TopicManager Aliased Topic::getId -> __toString to WampConnection can work with Topics or raw protocol strings API docs CS
This commit is contained in:
parent
9d0e6735db
commit
8d0cf5ffb2
@ -48,8 +48,8 @@ class ServerProtocol implements MessageComponentInterface, WsServerInterface {
|
|||||||
/**
|
/**
|
||||||
* @param WampServerInterface An class to propagate calls through
|
* @param WampServerInterface An class to propagate calls through
|
||||||
*/
|
*/
|
||||||
public function __construct(WampServerInterface $server_component) {
|
public function __construct(WampServerInterface $serverComponent) {
|
||||||
$this->_decorating = $server_component;
|
$this->_decorating = $serverComponent;
|
||||||
$this->connections = new \SplObjectStorage;
|
$this->connections = new \SplObjectStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,14 +24,21 @@ class Topic implements \IteratorAggregate, \Countable {
|
|||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __toString() {
|
||||||
|
return $this->getId();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a message to all the connectiosn in this topic
|
* Send a message to all the connectiosn in this topic
|
||||||
* @param string
|
* @param string
|
||||||
|
* @return Topic
|
||||||
*/
|
*/
|
||||||
public function broadcast($msg) {
|
public function broadcast($msg) {
|
||||||
foreach ($this->subscribers as $client) {
|
foreach ($this->subscribers as $client) {
|
||||||
$client->event($this->id, $msg);
|
$client->event($this->id, $msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,7 +49,7 @@ class TopicManager implements WsServerInterface, WampServerInterface {
|
|||||||
public function onUnsubscribe(ConnectionInterface $conn, $topic) {
|
public function onUnsubscribe(ConnectionInterface $conn, $topic) {
|
||||||
$topicObj = $this->getTopic($topic);
|
$topicObj = $this->getTopic($topic);
|
||||||
|
|
||||||
if ($conn->WAMP->topics->contains($topicobj) {
|
if ($conn->WAMP->topics->contains($topicobj)) {
|
||||||
$conn->WAMP->topics->remove($topicObj);
|
$conn->WAMP->topics->remove($topicObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,6 +93,10 @@ class TopicManager implements WsServerInterface, WampServerInterface {
|
|||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string
|
||||||
|
* @return Topic
|
||||||
|
*/
|
||||||
protected function getTopic($topic) {
|
protected function getTopic($topic) {
|
||||||
if (!array_key_exists($topic, $this->topicLookup)) {
|
if (!array_key_exists($topic, $this->topicLookup)) {
|
||||||
$this->topicLookup[$topic] = new Topic($topic);
|
$this->topicLookup[$topic] = new Topic($topic);
|
||||||
|
@ -10,6 +10,9 @@ use Ratchet\Wamp\ServerProtocol as WAMP;
|
|||||||
* @property stdClass $WAMP
|
* @property stdClass $WAMP
|
||||||
*/
|
*/
|
||||||
class WampConnection extends AbstractConnectionDecorator {
|
class WampConnection extends AbstractConnectionDecorator {
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function __construct(ConnectionInterface $conn) {
|
public function __construct(ConnectionInterface $conn) {
|
||||||
parent::__construct($conn);
|
parent::__construct($conn);
|
||||||
|
|
||||||
@ -21,6 +24,7 @@ class WampConnection extends AbstractConnectionDecorator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Successfully respond to a call made by the client
|
||||||
* @param string The unique ID given by the client to respond to
|
* @param string The unique ID given by the client to respond to
|
||||||
* @param array An array of data to return to the client
|
* @param array An array of data to return to the client
|
||||||
*/
|
*/
|
||||||
@ -29,13 +33,14 @@ class WampConnection extends AbstractConnectionDecorator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Respond with an error to a client call
|
||||||
* @param string The unique ID given by the client to respond to
|
* @param string The unique ID given by the client to respond to
|
||||||
* @param string The URI given by the client ot respond to
|
* @param string The URI given by the client ot respond to
|
||||||
* @param string A developer-oriented description of the error
|
* @param string A developer-oriented description of the error
|
||||||
* @param string|null An optional human readable detail message to send back
|
* @param string|null An optional human readable detail message to send back
|
||||||
*/
|
*/
|
||||||
public function callError($id, $topic, $desc = '', $details = null) {
|
public function callError($id, $topic, $desc = '', $details = null) {
|
||||||
$data = array(WAMP::MSG_CALL_ERROR, $id, $topic, $desc);
|
$data = array(WAMP::MSG_CALL_ERROR, $id, (string)$topic, $desc);
|
||||||
|
|
||||||
if (null !== $details) {
|
if (null !== $details) {
|
||||||
$data[] = $details;
|
$data[] = $details;
|
||||||
@ -49,7 +54,7 @@ class WampConnection extends AbstractConnectionDecorator {
|
|||||||
* @param mixed Data to send with the event. Anything that is json'able
|
* @param mixed Data to send with the event. Anything that is json'able
|
||||||
*/
|
*/
|
||||||
public function event($topic, $msg) {
|
public function event($topic, $msg) {
|
||||||
$this->send(json_encode(array(WAMP::MSG_EVENT, $topic, $msg)));
|
$this->send(json_encode(array(WAMP::MSG_EVENT, (string)$topic, $msg)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,8 +62,8 @@ class WampConnection extends AbstractConnectionDecorator {
|
|||||||
* @param string
|
* @param string
|
||||||
*/
|
*/
|
||||||
public function prefix($curie, $uri) {
|
public function prefix($curie, $uri) {
|
||||||
$this->WAMP->prefixes[$curie] = $uri;
|
$this->WAMP->prefixes[$curie] = (string)$uri;
|
||||||
$this->send(json_encode(array(WAMP::MSG_PREFIX, $curie, $uri)));
|
$this->send(json_encode(array(WAMP::MSG_PREFIX, $curie, (string)$uri)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,7 +12,7 @@ interface WampServerInterface extends ComponentInterface {
|
|||||||
* An RPC call has been received
|
* An RPC call has been received
|
||||||
* @param Ratchet\ConnectionInterface
|
* @param Ratchet\ConnectionInterface
|
||||||
* @param string The unique ID of the RPC, required to respond to
|
* @param string The unique ID of the RPC, required to respond to
|
||||||
* @param string The topic to execute the call against
|
* @param string|Topic The topic to execute the call against
|
||||||
* @param array Call parameters received from the client
|
* @param array Call parameters received from the client
|
||||||
*/
|
*/
|
||||||
function onCall(ConnectionInterface $conn, $id, $topic, array $params);
|
function onCall(ConnectionInterface $conn, $id, $topic, array $params);
|
||||||
@ -20,21 +20,21 @@ interface WampServerInterface extends ComponentInterface {
|
|||||||
/**
|
/**
|
||||||
* A request to subscribe to a topic has been made
|
* A request to subscribe to a topic has been made
|
||||||
* @param Ratchet\ConnectionInterface
|
* @param Ratchet\ConnectionInterface
|
||||||
* @param string The topic to subscribe to
|
* @param string|Topic The topic to subscribe to
|
||||||
*/
|
*/
|
||||||
function onSubscribe(ConnectionInterface $conn, $topic);
|
function onSubscribe(ConnectionInterface $conn, $topic);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A request to unsubscribe from a topic has been made
|
* A request to unsubscribe from a topic has been made
|
||||||
* @param Ratchet\ConnectionInterface
|
* @param Ratchet\ConnectionInterface
|
||||||
* @param The topic to unsubscribe from
|
* @param string|Topic The topic to unsubscribe from
|
||||||
*/
|
*/
|
||||||
function onUnSubscribe(ConnectionInterface $conn, $topic);
|
function onUnSubscribe(ConnectionInterface $conn, $topic);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A client is attempting to publish content to a subscribed connections on a URI
|
* A client is attempting to publish content to a subscribed connections on a URI
|
||||||
* @param Ratchet\ConnectionInterface
|
* @param Ratchet\ConnectionInterface
|
||||||
* @param The topic the user has attempted to publish to
|
* @param string|Topic The topic the user has attempted to publish to
|
||||||
* @param string Payload of the publish
|
* @param string Payload of the publish
|
||||||
* @param array A list of session IDs the message should be excluded from (blacklist)
|
* @param array A list of session IDs the message should be excluded from (blacklist)
|
||||||
* @param array A list of session Ids the message should be send to (whitelist)
|
* @param array A list of session Ids the message should be send to (whitelist)
|
||||||
|
97
tests/Ratchet/Tests/Wamp/TopicManagerTest.php
Normal file
97
tests/Ratchet/Tests/Wamp/TopicManagerTest.php
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Tests\Wamp;
|
||||||
|
use Ratchet\Wamp\TopicManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Ratchet\Wamp\TopicManager
|
||||||
|
*/
|
||||||
|
class TopicManagerTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
private $mock;
|
||||||
|
private $mngr;
|
||||||
|
private $conn;
|
||||||
|
|
||||||
|
public function setUp() {
|
||||||
|
$this->conn = $this->getMock('\\Ratchet\\ConnectionInterface');
|
||||||
|
$this->mock = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface');
|
||||||
|
$this->mngr = new TopicManager($this->mock);
|
||||||
|
|
||||||
|
$this->mngr->onOpen($this->conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isTopic() {
|
||||||
|
return new \PHPUnit_Framework_Constraint_IsInstanceOf('\\Ratchet\\Wamp\\Topic');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTopicReturnsTopicObject() {
|
||||||
|
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
|
||||||
|
$method = $class->getMethod('getTopic');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
|
||||||
|
$topic = $method->invokeArgs($this->mngr, array('The Topic'));
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\Ratchet\\Wamp\\Topic', $topic);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTopicCreatesTopicWithSameName() {
|
||||||
|
$name = 'The Topic';
|
||||||
|
|
||||||
|
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
|
||||||
|
$method = $class->getMethod('getTopic');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
|
||||||
|
$topic = $method->invokeArgs($this->mngr, array($name));
|
||||||
|
|
||||||
|
$this->assertEquals($name, $topic->getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTopicReturnsSameObject() {
|
||||||
|
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
|
||||||
|
$method = $class->getMethod('getTopic');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
|
||||||
|
$topic = $method->invokeArgs($this->mngr, array('No copy'));
|
||||||
|
$again = $method->invokeArgs($this->mngr, array('No copy'));
|
||||||
|
|
||||||
|
$this->assertSame($topic, $again);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOnOpen() {
|
||||||
|
$this->mock->expects($this->once())->method('onOpen');
|
||||||
|
$this->mngr->onOpen($this->conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOnCall() {
|
||||||
|
$id = uniqid();
|
||||||
|
|
||||||
|
$this->mock->expects($this->once())->method('onCall')->with(
|
||||||
|
$this->conn
|
||||||
|
, $id
|
||||||
|
, $this->isTopic()
|
||||||
|
, array()
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->mngr->onCall($this->conn, $id, 'new topic', array());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOnSubscribeCreatesTopicObject() {
|
||||||
|
$this->mock->expects($this->once())->method('onSubscribe')->with(
|
||||||
|
$this->conn, $this->isTopic()
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->mngr->onSubscribe($this->conn, 'new topic');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTopicIsInConnection() {
|
||||||
|
$name = 'New 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->assertTrue($this->conn->WAMP->topics->contains($topic));
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,6 @@
|
|||||||
namespace Ratchet\Tests\Wamp;
|
namespace Ratchet\Tests\Wamp;
|
||||||
use Ratchet\Wamp\Topic;
|
use Ratchet\Wamp\Topic;
|
||||||
use Ratchet\Wamp\WampConnection;
|
use Ratchet\Wamp\WampConnection;
|
||||||
use Ratchet\Tests\Mock\Connection as MockConnection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers Ratchet\Wamp\Topic
|
* @covers Ratchet\Wamp\Topic
|
||||||
@ -39,12 +38,12 @@ class TopicTest extends \PHPUnit_Framework_TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testBroadcast() {
|
public function testBroadcast() {
|
||||||
$msg = 'Hello World!';
|
$msg = 'Hello World!';
|
||||||
$name = 'batman';
|
$name = 'Batman';
|
||||||
$protocol = json_encode(array(8, $name, $msg));
|
$protocol = json_encode(array(8, $name, $msg));
|
||||||
|
|
||||||
$first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array(new MockConnection));
|
$first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
|
||||||
$second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array(new MockConnection));
|
$second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
|
||||||
|
|
||||||
$first->expects($this->once())
|
$first->expects($this->once())
|
||||||
->method('send')
|
->method('send')
|
||||||
@ -66,7 +65,7 @@ class TopicTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$second = $this->newConn();
|
$second = $this->newConn();
|
||||||
$third = $this->newConn();
|
$third = $this->newConn();
|
||||||
|
|
||||||
$topic = new Topic('joker');
|
$topic = new Topic('Joker');
|
||||||
$topic->add($first)->add($second)->add($third);
|
$topic->add($first)->add($second)->add($third);
|
||||||
|
|
||||||
$check = array($first, $second, $third);
|
$check = array($first, $second, $third);
|
||||||
@ -76,7 +75,14 @@ class TopicTest extends \PHPUnit_Framework_TestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testToString() {
|
||||||
|
$name = 'Bane';
|
||||||
|
$topic = new Topic($name);
|
||||||
|
|
||||||
|
$this->assertEquals($name, (string)$topic);
|
||||||
|
}
|
||||||
|
|
||||||
protected function newConn() {
|
protected function newConn() {
|
||||||
return new WampConnection(new MockConnection);
|
return new WampConnection($this->getMock('\\Ratchet\\ConnectionInterface'));
|
||||||
}
|
}
|
||||||
}
|
}
|
27
tests/Ratchet/Tests/Wamp/WampServerTest.php
Normal file
27
tests/Ratchet/Tests/Wamp/WampServerTest.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Tests\Wamp;
|
||||||
|
use Ratchet\Wamp\WampServer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Ratchet\Wamp\WampServer
|
||||||
|
*/
|
||||||
|
class WampServerTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
private $serv;
|
||||||
|
private $mock;
|
||||||
|
private $conn;
|
||||||
|
|
||||||
|
public function setUp() {
|
||||||
|
$this->mock = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface');
|
||||||
|
$this->serv = new WampServer($this->mock);
|
||||||
|
$this->conn = $this->getMock('\\Ratchet\\ConnectionInterface');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isWampConn() {
|
||||||
|
return new \PHPUnit_Framework_Constraint_IsInstanceOf('\\Ratchet\\Wamp\\WampConnection');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOpen() {
|
||||||
|
$this->mock->expects($this->once())->method('onOpen')->with($this->isWampConn());
|
||||||
|
$this->serv->onOpen($this->conn);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user