From d9bc1af385bc44a28404b7f652ea5c999ae78cf2 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Mon, 7 May 2012 20:59:47 -0400 Subject: [PATCH] [WAMP] Cleanup Removed all Command classes (WAMP and global) We-wrote WAMP unit tests to match refs #22 --- .../Component/Server/IpBlackListComponent.php | 10 +- .../Component/WAMP/WAMPServerComponent.php | 111 ++++-------------- src/Ratchet/Component/WAMP/WampConnection.php | 63 ++++++++-- .../WebSocket/WebSocketComponent.php | 4 +- .../Command/Action/ActionInterface.php | 20 ---- .../Command/Action/ActionTemplate.php | 18 --- .../Command/Action/CloseConnection.php | 31 ----- src/Ratchet/Resource/Command/Action/Null.php | 11 -- .../Resource/Command/Action/Runtime.php | 31 ----- .../Resource/Command/Action/SendMessage.php | 43 ------- .../Resource/Command/CommandInterface.php | 16 --- src/Ratchet/Resource/Command/Composite.php | 47 -------- src/Ratchet/Resource/Command/Factory.php | 82 ------------- .../WAMP/Command/Action/CallErrorTest.php | 72 ------------ .../WAMP/Command/Action/CallResultTest.php | 46 -------- .../WAMP/Command/Action/EventTest.php | 0 .../WAMP/WAMPServerComponentTest.php | 96 ++++++++------- .../Component/WAMP/WampConnectionTest.php | 67 +++++++++++ .../Command/Action/SendMessageTest.php | 22 ---- .../Tests/Resource/Command/CompositeTest.php | 63 ---------- 20 files changed, 208 insertions(+), 645 deletions(-) delete mode 100644 src/Ratchet/Resource/Command/Action/ActionInterface.php delete mode 100644 src/Ratchet/Resource/Command/Action/ActionTemplate.php delete mode 100644 src/Ratchet/Resource/Command/Action/CloseConnection.php delete mode 100644 src/Ratchet/Resource/Command/Action/Null.php delete mode 100644 src/Ratchet/Resource/Command/Action/Runtime.php delete mode 100644 src/Ratchet/Resource/Command/Action/SendMessage.php delete mode 100644 src/Ratchet/Resource/Command/CommandInterface.php delete mode 100644 src/Ratchet/Resource/Command/Composite.php delete mode 100644 src/Ratchet/Resource/Command/Factory.php delete mode 100644 tests/Ratchet/Tests/Component/WAMP/Command/Action/CallErrorTest.php delete mode 100644 tests/Ratchet/Tests/Component/WAMP/Command/Action/CallResultTest.php delete mode 100644 tests/Ratchet/Tests/Component/WAMP/Command/Action/EventTest.php create mode 100644 tests/Ratchet/Tests/Component/WAMP/WampConnectionTest.php delete mode 100644 tests/Ratchet/Tests/Resource/Command/Action/SendMessageTest.php delete mode 100644 tests/Ratchet/Tests/Resource/Command/CompositeTest.php diff --git a/src/Ratchet/Component/Server/IpBlackListComponent.php b/src/Ratchet/Component/Server/IpBlackListComponent.php index ad2e38d..f78f9ad 100644 --- a/src/Ratchet/Component/Server/IpBlackListComponent.php +++ b/src/Ratchet/Component/Server/IpBlackListComponent.php @@ -90,17 +90,17 @@ class IpBlackListComponent implements MessageComponentInterface { * {@inheritdoc} */ function onClose(ConnectionInterface $conn) { - if ($this->isBlocked($conn->remoteAddress)) { - return null; + if (!$this->isBlocked($conn->remoteAddress)) { + $this->_decorating->onClose($conn); } - - return $this->_decorating->onClose($conn); } /** * {@inheritdoc} */ function onError(ConnectionInterface $conn, \Exception $e) { - return $this->_decorating->onError($conn, $e); + if (!$this->isBlocked($conn->remoteAddress)) { + $this->_decorating->onError($conn, $e); + } } } \ No newline at end of file diff --git a/src/Ratchet/Component/WAMP/WAMPServerComponent.php b/src/Ratchet/Component/WAMP/WAMPServerComponent.php index 6e3e353..06e5099 100644 --- a/src/Ratchet/Component/WAMP/WAMPServerComponent.php +++ b/src/Ratchet/Component/WAMP/WAMPServerComponent.php @@ -2,11 +2,6 @@ namespace Ratchet\Component\WAMP; use Ratchet\Component\WebSocket\WebSocketComponentInterface; use Ratchet\Resource\ConnectionInterface; -use Ratchet\Resource\Command\Composite; -use Ratchet\Resource\Command\CommandInterface; -use Ratchet\Resource\Command\Factory as CmdFactory; -use Ratchet\Component\WAMP\Command\Action\Prefix; -use Ratchet\Component\WAMP\Command\Action\Welcome; /** * WebSocket Application Messaging Protocol @@ -45,11 +40,17 @@ class WAMPServerComponent implements WebSocketComponentInterface { protected $_decorating; /** - * Any server to client prefixes are stored here - * They're taxied along with the next outgoing message - * @var Ratchet\Resource\Command\Composite + * @var SplObjectStorage */ - protected $_msg_buffer = null; + protected $connections; + + /** + * @param WAMPServerComponentInterface An class to propagate calls through + */ + public function __construct(WAMPServerComponentInterface $server_component) { + $this->_decorating = $server_component; + $this->connections = new \SplObjectStorage; + } /** * {@inheritdoc} @@ -58,43 +59,14 @@ class WAMPServerComponent implements WebSocketComponentInterface { return 'wamp'; } - /** - * @todo WAMP spec does not say what to do when there is an error with PREFIX... - */ - public function addPrefix(ConnectionInterface $conn, $curie, $uri, $from_server = false) { - // validate uri - // validate curie - - // make sure the curie is shorter than the uri - - $conn->WAMP->prefixes[$curie] = $uri; - - if ($from_server) { - $prefix = new Prefix($conn); - $prefix->setPrefix($curie, $uri); - - $this->_msg_buffer->enqueue($prefix); - } - } - /** * {@inheritdoc} */ public function onOpen(ConnectionInterface $conn) { - $conn->WAMP = new \StdClass; - $conn->WAMP->sessionId = uniqid(); - $conn->WAMP->prefixes = array(); + $decor = new WampConnection($conn); + $this->connections->attach($conn, $decor); - $wamp = $this; - $conn->WAMP->addPrefix = function($curie, $uri) use ($wamp, $conn) { - $wamp->addPrefix($conn, $curie, $uri, true); - }; - - $welcome = new Welcome($conn); - $welcome->setWelcome($conn->WAMP->sessionId, \Ratchet\Resource\VERSION); - $this->_msg_buffer->enqueue($welcome); - - return $this->attachStack($this->_decorating->onOpen($conn)); + $this->_decorating->onOpen($decor); } /** @@ -103,13 +75,16 @@ class WAMPServerComponent implements WebSocketComponentInterface { * @throws JSONException */ public function onMessage(ConnectionInterface $from, $msg) { + $from = $this->connections[$from]; + if (null === ($json = @json_decode($msg, true))) { throw new JSONException; } switch ($json[0]) { case static::MSG_PREFIX: - $ret = $this->addPrefix($from, $json[1], $json[2]); + $from->WAMP->prefixes[$json[1]] = $json[2]; +// $from->WAMP->prefixes($json[1], $json[2]); break; case static::MSG_CALL: @@ -121,74 +96,40 @@ class WAMPServerComponent implements WebSocketComponentInterface { $json = $json[0]; } - $ret = $this->_decorating->onCall($from, $callID, $procURI, $json); + $this->_decorating->onCall($from, $callID, $procURI, $json); break; case static::MSG_SUBSCRIBE: - $ret = $this->_decorating->onSubscribe($from, $this->getUri($from, $json[1])); + $this->_decorating->onSubscribe($from, $from->getUri($json[1])); break; case static::MSG_UNSUBSCRIBE: - $ret = $this->_decorating->onUnSubscribe($from, $this->getUri($from, $json[1])); + $this->_decorating->onUnSubscribe($from, $from->getUri($json[1])); break; case static::MSG_PUBLISH: - $ret = $this->_decorating->onPublish($from, $this->getUri($from, $json[1]), $json[2]); + $this->_decorating->onPublish($from, $from->getUri($json[1]), $json[2]); break; default: throw new Exception('Invalid message type'); } - - return $this->attachStack($ret); - } - - /** - * Get the full request URI from the connection object if a prefix has been established for it - * @param Ratchet\Resource\Connection - * @param ... - * @return string - */ - protected function getUri(ConnectionInterface $conn, $uri) { - return (isset($conn->WAMP->prefixes[$uri]) ? $conn->WAMP->prefixes[$uri] : $uri); - } - - /** - * If the developer's application as set some server-to-client prefixes to be set, - * this method ensures those are taxied to the next outgoing message - * @param Ratchet\Resource\Command\CommandInterface|NULL - * @return Ratchet\Resource\Command\Composite - */ - protected function attachStack(CommandInterface $command = null) { - $stack = $this->_msg_buffer; - $stack->enqueue($command); - - $this->_msg_buffer = new Composite; - - return $stack; - } - - /** - * @param WAMPServerComponentInterface An class to propagate calls through - */ - public function __construct(WAMPServerComponentInterface $server_component) { - CmdFactory::registerActionPath(__NAMESPACE__ . '\\Command\\Action'); - - $this->_decorating = $server_component; - $this->_msg_buffer = new Composite; } /** * {@inheritdoc} */ public function onClose(ConnectionInterface $conn) { - return $this->_decorating->onClose($conn); + $decor = $this->connections[$conn]; + $this->connections->detach($conn); + + $this->_decorating->onClose($decor); } /** * {@inheritdoc} */ public function onError(ConnectionInterface $conn, \Exception $e) { - return $this->_decorating->onError($conn, $e); + return $this->_decorating->onError($this->connections[$conn], $e); } } \ No newline at end of file diff --git a/src/Ratchet/Component/WAMP/WampConnection.php b/src/Ratchet/Component/WAMP/WampConnection.php index fa6f921..e8edc01 100644 --- a/src/Ratchet/Component/WAMP/WampConnection.php +++ b/src/Ratchet/Component/WAMP/WampConnection.php @@ -1,36 +1,83 @@ WAMP = new \StdClass; $this->WAMP->sessionId = uniqid(); $this->WAMP->prefixes = array(); - $this->getConnection()->send(json_encode(array(WAMP::MSG_WELCOME, $this->WAMP->sessionId, 1, \Ratchet\Resource\VERSION))); + $this->send(json_encode(array(WAMP::MSG_WELCOME, $this->WAMP->sessionId, 1, \Ratchet\Resource\VERSION))); } - public function callResponse() { + /** + * @param string The unique ID given by the client to respond to + * @param array An array of data to return to the client + */ + public function callResult($id, array $data = array()) { + $this->send(json_encode(array(WAMP::MSG_CALL_RESULT, $id, $data))); } - public function callError() { + /** + * @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 + */ + public function callError($id, $uri, $desc = '', $details = null) { + $data = array(WAMP::MSG_CALL_ERROR, $id, $uri, $desc); + + if (null !== $details) { + $data[] = $details; + } + + $this->send(json_encode($data)); } - public function event() { + /** + * @param string The URI or CURIE to broadcast to + * @param mixed Data to send with the event. Anything that is json'able + */ + public function event($uri, $msg) { + $this->send(json_encode(array(WAMP::MSG_EVENT, $uri, $msg))); } - public function addPrefix($curie, $uri) { + /** + * @param string + * @param string + */ + public function prefix($curie, $uri) { + $this->WAMP->prefixes[$curie] = $uri; + $this->send(json_encode(array(WAMP::MSG_PREFIX, $curie, $uri))); } + /** + * Get the full request URI from the connection object if a prefix has been established for it + * @param string + * @return string + */ + public function getUri($uri) { + return (isset($this->WAMP->prefixes[$uri]) ? $this->WAMP->prefixes[$uri] : $uri); + } + + /** + * @internal + */ public function send($data) { $this->getConnection()->send($data); } + /** + * {@inheritdoc} + */ public function close() { $this->getConnection()->close(); } diff --git a/src/Ratchet/Component/WebSocket/WebSocketComponent.php b/src/Ratchet/Component/WebSocket/WebSocketComponent.php index 1cdbdd6..03a5ab1 100644 --- a/src/Ratchet/Component/WebSocket/WebSocketComponent.php +++ b/src/Ratchet/Component/WebSocket/WebSocketComponent.php @@ -138,8 +138,10 @@ class WebSocketComponent implements MessageComponentInterface { // WS::onOpen is not called when the socket connects, it's call when the handshake is done // The socket could close before WS calls onOpen, so we need to check if we've "opened" it for the developer yet if ($this->connections->contains($conn)) { - $this->_decorating->onClose($this->connections[$conn]); + $decor = $this->connections[$conn]; $this->connections->detach($conn); + + $this->_decorating->onClose($decor); } } diff --git a/src/Ratchet/Resource/Command/Action/ActionInterface.php b/src/Ratchet/Resource/Command/Action/ActionInterface.php deleted file mode 100644 index c5d982e..0000000 --- a/src/Ratchet/Resource/Command/Action/ActionInterface.php +++ /dev/null @@ -1,20 +0,0 @@ -_conn = $conn; - } - - public function getConnection() { - return $this->_conn; - } -} \ No newline at end of file diff --git a/src/Ratchet/Resource/Command/Action/CloseConnection.php b/src/Ratchet/Resource/Command/Action/CloseConnection.php deleted file mode 100644 index 6966a89..0000000 --- a/src/Ratchet/Resource/Command/Action/CloseConnection.php +++ /dev/null @@ -1,31 +0,0 @@ -onClose($this->getConnection()); - - if ($ret instanceof CommandInterface) { - $comp = new Composite; - $comp->enqueue($ret); - - $rt = new Runtime($this->getConnection()); - $rt->setCommand(function(ConnectionInterface $conn, ComponentInterface $scope) { - $conn->getSocket()->close(); - }); - $comp->enqueue($rt); - - return $comp; - } - - $this->getConnection()->getSocket()->close(); - } -} \ No newline at end of file diff --git a/src/Ratchet/Resource/Command/Action/Null.php b/src/Ratchet/Resource/Command/Action/Null.php deleted file mode 100644 index 0b8df4e..0000000 --- a/src/Ratchet/Resource/Command/Action/Null.php +++ /dev/null @@ -1,11 +0,0 @@ -_command = $callback; - } - - /** - * {@inheritdoc} - */ - public function execute(ComponentInterface $scope = null) { - $cmd = $this->_command; - - return $cmd($this->getConnection(), $scope); - } -} \ No newline at end of file diff --git a/src/Ratchet/Resource/Command/Action/SendMessage.php b/src/Ratchet/Resource/Command/Action/SendMessage.php deleted file mode 100644 index 3eca5fe..0000000 --- a/src/Ratchet/Resource/Command/Action/SendMessage.php +++ /dev/null @@ -1,43 +0,0 @@ -_message = (string)$msg; - return $this; - } - - /** - * Get the message from setMessage() - * @return string - */ - public function getMessage() { - return $this->_message; - } - - /** - * {@inheritdoc} - * @throws \UnexpectedValueException if a message was not set with setMessage() - */ - public function execute(ComponentInterface $scope = null) { - if (empty($this->_message)) { - throw new \UnexpectedValueException("Message is empty"); - } - - $this->getConnection()->getSocket()->deliver($this->_message); - } -} \ No newline at end of file diff --git a/src/Ratchet/Resource/Command/CommandInterface.php b/src/Ratchet/Resource/Command/CommandInterface.php deleted file mode 100644 index 5ea31c6..0000000 --- a/src/Ratchet/Resource/Command/CommandInterface.php +++ /dev/null @@ -1,16 +0,0 @@ -enqueue($cmd); - } - - return; - } - - parent::enqueue($command); - } - - /** - * {@inheritdoc} - */ - public function execute(ComponentInterface $scope = null) { - $this->setIteratorMode(static::IT_MODE_DELETE); - - $recursive = new self; - - foreach ($this as $command) { - $recursive->enqueue($command->execute($scope)); - } - - if (count($recursive) > 0) { - return $recursive; - } - } -} \ No newline at end of file diff --git a/src/Ratchet/Resource/Command/Factory.php b/src/Ratchet/Resource/Command/Factory.php deleted file mode 100644 index ebd2cc8..0000000 --- a/src/Ratchet/Resource/Command/Factory.php +++ /dev/null @@ -1,82 +0,0 @@ -addActionPath(__NAMESPACE__ . '\\Action'); - $this->_ignoreGlobals = (boolean)$ignoreGlobals; - } - - /** - * Add a new namespace of which CommandInterfaces reside under to autoload with $this->newCommand() - * @param string - */ - public function addActionPath($namespace) { - $this->_paths[] = $this->slashIt($namespace); - } - - public static function registerActionPath($namespace) { - static::$globalPaths[$namespace] = 1; - } - - /** - * @return Composite - */ - public function newComposite() { - return new Composite; - } - - /** - * @param string - * @return CommandInterface - * @throws UnexpectedValueException - */ - public function newCommand($name, ConnectionInterface $conn) { - if (isset($this->_mapped_commands[$name])) { - $cmd = $this->_mapped_commands[$name]; - return new $cmd($conn); - } - - foreach ($this->_paths as $path) { - if (class_exists($path . $name)) { - $this->_mapped_commands[$name] = $path . $name; - return $this->newCommand($name, $conn); - } - } - - if (false === $this->_ignoreGlobals) { - foreach (static::$globalPaths as $path => $one) { - $path = $this->slashIt($path); - if (class_exists($path . $name)) { - $this->_mapped_commands[$name] = $path . $name; - return $this->newCommand($name, $conn); - } - } - } - - throw new \UnexepctedValueException("Command {$name} not found"); - } - - /** - * @param string - * @return string - */ - protected function slashIt($ns) { - return (substr($ns, -1) == '\\' ? $ns : $ns . '\\'); - } -} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Component/WAMP/Command/Action/CallErrorTest.php b/tests/Ratchet/Tests/Component/WAMP/Command/Action/CallErrorTest.php deleted file mode 100644 index 5d93349..0000000 --- a/tests/Ratchet/Tests/Component/WAMP/Command/Action/CallErrorTest.php +++ /dev/null @@ -1,72 +0,0 @@ -setError($callId, $uri); - $resultString = $error->getMessage(); - - $this->assertEquals(array(4, $callId, $uri, ''), json_decode($resultString, true)); - } - - public function testDetailedCallError() { - $error = new CallError(new Connection); - - $callId = uniqid(); - $uri = 'http://example.com/end/point'; - $desc = 'beep boop beep'; - $detail = 'Error: Too much awesome'; - - $error->setError($callId, $uri, $desc, $detail); - $resultString = $error->getMessage(); - - $this->assertEquals(array(4, $callId, $uri, $desc, $detail), json_decode($resultString, true)); - } - - public function testGetId() { - $id = uniqid(); - - $error = new CallError(new Connection); - $error->setError($id, 'http://example.com'); - - $this->assertEquals($id, $error->getId()); - } - - public function testGetUri() { - $uri = 'http://example.com/end/point'; - - $error = new CallError(new Connection); - $error->setError(uniqid(), $uri); - - $this->assertEquals($uri, $error->getUri()); - } - - public function testGetDescription() { - $desc = uniqid(); - - $error = new CallError(new Connection); - $error->setError(uniqid(), 'curie', $desc); - - $this->assertEquals($desc, $error->getDescription()); - } - - public function testGetDetails() { - $detail = uniqid(); - - $error = new CallError(new Connection); - $this->assertNull($error->getDetails()); - $error->setError(uniqid(), 'http://socketo.me', 'desc', $detail); - - $this->assertEquals($detail, $error->getDetails()); - } -} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Component/WAMP/Command/Action/CallResultTest.php b/tests/Ratchet/Tests/Component/WAMP/Command/Action/CallResultTest.php deleted file mode 100644 index a931d4b..0000000 --- a/tests/Ratchet/Tests/Component/WAMP/Command/Action/CallResultTest.php +++ /dev/null @@ -1,46 +0,0 @@ - 'world', 'herp' => 'derp'); - - $result->setResult($callId, $data); - $resultString = $result->getMessage(); - - $this->assertEquals(array(3, $callId, $data), json_decode($resultString, true)); - } - - public function testGetId() { - $id = uniqid(); - - $result = new CallResult(new Connection); - $result->setResult($id, array()); - - $this->assertEquals($id, $result->getId()); - } - - public function testGetData() { - $data = array( - 'hello' => 'world' - , 'recursive' => array( - 'the' => 'quick' - , 'brown' => 'fox' - ) - , 'jumps' - ); - - $result = new CallResult(new Connection); - $result->setResult(uniqid(), $data); - - $this->assertEquals($data, $result->getData()); - } -} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Component/WAMP/Command/Action/EventTest.php b/tests/Ratchet/Tests/Component/WAMP/Command/Action/EventTest.php deleted file mode 100644 index e69de29..0000000 diff --git a/tests/Ratchet/Tests/Component/WAMP/WAMPServerComponentTest.php b/tests/Ratchet/Tests/Component/WAMP/WAMPServerComponentTest.php index c4dd680..ae2f01d 100644 --- a/tests/Ratchet/Tests/Component/WAMP/WAMPServerComponentTest.php +++ b/tests/Ratchet/Tests/Component/WAMP/WAMPServerComponentTest.php @@ -1,15 +1,14 @@ setExpectedException('\\Ratchet\\Component\\WAMP\\Exception'); - $this->_comp->onMessage($this->newConn(), json_encode(array($type))); + $conn = $this->newConn(); + $this->_comp->onOpen($conn); + $this->_comp->onMessage($conn, json_encode(array($type))); } - /** - * @covers Ratchet\Component\WAMP\Command\Action\Welcome - */ public function testWelcomeMessage() { - $conn = new Connection(); + $conn = $this->newConn(); - $return = $this->_comp->onOpen($conn); - $action = $return->pop(); - $message = $action->getMessage(); + $this->_comp->onOpen($conn); + + $message = $conn->last['send']; $json = json_decode($message); $this->assertEquals(4, count($json)); @@ -65,7 +63,10 @@ class WAMPServerComponentTest extends \PHPUnit_Framework_TestCase { $uri = 'http://example.com'; $clientMessage = array(5, $uri); - $this->_comp->onMessage($this->newConn(), json_encode($clientMessage)); + $conn = $this->newConn(); + + $this->_comp->onOpen($conn); + $this->_comp->onMessage($conn, json_encode($clientMessage)); $this->assertEquals($uri, $this->_app->last['onSubscribe'][1]); } @@ -74,7 +75,10 @@ class WAMPServerComponentTest extends \PHPUnit_Framework_TestCase { $uri = 'http://example.com/endpoint'; $clientMessage = array(6, $uri); - $this->_comp->onMessage($this->newConn(), json_encode($clientMessage)); + $conn = $this->newConn(); + + $this->_comp->onOpen($conn); + $this->_comp->onMessage($conn, json_encode($clientMessage)); $this->assertEquals($uri, $this->_app->last['onUnSubscribe'][1]); } @@ -103,7 +107,10 @@ class WAMPServerComponentTest extends \PHPUnit_Framework_TestCase { $id = uniqid(); $clientMessage = array_merge(array(2, $id, $uri), $args); - $this->_comp->onMessage($this->newConn(), json_encode($clientMessage)); + $conn = $this->newConn(); + + $this->_comp->onOpen($conn); + $this->_comp->onMessage($conn, json_encode($clientMessage)); $this->assertEquals($id, $this->_app->last['onCall'][1]); $this->assertEquals($uri, $this->_app->last['onCall'][2]); @@ -132,67 +139,68 @@ class WAMPServerComponentTest extends \PHPUnit_Framework_TestCase { /** * @dataProvider eventProvider - * @covers Ratchet\Component\WAMP\Command\Action\Event */ public function testEvent($topic, $payload) { - $event = new Event($this->newConn()); - $event->setEvent($topic, $payload); + $conn = new WampConnection($this->newConn()); + $conn->event($topic, $payload); - $eventString = $event->getMessage(); + $eventString = $conn->last['send']; $this->assertSame(array(8, $topic, $payload), json_decode($eventString, true)); } public function testOnClosePropagation() { - $conn = $this->newConn(); + $conn = new Connection; + $this->_comp->onOpen($conn); $this->_comp->onClose($conn); - $this->assertSame($conn, $this->_app->last['onClose'][0]); + $class = new \ReflectionClass('\\Ratchet\\Component\\WAMP\\WampConnection'); + $method = $class->getMethod('getConnection'); + $method->setAccessible(true); + + $check = $method->invokeArgs($this->_app->last['onClose'][0], array()); + + $this->assertSame($conn, $check); } public function testOnErrorPropagation() { - $conn = $this->newConn(); + $conn = new Connection; $e = new \Exception('Nope'); + $this->_comp->onOpen($conn); $this->_comp->onError($conn, $e); - $this->assertSame($conn, $this->_app->last['onError'][0]); + $class = new \ReflectionClass('\\Ratchet\\Component\\WAMP\\WampConnection'); + $method = $class->getMethod('getConnection'); + $method->setAccessible(true); + + $check = $method->invokeArgs($this->_app->last['onError'][0], array()); + + $this->assertSame($conn, $check); $this->assertSame($e, $this->_app->last['onError'][1]); } - /** - * @covers Ratchet\Component\WAMP\Command\Action\Prefix - */ public function testPrefix() { - $conn = $this->newConn(); + $conn = new WampConnection($this->newConn()); $this->_comp->onOpen($conn); - $shortOut = 'outgoing'; - $longOut = 'http://example.com/outoing'; + $shortIn = 'incoming'; + $longIn = 'http://example.com/incoming/'; - $shortIn = 'incoming'; - $shortIn = 'http://example.com/incoming/'; + $this->_comp->onMessage($conn, json_encode(array(1, $shortIn, $longIn))); - $this->assertTrue(is_callable($conn->WAMP->addPrefix)); - - $cb = $conn->WAMP->addPrefix; - $cb($shortOut, $longOut); - - $return = $this->_comp->onMessage($conn, json_encode(array(1, $shortIn, $shortOut))); - $command = $return->pop(); - - $this->assertInstanceOf('Ratchet\\Component\\WAMP\\Command\\Action\\Prefix', $command); - $this->assertEquals($shortOut, $command->getCurie()); - $this->assertEquals($longOut, $command->getUri()); - - $this->assertEquals(array(1, $shortOut, $longOut), json_decode($command->getMessage())); + $this->assertEquals($longIn, $conn->WAMP->prefixes[$shortIn]); + $this->assertEquals($longIn, $conn->getUri($shortIn)); } public function testMessageMustBeJson() { $this->setExpectedException('\\Ratchet\\Component\\WAMP\\JsonException'); - $this->_comp->onMessage($this->newConn(), 'Hello World!'); + $conn = new Connection; + + $this->_comp->onOpen($conn); + $this->_comp->onMessage($conn, 'Hello World!'); } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/Component/WAMP/WampConnectionTest.php b/tests/Ratchet/Tests/Component/WAMP/WampConnectionTest.php new file mode 100644 index 0000000..b77cfe5 --- /dev/null +++ b/tests/Ratchet/Tests/Component/WAMP/WampConnectionTest.php @@ -0,0 +1,67 @@ + 'world', 'herp' => 'derp'); + + + $decor->callResult($callId, $data); + $resultString = $conn->last['send']; + + $this->assertEquals(array(3, $callId, $data), json_decode($resultString, true)); + } + + public function testCallError() { + $conn = new Connection; + $decor = new WampConnection($conn); + + $callId = uniqid(); + $uri = 'http://example.com/end/point'; + + $decor->callError($callId, $uri); + $resultString = $conn->last['send']; + + $this->assertEquals(array(4, $callId, $uri, ''), json_decode($resultString, true)); + } + + public function testDetailedCallError() { + $conn = new Connection; + $decor = new WampConnection($conn); + + $callId = uniqid(); + $uri = 'http://example.com/end/point'; + $desc = 'beep boop beep'; + $detail = 'Error: Too much awesome'; + + $decor->callError($callId, $uri, $desc, $detail); + $resultString = $conn->last['send']; + + $this->assertEquals(array(4, $callId, $uri, $desc, $detail), json_decode($resultString, true)); + } + + public function testPrefix() { + $conn = new WampConnection(new Connection); + + $shortOut = 'outgoing'; + $longOut = 'http://example.com/outoing'; + + $conn->prefix($shortOut, $longOut); + } + + public function testGetUriWhenNoCurieGiven() { + $conn = new WampConnection(new Connection); + $uri = 'http://example.com/noshort'; + + $this->assertEquals($uri, $conn->getUri($uri)); + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Resource/Command/Action/SendMessageTest.php b/tests/Ratchet/Tests/Resource/Command/Action/SendMessageTest.php deleted file mode 100644 index fca0055..0000000 --- a/tests/Ratchet/Tests/Resource/Command/Action/SendMessageTest.php +++ /dev/null @@ -1,22 +0,0 @@ -assertInstanceOf('\\Ratchet\\Resource\\Command\\Action\\SendMessage', $cmd->setMessage('Hello World!')); - } - - public function testGetMessageMatchesSet() { - $msg = 'The quick brown fox jumps over the lazy dog.'; - $cmd = new SendMessage(new Connection); - $cmd->setMessage($msg); - - $this->assertEquals($msg, $cmd->getMessage()); - } -} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Resource/Command/CompositeTest.php b/tests/Ratchet/Tests/Resource/Command/CompositeTest.php deleted file mode 100644 index 9f0e278..0000000 --- a/tests/Ratchet/Tests/Resource/Command/CompositeTest.php +++ /dev/null @@ -1,63 +0,0 @@ -_comp = new Composite; - } - - protected function newNull() { - return new NullAction(new Connection); - } - - public function testCanEnqueueNull() { - $count = $this->_comp->count(); - - $this->_comp->enqueue(null); - - $this->assertEquals($count, $this->_comp->count()); - } - - public function testEnqueueCommand() { - $count = $this->_comp->count(); - - $this->_comp->enqueue($this->newNull()); - - $this->assertEquals($count + 1, $this->_comp->count()); - } - - public function badEnqueueProviders() { - return array( - array(array()) - , array('string') - ); - } - - /** - * @dataProvider badEnqueueProviders - */ - public function testCanNotPassOtherThings($object) { - $this->setExpectedException('InvalidArgumentException'); - - $this->_comp->enqueue($object); - } - - public function testCompositeComposite() { - $compTwo = new Composite; - $compTwo->enqueue($this->newNull()); - $compTwo->enqueue($this->newNull()); - - $this->_comp->enqueue($this->newNull()); - $this->_comp->enqueue($compTwo); - - $this->assertEquals(3, $this->_comp->count()); - } -} \ No newline at end of file