DispatcherLoopTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 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. $producer = new Client($parameters, REDIS_SERVER_VERSION);
  35. $producer->connect();
  36. $consumer = new Client($parameters, REDIS_SERVER_VERSION);
  37. $consumer->connect();
  38. $dispatcher = new DispatcherLoop($consumer);
  39. $function01 = $this->getMock('stdClass', array('__invoke'));
  40. $function01->expects($this->exactly(2))
  41. ->method('__invoke')
  42. ->with($this->logicalOr(
  43. $this->equalTo('01:argument'),
  44. $this->equalTo('01:quit')
  45. ))
  46. ->will($this->returnCallback(function($arg) use($dispatcher) {
  47. if ($arg === '01:quit') {
  48. $dispatcher->stop();
  49. }
  50. }));
  51. $function02 = $this->getMock('stdClass', array('__invoke'));
  52. $function02->expects($this->once())
  53. ->method('__invoke')
  54. ->with('02:argument');
  55. $function03 = $this->getMock('stdClass', array('__invoke'));
  56. $function03->expects($this->never())
  57. ->method('__invoke');
  58. $dispatcher->attachCallback('function:01', $function01);
  59. $dispatcher->attachCallback('function:02', $function02);
  60. $dispatcher->attachCallback('function:03', $function03);
  61. $producer->publish('function:01', '01:argument');
  62. $producer->publish('function:02', '02:argument');
  63. $producer->publish('function:01', '01:quit');
  64. $dispatcher->run();
  65. $this->assertTrue($consumer->ping());
  66. }
  67. }