AtomicTest.php 5.1 KB

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