AtomicTest.php 5.0 KB

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