ConnectionParametersTest.php 7.6 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. use PredisTestCase;
  12. /**
  13. * @todo ConnectionParameters::define();
  14. * @todo ConnectionParameters::undefine();
  15. */
  16. class ConnectionParametersTest extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testDefaultValues()
  22. {
  23. $defaults = $this->getDefaultParametersArray();
  24. $parameters = new ConnectionParameters();
  25. $this->assertEquals($defaults['scheme'], $parameters->scheme);
  26. $this->assertEquals($defaults['host'], $parameters->host);
  27. $this->assertEquals($defaults['port'], $parameters->port);
  28. $this->assertEquals($defaults['timeout'], $parameters->timeout);
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testIsSet()
  34. {
  35. $parameters = new ConnectionParameters();
  36. $this->assertTrue(isset($parameters->scheme));
  37. $this->assertFalse(isset($parameters->unknown));
  38. }
  39. /**
  40. * @group disconnected
  41. */
  42. public function testUserDefinedParameters()
  43. {
  44. $parameters = new ConnectionParameters(array('port' => 7000, 'custom' => 'foobar'));
  45. $this->assertTrue(isset($parameters->scheme));
  46. $this->assertSame('tcp', $parameters->scheme);
  47. $this->assertTrue(isset($parameters->port));
  48. $this->assertSame(7000, $parameters->port);
  49. $this->assertTrue(isset($parameters->custom));
  50. $this->assertSame('foobar', $parameters->custom);
  51. $this->assertFalse(isset($parameters->unknown));
  52. $this->assertNull($parameters->unknown);
  53. }
  54. /**
  55. * @group disconnected
  56. */
  57. public function testConstructWithUriString()
  58. {
  59. $defaults = $this->getDefaultParametersArray();
  60. $overrides = array(
  61. 'port' => 7000,
  62. 'database' => 5,
  63. 'iterable_multibulk' => false,
  64. 'custom' => 'foobar',
  65. );
  66. $parameters = new ConnectionParameters($this->getParametersString($overrides));
  67. $this->assertEquals($defaults['scheme'], $parameters->scheme);
  68. $this->assertEquals($defaults['host'], $parameters->host);
  69. $this->assertEquals($overrides['port'], $parameters->port);
  70. $this->assertEquals($overrides['database'], $parameters->database);
  71. $this->assertEquals($overrides['iterable_multibulk'], $parameters->iterable_multibulk);
  72. $this->assertTrue(isset($parameters->custom));
  73. $this->assertEquals($overrides['custom'], $parameters->custom);
  74. $this->assertFalse(isset($parameters->unknown));
  75. $this->assertNull($parameters->unknown);
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testToArray()
  81. {
  82. $additional = array('port' => 7000, 'custom' => 'foobar');
  83. $parameters = new ConnectionParameters($additional);
  84. $this->assertEquals($this->getParametersArray($additional), $parameters->toArray());
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testSerialization()
  90. {
  91. $parameters = new ConnectionParameters(array('port' => 7000, 'custom' => 'foobar'));
  92. $unserialized = unserialize(serialize($parameters));
  93. $this->assertEquals($parameters->scheme, $unserialized->scheme);
  94. $this->assertEquals($parameters->port, $unserialized->port);
  95. $this->assertTrue(isset($unserialized->custom));
  96. $this->assertEquals($parameters->custom, $unserialized->custom);
  97. $this->assertFalse(isset($unserialized->unknown));
  98. $this->assertNull($unserialized->unknown);
  99. }
  100. /**
  101. * @group disconnected
  102. */
  103. public function testParsingURI()
  104. {
  105. $uri = 'tcp://10.10.10.10:6400?timeout=0.5&persistent=1';
  106. $expected = array(
  107. 'scheme' => 'tcp',
  108. 'host' => '10.10.10.10',
  109. 'port' => 6400,
  110. 'timeout' => '0.5',
  111. 'persistent' => '1',
  112. );
  113. $this->assertSame($expected, ConnectionParameters::parseURI($uri));
  114. }
  115. /**
  116. * @group disconnected
  117. */
  118. public function testParsingUnixDomainURI()
  119. {
  120. $uri = 'unix:///tmp/redis.sock?timeout=0.5&persistent=1';
  121. $expected = array(
  122. 'scheme' => 'unix',
  123. 'host' => 'localhost',
  124. 'path' => '/tmp/redis.sock',
  125. 'timeout' => '0.5',
  126. 'persistent' => '1',
  127. );
  128. $this->assertSame($expected, ConnectionParameters::parseURI($uri));
  129. }
  130. /**
  131. * @group disconnected
  132. */
  133. public function testParsingURIWithIncompletePairInQueryString()
  134. {
  135. $uri = 'tcp://10.10.10.10?persistent=1&foo=&bar';
  136. $expected = array(
  137. 'scheme' => 'tcp',
  138. 'host' => '10.10.10.10',
  139. 'persistent' => '1',
  140. 'foo' => '',
  141. 'bar' => '',
  142. );
  143. $this->assertSame($expected, ConnectionParameters::parseURI($uri));
  144. }
  145. /**
  146. * @group disconnected
  147. */
  148. public function testParsingURIWithMoreThanOneEqualSignInQueryStringPairValue()
  149. {
  150. $uri = 'tcp://10.10.10.10?foobar=a=b=c&persistent=1';
  151. $expected = array(
  152. 'scheme' => 'tcp',
  153. 'host' => '10.10.10.10',
  154. 'foobar' => 'a=b=c',
  155. 'persistent' => '1',
  156. );
  157. $this->assertSame($expected, ConnectionParameters::parseURI($uri));
  158. }
  159. /**
  160. * @group disconnected
  161. */
  162. public function testParsingURIWhenQueryStringHasBracketsInFieldnames()
  163. {
  164. $uri = 'tcp://10.10.10.10?persistent=1&metavars[]=foo&metavars[]=hoge';
  165. $expected = array(
  166. 'scheme' => 'tcp',
  167. 'host' => '10.10.10.10',
  168. 'persistent' => '1',
  169. 'metavars' => array('foo', 'hoge'),
  170. );
  171. $this->assertSame($expected, ConnectionParameters::parseURI($uri));
  172. }
  173. /**
  174. * @group disconnected
  175. * @expectedException Predis\ClientException
  176. * @expectedExceptionMessage Invalid URI: tcp://invalid:uri
  177. */
  178. public function testParsingURIThrowOnInvalidURI()
  179. {
  180. ConnectionParameters::parseURI('tcp://invalid:uri');
  181. }
  182. // ******************************************************************** //
  183. // ---- HELPER METHODS ------------------------------------------------ //
  184. // ******************************************************************** //
  185. /**
  186. * Returns a named array with the default connection parameters and their values.
  187. *
  188. * @return Array Default connection parameters.
  189. */
  190. protected function getDefaultParametersArray()
  191. {
  192. return array(
  193. 'scheme' => 'tcp',
  194. 'host' => '127.0.0.1',
  195. 'port' => 6379,
  196. 'timeout' => 5.0,
  197. );
  198. }
  199. /**
  200. * Returns an URI string representation of the specified connection parameters.
  201. *
  202. * @param Array $parameters Array of connection parameters.
  203. * @return String URI string.
  204. */
  205. protected function getParametersString(Array $parameters)
  206. {
  207. $defaults = $this->getDefaultParametersArray();
  208. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  209. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  210. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  211. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  212. $uriString = "$scheme://$host:$port/?";
  213. foreach ($parameters as $k => $v) {
  214. $uriString .= "$k=$v&";
  215. }
  216. return $uriString;
  217. }
  218. }