[WAMP][Tests] Full namespace coverage

Added `has` method to Topic class
Covering all of WAMP code in unit tests
This commit is contained in:
Chris Boden 2012-07-19 11:27:31 -04:00
parent 9a317fefc7
commit dd8a59df69
4 changed files with 88 additions and 11 deletions

View File

@ -42,6 +42,14 @@ class Topic implements \IteratorAggregate, \Countable {
return $this;
}
/**
* @param WampConnection
* @return boolean
*/
public function has(ConnectionInterface $conn) {
return $this->subscribers->contains($conn);
}
/**
* @param WampConnection
* @return Topic

View File

@ -0,0 +1,7 @@
<?php
namespace Ratchet\Tests\Wamp\Stub;
use Ratchet\WebSocket\WsServerInterface;
use Ratchet\Wamp\WampServerInterface;
interface WsWampServerInterface extends WsServerInterface, WampServerInterface {
}

View File

@ -11,8 +11,8 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
private $conn;
public function setUp() {
$this->conn = $this->getMock('\\Ratchet\\ConnectionInterface');
$this->mock = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface');
$this->conn = $this->getMock('Ratchet\\ConnectionInterface');
$this->mock = $this->getMock('Ratchet\\Wamp\\WampServerInterface');
$this->mngr = new TopicManager($this->mock);
$this->conn->WAMP = new \StdClass;
@ -20,19 +20,19 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
}
public function testGetTopicReturnsTopicObject() {
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
$class = new \ReflectionClass('Ratchet\\Wamp\\TopicManager');
$method = $class->getMethod('getTopic');
$method->setAccessible(true);
$topic = $method->invokeArgs($this->mngr, array('The Topic'));
$this->assertInstanceOf('\\Ratchet\\Wamp\\Topic', $topic);
$this->assertInstanceOf('Ratchet\\Wamp\\Topic', $topic);
}
public function testGetTopicCreatesTopicWithSameName() {
$name = 'The Topic';
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
$class = new \ReflectionClass('Ratchet\\Wamp\\TopicManager');
$method = $class->getMethod('getTopic');
$method->setAccessible(true);
@ -42,7 +42,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
}
public function testGetTopicReturnsSameObject() {
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
$class = new \ReflectionClass('Ratchet\\Wamp\\TopicManager');
$method = $class->getMethod('getTopic');
$method->setAccessible(true);
@ -63,7 +63,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
$this->mock->expects($this->once())->method('onCall')->with(
$this->conn
, $id
, $this->isInstanceOf('\\Ratchet\\Wamp\\Topic')
, $this->isInstanceOf('Ratchet\\Wamp\\Topic')
, array()
);
@ -72,7 +72,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
public function testOnSubscribeCreatesTopicObject() {
$this->mock->expects($this->once())->method('onSubscribe')->with(
$this->conn, $this->isInstanceOf('\\Ratchet\\Wamp\\Topic')
$this->conn, $this->isInstanceOf('Ratchet\\Wamp\\Topic')
);
$this->mngr->onSubscribe($this->conn, 'new topic');
@ -81,7 +81,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
public function testTopicIsInConnectionOnSubscribe() {
$name = 'New Topic';
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
$class = new \ReflectionClass('Ratchet\\Wamp\\TopicManager');
$method = $class->getMethod('getTopic');
$method->setAccessible(true);
@ -102,7 +102,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
public function testUnsubscribeEvent() {
$name = 'in and out';
$this->mock->expects($this->once())->method('onUnsubscribe')->with(
$this->conn, $this->isInstanceOf('\\Ratchet\\Wamp\\Topic')
$this->conn, $this->isInstanceOf('Ratchet\\Wamp\\Topic')
);
$this->mngr->onSubscribe($this->conn, $name);
@ -121,7 +121,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
public function testUnsubscribeRemovesTopicFromConnection() {
$name = 'Bye Bye Topic';
$class = new \ReflectionClass('\\Ratchet\\Wamp\\TopicManager');
$class = new \ReflectionClass('Ratchet\\Wamp\\TopicManager');
$method = $class->getMethod('getTopic');
$method->setAccessible(true);
@ -133,11 +133,40 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
$this->assertFalse($this->conn->WAMP->topics->contains($topic));
}
public function testOnPublishBubbles() {
$msg = 'Cover all the code!';
$this->mock->expects($this->once())->method('onPublish')->with(
$this->conn
, $this->isInstanceOf('Ratchet\\Wamp\\Topic')
, $msg
, $this->isType('array')
, $this->isType('array')
);
$this->mngr->onPublish($this->conn, 'topic coverage', $msg);
}
public function testOnCloseBubbles() {
$this->mock->expects($this->once())->method('onClose')->with($this->conn);
$this->mngr->onClose($this->conn);
}
public function testConnIsRemovedFromTopicOnClose() {
$name = 'State testing';
$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->onClose($this->conn);
$this->assertFalse($topic->has($this->conn));
}
public function testOnErrorBubbles() {
$e = new \Exception('All work and no play makes Chris a dull boy');
$this->mock->expects($this->once())->method('onError')->with($this->conn, $e);
@ -148,4 +177,13 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
public function testGetSubProtocolsReturnsArray() {
$this->assertInternalType('array', $this->mngr->getSubProtocols());
}
public function testGetSubProtocolsBubbles() {
$subs = array('hello', 'world');
$app = $this->getMock('Ratchet\\Tests\\Wamp\\Stub\\WsWampServerInterface');
$app->expects($this->once())->method('getSubProtocols')->will($this->returnValue($subs));
$mngr = new TopicManager($app);
$this->assertEquals($subs, $mngr->getSubProtocols());
}
}

View File

@ -82,6 +82,30 @@ class TopicTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($name, (string)$topic);
}
public function testDoesHave() {
$conn = $this->newConn();
$topic = new Topic('Two Face');
$topic->add($conn);
$this->assertTrue($topic->has($conn));
}
public function testDoesNotHave() {
$conn = $this->newConn();
$topic = new Topic('Alfred');
$this->assertFalse($topic->has($conn));
}
public function testDoesNotHaveAfterRemove() {
$conn = $this->newConn();
$topic = new Topic('Ras');
$topic->add($conn)->remove($conn);
$this->assertFalse($topic->has($conn));
}
protected function newConn() {
return new WampConnection($this->getMock('\\Ratchet\\ConnectionInterface'));
}