From 913774e072401e91a5dab104ee4abff9fe5a635f Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 29 Apr 2012 20:39:09 -0400 Subject: [PATCH] Unit tests --- .../Session/SessionComponentTest.php | 69 ++++++++++++++----- .../WAMP/WAMPServerComponentTest.php | 6 ++ tests/Ratchet/Tests/Mock/Component.php | 3 + .../Tests/Mock/NullMessageComponent.php | 55 --------------- .../Command/Action/SendMessageTest.php | 22 ++++++ 5 files changed, 81 insertions(+), 74 deletions(-) delete mode 100644 tests/Ratchet/Tests/Mock/NullMessageComponent.php create mode 100644 tests/Ratchet/Tests/Resource/Command/Action/SendMessageTest.php diff --git a/tests/Ratchet/Tests/Component/Session/SessionComponentTest.php b/tests/Ratchet/Tests/Component/Session/SessionComponentTest.php index f335e3e..03527c1 100644 --- a/tests/Ratchet/Tests/Component/Session/SessionComponentTest.php +++ b/tests/Ratchet/Tests/Component/Session/SessionComponentTest.php @@ -1,22 +1,23 @@ 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]); + } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/Component/WAMP/WAMPServerComponentTest.php b/tests/Ratchet/Tests/Component/WAMP/WAMPServerComponentTest.php index 7e44885..b4f3875 100644 --- a/tests/Ratchet/Tests/Component/WAMP/WAMPServerComponentTest.php +++ b/tests/Ratchet/Tests/Component/WAMP/WAMPServerComponentTest.php @@ -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!'); + } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/Mock/Component.php b/tests/Ratchet/Tests/Mock/Component.php index 4a1d619..bcf0fbb 100644 --- a/tests/Ratchet/Tests/Mock/Component.php +++ b/tests/Ratchet/Tests/Mock/Component.php @@ -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(); diff --git a/tests/Ratchet/Tests/Mock/NullMessageComponent.php b/tests/Ratchet/Tests/Mock/NullMessageComponent.php deleted file mode 100644 index 0f93fde..0000000 --- a/tests/Ratchet/Tests/Mock/NullMessageComponent.php +++ /dev/null @@ -1,55 +0,0 @@ -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)); - } -} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Resource/Command/Action/SendMessageTest.php b/tests/Ratchet/Tests/Resource/Command/Action/SendMessageTest.php new file mode 100644 index 0000000..fca0055 --- /dev/null +++ b/tests/Ratchet/Tests/Resource/Command/Action/SendMessageTest.php @@ -0,0 +1,22 @@ +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()); + } +} \ No newline at end of file