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