TopicManagerTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Ratchet\Wamp;
  3. /**
  4. * @covers Ratchet\Wamp\TopicManager
  5. */
  6. class TopicManagerTest extends \PHPUnit_Framework_TestCase {
  7. private $mock;
  8. /**
  9. * @var \Ratchet\Wamp\TopicManager
  10. */
  11. private $mngr;
  12. /**
  13. * @var \Ratchet\ConnectionInterface
  14. */
  15. private $conn;
  16. public function setUp() {
  17. $this->conn = $this->getMock('\Ratchet\ConnectionInterface');
  18. $this->mock = $this->getMock('\Ratchet\Wamp\WampServerInterface');
  19. $this->mngr = new TopicManager($this->mock);
  20. $this->conn->WAMP = new \StdClass;
  21. $this->mngr->onOpen($this->conn);
  22. }
  23. public function testGetTopicReturnsTopicObject() {
  24. $class = new \ReflectionClass('Ratchet\Wamp\TopicManager');
  25. $method = $class->getMethod('getTopic');
  26. $method->setAccessible(true);
  27. $topic = $method->invokeArgs($this->mngr, array('The Topic'));
  28. $this->assertInstanceOf('Ratchet\Wamp\Topic', $topic);
  29. }
  30. public function testGetTopicCreatesTopicWithSameName() {
  31. $name = 'The Topic';
  32. $class = new \ReflectionClass('Ratchet\Wamp\TopicManager');
  33. $method = $class->getMethod('getTopic');
  34. $method->setAccessible(true);
  35. $topic = $method->invokeArgs($this->mngr, array($name));
  36. $this->assertEquals($name, $topic->getId());
  37. }
  38. public function testGetTopicReturnsSameObject() {
  39. $class = new \ReflectionClass('Ratchet\Wamp\TopicManager');
  40. $method = $class->getMethod('getTopic');
  41. $method->setAccessible(true);
  42. $topic = $method->invokeArgs($this->mngr, array('No copy'));
  43. $again = $method->invokeArgs($this->mngr, array('No copy'));
  44. $this->assertSame($topic, $again);
  45. }
  46. public function testOnOpen() {
  47. $this->mock->expects($this->once())->method('onOpen');
  48. $this->mngr->onOpen($this->conn);
  49. }
  50. public function testOnCall() {
  51. $id = uniqid();
  52. $this->mock->expects($this->once())->method('onCall')->with(
  53. $this->conn
  54. , $id
  55. , $this->isInstanceOf('Ratchet\Wamp\Topic')
  56. , array()
  57. );
  58. $this->mngr->onCall($this->conn, $id, 'new topic', array());
  59. }
  60. public function testOnSubscribeCreatesTopicObject() {
  61. $this->mock->expects($this->once())->method('onSubscribe')->with(
  62. $this->conn, $this->isInstanceOf('Ratchet\Wamp\Topic')
  63. );
  64. $this->mngr->onSubscribe($this->conn, 'new topic');
  65. }
  66. public function testTopicIsInConnectionOnSubscribe() {
  67. $name = 'New Topic';
  68. $class = new \ReflectionClass('Ratchet\Wamp\TopicManager');
  69. $method = $class->getMethod('getTopic');
  70. $method->setAccessible(true);
  71. $topic = $method->invokeArgs($this->mngr, array($name));
  72. $this->mngr->onSubscribe($this->conn, $name);
  73. $this->assertTrue($this->conn->WAMP->subscriptions->contains($topic));
  74. }
  75. public function testDoubleSubscriptionFiresOnce() {
  76. $this->mock->expects($this->exactly(1))->method('onSubscribe');
  77. $this->mngr->onSubscribe($this->conn, 'same topic');
  78. $this->mngr->onSubscribe($this->conn, 'same topic');
  79. }
  80. public function testUnsubscribeEvent() {
  81. $name = 'in and out';
  82. $this->mock->expects($this->once())->method('onUnsubscribe')->with(
  83. $this->conn, $this->isInstanceOf('Ratchet\Wamp\Topic')
  84. );
  85. $this->mngr->onSubscribe($this->conn, $name);
  86. $this->mngr->onUnsubscribe($this->conn, $name);
  87. }
  88. public function testUnsubscribeFiresOnce() {
  89. $name = 'getting sleepy';
  90. $this->mock->expects($this->exactly(1))->method('onUnsubscribe');
  91. $this->mngr->onSubscribe($this->conn, $name);
  92. $this->mngr->onUnsubscribe($this->conn, $name);
  93. $this->mngr->onUnsubscribe($this->conn, $name);
  94. }
  95. public function testUnsubscribeRemovesTopicFromConnection() {
  96. $name = 'Bye Bye Topic';
  97. $class = new \ReflectionClass('Ratchet\Wamp\TopicManager');
  98. $method = $class->getMethod('getTopic');
  99. $method->setAccessible(true);
  100. $topic = $method->invokeArgs($this->mngr, array($name));
  101. $this->mngr->onSubscribe($this->conn, $name);
  102. $this->mngr->onUnsubscribe($this->conn, $name);
  103. $this->assertFalse($this->conn->WAMP->subscriptions->contains($topic));
  104. }
  105. public function testOnPublishBubbles() {
  106. $msg = 'Cover all the code!';
  107. $this->mock->expects($this->once())->method('onPublish')->with(
  108. $this->conn
  109. , $this->isInstanceOf('Ratchet\Wamp\Topic')
  110. , $msg
  111. , $this->isType('array')
  112. , $this->isType('array')
  113. );
  114. $this->mngr->onPublish($this->conn, 'topic coverage', $msg, array(), array());
  115. }
  116. public function testOnCloseBubbles() {
  117. $this->mock->expects($this->once())->method('onClose')->with($this->conn);
  118. $this->mngr->onClose($this->conn);
  119. }
  120. protected function topicProvider($name) {
  121. $class = new \ReflectionClass('Ratchet\Wamp\TopicManager');
  122. $method = $class->getMethod('getTopic');
  123. $method->setAccessible(true);
  124. $attribute = $class->getProperty('topicLookup');
  125. $attribute->setAccessible(true);
  126. $topic = $method->invokeArgs($this->mngr, array($name));
  127. return array($topic, $attribute);
  128. }
  129. public function testConnIsRemovedFromTopicOnClose() {
  130. $name = 'State Testing';
  131. list($topic, $attribute) = $this->topicProvider($name);
  132. $this->assertCount(1, $attribute->getValue($this->mngr));
  133. $this->mngr->onSubscribe($this->conn, $name);
  134. $this->mngr->onClose($this->conn);
  135. $this->assertFalse($topic->has($this->conn));
  136. }
  137. public static function topicConnExpectationProvider() {
  138. return [
  139. [ 'onClose', 0]
  140. , ['onUnsubscribe', 0]
  141. ];
  142. }
  143. /**
  144. * @dataProvider topicConnExpectationProvider
  145. */
  146. public function testTopicRetentionFromLeavingConnections($methodCall, $expectation) {
  147. $topicName = 'checkTopic';
  148. list($topic, $attribute) = $this->topicProvider($topicName);
  149. $this->mngr->onSubscribe($this->conn, $topicName);
  150. call_user_func_array(array($this->mngr, $methodCall), array($this->conn, $topicName));
  151. $this->assertCount($expectation, $attribute->getValue($this->mngr));
  152. }
  153. public function testOnErrorBubbles() {
  154. $e = new \Exception('All work and no play makes Chris a dull boy');
  155. $this->mock->expects($this->once())->method('onError')->with($this->conn, $e);
  156. $this->mngr->onError($this->conn, $e);
  157. }
  158. public function testGetSubProtocolsReturnsArray() {
  159. $this->assertInternalType('array', $this->mngr->getSubProtocols());
  160. }
  161. public function testGetSubProtocolsBubbles() {
  162. $subs = array('hello', 'world');
  163. $app = $this->getMock('Ratchet\Wamp\Stub\WsWampServerInterface');
  164. $app->expects($this->once())->method('getSubProtocols')->will($this->returnValue($subs));
  165. $mngr = new TopicManager($app);
  166. $this->assertEquals($subs, $mngr->getSubProtocols());
  167. }
  168. }