PhpiredisStreamConnectionTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 testSupportsSchemeTCP()
  28. {
  29. $parameters = $this->getParameters(array('scheme' => 'tcp'));
  30. $connection = new PhpiredisStreamConnection($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 PhpiredisStreamConnection($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 PhpiredisStreamConnection($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 PhpiredisStreamConnection($parameters);
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testExposesParameters()
  65. {
  66. $parameters = $this->getParameters();
  67. $connection = new PhpiredisStreamConnection($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 PhpiredisStreamConnection($parameters);
  77. $unserialized = unserialize(serialize($connection));
  78. $this->assertInstanceOf('Predis\Connection\PhpiredisStreamConnection', $unserialized);
  79. $this->assertEquals($parameters, $unserialized->getParameters());
  80. }
  81. // ******************************************************************** //
  82. // ---- INTEGRATION TESTS --------------------------------------------- //
  83. // ******************************************************************** //
  84. /**
  85. * @group connected
  86. */
  87. public function testAcceptsTcpNodelayParameter()
  88. {
  89. if (!version_compare(PHP_VERSION, '5.4.0', '>=')) {
  90. $this->markTestSkipped('Setting TCP_NODELAY on PHP socket streams works on PHP >= 5.4.0');
  91. }
  92. $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => false)));
  93. $connection->connect();
  94. $this->assertTrue($connection->isConnected());
  95. $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => true)));
  96. $connection->connect();
  97. $this->assertTrue($connection->isConnected());
  98. }
  99. /**
  100. * @group connected
  101. */
  102. public function testExecutesCommandsOnServer()
  103. {
  104. $connection = $this->getConnection($profile, true);
  105. $cmdPing = $profile->createCommand('ping');
  106. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  107. $cmdGet = $profile->createCommand('get', array('foobar'));
  108. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  109. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  110. $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
  111. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  112. $this->assertNull($connection->executeCommand($cmdGet));
  113. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  114. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  115. }
  116. /**
  117. * @group connected
  118. */
  119. public function testPersistentParameterWithFalseLikeValues()
  120. {
  121. if ($this->isHHVM()) {
  122. $this->markTestSkipped('This test does not currently work on HHVM.');
  123. }
  124. $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 0)));
  125. $this->assertNonPersistentConnection($connection1);
  126. $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => false)));
  127. $this->assertNonPersistentConnection($connection2);
  128. $connection3 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => '0')));
  129. $this->assertNonPersistentConnection($connection3);
  130. $connection4 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'false')));
  131. $this->assertNonPersistentConnection($connection4);
  132. }
  133. /**
  134. * @group connected
  135. */
  136. public function testPersistentParameterWithTrueLikeValues()
  137. {
  138. if ($this->isHHVM()) {
  139. $this->markTestSkipped('This test does not currently work on HHVM.');
  140. }
  141. $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 1)));
  142. $this->assertPersistentConnection($connection1);
  143. $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true)));
  144. $this->assertPersistentConnection($connection2);
  145. $connection3 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => '1')));
  146. $this->assertPersistentConnection($connection3);
  147. $connection4 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'true')));
  148. $this->assertPersistentConnection($connection4);
  149. $connection1->disconnect();
  150. }
  151. /**
  152. * @group connected
  153. */
  154. public function testPersistentConnectionsToSameNodeShareResource()
  155. {
  156. if ($this->isHHVM()) {
  157. $this->markTestSkipped('This test does not currently work on HHVM.');
  158. }
  159. $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true)));
  160. $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true)));
  161. $this->assertPersistentConnection($connection1);
  162. $this->assertPersistentConnection($connection2);
  163. $this->assertSame($connection1->getResource(), $connection2->getResource());
  164. $connection1->disconnect();
  165. }
  166. /**
  167. * @group connected
  168. */
  169. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  170. {
  171. if ($this->isHHVM()) {
  172. $this->markTestSkipped('This test does not currently work on HHVM.');
  173. }
  174. $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'conn1')));
  175. $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'conn2')));
  176. $this->assertPersistentConnection($connection1);
  177. $this->assertPersistentConnection($connection2);
  178. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  179. }
  180. /**
  181. * @medium
  182. * @group connected
  183. * @expectedException \Predis\Protocol\ProtocolException
  184. * @expectedExceptionMessage Protocol error, got "P" as reply type byte
  185. */
  186. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  187. {
  188. $connection = $this->getConnection($profile);
  189. $socket = $connection->getResource();
  190. $connection->writeRequest($profile->createCommand('ping'));
  191. fread($socket, 1);
  192. $connection->read();
  193. }
  194. // ******************************************************************** //
  195. // ---- HELPER METHODS ------------------------------------------------ //
  196. // ******************************************************************** //
  197. /**
  198. * {@inheritdoc}
  199. */
  200. protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
  201. {
  202. $parameters = $this->getParameters($parameters);
  203. $profile = $this->getProfile();
  204. $connection = new PhpiredisStreamConnection($parameters);
  205. if ($initialize) {
  206. $connection->addConnectCommand(
  207. $profile->createCommand('select', array($parameters->database))
  208. );
  209. $connection->addConnectCommand(
  210. $profile->createCommand('flushdb')
  211. );
  212. }
  213. return $connection;
  214. }
  215. }