PhpiredisStreamConnectionTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * @requires extension phpiredis
  14. */
  15. class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructorDoesNotOpenConnection()
  21. {
  22. $connection = new PhpiredisStreamConnection($this->getParameters());
  23. $this->assertFalse($connection->isConnected());
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testSupportsSchemeTCP()
  29. {
  30. $parameters = $this->getParameters(array('scheme' => 'tcp'));
  31. $connection = new PhpiredisStreamConnection($parameters);
  32. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testSupportsSchemeRedis()
  38. {
  39. $parameters = $this->getParameters(array('scheme' => 'redis'));
  40. $connection = new PhpiredisStreamConnection($parameters);
  41. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testSupportsSchemeUnix()
  47. {
  48. $parameters = $this->getParameters(array('scheme' => 'unix'));
  49. $connection = new PhpiredisStreamConnection($parameters);
  50. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  51. }
  52. /**
  53. * @group disconnected
  54. * @expectedException \InvalidArgumentException
  55. * @expectedExceptionMessage Invalid scheme: 'udp'.
  56. */
  57. public function testThrowsExceptionOnInvalidScheme()
  58. {
  59. $parameters = $this->getParameters(array('scheme' => 'udp'));
  60. new PhpiredisStreamConnection($parameters);
  61. }
  62. /**
  63. * @group disconnected
  64. */
  65. public function testExposesParameters()
  66. {
  67. $parameters = $this->getParameters();
  68. $connection = new PhpiredisStreamConnection($parameters);
  69. $this->assertSame($parameters, $connection->getParameters());
  70. }
  71. /**
  72. * @group disconnected
  73. */
  74. public function testCanBeSerialized()
  75. {
  76. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  77. $connection = new PhpiredisStreamConnection($parameters);
  78. $unserialized = unserialize(serialize($connection));
  79. $this->assertInstanceOf('Predis\Connection\PhpiredisStreamConnection', $unserialized);
  80. $this->assertEquals($parameters, $unserialized->getParameters());
  81. }
  82. // ******************************************************************** //
  83. // ---- INTEGRATION TESTS --------------------------------------------- //
  84. // ******************************************************************** //
  85. /**
  86. * @group connected
  87. * @requires PHP 5.4
  88. */
  89. public function testAcceptsTcpNodelayParameter()
  90. {
  91. $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => false)));
  92. $connection->connect();
  93. $this->assertTrue($connection->isConnected());
  94. $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => true)));
  95. $connection->connect();
  96. $this->assertTrue($connection->isConnected());
  97. }
  98. /**
  99. * @group connected
  100. */
  101. public function testExecutesCommandsOnServer()
  102. {
  103. $connection = $this->getConnection($profile, true);
  104. $cmdPing = $profile->createCommand('ping');
  105. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  106. $cmdGet = $profile->createCommand('get', array('foobar'));
  107. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  108. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  109. $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
  110. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  111. $this->assertNull($connection->executeCommand($cmdGet));
  112. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  113. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  114. }
  115. /**
  116. * @group connected
  117. */
  118. public function testPersistentParameterWithFalseLikeValues()
  119. {
  120. if ($this->isHHVM()) {
  121. $this->markTestSkipped('This test does not currently work on HHVM.');
  122. }
  123. $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 0)));
  124. $this->assertNonPersistentConnection($connection1);
  125. $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => false)));
  126. $this->assertNonPersistentConnection($connection2);
  127. $connection3 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => '0')));
  128. $this->assertNonPersistentConnection($connection3);
  129. $connection4 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'false')));
  130. $this->assertNonPersistentConnection($connection4);
  131. }
  132. /**
  133. * @group connected
  134. */
  135. public function testPersistentParameterWithTrueLikeValues()
  136. {
  137. if ($this->isHHVM()) {
  138. $this->markTestSkipped('This test does not currently work on HHVM.');
  139. }
  140. $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 1)));
  141. $this->assertPersistentConnection($connection1);
  142. $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true)));
  143. $this->assertPersistentConnection($connection2);
  144. $connection3 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => '1')));
  145. $this->assertPersistentConnection($connection3);
  146. $connection4 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'true')));
  147. $this->assertPersistentConnection($connection4);
  148. $connection1->disconnect();
  149. }
  150. /**
  151. * @group connected
  152. */
  153. public function testPersistentConnectionsToSameNodeShareResource()
  154. {
  155. if ($this->isHHVM()) {
  156. $this->markTestSkipped('This test does not currently work on HHVM.');
  157. }
  158. $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true)));
  159. $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true)));
  160. $this->assertPersistentConnection($connection1);
  161. $this->assertPersistentConnection($connection2);
  162. $this->assertSame($connection1->getResource(), $connection2->getResource());
  163. $connection1->disconnect();
  164. }
  165. /**
  166. * @group connected
  167. */
  168. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  169. {
  170. if ($this->isHHVM()) {
  171. $this->markTestSkipped('This test does not currently work on HHVM.');
  172. }
  173. $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'conn1')));
  174. $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'conn2')));
  175. $this->assertPersistentConnection($connection1);
  176. $this->assertPersistentConnection($connection2);
  177. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  178. }
  179. /**
  180. * @medium
  181. * @group connected
  182. * @expectedException \Predis\Protocol\ProtocolException
  183. * @expectedExceptionMessage Protocol error, got "P" as reply type byte
  184. */
  185. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  186. {
  187. $connection = $this->getConnection($profile);
  188. $socket = $connection->getResource();
  189. $connection->writeRequest($profile->createCommand('ping'));
  190. fread($socket, 1);
  191. $connection->read();
  192. }
  193. // ******************************************************************** //
  194. // ---- HELPER METHODS ------------------------------------------------ //
  195. // ******************************************************************** //
  196. /**
  197. * {@inheritdoc}
  198. */
  199. protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
  200. {
  201. $parameters = $this->getParameters($parameters);
  202. $profile = $this->getProfile();
  203. $connection = new PhpiredisStreamConnection($parameters);
  204. if ($initialize) {
  205. $connection->addConnectCommand(
  206. $profile->createCommand('select', array($parameters->database))
  207. );
  208. $connection->addConnectCommand(
  209. $profile->createCommand('flushdb')
  210. );
  211. }
  212. return $connection;
  213. }
  214. }