PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/predis/predis/tests/Predis/PubSub/PubSubContextTest.php

https://bitbucket.org/larryg/powerhut
PHP | 308 lines | 183 code | 64 blank | 61 comment | 2 complexity | 55d625419f5df0c7355a49d0e2762c77 MD5 | raw file
  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\Profile\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\Profile\ServerProfileInterface');
  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 when using aggregated connections
  37. */
  38. public function testPubSubContextDoesNotWorkOnClusters()
  39. {
  40. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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\Connection\SingleConnectionInterface');
  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. * @group disconnected
  197. */
  198. public function testGetUnderlyingClientInstance()
  199. {
  200. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  201. $client = new Client($connection);
  202. $pubsub = new PubSubContext($client);
  203. $this->assertSame($client, $pubsub->getClient());
  204. }
  205. // ******************************************************************** //
  206. // ---- INTEGRATION TESTS --------------------------------------------- //
  207. // ******************************************************************** //
  208. /**
  209. * @group connected
  210. */
  211. public function testPubSubAgainstRedisServer()
  212. {
  213. $parameters = array(
  214. 'host' => REDIS_SERVER_HOST,
  215. 'port' => REDIS_SERVER_PORT,
  216. 'database' => REDIS_SERVER_DBNUM,
  217. // Prevents suite from handing on broken test
  218. 'read_write_timeout' => 2,
  219. );
  220. $options = array('profile' => REDIS_SERVER_VERSION);
  221. $messages = array();
  222. $producer = new Client($parameters, $options);
  223. $producer->connect();
  224. $consumer = new Client($parameters, $options);
  225. $consumer->connect();
  226. $pubsub = new PubSubContext($consumer);
  227. $pubsub->subscribe('channel:foo');
  228. $producer->publish('channel:foo', 'message1');
  229. $producer->publish('channel:foo', 'message2');
  230. $producer->publish('channel:foo', 'QUIT');
  231. foreach ($pubsub as $message) {
  232. if ($message->kind !== 'message') {
  233. continue;
  234. }
  235. $messages[] = ($payload = $message->payload);
  236. if ($payload === 'QUIT') {
  237. $pubsub->closeContext();
  238. }
  239. }
  240. $this->assertSame(array('message1', 'message2', 'QUIT'), $messages);
  241. $this->assertFalse($pubsub->valid());
  242. $this->assertTrue($consumer->ping());
  243. }
  244. }