PubSubContextTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\PubSub;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Client;
  13. use Predis\Profiles\ServerProfile;
  14. /**
  15. * @group realm-pubsub
  16. */
  17. class PubSubContextTest extends StandardTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. * @expectedException Predis\NotSupportedException
  22. * @expectedExceptionMessage The current profile does not support PUB/SUB related commands
  23. */
  24. public function testPubSubContextRequirePubSubRelatedCommand()
  25. {
  26. $profile = $this->getMock('Predis\Profiles\IServerProfile');
  27. $profile->expects($this->any())
  28. ->method('supportsCommands')
  29. ->will($this->returnValue(false));
  30. $client = new Client(null, array('profile' => $profile));
  31. $pubsub = new PubSubContext($client);
  32. }
  33. /**
  34. * @group disconnected
  35. * @expectedException Predis\NotSupportedException
  36. * @expectedExceptionMessage Cannot initialize a PUB/SUB context over a cluster of connections
  37. */
  38. public function testPubSubContextDoesNotWorkOnClusters()
  39. {
  40. $cluster = $this->getMock('Predis\Network\IConnectionCluster');
  41. $client = new Client($cluster);
  42. $pubsub = new PubSubContext($client);
  43. }
  44. /**
  45. * @group disconnected
  46. */
  47. public function testConstructorWithoutSubscriptionsDoesNotOpenContext()
  48. {
  49. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  50. $client = $this->getMock('Predis\Client', array('executeCommand'), array($connection));
  51. $client->expects($this->never())->method('executeCommand');
  52. $pubsub = new PubSubContext($client);
  53. }
  54. /**
  55. * @group disconnected
  56. */
  57. public function testConstructorWithSubscriptionsOpensContext()
  58. {
  59. $profile = ServerProfile::get(REDIS_SERVER_VERSION);
  60. $cmdSubscribe = $profile->createCommand('subscribe', array('channel:foo'));
  61. $cmdPsubscribe = $profile->createCommand('psubscribe', array('channels:*'));
  62. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  63. $connection->expects($this->exactly(2))->method('writeCommand');
  64. $client = $this->getMock('Predis\Client', array('createCommand', 'writeCommand'), array($connection));
  65. $client->expects($this->exactly(2))
  66. ->method('createCommand')
  67. ->with($this->logicalOr($this->equalTo('subscribe'), $this->equalTo('psubscribe')))
  68. ->will($this->returnCallback(function($id, $args) use($profile) {
  69. return $profile->createCommand($id, $args);
  70. }));
  71. $options = array('subscribe' => 'channel:foo', 'psubscribe' => 'channels:*');
  72. $pubsub = new PubSubContext($client, $options);
  73. }
  74. /**
  75. * @group disconnected
  76. */
  77. public function testClosingContextWithTrueClosesConnection()
  78. {
  79. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  80. $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
  81. $client->expects($this->exactly(1))->method('disconnect');
  82. $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
  83. $connection->expects($this->never())->method('writeCommand');
  84. $pubsub->closeContext(true);
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testClosingContextWithFalseSendsUnsubscriptions()
  90. {
  91. $profile = ServerProfile::get(REDIS_SERVER_VERSION);
  92. $classUnsubscribe = $profile->getCommandClass('unsubscribe');
  93. $classPunsubscribe = $profile->getCommandClass('punsubscribe');
  94. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  95. $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
  96. $options = array('subscribe' => 'channel:foo', 'psubscribe' => 'channels:*');
  97. $pubsub = new PubSubContext($client, $options);
  98. $connection->expects($this->exactly(2))
  99. ->method('writeCommand')
  100. ->with($this->logicalOr(
  101. $this->isInstanceOf($classUnsubscribe),
  102. $this->isInstanceOf($classPunsubscribe)
  103. ));
  104. $pubsub->closeContext(false);
  105. }
  106. /**
  107. * @group disconnected
  108. */
  109. public function testIsNotValidWhenNotSubscribed()
  110. {
  111. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  112. $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
  113. $pubsub = new PubSubContext($client);
  114. $this->assertFalse($pubsub->valid());
  115. $this->assertNull($pubsub->next());
  116. }
  117. /**
  118. * @group disconnected
  119. */
  120. public function testReadsMessageFromConnection()
  121. {
  122. $rawmessage = array('message', 'channel:foo', 'message from channel');
  123. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  124. $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
  125. $client = new Client($connection);
  126. $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
  127. $message = $pubsub->current();
  128. $this->assertSame('message', $message->kind);
  129. $this->assertSame('channel:foo', $message->channel);
  130. $this->assertSame('message from channel', $message->payload);
  131. }
  132. /**
  133. * @group disconnected
  134. */
  135. public function testReadsPmessageFromConnection()
  136. {
  137. $rawmessage = array('pmessage', 'channel:*', 'channel:foo', 'message from channel');
  138. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  139. $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
  140. $client = new Client($connection);
  141. $pubsub = new PubSubContext($client, array('psubscribe' => 'channel:*'));
  142. $message = $pubsub->current();
  143. $this->assertSame('pmessage', $message->kind);
  144. $this->assertSame('channel:*', $message->pattern);
  145. $this->assertSame('channel:foo', $message->channel);
  146. $this->assertSame('message from channel', $message->payload);
  147. }
  148. /**
  149. * @group disconnected
  150. */
  151. public function testReadsSubscriptionMessageFromConnection()
  152. {
  153. $rawmessage = array('subscribe', 'channel:foo', 1);
  154. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  155. $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
  156. $client = new Client($connection);
  157. $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
  158. $message = $pubsub->current();
  159. $this->assertSame('subscribe', $message->kind);
  160. $this->assertSame('channel:foo', $message->channel);
  161. $this->assertSame(1, $message->payload);
  162. }
  163. /**
  164. * @group disconnected
  165. */
  166. public function testReadsUnsubscriptionMessageFromConnection()
  167. {
  168. $rawmessage = array('unsubscribe', 'channel:foo', 1);
  169. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  170. $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
  171. $client = new Client($connection);
  172. $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
  173. $message = $pubsub->current();
  174. $this->assertSame('unsubscribe', $message->kind);
  175. $this->assertSame('channel:foo', $message->channel);
  176. $this->assertSame(1, $message->payload);
  177. }
  178. /**
  179. * @group disconnected
  180. */
  181. public function testUnsubscriptionMessageWithZeroChannelCountInvalidatesContext()
  182. {
  183. $rawmessage = array('unsubscribe', 'channel:foo', 0);
  184. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  185. $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
  186. $client = new Client($connection);
  187. $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
  188. $this->assertTrue($pubsub->valid());
  189. $message = $pubsub->current();
  190. $this->assertSame('unsubscribe', $message->kind);
  191. $this->assertSame('channel:foo', $message->channel);
  192. $this->assertSame(0, $message->payload);
  193. $this->assertFalse($pubsub->valid());
  194. }
  195. // ******************************************************************** //
  196. // ---- INTEGRATION TESTS --------------------------------------------- //
  197. // ******************************************************************** //
  198. /**
  199. * @group connected
  200. */
  201. public function testPubSubAgainstRedisServer()
  202. {
  203. $parameters = array(
  204. 'host' => REDIS_SERVER_HOST,
  205. 'port' => REDIS_SERVER_PORT,
  206. 'database' => REDIS_SERVER_DBNUM,
  207. // Prevents suite from handing on broken test
  208. 'read_write_timeout' => 2,
  209. );
  210. $messages = array();
  211. $producer = new Client($parameters, REDIS_SERVER_VERSION);
  212. $producer->connect();
  213. $consumer = new Client($parameters, REDIS_SERVER_VERSION);
  214. $consumer->connect();
  215. $pubsub = new PubSubContext($consumer);
  216. $pubsub->subscribe('channel:foo');
  217. $producer->publish('channel:foo', 'message1');
  218. $producer->publish('channel:foo', 'message2');
  219. $producer->publish('channel:foo', 'QUIT');
  220. foreach ($pubsub as $message) {
  221. if ($message->kind !== 'message') {
  222. continue;
  223. }
  224. $messages[] = ($payload = $message->payload);
  225. if ($payload === 'QUIT') {
  226. $pubsub->closeContext();
  227. }
  228. }
  229. $this->assertSame(array('message1', 'message2', 'QUIT'), $messages);
  230. $this->assertFalse($pubsub->valid());
  231. $this->assertTrue($consumer->ping());
  232. }
  233. }