[Http][Tests] Refactored unit tests, added new for HTTP

This commit is contained in:
Chris Boden 2013-04-27 10:27:45 -04:00
parent 9d389b14c8
commit 5c41b2fbe1
11 changed files with 143 additions and 128 deletions

View File

@ -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 {

View File

@ -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);
}

View File

@ -1,7 +1,6 @@
<?php
namespace Ratchet\Tests;
use Ratchet\Tests\Mock\ConnectionDecorator;
use Ratchet\Tests\Mock\Connection;
/**
* @covers Ratchet\AbstractConnectionDecorator
@ -13,7 +12,7 @@ class AbstractConnectionDecoratorTest extends \PHPUnit_Framework_TestCase {
protected $l2;
public function setUp() {
$this->mock = new Connection;
$this->mock = $this->getMock('\Ratchet\ConnectionInterface');
$this->l1 = new ConnectionDecorator($this->mock);
$this->l2 = new ConnectionDecorator($this->l1);
}

View File

@ -0,0 +1,41 @@
<?php
namespace Ratchet\Tests;
abstract class AbstractMessageComponentTestCase extends \PHPUnit_Framework_TestCase {
protected $_app;
protected $_serv;
protected $_conn;
abstract public function getConnectionClassString();
abstract public function getDecoratorClassString();
abstract public function getComponentClassString();
public function setUp() {
$this->_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);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace Ratchet\Tests\Http;
use Ratchet\Tests\AbstractMessageComponentTestCase;
/**
* @covers Ratchet\Http\HttpServer
*/
class HttpServerTest extends AbstractMessageComponentTestCase {
public function setUp() {
parent::setUp();
$this->_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);
}
}

View File

@ -1,39 +0,0 @@
<?php
namespace Ratchet\Tests\Mock;
class MemorySessionHandler implements \SessionHandlerInterface {
protected $_sessions = array();
public function close() {
}
public function destroy($session_id) {
if (isset($this->_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;
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Ratchet\WebSocket\WsServerInterface;
use Ratchet\Wamp\WampServerInterface;
class NullComponent implements MessageComponentInterface, WsServerInterface, WampServerInterface {
public function onOpen(ConnectionInterface $conn) {}
public function onMessage(ConnectionInterface $conn, $msg) {}
public function onClose(ConnectionInterface $conn) {}
public function onError(ConnectionInterface $conn, \Exception $e) {}
public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {}
public function onSubscribe(ConnectionInterface $conn, $topic) {}
public function onUnSubscribe(ConnectionInterface $conn, $topic) {}
public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude = array(), array $eligible = array()) {}
public function getSubProtocols() {
return array();
}
}

View File

@ -1,5 +1,6 @@
<?php
namespace Ratchet\Tests\Session;
use Ratchet\Tests\AbstractMessageComponentTestCase;
use Ratchet\Session\SessionProvider;
use Ratchet\Tests\Mock\MemorySessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
@ -11,11 +12,26 @@ use Guzzle\Http\Message\Request;
* @covers Ratchet\Session\Storage\VirtualSessionStorage
* @covers Ratchet\Session\Storage\Proxy\VirtualProxy
*/
class SessionProviderTest extends \PHPUnit_Framework_TestCase {
class SessionProviderTest extends AbstractMessageComponentTestCase {
public function setUp() {
if (!class_exists('Symfony\\Component\\HttpFoundation\\Session\\Session')) {
if (!class_exists('Symfony\Component\HttpFoundation\Session\Session')) {
return $this->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);

View File

@ -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);

View File

@ -1,59 +1,44 @@
<?php
namespace Ratchet\Tests\Wamp;
use Ratchet\Wamp\WampServer;
use Ratchet\Tests\AbstractMessageComponentTestCase;
/**
* @covers Ratchet\Wamp\WampServer
*/
class WampServerTest extends \PHPUnit_Framework_TestCase {
class WampServerTest extends AbstractMessageComponentTestCase {
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');
$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());
}
}

View File

@ -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);
}
*/
}