MultiExecExecutorTest.php 5.1 KB

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