AtomicTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 Predis\Client;
  12. use Predis\Response;
  13. use PredisTestCase;
  14. /**
  15. *
  16. */
  17. class AtomicTest extends PredisTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testPipelineWithSingleConnection()
  23. {
  24. $pong = new Response\Status('PONG');
  25. $queued = new Response\Status('QUEUED');
  26. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  27. $connection
  28. ->expects($this->exactly(2))
  29. ->method('executeCommand')
  30. ->will($this->onConsecutiveCalls(true, array($pong, $pong, $pong)));
  31. $connection
  32. ->expects($this->exactly(3))
  33. ->method('writeRequest');
  34. $connection
  35. ->expects($this->at(3))
  36. ->method('readResponse')
  37. ->will($this->onConsecutiveCalls($queued, $queued, $queued));
  38. $pipeline = new Atomic(new Client($connection));
  39. $pipeline->ping();
  40. $pipeline->ping();
  41. $pipeline->ping();
  42. $this->assertSame(array($pong, $pong, $pong), $pipeline->execute());
  43. }
  44. /**
  45. * @group disconnected
  46. * @expectedException \Predis\ClientException
  47. * @expectedExceptionMessage The underlying transaction has been aborted by the server.
  48. */
  49. public function testThrowsExceptionOnAbortedTransaction()
  50. {
  51. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  52. $connection
  53. ->expects($this->exactly(2))
  54. ->method('executeCommand')
  55. ->will($this->onConsecutiveCalls(true, null));
  56. $pipeline = new Atomic(new Client($connection));
  57. $pipeline->ping();
  58. $pipeline->ping();
  59. $pipeline->ping();
  60. $pipeline->execute();
  61. }
  62. /**
  63. * @group disconnected
  64. * @expectedException \Predis\Response\ServerException
  65. * @expectedExceptionMessage ERR Test error
  66. */
  67. public function testPipelineWithErrorInTransaction()
  68. {
  69. $queued = new Response\Status('QUEUED');
  70. $error = new Response\Error('ERR Test error');
  71. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  72. $connection
  73. ->expects($this->at(0))
  74. ->method('executeCommand')
  75. ->will($this->returnValue(true));
  76. $connection
  77. ->expects($this->exactly(3))
  78. ->method('readResponse')
  79. ->will($this->onConsecutiveCalls($queued, $queued, $error));
  80. $connection
  81. ->expects($this->at(7))
  82. ->method('executeCommand')
  83. ->with($this->isRedisCommand('DISCARD'));
  84. $pipeline = new Atomic(new Client($connection));
  85. $pipeline->ping();
  86. $pipeline->ping();
  87. $pipeline->ping();
  88. $pipeline->execute();
  89. }
  90. /**
  91. * @group disconnected
  92. * @expectedException \Predis\Response\ServerException
  93. * @expectedExceptionMessage ERR Test error
  94. */
  95. public function testThrowsServerExceptionOnResponseErrorByDefault()
  96. {
  97. $error = new Response\Error('ERR Test error');
  98. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  99. $connection
  100. ->expects($this->once())
  101. ->method('readResponse')
  102. ->will($this->returnValue($error));
  103. $pipeline = new Atomic(new Client($connection));
  104. $pipeline->ping();
  105. $pipeline->ping();
  106. $pipeline->execute();
  107. }
  108. /**
  109. * @group disconnected
  110. */
  111. public function testReturnsResponseErrorWithClientExceptionsSetToFalse()
  112. {
  113. $pong = new Response\Status('PONG');
  114. $queued = new Response\Status('QUEUED');
  115. $error = new Response\Error('ERR Test error');
  116. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  117. $connection
  118. ->expects($this->exactly(3))
  119. ->method('readResponse')
  120. ->will($this->onConsecutiveCalls($queued, $queued, $queued));
  121. $connection
  122. ->expects($this->at(7))
  123. ->method('executeCommand')
  124. ->will($this->returnValue(array($pong, $pong, $error)));
  125. $pipeline = new Atomic(new Client($connection, array('exceptions' => false)));
  126. $pipeline->ping();
  127. $pipeline->ping();
  128. $pipeline->ping();
  129. $this->assertSame(array($pong, $pong, $error), $pipeline->execute());
  130. }
  131. /**
  132. * @group disconnected
  133. * @expectedException \Predis\ClientException
  134. * @expectedExceptionMessage The class 'Predis\Pipeline\Atomic' does not support aggregate connections.
  135. */
  136. public function testExecutorWithAggregateConnection()
  137. {
  138. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  139. $pipeline = new Atomic(new Client($connection));
  140. $pipeline->ping();
  141. $pipeline->execute();
  142. }
  143. }