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 PHPUnit_Framework_TestCase as StandardTestCase;
  12. use SplQueue;
  13. use Predis\Client;
  14. use Predis\Response;
  15. /**
  16. *
  17. */
  18. class AtomicTest extends StandardTestCase
  19. {
  20. /**
  21. * @group disconnected
  22. */
  23. public function testPipelineWithSingleConnection()
  24. {
  25. $queued = new Response\StatusQueued();
  26. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  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('writeCommand');
  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(true, true, true), $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\SingleConnectionInterface');
  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\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. $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\SingleConnectionInterface');
  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. $queued = new Response\StatusQueued();
  106. $error = new Response\Error('ERR Test error');
  107. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  108. $connection->expects($this->exactly(3))
  109. ->method('readResponse')
  110. ->will($this->onConsecutiveCalls($queued, $queued, $queued));
  111. $connection->expects($this->at(7))
  112. ->method('executeCommand')
  113. ->will($this->returnValue(array('PONG', 'PONG', $error)));
  114. $pipeline = new Atomic(new Client($connection, array('exceptions' => false)));
  115. $pipeline->ping();
  116. $pipeline->ping();
  117. $pipeline->ping();
  118. $this->assertSame(array(true, true, $error), $pipeline->execute());
  119. }
  120. /**
  121. * @group disconnected
  122. * @expectedException Predis\ClientException
  123. * @expectedExceptionMessage Predis\Pipeline\Atomic can be used only with connections to single nodes
  124. */
  125. public function testExecutorWithAggregatedConnection()
  126. {
  127. $connection = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  128. $pipeline = new Atomic(new Client($connection));
  129. $pipeline->ping();
  130. $pipeline->execute();
  131. }
  132. }