Merge branch 'refs/heads/unit-tests' into 0.2-beta

This commit is contained in:
Chris Boden 2012-07-20 10:25:18 -04:00
commit e7e8936b77
8 changed files with 234 additions and 94 deletions

View File

@ -11,6 +11,7 @@ interface ConnectionInterface {
/**
* Send data to the connection
* @param string
* @return ConnectionInterface
*/
function send($data);

View File

@ -13,14 +13,16 @@ class IoConnection implements ConnectionInterface {
protected $conn;
public function __construct(ReactConn $conn) {
$this->conn = $conn;
$this->conn = $conn;
}
/**
* {@inheritdoc}
*/
public function send($data) {
return $this->conn->write($data);
$this->conn->write($data);
return $this;
}
/**

View File

@ -17,6 +17,7 @@ class FlashPolicyTest extends \PHPUnit_Framework_TestCase {
$this->_policy->setSiteControl('all');
$this->_policy->addAllowedAccess('example.com', '*');
$this->_policy->addAllowedAccess('dev.example.com', '*');
$this->assertInstanceOf('SimpleXMLElement', $this->_policy->renderPolicy());
}
@ -108,4 +109,32 @@ class FlashPolicyTest extends \PHPUnit_Framework_TestCase {
, array(false, '838*')
);
}
public function testAddAllowedAccessOnlyAcceptsValidPorts() {
$this->setExpectedException('UnexpectedValueException');
$this->_policy->addAllowedAccess('*', 'nope');
}
public function testSetSiteControlThrowsException() {
$this->setExpectedException('UnexpectedValueException');
$this->_policy->setSiteControl('nope');
}
public function testErrorClosesConnection() {
$conn = $this->getMock('\\Ratchet\\ConnectionInterface');
$conn->expects($this->once())->method('close');
$this->_policy->onError($conn, new \Exception);
}
public function testOnMessageSendsString() {
$this->_policy->addAllowedAccess('*', '*');
$conn = $this->getMock('\\Ratchet\\ConnectionInterface');
$conn->expects($this->once())->method('send')->with($this->isType('string'));
$this->_policy->onMessage($conn, ' ');
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace Ratchet\Tests\Application\Server;
use Ratchet\Server\IoConnection;
/**
* @covers Ratchet\Server\IoConnection
*/
class IoConnectionTest extends \PHPUnit_Framework_TestCase {
protected $sock;
protected $conn;
public function setUp() {
$this->sock = $this->getMock('\\React\\Socket\\ConnectionInterface');
$this->conn = new IoConnection($this->sock);
}
public function testCloseBubbles() {
$this->sock->expects($this->once())->method('end');
$this->conn->close();
}
public function testSendBubbles() {
$msg = '6 hour rides are productive';
$this->sock->expects($this->once())->method('write')->with($msg);
$this->conn->send($msg);
}
public function testSendReturnsSelf() {
$this->assertSame($this->conn, $this->conn->send('fluent interface'));
}
}

View File

@ -3,7 +3,6 @@ namespace Ratchet\Tests\Server;
use Ratchet\Server\IoServer;
use React\EventLoop\StreamSelectLoop;
use React\Socket\Server;
use Ratchet\Tests\Mock\Component;
/**
* @covers Ratchet\Server\IoServer
@ -18,7 +17,7 @@ class IoServerTest extends \PHPUnit_Framework_TestCase {
protected $reactor;
public function setUp() {
$this->app = new Component;
$this->app = $this->getMock('\\Ratchet\\MessageComponentInterface');
$loop = new StreamSelectLoop;
$this->reactor = new Server($loop);
@ -29,18 +28,24 @@ class IoServerTest extends \PHPUnit_Framework_TestCase {
}
public function testOnOpen() {
$this->app->expects($this->once())->method('onOpen')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'));
$client = stream_socket_client("tcp://localhost:{$this->port}");
$this->server->loop->tick();
$this->assertInstanceOf('\\Ratchet\\ConnectionInterface', $this->app->last['onOpen'][0]);
$this->assertTrue(is_string($this->app->last['onOpen'][0]->remoteAddress));
$this->assertTrue(is_int($this->app->last['onOpen'][0]->resourceId));
//$this->assertTrue(is_string($this->app->last['onOpen'][0]->remoteAddress));
//$this->assertTrue(is_int($this->app->last['onOpen'][0]->resourceId));
}
public function testOnData() {
$msg = 'Hello World!';
$this->app->expects($this->once())->method('onMessage')->with(
$this->isInstanceOf('\\Ratchet\\ConnectionInterface')
, $msg
);
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($client, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 4096);
@ -56,14 +61,12 @@ class IoServerTest extends \PHPUnit_Framework_TestCase {
socket_shutdown($client, 0);
socket_close($client);
usleep(5000);
$this->server->loop->tick();
$this->assertEquals($msg, $this->app->last['onMessage'][1]);
}
public function testOnClose() {
$this->app->expects($this->once())->method('onClose')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'));
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($client, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 4096);
@ -77,10 +80,6 @@ class IoServerTest extends \PHPUnit_Framework_TestCase {
socket_close($client);
$this->server->loop->tick();
usleep(5000);
$this->assertSame($this->app->last['onOpen'][0], $this->app->last['onClose'][0]);
}
public function testFactory() {
@ -88,10 +87,32 @@ class IoServerTest extends \PHPUnit_Framework_TestCase {
}
public function testNoLoopProvidedError() {
$loop = new StreamSelectLoop;
$io = new IoServer(new Component, new Server($loop));
$this->setExpectedException('RuntimeException');
$io = new IoServer($this->app, $this->reactor);
$io->run();
}
public function testOnErrorPassesException() {
$conn = $this->getMock('\\React\\Socket\\ConnectionInterface');
$conn->decor = $this->getMock('\\Ratchet\\ConnectionInterface');
$err = new \Exception("Nope");
$this->app->expects($this->once())->method('onError')->with($conn->decor, $err);
$this->server->handleError($err, $conn);
}
public function onErrorCalledWhenExceptionThrown() {
$this->markTestIncomplete("Need to learn how to throw an exception from a mock");
$conn = $this->getMock('\\React\\Socket\\ConnectionInterface');
$this->server->handleConnect($conn);
$e = new \Exception;
$this->app->expects($this->once())->method('onMessage')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'), 'f')->will($e);
$this->app->expects($this->once())->method('onError')->with($this->instanceOf('\\Ratchet\\ConnectionInterface', $e));
$this->server->handleData('f', $conn);
}
}

View File

@ -1,29 +1,75 @@
<?php
namespace Ratchet\Tests\Server;
use Ratchet\Server\IpBlackList;
use Ratchet\Tests\Mock\Connection;
use Ratchet\Tests\Mock\Component as MockComponent;
/**
* @covers Ratchet\Server\IpBlackList
*/
class IpBlackListTest extends \PHPUnit_Framework_TestCase {
protected $_comp;
protected $_mock;
protected $blocker;
protected $mock;
public function setUp() {
$this->_mock = new MockComponent;
$this->_comp = new IpBlackList($this->_mock);
$this->mock = $this->getMock('\\Ratchet\\MessageComponentInterface');
$this->blocker = new IpBlackList($this->mock);
}
public function testBlockAndCloseOnOpen() {
$conn = new Connection;
public function testOnOpen() {
$this->mock->expects($this->exactly(3))->method('onOpen');
$this->_comp->blockAddress($conn->remoteAddress);
$conn1 = $this->newConn();
$conn2 = $this->newConn();
$conn3 = $this->newConn();
$ret = $this->_comp->onOpen($conn);
$this->blocker->onOpen($conn1);
$this->blocker->onOpen($conn3);
$this->blocker->onOpen($conn2);
}
$this->assertTrue($conn->last['close']);
public function testBlockDoesNotTriggerOnOpen() {
$conn = $this->newConn();
$this->blocker->blockAddress($conn->remoteAddress);
$this->mock->expects($this->never())->method('onOpen');
$ret = $this->blocker->onOpen($conn);
}
public function testBlockDoesNotTriggerOnClose() {
$conn = $this->newConn();
$this->blocker->blockAddress($conn->remoteAddress);
$this->mock->expects($this->never())->method('onClose');
$ret = $this->blocker->onOpen($conn);
}
public function testOnMessageDecoration() {
$conn = $this->newConn();
$msg = 'Hello not being blocked';
$this->mock->expects($this->once())->method('onMessage')->with($conn, $msg);
$this->blocker->onMessage($conn, $msg);
}
public function testOnCloseDecoration() {
$conn = $this->newConn();
$this->mock->expects($this->once())->method('onClose')->with($conn);
$this->blocker->onClose($conn);
}
public function testBlockClosesConnection() {
$conn = $this->newConn();
$this->blocker->blockAddress($conn->remoteAddress);
$conn->expects($this->once())->method('close');
$this->blocker->onOpen($conn);
}
public function testAddAndRemoveWithFluentInterfaces() {
@ -31,42 +77,23 @@ class IpBlackListTest extends \PHPUnit_Framework_TestCase {
$blockTwo = '192.168.1.1';
$unblock = '75.119.207.140';
$this->_comp
$this->blocker
->blockAddress($unblock)
->blockAddress($blockOne)
->unblockAddress($unblock)
->blockAddress($blockTwo)
;
$this->assertEquals(array($blockOne, $blockTwo), $this->_comp->getBlockedAddresses());
$this->assertEquals(array($blockOne, $blockTwo), $this->blocker->getBlockedAddresses());
}
public function testDecoratingMethods() {
$conn1 = new Connection;
$conn2 = new Connection;
$conn3 = new Connection;
public function testDecoratorPassesErrors() {
$conn = $this->newConn();
$e = new \Exception('I threw an error');
$this->_comp->onOpen($conn1);
$this->_comp->onOpen($conn3);
$this->_comp->onOpen($conn2);
$this->assertSame($conn2, $this->_mock->last['onOpen'][0]);
$this->mock->expects($this->once())->method('onError')->with($conn, $e);
$msg = 'Hello World!';
$this->_comp->onMessage($conn1, $msg);
$this->assertSame($conn1, $this->_mock->last['onMessage'][0]);
$this->assertEquals($msg, $this->_mock->last['onMessage'][1]);
$this->_comp->onClose($conn3);
$this->assertSame($conn3, $this->_mock->last['onClose'][0]);
try {
throw new \Exception('I threw an error');
} catch (\Exception $e) {
}
$this->_comp->onError($conn2, $e);
$this->assertEquals($conn2, $this->_mock->last['onError'][0]);
$this->assertEquals($e, $this->_mock->last['onError'][1]);
$this->blocker->onError($conn, $e);
}
public function addressProvider() {
@ -82,10 +109,17 @@ class IpBlackListTest extends \PHPUnit_Framework_TestCase {
* @dataProvider addressProvider
*/
public function testFilterAddress($expected, $input) {
$this->assertEquals($expected, $this->_comp->filterAddress($input));
$this->assertEquals($expected, $this->blocker->filterAddress($input));
}
public function testUnblockingSilentlyFails() {
$this->assertInstanceOf('\\Ratchet\\Server\\IpBlackList', $this->_comp->unblockAddress('localhost'));
$this->assertInstanceOf('\\Ratchet\\Server\\IpBlackList', $this->blocker->unblockAddress('localhost'));
}
protected function newConn() {
$conn = $this->getMock('\\Ratchet\\ConnectionInterface');
$conn->remoteAddress = '127.0.0.1';
return $conn;
}
}

View File

@ -1,9 +1,7 @@
<?php
namespace Ratchet\Tests\Session;
use Ratchet\Session\SessionProvider;
use Ratchet\Tests\Mock\Component as MockComponent;
use Ratchet\Tests\Mock\MemorySessionHandler;
use Ratchet\Tests\Mock\Connection;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Guzzle\Http\Message\Request;
@ -35,7 +33,7 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase {
$method = $ref->getMethod('toClassCase');
$method->setAccessible(true);
$component = new SessionProvider(new MockComponent, new MemorySessionHandler);
$component = new SessionProvider($this->getMock('Ratchet\\MessageComponentInterface'), new MemorySessionHandler);
$this->assertEquals($out, $method->invokeArgs($component, array($in)));
}
@ -44,7 +42,7 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase {
*/
public function testConnectionValueFromPdo() {
if (!extension_loaded('PDO')) {
return $this->markTestSkipped();
return $this->markTestSkipped('Session test requires PDO');
}
$sessionId = md5('testSession');
@ -60,8 +58,8 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase {
$pdo->exec(vsprintf("CREATE TABLE %s (%s VARCHAR(255) PRIMARY KEY, %s TEXT, %s INTEGER)", $dbOptions));
$pdo->prepare(vsprintf("INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)", $dbOptions))->execute(array($sessionId, base64_encode('_sf2_attributes|a:2:{s:5:"hello";s:5:"world";s:4:"last";i:1332872102;}_sf2_flashes|a:0:{}'), time()));
$component = new SessionProvider(new MockComponent, new PdoSessionHandler($pdo, $dbOptions), array('auto_start' => 1));
$connection = new Connection();
$component = new SessionProvider($this->getMock('Ratchet\\MessageComponentInterface'), new PdoSessionHandler($pdo, $dbOptions), array('auto_start' => 1));
$connection = $this->getMock('Ratchet\\ConnectionInterface');
$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($sessionId));
@ -74,54 +72,70 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals('world', $connection->Session->get('hello'));
}
public function testDecoratingMethods() {
$conns = array();
for ($i = 1; $i <= 3; $i++) {
$conns[$i] = new Connection;
protected function newConn() {
$conn = $this->getMock('Ratchet\\ConnectionInterface');
$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));
$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));
$conns[$i]->WebSocket = new \StdClass;
$conns[$i]->WebSocket->request = $headers;
}
$conn->WebSocket = new \StdClass;
$conn->WebSocket->request = $headers;
$mock = new MockComponent;
return $conn;
}
public function testOnOpenBubbles() {
$conn = $this->newConn();
$mock = $this->getMock('Ratchet\\MessageComponentInterface');
$comp = new SessionProvider($mock, new NullSessionHandler);
$comp->onOpen($conns[1]);
$comp->onOpen($conns[3]);
$comp->onOpen($conns[2]);
$this->assertSame($conns[2], $mock->last['onOpen'][0]);
$mock->expects($this->once())->method('onOpen')->with($conn);
$comp->onOpen($conn);
}
$msg = 'Hello World!';
$comp->onMessage($conns[1], $msg);
$this->assertSame($conns[1], $mock->last['onMessage'][0]);
$this->assertEquals($msg, $mock->last['onMessage'][1]);
protected function getOpenConn() {
$conn = $this->newConn();
$mock = $this->getMock('Ratchet\\MessageComponentInterface');
$prov = new SessionProvider($mock, new NullSessionHandler);
$comp->onClose($conns[3]);
$this->assertSame($conns[3], $mock->last['onClose'][0]);
$prov->onOpen($conn);
try {
throw new \Exception('I threw an error');
} catch (\Exception $e) {
}
return array($conn, $mock, $prov);
}
$comp->onError($conns[2], $e);
$this->assertEquals($conns[2], $mock->last['onError'][0]);
$this->assertEquals($e, $mock->last['onError'][1]);
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 = new MockComponent;
$mock = $this->getMock('Ratchet\\MessageComponentInterface');
$comp = new SessionProvider($mock, new NullSessionHandler);
$this->assertTrue(is_array($comp->getSubProtocols()));
$this->assertInternalType('array', $comp->getSubProtocols());
}
public function testGetSubProtocolsGetFromApp() {
$mock = new MockComponent;
$mock->protocols = array('hello', 'world');
$mock = $this->getMock('Ratchet\\Tests\\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()));

View File

@ -0,0 +1,7 @@
<?php
namespace Ratchet\Tests\WebSocket\Stub;
use Ratchet\MessageComponentInterface;
use Ratchet\WebSocket\WsServerInterface;
interface WsMessageComponentInterface extends MessageComponentInterface, WsServerInterface {
}