TopicTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Ratchet\Wamp;
  3. /**
  4. * @covers Ratchet\Wamp\Topic
  5. */
  6. class TopicTest extends \PHPUnit_Framework_TestCase {
  7. public function testGetId() {
  8. $id = uniqid();
  9. $topic = new Topic($id);
  10. $this->assertEquals($id, $topic->getId());
  11. }
  12. public function testAddAndCount() {
  13. $topic = new Topic('merp');
  14. $topic->add($this->newConn());
  15. $topic->add($this->newConn());
  16. $topic->add($this->newConn());
  17. $this->assertEquals(3, count($topic));
  18. }
  19. public function testRemove() {
  20. $topic = new Topic('boop');
  21. $tracked = $this->newConn();
  22. $topic->add($this->newConn());
  23. $topic->add($tracked);
  24. $topic->add($this->newConn());
  25. $topic->remove($tracked);
  26. $this->assertEquals(2, count($topic));
  27. }
  28. public function testBroadcast() {
  29. $msg = 'Hello World!';
  30. $name = 'Batman';
  31. $protocol = json_encode(array(8, $name, $msg));
  32. $first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
  33. $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
  34. $first->expects($this->once())
  35. ->method('send')
  36. ->with($this->equalTo($protocol));
  37. $second->expects($this->once())
  38. ->method('send')
  39. ->with($this->equalTo($protocol));
  40. $topic = new Topic($name);
  41. $topic->add($first);
  42. $topic->add($second);
  43. $topic->broadcast($msg);
  44. }
  45. public function testBroadcastWithExclude() {
  46. $msg = 'Hello odd numbers';
  47. $name = 'Excluding';
  48. $protocol = json_encode(array(8, $name, $msg));
  49. $first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
  50. $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
  51. $third = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
  52. $first->expects($this->once())
  53. ->method('send')
  54. ->with($this->equalTo($protocol));
  55. $second->expects($this->never())->method('send');
  56. $third->expects($this->once())
  57. ->method('send')
  58. ->with($this->equalTo($protocol));
  59. $topic = new Topic($name);
  60. $topic->add($first);
  61. $topic->add($second);
  62. $topic->add($third);
  63. $topic->broadcast($msg, array($second->WAMP->sessionId));
  64. }
  65. public function testBroadcastWithEligible() {
  66. $msg = 'Hello white list';
  67. $name = 'Eligible';
  68. $protocol = json_encode(array(8, $name, $msg));
  69. $first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
  70. $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
  71. $third = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface')));
  72. $first->expects($this->once())
  73. ->method('send')
  74. ->with($this->equalTo($protocol));
  75. $second->expects($this->never())->method('send');
  76. $third->expects($this->once())
  77. ->method('send')
  78. ->with($this->equalTo($protocol));
  79. $topic = new Topic($name);
  80. $topic->add($first);
  81. $topic->add($second);
  82. $topic->add($third);
  83. $topic->broadcast($msg, array(), array($first->WAMP->sessionId, $third->WAMP->sessionId));
  84. }
  85. public function testIterator() {
  86. $first = $this->newConn();
  87. $second = $this->newConn();
  88. $third = $this->newConn();
  89. $topic = new Topic('Joker');
  90. $topic->add($first)->add($second)->add($third);
  91. $check = array($first, $second, $third);
  92. foreach ($topic as $mock) {
  93. $this->assertNotSame(false, array_search($mock, $check));
  94. }
  95. }
  96. public function testToString() {
  97. $name = 'Bane';
  98. $topic = new Topic($name);
  99. $this->assertEquals($name, (string)$topic);
  100. }
  101. public function testDoesHave() {
  102. $conn = $this->newConn();
  103. $topic = new Topic('Two Face');
  104. $topic->add($conn);
  105. $this->assertTrue($topic->has($conn));
  106. }
  107. public function testDoesNotHave() {
  108. $conn = $this->newConn();
  109. $topic = new Topic('Alfred');
  110. $this->assertFalse($topic->has($conn));
  111. }
  112. public function testDoesNotHaveAfterRemove() {
  113. $conn = $this->newConn();
  114. $topic = new Topic('Ras');
  115. $topic->add($conn)->remove($conn);
  116. $this->assertFalse($topic->has($conn));
  117. }
  118. protected function newConn() {
  119. return new WampConnection($this->getMock('\\Ratchet\\ConnectionInterface'));
  120. }
  121. }