[WAMP][Tests] Coverage
Sight bug fixes in WAMP topics Unit tests coverage
This commit is contained in:
parent
4078a360a8
commit
bd5b7d09aa
@ -71,7 +71,9 @@ class IoServer {
|
|||||||
throw new \RuntimeException("A React Loop was not provided during instantiation");
|
throw new \RuntimeException("A React Loop was not provided during instantiation");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
$this->loop->run();
|
$this->loop->run();
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Wamp;
|
namespace Ratchet\Wamp;
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A topic/channel containing connections that have subscribed to it
|
* A topic/channel containing connections that have subscribed to it
|
||||||
@ -45,7 +46,7 @@ class Topic implements \IteratorAggregate, \Countable {
|
|||||||
* @param WampConnection
|
* @param WampConnection
|
||||||
* @return Topic
|
* @return Topic
|
||||||
*/
|
*/
|
||||||
public function add(WampConnection $conn) {
|
public function add(ConnectionInterface $conn) {
|
||||||
$this->subscribers->attach($conn);
|
$this->subscribers->attach($conn);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -55,7 +56,7 @@ class Topic implements \IteratorAggregate, \Countable {
|
|||||||
* @param WampConnection
|
* @param WampConnection
|
||||||
* @return Topic
|
* @return Topic
|
||||||
*/
|
*/
|
||||||
public function remove(WampConnection $conn) {
|
public function remove(ConnectionInterface $conn) {
|
||||||
if ($this->subscribers->contains($conn)) {
|
if ($this->subscribers->contains($conn)) {
|
||||||
$this->subscribers->detach($conn);
|
$this->subscribers->detach($conn);
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,10 @@ class TopicManager implements WsServerInterface, WampServerInterface {
|
|||||||
public function onSubscribe(ConnectionInterface $conn, $topic) {
|
public function onSubscribe(ConnectionInterface $conn, $topic) {
|
||||||
$topicObj = $this->getTopic($topic);
|
$topicObj = $this->getTopic($topic);
|
||||||
|
|
||||||
|
if ($conn->WAMP->topics->contains($topicObj)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$conn->WAMP->topics->attach($topicObj);
|
$conn->WAMP->topics->attach($topicObj);
|
||||||
$this->app->onSubscribe($conn, $topicObj);
|
$this->app->onSubscribe($conn, $topicObj);
|
||||||
}
|
}
|
||||||
@ -50,7 +54,9 @@ class TopicManager implements WsServerInterface, WampServerInterface {
|
|||||||
$topicObj = $this->getTopic($topic);
|
$topicObj = $this->getTopic($topic);
|
||||||
|
|
||||||
if ($conn->WAMP->topics->contains($topicObj)) {
|
if ($conn->WAMP->topics->contains($topicObj)) {
|
||||||
$conn->WAMP->topics->remove($topicObj);
|
$conn->WAMP->topics->detach($topicObj);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->topicLookup[$topic]->remove($conn);
|
$this->topicLookup[$topic]->remove($conn);
|
||||||
|
@ -240,4 +240,11 @@ class ServerProtocolTest extends \PHPUnit_Framework_TestCase {
|
|||||||
|
|
||||||
$this->assertGreaterThanOrEqual(3, count($this->_comp->getSubProtocols()));
|
$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());
|
||||||
|
}
|
||||||
}
|
}
|
@ -82,7 +82,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->mngr->onSubscribe($this->conn, 'new topic');
|
$this->mngr->onSubscribe($this->conn, 'new topic');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTopicIsInConnection() {
|
public function testTopicIsInConnectionOnSubscribe() {
|
||||||
$name = 'New Topic';
|
$name = 'New Topic';
|
||||||
|
|
||||||
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
|
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
|
||||||
@ -95,4 +95,45 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
|
|||||||
|
|
||||||
$this->assertTrue($this->conn->WAMP->topics->contains($topic));
|
$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));
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,67 +1,69 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Tests\Wamp;
|
namespace Ratchet\Tests\Wamp;
|
||||||
use Ratchet\Wamp\WampConnection;
|
use Ratchet\Wamp\WampConnection;
|
||||||
use Ratchet\Tests\Mock\Connection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers Ratchet\Wamp\WampConnection
|
* @covers Ratchet\Wamp\WampConnection
|
||||||
*/
|
*/
|
||||||
class WampConnectionTest extends \PHPUnit_Framework_TestCase {
|
class WampConnectionTest extends \PHPUnit_Framework_TestCase {
|
||||||
public function testCallResult() {
|
protected $conn;
|
||||||
$conn = new Connection;
|
protected $mock;
|
||||||
$decor = new WampConnection($conn);
|
|
||||||
|
|
||||||
|
public function setUp() {
|
||||||
|
$this->mock = $this->getMock('\\Ratchet\\ConnectionInterface');
|
||||||
|
$this->conn = new WampConnection($this->mock);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCallResult() {
|
||||||
$callId = uniqid();
|
$callId = uniqid();
|
||||||
$data = array('hello' => 'world', 'herp' => 'derp');
|
$data = array('hello' => 'world', 'herp' => 'derp');
|
||||||
|
|
||||||
|
$this->mock->expects($this->once())->method('send')->with(json_encode(array(3, $callId, $data)));
|
||||||
|
|
||||||
$decor->callResult($callId, $data);
|
$this->conn->callResult($callId, $data);
|
||||||
$resultString = $conn->last['send'];
|
|
||||||
|
|
||||||
$this->assertEquals(array(3, $callId, $data), json_decode($resultString, true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCallError() {
|
public function testCallError() {
|
||||||
$conn = new Connection;
|
|
||||||
$decor = new WampConnection($conn);
|
|
||||||
|
|
||||||
$callId = uniqid();
|
$callId = uniqid();
|
||||||
$uri = 'http://example.com/end/point';
|
$uri = 'http://example.com/end/point';
|
||||||
|
|
||||||
$decor->callError($callId, $uri);
|
$this->mock->expects($this->once())->method('send')->with(json_encode(array(4, $callId, $uri, '')));
|
||||||
$resultString = $conn->last['send'];
|
|
||||||
|
|
||||||
$this->assertEquals(array(4, $callId, $uri, ''), json_decode($resultString, true));
|
$this->conn->callError($callId, $uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDetailedCallError() {
|
public function testDetailedCallError() {
|
||||||
$conn = new Connection;
|
|
||||||
$decor = new WampConnection($conn);
|
|
||||||
|
|
||||||
$callId = uniqid();
|
$callId = uniqid();
|
||||||
$uri = 'http://example.com/end/point';
|
$uri = 'http://example.com/end/point';
|
||||||
$desc = 'beep boop beep';
|
$desc = 'beep boop beep';
|
||||||
$detail = 'Error: Too much awesome';
|
$detail = 'Error: Too much awesome';
|
||||||
|
|
||||||
$decor->callError($callId, $uri, $desc, $detail);
|
$this->mock->expects($this->once())->method('send')->with(json_encode(array(4, $callId, $uri, $desc, $detail)));
|
||||||
$resultString = $conn->last['send'];
|
|
||||||
|
|
||||||
$this->assertEquals(array(4, $callId, $uri, $desc, $detail), json_decode($resultString, true));
|
$this->conn->callError($callId, $uri, $desc, $detail);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPrefix() {
|
public function testPrefix() {
|
||||||
$conn = new WampConnection(new Connection);
|
|
||||||
|
|
||||||
$shortOut = 'outgoing';
|
$shortOut = 'outgoing';
|
||||||
$longOut = 'http://example.com/outoing';
|
$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() {
|
public function testGetUriWhenNoCurieGiven() {
|
||||||
$conn = new WampConnection(new Connection);
|
|
||||||
$uri = 'http://example.com/noshort';
|
$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();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user