StreamConnectionTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 testSupportsSchemeTCP()
  28. {
  29. $parameters = $this->getParameters(array('scheme' => 'tcp'));
  30. $connection = new StreamConnection($parameters);
  31. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testSupportsSchemeRedis()
  37. {
  38. $parameters = $this->getParameters(array('scheme' => 'redis'));
  39. $connection = new StreamConnection($parameters);
  40. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  41. }
  42. /**
  43. * @group disconnected
  44. */
  45. public function testSupportsSchemeUnix()
  46. {
  47. $parameters = $this->getParameters(array('scheme' => 'unix'));
  48. $connection = new StreamConnection($parameters);
  49. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  50. }
  51. /**
  52. * @group disconnected
  53. * @expectedException \InvalidArgumentException
  54. * @expectedExceptionMessage Invalid scheme: 'udp'.
  55. */
  56. public function testThrowsExceptionOnInvalidScheme()
  57. {
  58. $parameters = $this->getParameters(array('scheme' => 'udp'));
  59. new StreamConnection($parameters);
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testExposesParameters()
  65. {
  66. $parameters = $this->getParameters();
  67. $connection = new StreamConnection($parameters);
  68. $this->assertSame($parameters, $connection->getParameters());
  69. }
  70. /**
  71. * @group disconnected
  72. */
  73. public function testCanBeSerialized()
  74. {
  75. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  76. $connection = new StreamConnection($parameters);
  77. $unserialized = unserialize(serialize($connection));
  78. $this->assertEquals($connection, $unserialized);
  79. }
  80. // ******************************************************************** //
  81. // ---- INTEGRATION TESTS --------------------------------------------- //
  82. // ******************************************************************** //
  83. /**
  84. * @group connected
  85. */
  86. public function testAcceptsTcpNodelayParameter()
  87. {
  88. if (!version_compare(PHP_VERSION, '5.4.0', '>=')) {
  89. $this->markTestSkipped('Setting TCP_NODELAY on PHP socket streams works on PHP >= 5.4.0');
  90. }
  91. $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => false)));
  92. $connection->connect();
  93. $this->assertTrue($connection->isConnected());
  94. $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => true)));
  95. $connection->connect();
  96. $this->assertTrue($connection->isConnected());
  97. }
  98. /**
  99. * @group connected
  100. * @expectedException \Predis\Protocol\ProtocolException
  101. * @expectedExceptionMessage Unknown response prefix: 'P'.
  102. */
  103. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  104. {
  105. $connection = $this->getConnection($profile);
  106. $stream = $connection->getResource();
  107. $connection->writeRequest($profile->createCommand('ping'));
  108. fread($stream, 1);
  109. $connection->read();
  110. }
  111. // ******************************************************************** //
  112. // ---- HELPER METHODS ------------------------------------------------ //
  113. // ******************************************************************** //
  114. /**
  115. * {@inheritdoc}
  116. */
  117. protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
  118. {
  119. $parameters = $this->getParameters($parameters);
  120. $profile = $this->getProfile();
  121. $connection = new StreamConnection($parameters);
  122. if ($initialize) {
  123. $connection->addConnectCommand(
  124. $profile->createCommand('select', array($parameters->database))
  125. );
  126. $connection->addConnectCommand(
  127. $profile->createCommand('flushdb')
  128. );
  129. }
  130. return $connection;
  131. }
  132. }