[Http][Tests] Refactored unit tests, added new for HTTP
This commit is contained in:
parent
9d389b14c8
commit
5c41b2fbe1
@ -71,7 +71,7 @@ class SessionProvider implements MessageComponentInterface, WsServerInterface {
|
|||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function onOpen(ConnectionInterface $conn) {
|
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;
|
$saveHandler = $this->_null;
|
||||||
$id = '';
|
$id = '';
|
||||||
} else {
|
} else {
|
||||||
|
@ -110,7 +110,6 @@ class WsServer implements HttpServerInterface {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This needs to be refactored later on, incorporated with routing
|
|
||||||
if ('' !== ($agreedSubProtocols = $this->getSubProtocolString($conn->WebSocket->request->getTokenizedHeader('Sec-WebSocket-Protocol', ',')))) {
|
if ('' !== ($agreedSubProtocols = $this->getSubProtocolString($conn->WebSocket->request->getTokenizedHeader('Sec-WebSocket-Protocol', ',')))) {
|
||||||
$response->setHeader('Sec-WebSocket-Protocol', $agreedSubProtocols);
|
$response->setHeader('Sec-WebSocket-Protocol', $agreedSubProtocols);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Tests;
|
namespace Ratchet\Tests;
|
||||||
use Ratchet\Tests\Mock\ConnectionDecorator;
|
use Ratchet\Tests\Mock\ConnectionDecorator;
|
||||||
use Ratchet\Tests\Mock\Connection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers Ratchet\AbstractConnectionDecorator
|
* @covers Ratchet\AbstractConnectionDecorator
|
||||||
@ -13,7 +12,7 @@ class AbstractConnectionDecoratorTest extends \PHPUnit_Framework_TestCase {
|
|||||||
protected $l2;
|
protected $l2;
|
||||||
|
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
$this->mock = new Connection;
|
$this->mock = $this->getMock('\Ratchet\ConnectionInterface');
|
||||||
$this->l1 = new ConnectionDecorator($this->mock);
|
$this->l1 = new ConnectionDecorator($this->mock);
|
||||||
$this->l2 = new ConnectionDecorator($this->l1);
|
$this->l2 = new ConnectionDecorator($this->l1);
|
||||||
}
|
}
|
||||||
|
41
tests/Ratchet/Tests/AbstractMessageComponentTestCase.php
Normal file
41
tests/Ratchet/Tests/AbstractMessageComponentTestCase.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
33
tests/Ratchet/Tests/Http/HttpServerTest.php
Normal file
33
tests/Ratchet/Tests/Http/HttpServerTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
28
tests/Ratchet/Tests/Mock/NullComponent.php
Normal file
28
tests/Ratchet/Tests/Mock/NullComponent.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Tests\Session;
|
namespace Ratchet\Tests\Session;
|
||||||
|
use Ratchet\Tests\AbstractMessageComponentTestCase;
|
||||||
use Ratchet\Session\SessionProvider;
|
use Ratchet\Session\SessionProvider;
|
||||||
use Ratchet\Tests\Mock\MemorySessionHandler;
|
use Ratchet\Tests\Mock\MemorySessionHandler;
|
||||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
|
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\VirtualSessionStorage
|
||||||
* @covers Ratchet\Session\Storage\Proxy\VirtualProxy
|
* @covers Ratchet\Session\Storage\Proxy\VirtualProxy
|
||||||
*/
|
*/
|
||||||
class SessionProviderTest extends \PHPUnit_Framework_TestCase {
|
class SessionProviderTest extends AbstractMessageComponentTestCase {
|
||||||
public function setUp() {
|
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');
|
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() {
|
public function classCaseProvider() {
|
||||||
@ -33,7 +49,7 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$method = $ref->getMethod('toClassCase');
|
$method = $ref->getMethod('toClassCase');
|
||||||
$method->setAccessible(true);
|
$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)));
|
$this->assertEquals($out, $method->invokeArgs($component, array($in)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,48 +100,6 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase {
|
|||||||
return $conn;
|
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() {
|
public function testGetSubProtocolsReturnsArray() {
|
||||||
$mock = $this->getMock('Ratchet\\MessageComponentInterface');
|
$mock = $this->getMock('Ratchet\\MessageComponentInterface');
|
||||||
$comp = new SessionProvider($mock, new NullSessionHandler);
|
$comp = new SessionProvider($mock, new NullSessionHandler);
|
||||||
|
@ -38,7 +38,7 @@ class ServerProtocolTest extends \PHPUnit_Framework_TestCase {
|
|||||||
* @dataProvider invalidMessageProvider
|
* @dataProvider invalidMessageProvider
|
||||||
*/
|
*/
|
||||||
public function testInvalidMessages($type) {
|
public function testInvalidMessages($type) {
|
||||||
$this->setExpectedException('\\Ratchet\\Wamp\\Exception');
|
$this->setExpectedException('\Ratchet\Wamp\Exception');
|
||||||
|
|
||||||
$conn = $this->newConn();
|
$conn = $this->newConn();
|
||||||
$this->_comp->onOpen($conn);
|
$this->_comp->onOpen($conn);
|
||||||
|
@ -1,59 +1,44 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Tests\Wamp;
|
namespace Ratchet\Tests\Wamp;
|
||||||
use Ratchet\Wamp\WampServer;
|
use Ratchet\Wamp\WampServer;
|
||||||
|
use Ratchet\Tests\AbstractMessageComponentTestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers Ratchet\Wamp\WampServer
|
* @covers Ratchet\Wamp\WampServer
|
||||||
*/
|
*/
|
||||||
class WampServerTest extends \PHPUnit_Framework_TestCase {
|
class WampServerTest extends AbstractMessageComponentTestCase {
|
||||||
private $serv;
|
private $serv;
|
||||||
private $mock;
|
private $mock;
|
||||||
private $conn;
|
private $conn;
|
||||||
|
|
||||||
public function setUp() {
|
public function getConnectionClassString() {
|
||||||
$this->mock = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface');
|
return '\Ratchet\Wamp\WampConnection';
|
||||||
$this->serv = new WampServer($this->mock);
|
|
||||||
$this->conn = $this->getMock('\\Ratchet\\ConnectionInterface');
|
|
||||||
|
|
||||||
$this->serv->onOpen($this->conn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isWampConn() {
|
public function getDecoratorClassString() {
|
||||||
return new \PHPUnit_Framework_Constraint_IsInstanceOf('\\Ratchet\\Wamp\\WampConnection');
|
return 'Ratchet\Wamp\WampServer';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testOpen() {
|
public function getComponentClassString() {
|
||||||
$this->mock->expects($this->once())->method('onOpen')->with($this->isWampConn());
|
return '\Ratchet\Wamp\WampServerInterface';
|
||||||
$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 testOnMessageToEvent() {
|
public function testOnMessageToEvent() {
|
||||||
$published = 'Client published this message';
|
$published = 'Client published this message';
|
||||||
|
|
||||||
$this->mock->expects($this->once())->method('onPublish')->with(
|
$this->_app->expects($this->once())->method('onPublish')->with(
|
||||||
$this->isWampConn()
|
$this->isExpectedConnection()
|
||||||
, new \PHPUnit_Framework_Constraint_IsInstanceOf('\\Ratchet\\Wamp\\Topic')
|
, new \PHPUnit_Framework_Constraint_IsInstanceOf('\Ratchet\Wamp\Topic')
|
||||||
, $published
|
, $published
|
||||||
, array()
|
, array()
|
||||||
, 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() {
|
public function testGetSubProtocols() {
|
||||||
// todo: could expand on this
|
// todo: could expand on this
|
||||||
$this->assertInternalType('array', $this->serv->getSubProtocols());
|
$this->assertInternalType('array', $this->_serv->getSubProtocols());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -54,16 +54,13 @@ class Hixie76Test extends \PHPUnit_Framework_TestCase {
|
|||||||
return $headers;
|
return $headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @todo Re-enable and fix these tests later - bad functional tests atm, break into units
|
|
||||||
public function testNoUpgradeBeforeBody() {
|
public function testNoUpgradeBeforeBody() {
|
||||||
$headers = $this->headerProvider();
|
$headers = $this->headerProvider();
|
||||||
$body = base64_decode($this->_body);
|
|
||||||
|
|
||||||
$mockConn = $this->getMock('\\Ratchet\\ConnectionInterface');
|
$mockConn = $this->getMock('\Ratchet\ConnectionInterface');
|
||||||
$mockApp = $this->getMock('\\Ratchet\\MessageComponentInterface');
|
$mockApp = $this->getMock('\Ratchet\MessageComponentInterface');
|
||||||
|
|
||||||
$server = new HttpServer;
|
$server = new HttpServer(new WsServer($mockApp));
|
||||||
$server->addRoute('test', '/', new WsServer($mockApp));
|
|
||||||
$server->onOpen($mockConn);
|
$server->onOpen($mockConn);
|
||||||
$mockApp->expects($this->exactly(0))->method('onOpen');
|
$mockApp->expects($this->exactly(0))->method('onOpen');
|
||||||
$server->onMessage($mockConn, $headers);
|
$server->onMessage($mockConn, $headers);
|
||||||
@ -73,16 +70,14 @@ class Hixie76Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$headers = $this->headerProvider();
|
$headers = $this->headerProvider();
|
||||||
$body = base64_decode($this->_body);
|
$body = base64_decode($this->_body);
|
||||||
|
|
||||||
$mockConn = $this->getMock('\\Ratchet\\ConnectionInterface');
|
$mockConn = $this->getMock('\Ratchet\ConnectionInterface');
|
||||||
$mockApp = $this->getMock('\\Ratchet\\MessageComponentInterface');
|
$mockApp = $this->getMock('\Ratchet\MessageComponentInterface');
|
||||||
|
|
||||||
$server = new HttpServer;
|
$server = new HttpServer(new WsServer($mockApp));
|
||||||
$server->addRoute('test', '/', new WsServer($mockApp));
|
|
||||||
$server->onOpen($mockConn);
|
$server->onOpen($mockConn);
|
||||||
$server->onMessage($mockConn, $headers);
|
$server->onMessage($mockConn, $headers);
|
||||||
|
|
||||||
$mockApp->expects($this->once())->method('onOpen');
|
$mockApp->expects($this->once())->method('onOpen');
|
||||||
$server->onMessage($mockConn, $body . $this->_crlf . $this->_crlf);
|
$server->onMessage($mockConn, $body . $this->_crlf . $this->_crlf);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user