StandardExecutorTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Profile\ServerProfile;
  15. /**
  16. *
  17. */
  18. class StandardExecutorTest extends StandardTestCase
  19. {
  20. /**
  21. * @group disconnected
  22. */
  23. public function testExecutorWithSingleConnection()
  24. {
  25. $executor = new StandardExecutor();
  26. $pipeline = $this->getCommandsQueue();
  27. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  28. $connection->expects($this->exactly(3))
  29. ->method('writeCommand');
  30. $connection->expects($this->exactly(3))
  31. ->method('readResponse')
  32. ->will($this->returnValue('PONG'));
  33. $replies = $executor->execute($connection, $pipeline);
  34. $this->assertTrue($pipeline->isEmpty());
  35. $this->assertSame(array('PONG', 'PONG', 'PONG'), $replies);
  36. }
  37. /**
  38. * @group disconnected
  39. */
  40. public function testExecutorWithReplicationConnection()
  41. {
  42. $executor = new StandardExecutor();
  43. $pipeline = $this->getCommandsQueue();
  44. $connection = $this->getMock('Predis\Connection\ReplicationConnectionInterface');
  45. $connection->expects($this->once())
  46. ->method('switchTo')
  47. ->with('master');
  48. $connection->expects($this->exactly(3))
  49. ->method('writeCommand');
  50. $connection->expects($this->exactly(3))
  51. ->method('readResponse')
  52. ->will($this->returnValue('PONG'));
  53. $replies = $executor->execute($connection, $pipeline);
  54. $this->assertTrue($pipeline->isEmpty());
  55. $this->assertSame(array('PONG', 'PONG', 'PONG'), $replies);
  56. }
  57. /**
  58. * @group disconnected
  59. * @expectedException Predis\ServerException
  60. * @expectedExceptionMessage ERR Test error
  61. */
  62. public function testExecutorCanThrowExceptions()
  63. {
  64. $executor = new StandardExecutor(true);
  65. $pipeline = $this->getCommandsQueue();
  66. $error = new ResponseError('ERR Test error');
  67. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  68. $connection->expects($this->once())
  69. ->method('readResponse')
  70. ->will($this->returnValue($error));
  71. $executor->execute($connection, $pipeline);
  72. }
  73. // ******************************************************************** //
  74. // ---- HELPER METHODS ------------------------------------------------ //
  75. // ******************************************************************** //
  76. /**
  77. * Returns a list of queued command instances.
  78. *
  79. * @return SplQueue
  80. */
  81. protected function getCommandsQueue()
  82. {
  83. $profile = ServerProfile::getDevelopment();
  84. $pipeline = new SplQueue();
  85. $pipeline->enqueue($profile->createCommand('ping'));
  86. $pipeline->enqueue($profile->createCommand('ping'));
  87. $pipeline->enqueue($profile->createCommand('ping'));
  88. return $pipeline;
  89. }
  90. }