DispatcherLoopTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Predis\Client;
  12. use PredisTestCase;
  13. /**
  14. * @group realm-pubsub
  15. */
  16. class DispatcherLoopTest extends PredisTestCase
  17. {
  18. // ******************************************************************** //
  19. // ---- INTEGRATION TESTS --------------------------------------------- //
  20. // ******************************************************************** //
  21. /**
  22. * @group connected
  23. * @requiresRedisVersion >= 2.0.0
  24. */
  25. public function testDispatcherLoopAgainstRedisServer()
  26. {
  27. $parameters = array(
  28. 'host' => REDIS_SERVER_HOST,
  29. 'port' => REDIS_SERVER_PORT,
  30. 'database' => REDIS_SERVER_DBNUM,
  31. // Prevents suite from hanging on broken test
  32. 'read_write_timeout' => 2,
  33. );
  34. $producer = new Client($parameters);
  35. $producer->connect();
  36. $consumer = new Client($parameters);
  37. $consumer->connect();
  38. $pubsub = new Consumer($consumer);
  39. $dispatcher = new DispatcherLoop($pubsub);
  40. $function01 = $this->getMock('stdClass', array('__invoke'));
  41. $function01
  42. ->expects($this->exactly(2))
  43. ->method('__invoke')
  44. ->with($this->logicalOr(
  45. $this->equalTo('01:argument'),
  46. $this->equalTo('01:quit')
  47. ), $dispatcher)
  48. ->will($this->returnCallback(function ($arg, $dispatcher) {
  49. if ($arg === '01:quit') {
  50. $dispatcher->stop();
  51. }
  52. }));
  53. $function02 = $this->getMock('stdClass', array('__invoke'));
  54. $function02
  55. ->expects($this->once())
  56. ->method('__invoke')
  57. ->with('02:argument');
  58. $function03 = $this->getMock('stdClass', array('__invoke'));
  59. $function03
  60. ->expects($this->never())
  61. ->method('__invoke');
  62. $dispatcher->attachCallback('function:01', $function01);
  63. $dispatcher->attachCallback('function:02', $function02);
  64. $dispatcher->attachCallback('function:03', $function03);
  65. $producer->publish('function:01', '01:argument');
  66. $producer->publish('function:02', '02:argument');
  67. $producer->publish('function:01', '01:quit');
  68. $dispatcher->run();
  69. $this->assertEquals('PONG', $consumer->ping());
  70. }
  71. /**
  72. * @group connected
  73. * @requiresRedisVersion >= 2.0.0
  74. */
  75. public function testDispatcherLoopAgainstRedisServerWithPrefix()
  76. {
  77. $parameters = array(
  78. 'host' => REDIS_SERVER_HOST,
  79. 'port' => REDIS_SERVER_PORT,
  80. 'database' => REDIS_SERVER_DBNUM,
  81. // Prevents suite from handing on broken test
  82. 'read_write_timeout' => 2,
  83. );
  84. $producerNonPfx = new Client($parameters);
  85. $producerNonPfx->connect();
  86. $producerPfx = new Client($parameters, array('prefix' => 'foobar'));
  87. $producerPfx->connect();
  88. $consumer = new Client($parameters, array('prefix' => 'foobar'));
  89. $pubsub = new Consumer($consumer);
  90. $dispatcher = new DispatcherLoop($pubsub);
  91. $callback = $this->getMock('stdClass', array('__invoke'));
  92. $callback
  93. ->expects($this->exactly(1))
  94. ->method('__invoke')
  95. ->with($this->equalTo('arg:prefixed'), $dispatcher)
  96. ->will($this->returnCallback(function ($arg, $dispatcher) {
  97. $dispatcher->stop();
  98. }));
  99. $dispatcher->attachCallback('callback', $callback);
  100. $producerNonPfx->publish('callback', 'arg:non-prefixed');
  101. $producerPfx->publish('callback', 'arg:prefixed');
  102. $dispatcher->run();
  103. $this->assertEquals('PONG', $consumer->ping());
  104. }
  105. }