FireAndForgetExecutorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use SplQueue;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. *
  16. */
  17. class FireAndForgetExecutorTest extends StandardTestCase
  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. }