StandardExecutorTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\ResponseError;
  14. use Predis\ResponseObjectInterface;
  15. use Predis\Profile\ServerProfile;
  16. /**
  17. *
  18. */
  19. class StandardExecutorTest extends PredisTestCase
  20. {
  21. /**
  22. * @group disconnected
  23. */
  24. public function testExecutorWithSingleConnection()
  25. {
  26. $executor = new StandardExecutor();
  27. $pipeline = $this->getCommandsQueue();
  28. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  29. $connection->expects($this->exactly(3))
  30. ->method('writeCommand');
  31. $connection->expects($this->exactly(3))
  32. ->method('readResponse')
  33. ->will($this->returnValue('PONG'));
  34. $replies = $executor->execute($connection, $pipeline);
  35. $this->assertTrue($pipeline->isEmpty());
  36. $this->assertSame(array(true, true, true), $replies);
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testExecutorWithReplicationConnection()
  42. {
  43. $executor = new StandardExecutor();
  44. $pipeline = $this->getCommandsQueue();
  45. $connection = $this->getMock('Predis\Connection\ReplicationConnectionInterface');
  46. $connection->expects($this->once())
  47. ->method('switchTo')
  48. ->with('master');
  49. $connection->expects($this->exactly(3))
  50. ->method('writeCommand');
  51. $connection->expects($this->exactly(3))
  52. ->method('readResponse')
  53. ->will($this->returnValue('PONG'));
  54. $replies = $executor->execute($connection, $pipeline);
  55. $this->assertTrue($pipeline->isEmpty());
  56. $this->assertSame(array(true, true, true), $replies);
  57. }
  58. /**
  59. * @group disconnected
  60. */
  61. public function testExecutorDoesNotParseResponseObjects()
  62. {
  63. $executor = new StandardExecutor();
  64. $response = $this->getMock('Predis\ResponseObjectInterface');
  65. $this->simpleResponseObjectTest($executor, $response);
  66. }
  67. /**
  68. * @group disconnected
  69. */
  70. public function testExecutorCanReturnRedisErrors()
  71. {
  72. $executor = new StandardExecutor(false);
  73. $response = $this->getMock('Predis\ResponseErrorInterface');
  74. $this->simpleResponseObjectTest($executor, $response);
  75. }
  76. /**
  77. * @group disconnected
  78. * @expectedException Predis\ServerException
  79. * @expectedExceptionMessage ERR Test error
  80. */
  81. public function testExecutorCanThrowExceptions()
  82. {
  83. $executor = new StandardExecutor(true);
  84. $pipeline = $this->getCommandsQueue();
  85. $error = new ResponseError('ERR Test error');
  86. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  87. $connection->expects($this->once())
  88. ->method('readResponse')
  89. ->will($this->returnValue($error));
  90. $executor->execute($connection, $pipeline);
  91. }
  92. // ******************************************************************** //
  93. // ---- HELPER METHODS ------------------------------------------------ //
  94. // ******************************************************************** //
  95. /**
  96. * Executes a test for the Predis\ResponseObjectInterface type.
  97. *
  98. * @param PipelineExecutorInterface $executor
  99. * @param ResponseObjectInterface $response
  100. */
  101. protected function simpleResponseObjectTest(PipelineExecutorInterface $executor, ResponseObjectInterface $response)
  102. {
  103. $pipeline = new SplQueue();
  104. $command = $this->getMock('Predis\Command\CommandInterface');
  105. $command->expects($this->never())
  106. ->method('parseResponse');
  107. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  108. $connection->expects($this->once())
  109. ->method('writeCommand');
  110. $connection->expects($this->once())
  111. ->method('readResponse')
  112. ->will($this->returnValue($response));
  113. $pipeline->enqueue($command);
  114. $replies = $executor->execute($connection, $pipeline);
  115. $this->assertTrue($pipeline->isEmpty());
  116. $this->assertSame(array($response), $replies);
  117. }
  118. /**
  119. * Returns a list of queued command instances.
  120. *
  121. * @return SplQueue
  122. */
  123. protected function getCommandsQueue()
  124. {
  125. $profile = ServerProfile::getDevelopment();
  126. $pipeline = new SplQueue();
  127. $pipeline->enqueue($profile->createCommand('ping'));
  128. $pipeline->enqueue($profile->createCommand('ping'));
  129. $pipeline->enqueue($profile->createCommand('ping'));
  130. return $pipeline;
  131. }
  132. }