[WAMP] Work

Unit testing WAMP; 0 -> 84% coverage
Fixed an Event command bug
API documentation
This commit is contained in:
Chris Boden 2012-04-28 19:32:32 -04:00
parent 228d0b8627
commit 9e667dfc8b
5 changed files with 104 additions and 5 deletions

View File

@ -3,30 +3,76 @@ namespace Ratchet\Component\WAMP\Command\Action;
use Ratchet\Resource\Command\Action\SendMessage; use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Component\WAMP\WAMPServerComponent as WAMP; use Ratchet\Component\WAMP\WAMPServerComponent as WAMP;
/**
* Respond to a client RPC with an error
*/
class CallError extends SendMessage { class CallError extends SendMessage {
/**
* @var string
*/
protected $_id; protected $_id;
/**
* @var string
*/
protected $_uri; protected $_uri;
protected $_desc; /**
* @var string
*/
protected $_desc = '';
public function setError($callId, $uri, $desc) { /**
* @var string
*/
protected $_details;
/**
* @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 A developer-oriented description of the error
* @param string|null An optional human readable detail message to send back
* @return CallError
*/
public function setError($callId, $uri, $desc = '', $details = null) {
$this->_id = $callId; $this->_id = $callId;
$this->_uri = $uri; $this->_uri = $uri;
$this->_desc = $desc; $this->_desc = $desc;
return $this->setMessage(json_encode(array(WAMP::MSG_CALL_ERROR, $callId, $uri, $desc))); $data = array(WAMP::MSG_CALL_ERROR, $callId, $uri, $desc);
if (null !== $details) {
$data[] = $details;
}
return $this->setMessage(json_encode($data));
} }
/**
* @return string|null
*/
public function getId() { public function getId() {
return $this->_id; return $this->_id;
} }
/**
* @return string|null
*/
public function getUri() { public function getUri() {
return $this->_uri; return $this->_uri;
} }
/**
* @return string
*/
public function getDescription() { public function getDescription() {
return $this->_desc; return $this->_desc;
} }
/**
* @return string|null
*/
public function getDetails() {
return $this->_details;
}
} }

View File

@ -4,12 +4,24 @@ use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Component\WAMP\WAMPServerComponent as WAMP; use Ratchet\Component\WAMP\WAMPServerComponent as WAMP;
/** /**
* Respond to a client RPC
*/ */
class CallResult extends SendMessage { class CallResult extends SendMessage {
/**
* @var string
*/
protected $_id; protected $_id;
/**
* @var array
*/
protected $_data; protected $_data;
/**
* @param string The unique ID given by the client to respond to
* @param array An array of data to return to the client
* @return CallResult
*/
public function setResult($callId, array $data = array()) { public function setResult($callId, array $data = array()) {
$this->_id = $callId; $this->_id = $callId;
$this->_data = $data; $this->_data = $data;
@ -17,10 +29,16 @@ class CallResult extends SendMessage {
return $this->setMessage(json_encode(array(WAMP::MSG_CALL_RESULT, $callId, $data))); return $this->setMessage(json_encode(array(WAMP::MSG_CALL_RESULT, $callId, $data)));
} }
/**
* @return string|null
*/
public function getId() { public function getId() {
return $this->_id; return $this->_id;
} }
/**
* @return array|null
*/
public function getData() { public function getData() {
return $this->_data; return $this->_data;
} }

View File

@ -14,6 +14,6 @@ class Event extends SendMessage {
* @return Event * @return Event
*/ */
public function setEvent($uri, $msg) { public function setEvent($uri, $msg) {
return $this->setMessage(json_encode(array(WAMP::MSG_EVENT, $uri, (string)$msg))); return $this->setMessage(json_encode(array(WAMP::MSG_EVENT, $uri, $msg)));
} }
} }

View File

@ -84,7 +84,6 @@ class WAMPServerComponent implements WebSocketComponentInterface {
$conn->WAMP = new \StdClass; $conn->WAMP = new \StdClass;
$conn->WAMP->sessionId = uniqid(); $conn->WAMP->sessionId = uniqid();
$conn->WAMP->prefixes = array(); $conn->WAMP->prefixes = array();
$conn->WAMP->subscriptions = array();
$wamp = $this; $wamp = $this;
$conn->WAMP->addPrefix = function($curie, $uri) use ($wamp, $conn) { $conn->WAMP->addPrefix = function($curie, $uri) use ($wamp, $conn) {

View File

@ -0,0 +1,36 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\Component\WAMP\WAMPServerComponentInterface;
use Ratchet\Resource\ConnectionInterface;
class WAMPComponent implements WAMPServerComponentInterface {
public $last = array();
public function onCall(ConnectionInterface $conn, $id, $procURI, array $params) {
$this->last[__FUNCTION__] = func_get_args();
}
public function onSubscribe(ConnectionInterface $conn, $uri) {
$this->last[__FUNCTION__] = func_get_args();
}
public function onUnSubscribe(ConnectionInterface $conn, $uri) {
$this->last[__FUNCTION__] = func_get_args();
}
public function onPublish(ConnectionInterface $conn, $uri, $event) {
$this->last[__FUNCTION__] = func_get_args();
}
public function onOpen(ConnectionInterface $conn) {
$this->last[__FUNCTION__] = func_get_args();
}
public function onClose(ConnectionInterface $conn) {
$this->last[__FUNCTION__] = func_get_args();
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$this->last[__FUNCTION__] = func_get_args();
}
}