PhpiredisStreamConnectionTest.php 4.8 KB

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