diff --git a/src/Ratchet/Session/SessionProvider.php b/src/Ratchet/Session/SessionProvider.php index 7203dda..b2c549b 100644 --- a/src/Ratchet/Session/SessionProvider.php +++ b/src/Ratchet/Session/SessionProvider.php @@ -71,7 +71,7 @@ class SessionProvider implements MessageComponentInterface, WsServerInterface { * {@inheritdoc} */ function onOpen(ConnectionInterface $conn) { - if (null === ($id = $conn->WebSocket->request->getCookie(ini_get('session.name')))) { + if (!isset($conn->WebSocket) || null === ($id = $conn->WebSocket->request->getCookie(ini_get('session.name')))) { $saveHandler = $this->_null; $id = ''; } else { diff --git a/src/Ratchet/WebSocket/WsServer.php b/src/Ratchet/WebSocket/WsServer.php index 77e3fe1..5245019 100644 --- a/src/Ratchet/WebSocket/WsServer.php +++ b/src/Ratchet/WebSocket/WsServer.php @@ -110,7 +110,6 @@ class WsServer implements HttpServerInterface { return; } - // This needs to be refactored later on, incorporated with routing if ('' !== ($agreedSubProtocols = $this->getSubProtocolString($conn->WebSocket->request->getTokenizedHeader('Sec-WebSocket-Protocol', ',')))) { $response->setHeader('Sec-WebSocket-Protocol', $agreedSubProtocols); } diff --git a/tests/Ratchet/Tests/AbstractConnectionDecoratorTest.php b/tests/Ratchet/Tests/AbstractConnectionDecoratorTest.php index 707460c..f196c79 100644 --- a/tests/Ratchet/Tests/AbstractConnectionDecoratorTest.php +++ b/tests/Ratchet/Tests/AbstractConnectionDecoratorTest.php @@ -1,7 +1,6 @@ mock = new Connection; + $this->mock = $this->getMock('\Ratchet\ConnectionInterface'); $this->l1 = new ConnectionDecorator($this->mock); $this->l2 = new ConnectionDecorator($this->l1); } diff --git a/tests/Ratchet/Tests/AbstractMessageComponentTestCase.php b/tests/Ratchet/Tests/AbstractMessageComponentTestCase.php new file mode 100644 index 0000000..a9223af --- /dev/null +++ b/tests/Ratchet/Tests/AbstractMessageComponentTestCase.php @@ -0,0 +1,41 @@ +_app = $this->getMock($this->getComponentClassString()); + $decorator = $this->getDecoratorClassString(); + $this->_serv = new $decorator($this->_app); + $this->_conn = $this->getMock('\Ratchet\ConnectionInterface'); + + $this->_serv->onOpen($this->_conn); + } + + public function isExpectedConnection() { + return new \PHPUnit_Framework_Constraint_IsInstanceOf($this->getConnectionClassString()); + } + + public function testOpen() { + $this->_app->expects($this->once())->method('onOpen')->with($this->isExpectedConnection()); + $this->_serv->onOpen($this->getMock('\Ratchet\ConnectionInterface')); + } + + public function testOnClose() { + $this->_app->expects($this->once())->method('onClose')->with($this->isExpectedConnection()); + $this->_serv->onClose($this->_conn); + } + + public function testOnError() { + $e = new \Exception('Whoops!'); + $this->_app->expects($this->once())->method('onError')->with($this->isExpectedConnection(), $e); + $this->_serv->onError($this->_conn, $e); + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Http/HttpServerTest.php b/tests/Ratchet/Tests/Http/HttpServerTest.php new file mode 100644 index 0000000..c7fef74 --- /dev/null +++ b/tests/Ratchet/Tests/Http/HttpServerTest.php @@ -0,0 +1,33 @@ +_conn->httpHeadersReceived = true; + } + + public function getConnectionClassString() { + return '\Ratchet\ConnectionInterface'; + } + + public function getDecoratorClassString() { + return '\Ratchet\Http\HttpServer'; + } + + public function getComponentClassString() { + return '\Ratchet\Http\HttpServerInterface'; + } + + public function testOpen() { + $headers = "GET / HTTP/1.1\r\nHost: socketo.me\r\n\r\n"; + + $this->_conn->httpHeadersReceived = false; + $this->_app->expects($this->once())->method('onOpen')->with($this->isExpectedConnection()); + $this->_serv->onMessage($this->_conn, $headers); + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Mock/MemorySessionHandler.php b/tests/Ratchet/Tests/Mock/MemorySessionHandler.php deleted file mode 100644 index 87e9953..0000000 --- a/tests/Ratchet/Tests/Mock/MemorySessionHandler.php +++ /dev/null @@ -1,39 +0,0 @@ -_sessions[$session_id])) { - unset($this->_sessions[$session_id]); - } - - return true; - } - - public function gc($maxlifetime) { - return true; - } - - public function open($save_path, $session_id) { - if (!isset($this->_sessions[$session_id])) { - $this->_sessions[$session_id] = ''; - } - - return true; - } - - public function read($session_id) { - return $this->_sessions[$session_id]; - } - - public function write($session_id, $session_data) { - $this->_sessions[$session_id] = $session_data; - - return true; - } -} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Mock/NullComponent.php b/tests/Ratchet/Tests/Mock/NullComponent.php new file mode 100644 index 0000000..73a44a1 --- /dev/null +++ b/tests/Ratchet/Tests/Mock/NullComponent.php @@ -0,0 +1,28 @@ +markTestSkipped('Dependency of Symfony HttpFoundation failed'); } + + parent::setUp(); + $this->_serv = new SessionProvider($this->_app, new NullSessionHandler); + } + + public function getConnectionClassString() { + return '\Ratchet\ConnectionInterface'; + } + + public function getDecoratorClassString() { + return '\Ratchet\Tests\Mock\NullComponent'; + } + + public function getComponentClassString() { + return '\Ratchet\MessageComponentInterface'; } public function classCaseProvider() { @@ -33,7 +49,7 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase { $method = $ref->getMethod('toClassCase'); $method->setAccessible(true); - $component = new SessionProvider($this->getMock('Ratchet\\MessageComponentInterface'), new MemorySessionHandler); + $component = new SessionProvider($this->getMock('Ratchet\\MessageComponentInterface'), $this->getMock('\SessionHandlerInterface')); $this->assertEquals($out, $method->invokeArgs($component, array($in))); } @@ -84,48 +100,6 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase { return $conn; } - public function testOnOpenBubbles() { - $conn = $this->newConn(); - $mock = $this->getMock('Ratchet\\MessageComponentInterface'); - $comp = new SessionProvider($mock, new NullSessionHandler); - - $mock->expects($this->once())->method('onOpen')->with($conn); - $comp->onOpen($conn); - } - - protected function getOpenConn() { - $conn = $this->newConn(); - $mock = $this->getMock('Ratchet\\MessageComponentInterface'); - $prov = new SessionProvider($mock, new NullSessionHandler); - - $prov->onOpen($conn); - - return array($conn, $mock, $prov); - } - - public function testOnMessageBubbles() { - list($conn, $mock, $prov) = $this->getOpenConn(); - $msg = 'No sessions here'; - - $mock->expects($this->once())->method('onMessage')->with($conn, $msg); - $prov->onMessage($conn, $msg); - } - - public function testOnCloseBubbles() { - list($conn, $mock, $prov) = $this->getOpenConn(); - - $mock->expects($this->once())->method('onClose')->with($conn); - $prov->onClose($conn); - } - - public function testOnErrorBubbles() { - list($conn, $mock, $prov) = $this->getOpenConn(); - $e = new \Exception('I made a boo boo'); - - $mock->expects($this->once())->method('onError')->with($conn, $e); - $prov->onError($conn, $e); - } - public function testGetSubProtocolsReturnsArray() { $mock = $this->getMock('Ratchet\\MessageComponentInterface'); $comp = new SessionProvider($mock, new NullSessionHandler); diff --git a/tests/Ratchet/Tests/Wamp/ServerProtocolTest.php b/tests/Ratchet/Tests/Wamp/ServerProtocolTest.php index cf5d803..4217ea3 100644 --- a/tests/Ratchet/Tests/Wamp/ServerProtocolTest.php +++ b/tests/Ratchet/Tests/Wamp/ServerProtocolTest.php @@ -38,7 +38,7 @@ class ServerProtocolTest extends \PHPUnit_Framework_TestCase { * @dataProvider invalidMessageProvider */ public function testInvalidMessages($type) { - $this->setExpectedException('\\Ratchet\\Wamp\\Exception'); + $this->setExpectedException('\Ratchet\Wamp\Exception'); $conn = $this->newConn(); $this->_comp->onOpen($conn); diff --git a/tests/Ratchet/Tests/Wamp/WampServerTest.php b/tests/Ratchet/Tests/Wamp/WampServerTest.php index 6a92d57..e83dc28 100644 --- a/tests/Ratchet/Tests/Wamp/WampServerTest.php +++ b/tests/Ratchet/Tests/Wamp/WampServerTest.php @@ -1,59 +1,44 @@ mock = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface'); - $this->serv = new WampServer($this->mock); - $this->conn = $this->getMock('\\Ratchet\\ConnectionInterface'); - - $this->serv->onOpen($this->conn); + public function getConnectionClassString() { + return '\Ratchet\Wamp\WampConnection'; } - public function isWampConn() { - return new \PHPUnit_Framework_Constraint_IsInstanceOf('\\Ratchet\\Wamp\\WampConnection'); + public function getDecoratorClassString() { + return 'Ratchet\Wamp\WampServer'; } - public function testOpen() { - $this->mock->expects($this->once())->method('onOpen')->with($this->isWampConn()); - $this->serv->onOpen($this->getMock('\\Ratchet\\ConnectionInterface')); - } - - public function testOnClose() { - $this->mock->expects($this->once())->method('onClose')->with($this->isWampConn()); - $this->serv->onClose($this->conn); - } - - public function testOnError() { - $e = new \Exception('hurr hurr'); - $this->mock->expects($this->once())->method('onError')->with($this->isWampConn(), $e); - $this->serv->onError($this->conn, $e); + public function getComponentClassString() { + return '\Ratchet\Wamp\WampServerInterface'; } public function testOnMessageToEvent() { $published = 'Client published this message'; - $this->mock->expects($this->once())->method('onPublish')->with( - $this->isWampConn() - , new \PHPUnit_Framework_Constraint_IsInstanceOf('\\Ratchet\\Wamp\\Topic') + $this->_app->expects($this->once())->method('onPublish')->with( + $this->isExpectedConnection() + , new \PHPUnit_Framework_Constraint_IsInstanceOf('\Ratchet\Wamp\Topic') , $published , array() , array() ); - $this->serv->onMessage($this->conn, json_encode(array(7, 'topic', $published))); + $this->_serv->onMessage($this->_conn, json_encode(array(7, 'topic', $published))); } public function testGetSubProtocols() { // todo: could expand on this - $this->assertInternalType('array', $this->serv->getSubProtocols()); + $this->assertInternalType('array', $this->_serv->getSubProtocols()); } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/WebSocket/Version/Hixie76Test.php b/tests/Ratchet/Tests/WebSocket/Version/Hixie76Test.php index 2e80307..3ac1f4a 100644 --- a/tests/Ratchet/Tests/WebSocket/Version/Hixie76Test.php +++ b/tests/Ratchet/Tests/WebSocket/Version/Hixie76Test.php @@ -54,16 +54,13 @@ class Hixie76Test extends \PHPUnit_Framework_TestCase { return $headers; } -/* @todo Re-enable and fix these tests later - bad functional tests atm, break into units public function testNoUpgradeBeforeBody() { $headers = $this->headerProvider(); - $body = base64_decode($this->_body); - $mockConn = $this->getMock('\\Ratchet\\ConnectionInterface'); - $mockApp = $this->getMock('\\Ratchet\\MessageComponentInterface'); + $mockConn = $this->getMock('\Ratchet\ConnectionInterface'); + $mockApp = $this->getMock('\Ratchet\MessageComponentInterface'); - $server = new HttpServer; - $server->addRoute('test', '/', new WsServer($mockApp)); + $server = new HttpServer(new WsServer($mockApp)); $server->onOpen($mockConn); $mockApp->expects($this->exactly(0))->method('onOpen'); $server->onMessage($mockConn, $headers); @@ -73,16 +70,14 @@ class Hixie76Test extends \PHPUnit_Framework_TestCase { $headers = $this->headerProvider(); $body = base64_decode($this->_body); - $mockConn = $this->getMock('\\Ratchet\\ConnectionInterface'); - $mockApp = $this->getMock('\\Ratchet\\MessageComponentInterface'); + $mockConn = $this->getMock('\Ratchet\ConnectionInterface'); + $mockApp = $this->getMock('\Ratchet\MessageComponentInterface'); - $server = new HttpServer; - $server->addRoute('test', '/', new WsServer($mockApp)); + $server = new HttpServer(new WsServer($mockApp)); $server->onOpen($mockConn); $server->onMessage($mockConn, $headers); $mockApp->expects($this->once())->method('onOpen'); $server->onMessage($mockConn, $body . $this->_crlf . $this->_crlf); } -*/ } \ No newline at end of file