MultiExecExecutorTest.php 5.1 KB

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