FireAndForgetExecutorTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Pipeline;
  11. use SplQueue;
  12. use PredisTestCase;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. *
  16. */
  17. class FireAndForgetExecutorTest extends PredisTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testExecutorWithSingleConnection()
  23. {
  24. $executor = new FireAndForgetExecutor();
  25. $pipeline = $this->getCommandsQueue();
  26. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  27. $connection->expects($this->exactly(3))
  28. ->method('writeCommand');
  29. $connection->expects($this->never())
  30. ->method('readResponse');
  31. $replies = $executor->execute($connection, $pipeline);
  32. $this->assertTrue($pipeline->isEmpty());
  33. $this->assertEmpty($replies);
  34. }
  35. /**
  36. * @group disconnected
  37. */
  38. public function testExecutorWithReplicationConnection()
  39. {
  40. $executor = new FireAndForgetExecutor();
  41. $pipeline = $this->getCommandsQueue();
  42. $connection = $this->getMock('Predis\Connection\ReplicationConnectionInterface');
  43. $connection->expects($this->once())
  44. ->method('switchTo')
  45. ->with('master');
  46. $connection->expects($this->exactly(3))
  47. ->method('writeCommand');
  48. $connection->expects($this->never())
  49. ->method('readResponse');
  50. $replies = $executor->execute($connection, $pipeline);
  51. $this->assertTrue($pipeline->isEmpty());
  52. $this->assertEmpty($replies);
  53. }
  54. // ******************************************************************** //
  55. // ---- HELPER METHODS ------------------------------------------------ //
  56. // ******************************************************************** //
  57. /**
  58. * Returns a list of queued command instances.
  59. *
  60. * @return SplQueue
  61. */
  62. protected function getCommandsQueue()
  63. {
  64. $profile = ServerProfile::getDevelopment();
  65. $pipeline = new SplQueue();
  66. $pipeline->enqueue($profile->createCommand('ping'));
  67. $pipeline->enqueue($profile->createCommand('ping'));
  68. $pipeline->enqueue($profile->createCommand('ping'));
  69. return $pipeline;
  70. }
  71. }