StreamConnectionTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @expectedException Predis\Protocol\ProtocolException
  74. * @expectedExceptionMessage Unknown prefix: 'P'
  75. */
  76. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  77. {
  78. $connection = $this->getConnection($profile);
  79. $stream = $connection->getResource();
  80. $connection->writeRequest($profile->createCommand('ping'));
  81. fread($stream, 1);
  82. $connection->read();
  83. }
  84. // ******************************************************************** //
  85. // ---- HELPER METHODS ------------------------------------------------ //
  86. // ******************************************************************** //
  87. /**
  88. * {@inheritdoc}
  89. */
  90. protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
  91. {
  92. $parameters = $this->getParameters($parameters);
  93. $profile = $this->getProfile();
  94. $connection = new StreamConnection($parameters);
  95. if ($initialize) {
  96. $connection->addConnectCommand(
  97. $profile->createCommand('select', array($parameters->database))
  98. );
  99. $connection->addConnectCommand(
  100. $profile->createCommand('flushdb')
  101. );
  102. }
  103. return $connection;
  104. }
  105. }