MultiExecExecutorTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 ArrayIterator;
  13. use SplQueue;
  14. use Predis\ResponseError;
  15. use Predis\ResponseObjectInterface;
  16. use Predis\ResponseQueued;
  17. use Predis\Profile\ServerProfile;
  18. /**
  19. *
  20. */
  21. class ResponseIteratorStub extends ArrayIterator implements ResponseObjectInterface
  22. {
  23. }
  24. /**
  25. *
  26. */
  27. class MultiExecExecutorTest extends StandardTestCase
  28. {
  29. /**
  30. * @group disconnected
  31. */
  32. public function testExecutorWithSingleConnection()
  33. {
  34. $executor = new MultiExecExecutor();
  35. $pipeline = $this->getCommandsQueue();
  36. $queued = new ResponseQueued();
  37. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  38. $connection->expects($this->exactly(2))
  39. ->method('executeCommand')
  40. ->will($this->onConsecutiveCalls(true, array('PONG', 'PONG', 'PONG')));
  41. $connection->expects($this->exactly(3))
  42. ->method('writeCommand');
  43. $connection->expects($this->at(3))
  44. ->method('readResponse')
  45. ->will($this->onConsecutiveCalls($queued, $queued, $queued));
  46. $replies = $executor->execute($connection, $pipeline);
  47. $this->assertTrue($pipeline->isEmpty());
  48. $this->assertSame(array(true, true, true), $replies);
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testExecutorWithSingleConnectionReturningIterator()
  54. {
  55. $executor = new MultiExecExecutor();
  56. $pipeline = $this->getCommandsQueue();
  57. $queued = new ResponseQueued();
  58. $execResponse = new ResponseIteratorStub(array('PONG', 'PONG', 'PONG'));
  59. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  60. $connection->expects($this->exactly(2))
  61. ->method('executeCommand')
  62. ->will($this->onConsecutiveCalls(true, $execResponse));
  63. $connection->expects($this->exactly(3))
  64. ->method('writeCommand');
  65. $connection->expects($this->at(3))
  66. ->method('readResponse')
  67. ->will($this->onConsecutiveCalls($queued, $queued, $queued));
  68. $replies = $executor->execute($connection, $pipeline);
  69. $this->assertTrue($pipeline->isEmpty());
  70. $this->assertSame(array(true, true, true), $replies);
  71. }
  72. /**
  73. * @group disconnected
  74. * @expectedException Predis\ClientException
  75. * @expectedExceptionMessage The underlying transaction has been aborted by the server
  76. */
  77. public function testExecutorWithAbortedTransaction()
  78. {
  79. $executor = new MultiExecExecutor();
  80. $pipeline = $this->getCommandsQueue();
  81. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  82. $connection->expects($this->exactly(2))
  83. ->method('executeCommand')
  84. ->will($this->onConsecutiveCalls(true, null));
  85. $executor->execute($connection, $pipeline);
  86. }
  87. /**
  88. * @group disconnected
  89. * @expectedException Predis\ServerException
  90. * @expectedExceptionMessage ERR Test error
  91. */
  92. public function testExecutorWithErrorInTransaction()
  93. {
  94. $executor = new MultiExecExecutor();
  95. $pipeline = $this->getCommandsQueue();
  96. $queued = new ResponseQueued();
  97. $error = new ResponseError('ERR Test error');
  98. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  99. $connection->expects($this->at(0))
  100. ->method('executeCommand')
  101. ->will($this->returnValue(true));
  102. $connection->expects($this->exactly(3))
  103. ->method('readResponse')
  104. ->will($this->onConsecutiveCalls($queued, $queued, $error));
  105. $connection->expects($this->at(7))
  106. ->method('executeCommand')
  107. ->with($this->isInstanceOf('Predis\Command\TransactionDiscard'));
  108. $executor->execute($connection, $pipeline);
  109. }
  110. /**
  111. * @group disconnected
  112. */
  113. public function testExecutorWithErrorInCommandResponse()
  114. {
  115. $executor = new MultiExecExecutor();
  116. $pipeline = $this->getCommandsQueue();
  117. $queued = new ResponseQueued();
  118. $error = new ResponseError('ERR Test error');
  119. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  120. $connection->expects($this->exactly(3))
  121. ->method('readResponse')
  122. ->will($this->onConsecutiveCalls($queued, $queued, $queued));
  123. $connection->expects($this->at(7))
  124. ->method('executeCommand')
  125. ->will($this->returnValue(array('PONG', 'PONG', $error)));
  126. $replies = $executor->execute($connection, $pipeline);
  127. $this->assertSame(array(true, true, $error), $replies);
  128. }
  129. /**
  130. * @group disconnected
  131. * @expectedException Predis\ClientException
  132. * @expectedExceptionMessage Predis\Pipeline\MultiExecExecutor can be used only with single connections
  133. */
  134. public function testExecutorWithAggregatedConnection()
  135. {
  136. $executor = new MultiExecExecutor();
  137. $pipeline = $this->getCommandsQueue();
  138. $connection = $this->getMock('Predis\Connection\ReplicationConnectionInterface');
  139. $replies = $executor->execute($connection, $pipeline);
  140. }
  141. // ******************************************************************** //
  142. // ---- HELPER METHODS ------------------------------------------------ //
  143. // ******************************************************************** //
  144. /**
  145. * Returns a list of queued command instances.
  146. *
  147. * @return SplQueue
  148. */
  149. protected function getCommandsQueue()
  150. {
  151. $profile = ServerProfile::getDevelopment();
  152. $pipeline = new SplQueue();
  153. $pipeline->enqueue($profile->createCommand('ping'));
  154. $pipeline->enqueue($profile->createCommand('ping'));
  155. $pipeline->enqueue($profile->createCommand('ping'));
  156. return $pipeline;
  157. }
  158. }