PhpiredisStreamConnectionTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\Connection;
  11. use PredisTestCase;
  12. use Predis\Profile\ServerProfile;
  13. /**
  14. * @group ext-phpiredis
  15. */
  16. class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testConstructorDoesNotOpenConnection()
  22. {
  23. $connection = new PhpiredisStreamConnection($this->getParameters());
  24. $this->assertFalse($connection->isConnected());
  25. }
  26. /**
  27. * @group disconnected
  28. */
  29. public function testExposesParameters()
  30. {
  31. $parameters = $this->getParameters();
  32. $connection = new PhpiredisStreamConnection($parameters);
  33. $this->assertSame($parameters, $connection->getParameters());
  34. }
  35. /**
  36. * @group disconnected
  37. * @expectedException InvalidArgumentException
  38. * @expectedExceptionMessage Invalid scheme: udp
  39. */
  40. public function testThrowsExceptionOnInvalidScheme()
  41. {
  42. $parameters = $this->getParameters(array('scheme' => 'udp'));
  43. $connection = new PhpiredisStreamConnection($parameters);
  44. }
  45. /**
  46. * @group disconnected
  47. */
  48. public function testCanBeSerialized()
  49. {
  50. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  51. $connection = new PhpiredisStreamConnection($parameters);
  52. $unserialized = unserialize(serialize($connection));
  53. $this->assertInstanceOf('Predis\Connection\PhpiredisStreamConnection', $unserialized);
  54. $this->assertEquals($parameters, $unserialized->getParameters());
  55. }
  56. // ******************************************************************** //
  57. // ---- INTEGRATION TESTS --------------------------------------------- //
  58. // ******************************************************************** //
  59. /**
  60. * @group connected
  61. */
  62. public function testAcceptsTcpNodelayParameter()
  63. {
  64. if (!version_compare(PHP_VERSION, '5.4.0', '>=')) {
  65. $this->markTestSkipped('Setting TCP_NODELAY on PHP socket streams works on PHP >= 5.4.0');
  66. }
  67. $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => false)));
  68. $connection->connect();
  69. $this->assertTrue($connection->isConnected());
  70. $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => true)));
  71. $connection->connect();
  72. $this->assertTrue($connection->isConnected());
  73. }
  74. /**
  75. * @group connected
  76. */
  77. public function testExecutesCommandsOnServer()
  78. {
  79. $connection = $this->getConnection($profile, true);
  80. $cmdPing = $profile->createCommand('ping');
  81. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  82. $cmdGet = $profile->createCommand('get', array('foobar'));
  83. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  84. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  85. $this->assertSame('PONG', $connection->executeCommand($cmdPing));
  86. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  87. $this->assertNull($connection->executeCommand($cmdGet));
  88. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  89. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  90. }
  91. /**
  92. * @medium
  93. * @group connected
  94. * @expectedException Predis\Protocol\ProtocolException
  95. * @expectedExceptionMessage Protocol error, got "P" as reply type byte
  96. */
  97. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  98. {
  99. $connection = $this->getConnection($profile);
  100. $socket = $connection->getResource();
  101. $connection->writeCommand($profile->createCommand('ping'));
  102. fread($socket, 1);
  103. $connection->read();
  104. }
  105. // ******************************************************************** //
  106. // ---- HELPER METHODS ------------------------------------------------ //
  107. // ******************************************************************** //
  108. /**
  109. * {@inheritdoc}
  110. */
  111. protected function getConnection(&$profile = null, $initialize = false, Array $parameters = array())
  112. {
  113. $parameters = $this->getParameters($parameters);
  114. $profile = $this->getProfile();
  115. $connection = new PhpiredisStreamConnection($parameters);
  116. if ($initialize) {
  117. $connection->pushInitCommand($profile->createCommand('select', array($parameters->database)));
  118. $connection->pushInitCommand($profile->createCommand('flushdb'));
  119. }
  120. return $connection;
  121. }
  122. }