MultiExecExecutorTest.php 5.1 KB

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