[Sessions][Tests] Coverage

More code coverage on SessionProvider unit tests
Removed usage of MockConnection in favour of PHPUnit mocks
This commit is contained in:
Chris Boden 2012-07-19 12:01:18 -04:00
parent 4a4e80a945
commit 21bec3f74e
2 changed files with 58 additions and 37 deletions

View File

@ -1,9 +1,7 @@
<?php <?php
namespace Ratchet\Tests\Session; namespace Ratchet\Tests\Session;
use Ratchet\Session\SessionProvider; use Ratchet\Session\SessionProvider;
use Ratchet\Tests\Mock\Component as MockComponent;
use Ratchet\Tests\Mock\MemorySessionHandler; 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\PdoSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler; use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Guzzle\Http\Message\Request; use Guzzle\Http\Message\Request;
@ -35,7 +33,7 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase {
$method = $ref->getMethod('toClassCase'); $method = $ref->getMethod('toClassCase');
$method->setAccessible(true); $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))); $this->assertEquals($out, $method->invokeArgs($component, array($in)));
} }
@ -44,7 +42,7 @@ class SessionProviderTest extends \PHPUnit_Framework_TestCase {
*/ */
public function testConnectionValueFromPdo() { public function testConnectionValueFromPdo() {
if (!extension_loaded('PDO')) { if (!extension_loaded('PDO')) {
return $this->markTestSkipped(); return $this->markTestSkipped('Session test requires PDO');
} }
$sessionId = md5('testSession'); $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->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())); $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)); $component = new SessionProvider($this->getMock('Ratchet\\MessageComponentInterface'), new PdoSessionHandler($pdo, $dbOptions), array('auto_start' => 1));
$connection = new Connection(); $connection = $this->getMock('Ratchet\\ConnectionInterface');
$headers = $this->getMock('Guzzle\\Http\\Message\\Request', array('getCookie'), array('POST', '/', array())); $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)); $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')); $this->assertEquals('world', $connection->Session->get('hello'));
} }
public function testDecoratingMethods() { protected function newConn() {
$conns = array(); $conn = $this->getMock('Ratchet\\ConnectionInterface');
for ($i = 1; $i <= 3; $i++) {
$conns[$i] = new Connection;
$headers = $this->getMock('Guzzle\\Http\\Message\\Request', array('getCookie'), array('POST', '/', array())); $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->expects($this->once())->method('getCookie', array(ini_get('session.name')))->will($this->returnValue(null));
$conns[$i]->WebSocket = new \StdClass; $conn->WebSocket = new \StdClass;
$conns[$i]->WebSocket->request = $headers; $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 = new SessionProvider($mock, new NullSessionHandler);
$comp->onOpen($conns[1]); $mock->expects($this->once())->method('onOpen')->with($conn);
$comp->onOpen($conns[3]); $comp->onOpen($conn);
$comp->onOpen($conns[2]); }
$this->assertSame($conns[2], $mock->last['onOpen'][0]);
$msg = 'Hello World!'; protected function getOpenConn() {
$comp->onMessage($conns[1], $msg); $conn = $this->newConn();
$this->assertSame($conns[1], $mock->last['onMessage'][0]); $mock = $this->getMock('Ratchet\\MessageComponentInterface');
$this->assertEquals($msg, $mock->last['onMessage'][1]); $prov = new SessionProvider($mock, new NullSessionHandler);
$comp->onClose($conns[3]); $prov->onOpen($conn);
$this->assertSame($conns[3], $mock->last['onClose'][0]);
try { return array($conn, $mock, $prov);
throw new \Exception('I threw an error'); }
} catch (\Exception $e) {
}
$comp->onError($conns[2], $e); public function testOnMessageBubbles() {
$this->assertEquals($conns[2], $mock->last['onError'][0]); list($conn, $mock, $prov) = $this->getOpenConn();
$this->assertEquals($e, $mock->last['onError'][1]); $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 = new MockComponent; $mock = $this->getMock('Ratchet\\MessageComponentInterface');
$comp = new SessionProvider($mock, new NullSessionHandler); $comp = new SessionProvider($mock, new NullSessionHandler);
$this->assertTrue(is_array($comp->getSubProtocols())); $this->assertInternalType('array', $comp->getSubProtocols());
} }
public function testGetSubProtocolsGetFromApp() { public function testGetSubProtocolsGetFromApp() {
$mock = new MockComponent; $mock = $this->getMock('Ratchet\\Tests\\WebSocket\\Stub\\WsMessageComponentInterface');
$mock->protocols = array('hello', 'world'); $mock->expects($this->once())->method('getSubProtocols')->will($this->returnValue(array('hello', 'world')));
$comp = new SessionProvider($mock, new NullSessionHandler); $comp = new SessionProvider($mock, new NullSessionHandler);
$this->assertGreaterThanOrEqual(2, count($comp->getSubProtocols())); $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 {
}