[WAMP][Tests] Coverage

Sight bug fixes in WAMP topics
Unit tests coverage
This commit is contained in:
Chris Boden 2012-07-19 01:38:08 -04:00
parent 4078a360a8
commit bd5b7d09aa
6 changed files with 88 additions and 29 deletions

View File

@ -71,7 +71,9 @@ class IoServer {
throw new \RuntimeException("A React Loop was not provided during instantiation");
}
// @codeCoverageIgnoreStart
$this->loop->run();
// @codeCoverageIgnoreEnd
}
/**

View File

@ -1,5 +1,6 @@
<?php
namespace Ratchet\Wamp;
use Ratchet\ConnectionInterface;
/**
* A topic/channel containing connections that have subscribed to it
@ -45,7 +46,7 @@ class Topic implements \IteratorAggregate, \Countable {
* @param WampConnection
* @return Topic
*/
public function add(WampConnection $conn) {
public function add(ConnectionInterface $conn) {
$this->subscribers->attach($conn);
return $this;
@ -55,7 +56,7 @@ class Topic implements \IteratorAggregate, \Countable {
* @param WampConnection
* @return Topic
*/
public function remove(WampConnection $conn) {
public function remove(ConnectionInterface $conn) {
if ($this->subscribers->contains($conn)) {
$this->subscribers->detach($conn);
}

View File

@ -39,6 +39,10 @@ class TopicManager implements WsServerInterface, WampServerInterface {
public function onSubscribe(ConnectionInterface $conn, $topic) {
$topicObj = $this->getTopic($topic);
if ($conn->WAMP->topics->contains($topicObj)) {
return;
}
$conn->WAMP->topics->attach($topicObj);
$this->app->onSubscribe($conn, $topicObj);
}
@ -50,7 +54,9 @@ class TopicManager implements WsServerInterface, WampServerInterface {
$topicObj = $this->getTopic($topic);
if ($conn->WAMP->topics->contains($topicObj)) {
$conn->WAMP->topics->remove($topicObj);
$conn->WAMP->topics->detach($topicObj);
} else {
return;
}
$this->topicLookup[$topic]->remove($conn);

View File

@ -240,4 +240,11 @@ class ServerProtocolTest extends \PHPUnit_Framework_TestCase {
$this->assertGreaterThanOrEqual(3, count($this->_comp->getSubProtocols()));
}
public function testWampOnMessageApp() {
$app = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface');
$wamp = new ServerProtocol($app);
$this->assertContains('wamp', $wamp->getSubProtocols());
}
}

View File

@ -82,7 +82,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
$this->mngr->onSubscribe($this->conn, 'new topic');
}
public function testTopicIsInConnection() {
public function testTopicIsInConnectionOnSubscribe() {
$name = 'New Topic';
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
@ -95,4 +95,45 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
$this->assertTrue($this->conn->WAMP->topics->contains($topic));
}
public function testDoubleSubscriptionFiresOnce() {
$this->mock->expects($this->exactly(1))->method('onSubscribe');
$this->mngr->onSubscribe($this->conn, 'same topic');
$this->mngr->onSubscribe($this->conn, 'same topic');
}
public function testUnsubscribeEvent() {
$name = 'in and out';
$this->mock->expects($this->once())->method('onUnsubscribe')->with(
$this->conn, $this->isTopic()
);
$this->mngr->onSubscribe($this->conn, $name);
$this->mngr->onUnsubscribe($this->conn, $name);
}
public function testUnsubscribeFiresOnce() {
$name = 'getting sleepy';
$this->mock->expects($this->exactly(1))->method('onUnsubscribe');
$this->mngr->onSubscribe($this->conn, $name);
$this->mngr->onUnsubscribe($this->conn, $name);
$this->mngr->onUnsubscribe($this->conn, $name);
}
public function testUnsubscribeRemovesTopicFromConnection() {
$name = 'Bye Bye Topic';
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
$method = $class->getMethod('getTopic');
$method->setAccessible(true);
$topic = $method->invokeArgs($this->mngr, array($name));
$this->mngr->onSubscribe($this->conn, $name);
$this->mngr->onUnsubscribe($this->conn, $name);
$this->assertFalse($this->conn->WAMP->topics->contains($topic));
}
}

View File

@ -1,67 +1,69 @@
<?php
namespace Ratchet\Tests\Wamp;
use Ratchet\Wamp\WampConnection;
use Ratchet\Tests\Mock\Connection;
/**
* @covers Ratchet\Wamp\WampConnection
*/
class WampConnectionTest extends \PHPUnit_Framework_TestCase {
public function testCallResult() {
$conn = new Connection;
$decor = new WampConnection($conn);
protected $conn;
protected $mock;
public function setUp() {
$this->mock = $this->getMock('\\Ratchet\\ConnectionInterface');
$this->conn = new WampConnection($this->mock);
}
public function testCallResult() {
$callId = uniqid();
$data = array('hello' => 'world', 'herp' => 'derp');
$this->mock->expects($this->once())->method('send')->with(json_encode(array(3, $callId, $data)));
$decor->callResult($callId, $data);
$resultString = $conn->last['send'];
$this->assertEquals(array(3, $callId, $data), json_decode($resultString, true));
$this->conn->callResult($callId, $data);
}
public function testCallError() {
$conn = new Connection;
$decor = new WampConnection($conn);
$callId = uniqid();
$uri = 'http://example.com/end/point';
$decor->callError($callId, $uri);
$resultString = $conn->last['send'];
$this->mock->expects($this->once())->method('send')->with(json_encode(array(4, $callId, $uri, '')));
$this->assertEquals(array(4, $callId, $uri, ''), json_decode($resultString, true));
$this->conn->callError($callId, $uri);
}
public function testDetailedCallError() {
$conn = new Connection;
$decor = new WampConnection($conn);
$callId = uniqid();
$uri = 'http://example.com/end/point';
$desc = 'beep boop beep';
$detail = 'Error: Too much awesome';
$decor->callError($callId, $uri, $desc, $detail);
$resultString = $conn->last['send'];
$this->mock->expects($this->once())->method('send')->with(json_encode(array(4, $callId, $uri, $desc, $detail)));
$this->assertEquals(array(4, $callId, $uri, $desc, $detail), json_decode($resultString, true));
$this->conn->callError($callId, $uri, $desc, $detail);
}
public function testPrefix() {
$conn = new WampConnection(new Connection);
$shortOut = 'outgoing';
$longOut = 'http://example.com/outoing';
$conn->prefix($shortOut, $longOut);
$this->mock->expects($this->once())->method('send')->with(json_encode(array(1, $shortOut, $longOut)));
$this->conn->prefix($shortOut, $longOut);
}
public function testGetUriWhenNoCurieGiven() {
$conn = new WampConnection(new Connection);
$uri = 'http://example.com/noshort';
$this->assertEquals($uri, $conn->getUri($uri));
$this->assertEquals($uri, $this->conn->getUri($uri));
}
public function testClose() {
$mock = $this->getMock('\\Ratchet\\ConnectionInterface');
$conn = new WampConnection($mock);
$mock->expects($this->once())->method('close');
$conn->close();
}
}