StreamConnectionTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. *
  13. */
  14. class StreamConnectionTest extends PredisConnectionTestCase
  15. {
  16. /**
  17. * @group disconnected
  18. */
  19. public function testConstructorDoesNotOpenConnection()
  20. {
  21. $connection = new StreamConnection($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 StreamConnection($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 StreamConnection($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 StreamConnection($parameters);
  50. $unserialized = unserialize(serialize($connection));
  51. $this->assertEquals($connection, $unserialized);
  52. }
  53. // ******************************************************************** //
  54. // ---- INTEGRATION TESTS --------------------------------------------- //
  55. // ******************************************************************** //
  56. /**
  57. * @group connected
  58. */
  59. public function testAcceptsTcpNodelayParameter()
  60. {
  61. if (!version_compare(PHP_VERSION, '5.4.0', '>=')) {
  62. $this->markTestSkipped('Setting TCP_NODELAY on PHP socket streams works on PHP >= 5.4.0');
  63. }
  64. $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => false)));
  65. $connection->connect();
  66. $this->assertTrue($connection->isConnected());
  67. $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => true)));
  68. $connection->connect();
  69. $this->assertTrue($connection->isConnected());
  70. }
  71. /**
  72. * @group connected
  73. */
  74. public function testReadsMultibulkRepliesAsIterators()
  75. {
  76. $connection = $this->getConnection($profile, true, array('iterable_multibulk' => true));
  77. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  78. $connection->writeCommand($profile->createCommand('lrange', array('metavars', 0, -1)));
  79. $this->assertInstanceOf('Predis\Iterator\MultiBulkResponse', $iterator = $connection->read());
  80. $this->assertSame(array('foo', 'hoge', 'lol'), iterator_to_array($iterator));
  81. }
  82. /**
  83. * @group connected
  84. * @expectedException Predis\Protocol\ProtocolException
  85. * @expectedExceptionMessage Unknown prefix: 'P'
  86. */
  87. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  88. {
  89. $connection = $this->getConnection($profile);
  90. $stream = $connection->getResource();
  91. $connection->writeCommand($profile->createCommand('ping'));
  92. fread($stream, 1);
  93. $connection->read();
  94. }
  95. // ******************************************************************** //
  96. // ---- HELPER METHODS ------------------------------------------------ //
  97. // ******************************************************************** //
  98. /**
  99. * {@inheritdoc}
  100. */
  101. protected function getConnection(&$profile = null, $initialize = false, Array $parameters = array())
  102. {
  103. $parameters = $this->getParameters($parameters);
  104. $profile = $this->getProfile();
  105. $connection = new StreamConnection($parameters);
  106. if ($initialize) {
  107. $connection->pushInitCommand($profile->createCommand('select', array($parameters->database)));
  108. $connection->pushInitCommand($profile->createCommand('flushdb'));
  109. }
  110. return $connection;
  111. }
  112. }