From c11ecd9fb4867ac71854732ffe9e31d2e8090f78 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Mon, 17 Feb 2014 11:23:35 -0500 Subject: [PATCH] [Sessions] Decouple interface from WS, align with HTTP --- src/Ratchet/Session/SessionProvider.php | 20 +++++------------ tests/unit/Session/SessionComponentTest.php | 25 +-------------------- 2 files changed, 6 insertions(+), 39 deletions(-) diff --git a/src/Ratchet/Session/SessionProvider.php b/src/Ratchet/Session/SessionProvider.php index 9a885e8..445f08a 100644 --- a/src/Ratchet/Session/SessionProvider.php +++ b/src/Ratchet/Session/SessionProvider.php @@ -2,7 +2,8 @@ namespace Ratchet\Session; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; -use Ratchet\WebSocket\WsServerInterface; +use Ratchet\Http\HttpServerInterface; +use Guzzle\Http\Message\RequestInterface; use Ratchet\Session\Storage\VirtualSessionStorage; use Ratchet\Session\Serialize\HandlerInterface; use Symfony\Component\HttpFoundation\Session\Session; @@ -14,7 +15,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler; * Your website must also use Symfony HttpFoundation Sessions to read your sites session data * If your are not using at least PHP 5.4 you must include a SessionHandlerInterface stub (is included in Symfony HttpFoundation, loaded w/ composer) */ -class SessionProvider implements MessageComponentInterface, WsServerInterface { +class SessionProvider implements HttpServerInterface { /** * @var \Ratchet\MessageComponentInterface */ @@ -76,8 +77,8 @@ class SessionProvider implements MessageComponentInterface, WsServerInterface { /** * {@inheritdoc} */ - function onOpen(ConnectionInterface $conn) { - if (!isset($conn->WebSocket) || null === ($id = $conn->WebSocket->request->getCookie(ini_get('session.name')))) { + public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { + if (null === $request || null === ($id = $request->getCookie(ini_get('session.name')))) { $saveHandler = $this->_null; $id = ''; } else { @@ -116,17 +117,6 @@ class SessionProvider implements MessageComponentInterface, WsServerInterface { return $this->_app->onError($conn, $e); } - /** - * {@inheritdoc} - */ - public function getSubProtocols() { - if ($this->_app instanceof WsServerInterface) { - return $this->_app->getSubProtocols(); - } else { - return array(); - } - } - /** * Set all the php session. ini options * © Symfony diff --git a/tests/unit/Session/SessionComponentTest.php b/tests/unit/Session/SessionComponentTest.php index c91f13e..4ad69aa 100644 --- a/tests/unit/Session/SessionComponentTest.php +++ b/tests/unit/Session/SessionComponentTest.php @@ -1,11 +1,9 @@ getMock('Guzzle\\Http\\Message\\Request', array('getCookie'), array('POST', '/', array())); $headers->expects($this->once())->method('getCookie', array(ini_get('session.name')))->will($this->returnValue($sessionId)); - $connection->WebSocket = new \StdClass; - $connection->WebSocket->request = $headers; - - $component->onOpen($connection); + $component->onOpen($connection, $headers); $this->assertEquals('world', $connection->Session->get('hello')); } @@ -99,9 +94,6 @@ class SessionProviderTest extends AbstractMessageComponentTestCase { $headers = $this->getMock('Guzzle\Http\Message\Request', array('getCookie'), array('POST', '/', array())); $headers->expects($this->once())->method('getCookie', array(ini_get('session.name')))->will($this->returnValue(null)); - $conn->WebSocket = new \StdClass; - $conn->WebSocket->request = $headers; - return $conn; } @@ -111,21 +103,6 @@ class SessionProviderTest extends AbstractMessageComponentTestCase { $this->_serv->onMessage($this->_conn, $message); } - public function testGetSubProtocolsReturnsArray() { - $mock = $this->getMock('Ratchet\\MessageComponentInterface'); - $comp = new SessionProvider($mock, new NullSessionHandler); - - $this->assertInternalType('array', $comp->getSubProtocols()); - } - - public function testGetSubProtocolsGetFromApp() { - $mock = $this->getMock('Ratchet\WebSocket\Stub\WsMessageComponentInterface'); - $mock->expects($this->once())->method('getSubProtocols')->will($this->returnValue(array('hello', 'world'))); - $comp = new SessionProvider($mock, new NullSessionHandler); - - $this->assertGreaterThanOrEqual(2, count($comp->getSubProtocols())); - } - public function testRejectInvalidSeralizers() { if (!function_exists('wddx_serialize_value')) { $this->markTestSkipped();