DispatcherLoopTest.php 4.0 KB

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