Unit tests

This commit is contained in:
Chris Boden 2012-04-29 20:39:09 -04:00
parent 86b3343b6f
commit 913774e072
5 changed files with 81 additions and 74 deletions

View File

@ -1,22 +1,23 @@
<?php
namespace Ratchet\Tests\Component\Session;
use Ratchet\Component\Session\SessionComponent;
use Ratchet\Tests\Mock\NullMessageComponent;
use Ratchet\Tests\Mock\Component as MockComponent;
use Ratchet\Tests\Mock\MemorySessionHandler;
use Ratchet\Resource\Connection;
use Ratchet\Tests\Mock\FakeSocket;
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;
/**
* @covers Ratchet\Component\Session\SessionComponent
* @covers Ratchet\Component\Session\Storage\VirtualSessionStorage
* @covers Ratchet\Component\Session\Storage\Proxy\VirtualProxy
*/
class SessionComponentTest extends \PHPUnit_Framework_TestCase {
/**
* @return bool
*/
public function checkSymfonyPresent() {
return class_exists('Symfony\\Component\\HttpFoundation\\Session\\Session');
public function setUp() {
if (!class_exists('Symfony\\Component\\HttpFoundation\\Session\\Session')) {
return $this->markTestSkipped('Dependency of Symfony HttpFoundation failed');
}
}
public function classCaseProvider() {
@ -30,15 +31,11 @@ class SessionComponentTest extends \PHPUnit_Framework_TestCase {
* @dataProvider classCaseProvider
*/
public function testToClassCase($in, $out) {
if (!interface_exists('SessionHandlerInterface')) {
return $this->markTestSkipped('SessionHandlerInterface not defined. Requires PHP 5.4 or Symfony HttpFoundation');
}
$ref = new \ReflectionClass('\\Ratchet\\Component\\Session\\SessionComponent');
$method = $ref->getMethod('toClassCase');
$method->setAccessible(true);
$component = new SessionComponent(new NullMessageComponent, new MemorySessionHandler);
$component = new SessionComponent(new MockComponent, new MemorySessionHandler);
$this->assertEquals($out, $method->invokeArgs($component, array($in)));
}
@ -46,10 +43,6 @@ class SessionComponentTest extends \PHPUnit_Framework_TestCase {
* I think I have severly butchered this test...it's not so much of a unit test as it is a full-fledged component test
*/
public function testConnectionValueFromPdo() {
if (false === $this->checkSymfonyPresent()) {
return $this->markTestSkipped('Dependency of Symfony HttpFoundation failed');
}
$sessionId = md5('testSession');
$dbOptions = array(
@ -63,8 +56,8 @@ class SessionComponentTest 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 SessionComponent(new NullMessageComponent, new PdoSessionHandler($pdo, $dbOptions), array('auto_start' => 1));
$connection = new Connection(new FakeSocket);
$component = new SessionComponent(new MockComponent, new PdoSessionHandler($pdo, $dbOptions), array('auto_start' => 1));
$connection = new Connection();
$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));
@ -76,4 +69,42 @@ class SessionComponentTest 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;
$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->headers = $headers;
}
$mock = new MockComponent;
$comp = new SessionComponent($mock, new NullSessionHandler);
$comp->onOpen($conns[1]);
$comp->onOpen($conns[3]);
$comp->onOpen($conns[2]);
$this->assertSame($conns[2], $mock->last['onOpen'][0]);
$msg = 'Hello World!';
$comp->onMessage($conns[1], $msg);
$this->assertSame($conns[1], $mock->last['onMessage'][0]);
$this->assertEquals($msg, $mock->last['onMessage'][1]);
$comp->onClose($conns[3]);
$this->assertSame($conns[3], $mock->last['onClose'][0]);
try {
throw new \Exception('I threw an error');
} catch (\Exception $e) {
}
$comp->onError($conns[2], $e);
$this->assertEquals($conns[2], $mock->last['onError'][0]);
$this->assertEquals($e, $mock->last['onError'][1]);
}
}

View File

@ -193,4 +193,10 @@ class WAMPServerComponentTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals(array(1, $shortOut, $longOut), json_decode($command->getMessage()));
}
public function testMessageMustBeJson() {
$this->setExpectedException('\\Ratchet\\Component\\WAMP\\JsonException');
$this->_comp->onMessage($this->newConn(), 'Hello World!');
}
}

View File

@ -4,6 +4,9 @@ use Ratchet\Component\MessageComponentInterface;
use Ratchet\Tests\Mock\Socket as MockSocket;
use Ratchet\Resource\ConnectionInterface;
/**
* @todo Rename to MessageComponent
*/
class Component implements MessageComponentInterface {
public $last = array();

View File

@ -1,55 +0,0 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\Component\MessageComponentInterface;
use Ratchet\Resource\ConnectionInterface;
class NullMessageComponent implements MessageComponentInterface {
/**
* @var SplObjectStorage
*/
public $connections;
/**
* @var SplQueue
*/
public $messageHistory;
/**
* @var SplQueue
*/
public $errorHistory;
public function __construct() {
$this->connections = new \SplObjectStorage;
$this->messageHistory = new \SplQueue;
$this->errorHistory = new \SplQueue;
}
/**
* {@inheritdoc}
*/
function onOpen(ConnectionInterface $conn) {
$this->connections->attach($conn);
}
/**
* {@inheritdoc}
*/
function onMessage(ConnectionInterface $from, $msg) {
$this->messageHistory->enqueue(array('from' => $from, 'msg' => $msg));
}
/**
* {@inheritdoc}
*/
function onClose(ConnectionInterface $conn) {
$this->connections->detach($conn);
}
/**
* {@inheritdoc}
*/
function onError(ConnectionInterface $conn, \Exception $e) {
$this->errorHistory->enqueue(array('conn' => $conn, 'exception' => $e));
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Ratchet\Tests\Resource\Command\Action;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Tests\Mock\Connection;
/**
* @covers Ratchet\Resource\Command\Action\SendMessage
*/
class SendMessageTest extends \PHPUnit_Framework_TestCase {
public function testFluentInterface() {
$cmd = new SendMessage(new Connection);
$this->assertInstanceOf('\\Ratchet\\Resource\\Command\\Action\\SendMessage', $cmd->setMessage('Hello World!'));
}
public function testGetMessageMatchesSet() {
$msg = 'The quick brown fox jumps over the lazy dog.';
$cmd = new SendMessage(new Connection);
$cmd->setMessage($msg);
$this->assertEquals($msg, $cmd->getMessage());
}
}