CompositeStreamConnectionTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 CompositeStreamConnectionTest extends PredisConnectionTestCase
  15. {
  16. /**
  17. * @group disconnected
  18. */
  19. public function testConstructorDoesNotOpenConnection()
  20. {
  21. $connection = new CompositeStreamConnection($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 CompositeStreamConnection($parameters);
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testExposesParameters()
  65. {
  66. $parameters = $this->getParameters();
  67. $connection = new CompositeStreamConnection($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 CompositeStreamConnection($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 testReadsMultibulkResponsesAsIterators()
  87. {
  88. $connection = $this->getConnection($profile, true);
  89. $connection->getProtocol()->useIterableMultibulk(true);
  90. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  91. $connection->writeRequest($profile->createCommand('lrange', array('metavars', 0, -1)));
  92. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulkIterator', $iterator = $connection->read());
  93. $this->assertSame(array('foo', 'hoge', 'lol'), iterator_to_array($iterator));
  94. }
  95. /**
  96. * @group connected
  97. */
  98. public function testPersistentParameterWithFalseLikeValues()
  99. {
  100. if ($this->isHHVM()) {
  101. $this->markTestSkipped('This test does not currently work on HHVM.');
  102. }
  103. $connection1 = new CompositeStreamConnection($this->getParameters(array('persistent' => 0)));
  104. $this->assertNonPersistentConnection($connection1);
  105. $connection2 = new CompositeStreamConnection($this->getParameters(array('persistent' => false)));
  106. $this->assertNonPersistentConnection($connection2);
  107. $connection3 = new CompositeStreamConnection($this->getParameters(array('persistent' => '0')));
  108. $this->assertNonPersistentConnection($connection3);
  109. $connection4 = new CompositeStreamConnection($this->getParameters(array('persistent' => 'false')));
  110. $this->assertNonPersistentConnection($connection4);
  111. }
  112. /**
  113. * @group connected
  114. */
  115. public function testPersistentParameterWithTrueLikeValues()
  116. {
  117. if ($this->isHHVM()) {
  118. $this->markTestSkipped('This test does not currently work on HHVM.');
  119. }
  120. $connection1 = new CompositeStreamConnection($this->getParameters(array('persistent' => 1)));
  121. $this->assertPersistentConnection($connection1);
  122. $connection2 = new CompositeStreamConnection($this->getParameters(array('persistent' => true)));
  123. $this->assertPersistentConnection($connection2);
  124. $connection3 = new CompositeStreamConnection($this->getParameters(array('persistent' => '1')));
  125. $this->assertPersistentConnection($connection3);
  126. $connection4 = new CompositeStreamConnection($this->getParameters(array('persistent' => 'true')));
  127. $this->assertPersistentConnection($connection4);
  128. $connection1->disconnect();
  129. }
  130. /**
  131. * @group connected
  132. */
  133. public function testPersistentConnectionsToSameNodeShareResource()
  134. {
  135. if ($this->isHHVM()) {
  136. $this->markTestSkipped('This test does not currently work on HHVM.');
  137. }
  138. $connection1 = new CompositeStreamConnection($this->getParameters(array('persistent' => true)));
  139. $connection2 = new CompositeStreamConnection($this->getParameters(array('persistent' => true)));
  140. $this->assertPersistentConnection($connection1);
  141. $this->assertPersistentConnection($connection2);
  142. $this->assertSame($connection1->getResource(), $connection2->getResource());
  143. $connection1->disconnect();
  144. }
  145. /**
  146. * @group connected
  147. */
  148. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  149. {
  150. if ($this->isHHVM()) {
  151. $this->markTestSkipped('This test does not currently work on HHVM.');
  152. }
  153. $connection1 = new CompositeStreamConnection($this->getParameters(array('persistent' => 'conn1')));
  154. $connection2 = new CompositeStreamConnection($this->getParameters(array('persistent' => 'conn2')));
  155. $this->assertPersistentConnection($connection1);
  156. $this->assertPersistentConnection($connection2);
  157. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  158. }
  159. /**
  160. * @group connected
  161. * @expectedException \Predis\Protocol\ProtocolException
  162. * @expectedExceptionMessage Unknown response prefix: 'P'.
  163. */
  164. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  165. {
  166. $connection = $this->getConnection($profile);
  167. $stream = $connection->getResource();
  168. $connection->writeRequest($profile->createCommand('ping'));
  169. fread($stream, 1);
  170. $connection->read();
  171. }
  172. // ******************************************************************** //
  173. // ---- HELPER METHODS ------------------------------------------------ //
  174. // ******************************************************************** //
  175. /**
  176. * {@inheritdoc}
  177. */
  178. protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
  179. {
  180. $parameters = $this->getParameters($parameters);
  181. $profile = $this->getProfile();
  182. $connection = new CompositeStreamConnection($parameters);
  183. if ($initialize) {
  184. $connection->addConnectCommand(
  185. $profile->createCommand('select', array($parameters->database))
  186. );
  187. $connection->addConnectCommand(
  188. $profile->createCommand('flushdb')
  189. );
  190. }
  191. return $connection;
  192. }
  193. }