MultiExecExecutorTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * @expectedException Predis\ClientException
  83. * @expectedExceptionMessage Predis\Pipeline\MultiExecExecutor can be used only with single connections
  84. */
  85. public function testExecutorWithAggregatedConnection()
  86. {
  87. $executor = new MultiExecExecutor();
  88. $pipeline = $this->getCommandsQueue();
  89. $connection = $this->getMock('Predis\Connection\ReplicationConnectionInterface');
  90. $replies = $executor->execute($connection, $pipeline);
  91. }
  92. // ******************************************************************** //
  93. // ---- HELPER METHODS ------------------------------------------------ //
  94. // ******************************************************************** //
  95. /**
  96. * Returns a list of queued command instances.
  97. *
  98. * @return SplQueue
  99. */
  100. protected function getCommandsQueue()
  101. {
  102. $profile = ServerProfile::getDevelopment();
  103. $pipeline = new SplQueue();
  104. $pipeline->enqueue($profile->createCommand('ping'));
  105. $pipeline->enqueue($profile->createCommand('ping'));
  106. $pipeline->enqueue($profile->createCommand('ping'));
  107. return $pipeline;
  108. }
  109. }