PhpiredisStreamConnectionTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 Predis\Command\RawCommand;
  12. use Predis\Response\Error as ErrorResponse;
  13. /**
  14. * @group ext-phpiredis
  15. * @requires extension phpiredis
  16. */
  17. class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
  18. {
  19. const CONNECTION_CLASS = 'Predis\Connection\PhpiredisStreamConnection';
  20. /**
  21. * @group disconnected
  22. * @expectedException \InvalidArgumentException
  23. * @expectedExceptionMessage SSL encryption is not supported by this connection backend.
  24. */
  25. public function testSupportsSchemeTls()
  26. {
  27. $connection = $this->createConnectionWithParams(array('scheme' => 'tls'));
  28. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  29. }
  30. /**
  31. * @group disconnected
  32. * @expectedException \InvalidArgumentException
  33. * @expectedExceptionMessage SSL encryption is not supported by this connection backend.
  34. */
  35. public function testSupportsSchemeRediss()
  36. {
  37. $connection = $this->createConnectionWithParams(array('scheme' => 'rediss'));
  38. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  39. }
  40. /**
  41. * @group disconnected
  42. * @expectedException \Predis\Connection\ConnectionException
  43. * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379]
  44. */
  45. public function testThrowsExceptionOnInitializationCommandFailure()
  46. {
  47. $cmdSelect = RawCommand::create('SELECT', '1000');
  48. $connection = $this
  49. ->getMockBuilder(static::CONNECTION_CLASS)
  50. ->setMethods(array('executeCommand', 'createResource'))
  51. ->setConstructorArgs(array(new Parameters()))
  52. ->getMock();
  53. $connection
  54. ->method('executeCommand')
  55. ->with($cmdSelect)
  56. ->will($this->returnValue(
  57. new ErrorResponse('ERR invalid DB index')
  58. ));
  59. $connection->method('createResource');
  60. $connection->addConnectCommand($cmdSelect);
  61. $connection->connect();
  62. }
  63. // ******************************************************************** //
  64. // ---- INTEGRATION TESTS --------------------------------------------- //
  65. // ******************************************************************** //
  66. /**
  67. * @group connected
  68. * @group slow
  69. * @requires PHP 5.4
  70. * @expectedException \Predis\Connection\ConnectionException
  71. */
  72. public function testThrowsExceptionOnReadWriteTimeout()
  73. {
  74. $connection = $this->createConnectionWithParams(array(
  75. 'read_write_timeout' => 0.5,
  76. ), true);
  77. $connection->executeCommand(
  78. $this->getCommandFactory()->createCommand('brpop', array('foo', 3))
  79. );
  80. }
  81. /**
  82. * @medium
  83. * @group connected
  84. * @expectedException \Predis\Protocol\ProtocolException
  85. */
  86. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  87. {
  88. $connection = $this->createConnection();
  89. $stream = $connection->getResource();
  90. $connection->writeRequest($this->getCommandFactory()->createCommand('ping'));
  91. stream_socket_recvfrom($stream, 1);
  92. $connection->read();
  93. }
  94. /**
  95. * @group connected
  96. * @requires PHP 5.4
  97. */
  98. public function testPersistentParameterWithFalseLikeValues()
  99. {
  100. $connection1 = $this->createConnectionWithParams(array('persistent' => 0));
  101. $this->assertNonPersistentConnection($connection1);
  102. $connection2 = $this->createConnectionWithParams(array('persistent' => false));
  103. $this->assertNonPersistentConnection($connection2);
  104. $connection3 = $this->createConnectionWithParams(array('persistent' => '0'));
  105. $this->assertNonPersistentConnection($connection3);
  106. $connection4 = $this->createConnectionWithParams(array('persistent' => 'false'));
  107. $this->assertNonPersistentConnection($connection4);
  108. }
  109. /**
  110. * @group connected
  111. * @requires PHP 5.4
  112. */
  113. public function testPersistentParameterWithTrueLikeValues()
  114. {
  115. $connection1 = $this->createConnectionWithParams(array('persistent' => 1));
  116. $this->assertPersistentConnection($connection1);
  117. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  118. $this->assertPersistentConnection($connection2);
  119. $connection3 = $this->createConnectionWithParams(array('persistent' => '1'));
  120. $this->assertPersistentConnection($connection3);
  121. $connection4 = $this->createConnectionWithParams(array('persistent' => 'true'));
  122. $this->assertPersistentConnection($connection4);
  123. $connection1->disconnect();
  124. }
  125. /**
  126. * @group connected
  127. * @requires PHP 5.4
  128. */
  129. public function testPersistentConnectionsToSameNodeShareResource()
  130. {
  131. $connection1 = $this->createConnectionWithParams(array('persistent' => true));
  132. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  133. $this->assertPersistentConnection($connection1);
  134. $this->assertPersistentConnection($connection2);
  135. $this->assertSame($connection1->getResource(), $connection2->getResource());
  136. $connection1->disconnect();
  137. }
  138. /**
  139. * @group connected
  140. * @requires PHP 5.4
  141. */
  142. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  143. {
  144. $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1'));
  145. $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2'));
  146. $this->assertPersistentConnection($connection1);
  147. $this->assertPersistentConnection($connection2);
  148. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  149. }
  150. }