ParametersTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 Parameters::define();
  14. * @todo Parameters::undefine();
  15. */
  16. class ParametersTest extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testDefaultValues()
  22. {
  23. $defaults = $this->getDefaultParametersArray();
  24. $parameters = new Parameters();
  25. $this->assertEquals($defaults['scheme'], $parameters->scheme);
  26. $this->assertEquals($defaults['host'], $parameters->host);
  27. $this->assertEquals($defaults['port'], $parameters->port);
  28. }
  29. /**
  30. * @group disconnected
  31. */
  32. public function testIsSet()
  33. {
  34. $parameters = new Parameters();
  35. $this->assertTrue(isset($parameters->scheme));
  36. $this->assertFalse(isset($parameters->unknown));
  37. }
  38. public function sharedTestsWithArrayParameters(Parameters $parameters)
  39. {
  40. $this->assertTrue(isset($parameters->scheme));
  41. $this->assertSame('tcp', $parameters->scheme);
  42. $this->assertTrue(isset($parameters->port));
  43. $this->assertSame(7000, $parameters->port);
  44. $this->assertTrue(isset($parameters->custom));
  45. $this->assertSame('foobar', $parameters->custom);
  46. $this->assertFalse(isset($parameters->unknown));
  47. $this->assertNull($parameters->unknown);
  48. }
  49. /**
  50. * @group disconnected
  51. */
  52. public function testConstructWithArrayParameters()
  53. {
  54. $parameters = new Parameters(array(
  55. 'port' => 7000,
  56. 'custom' => 'foobar',
  57. ));
  58. $this->sharedTestsWithArrayParameters($parameters);
  59. }
  60. /**
  61. * @group disconnected
  62. */
  63. public function testCreateWithArrayParameters()
  64. {
  65. $parameters = new Parameters(array(
  66. 'port' => 7000,
  67. 'custom' => 'foobar',
  68. ));
  69. $this->sharedTestsWithArrayParameters($parameters);
  70. }
  71. /**
  72. * @group disconnected
  73. */
  74. public function testCreateWithUriString()
  75. {
  76. $overrides = array(
  77. 'port' => 7000,
  78. 'database' => 5,
  79. 'custom' => 'foobar',
  80. );
  81. $uriString = $this->getParametersString($overrides);
  82. $parameters = Parameters::create($uriString);
  83. $this->sharedTestsWithArrayParameters($parameters);
  84. $this->assertEquals($overrides['database'], $parameters->database);
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testToArray()
  90. {
  91. $additional = array('port' => 7000, 'custom' => 'foobar');
  92. $parameters = new Parameters($additional);
  93. $this->assertEquals($this->getParametersArray($additional), $parameters->toArray());
  94. }
  95. /**
  96. * @group disconnected
  97. */
  98. public function testSerialization()
  99. {
  100. $parameters = new Parameters(array('port' => 7000, 'custom' => 'foobar'));
  101. $unserialized = unserialize(serialize($parameters));
  102. $this->assertEquals($parameters->scheme, $unserialized->scheme);
  103. $this->assertEquals($parameters->port, $unserialized->port);
  104. $this->assertTrue(isset($unserialized->custom));
  105. $this->assertEquals($parameters->custom, $unserialized->custom);
  106. $this->assertFalse(isset($unserialized->unknown));
  107. $this->assertNull($unserialized->unknown);
  108. }
  109. /**
  110. * @group disconnected
  111. */
  112. public function testParsingURI()
  113. {
  114. $uri = 'tcp://10.10.10.10:6400?timeout=0.5&persistent=1&database=5&password=secret';
  115. $expected = array(
  116. 'scheme' => 'tcp',
  117. 'host' => '10.10.10.10',
  118. 'port' => 6400,
  119. 'timeout' => '0.5',
  120. 'persistent' => '1',
  121. 'database' => '5',
  122. 'password' => 'secret',
  123. );
  124. $this->assertSame($expected, Parameters::parse($uri));
  125. }
  126. /**
  127. * @group disconnected
  128. */
  129. public function testParsingURIWithRedisScheme()
  130. {
  131. $uri = 'redis://:secret@10.10.10.10:6400/5?timeout=0.5&persistent=1';
  132. $expected = array(
  133. 'scheme' => 'redis',
  134. 'host' => '10.10.10.10',
  135. 'port' => 6400,
  136. 'timeout' => '0.5',
  137. 'persistent' => '1',
  138. 'password' => 'secret',
  139. 'database' => '5',
  140. );
  141. $parameters = Parameters::parse($uri);
  142. // TODO: parse_url() in PHP >= 5.6 returns an empty "user" entry in the
  143. // dictionary when no username has been provided in the URI string. This
  144. // actually makes sense, but let's keep the test ugly & simple for now.
  145. unset($parameters['user']);
  146. $this->assertSame($expected, $parameters);
  147. }
  148. /**
  149. * @group disconnected
  150. */
  151. public function testRedisSchemeOverridesPasswordAndDatabaseInQueryString()
  152. {
  153. $parameters = Parameters::parse('redis://:secret@10.10.10.10/5?password=ignored&database=4');
  154. $this->assertSame('secret', $parameters['password']);
  155. $this->assertSame('5', $parameters['database']);
  156. }
  157. /**
  158. * @group disconnected
  159. */
  160. public function testParsingURIWithUnixDomain()
  161. {
  162. $uri = 'unix:///tmp/redis.sock?timeout=0.5&persistent=1';
  163. $expected = array(
  164. 'scheme' => 'unix',
  165. 'host' => 'localhost',
  166. 'path' => '/tmp/redis.sock',
  167. 'timeout' => '0.5',
  168. 'persistent' => '1',
  169. );
  170. $this->assertSame($expected, Parameters::parse($uri));
  171. }
  172. /**
  173. * @group disconnected
  174. */
  175. public function testParsingURIWithIncompletePairInQueryString()
  176. {
  177. $uri = 'tcp://10.10.10.10?persistent=1&foo=&bar';
  178. $expected = array(
  179. 'scheme' => 'tcp',
  180. 'host' => '10.10.10.10',
  181. 'persistent' => '1',
  182. 'foo' => '',
  183. 'bar' => '',
  184. );
  185. $this->assertSame($expected, Parameters::parse($uri));
  186. }
  187. /**
  188. * @group disconnected
  189. */
  190. public function testParsingURIWithMoreThanOneEqualSignInQueryStringPairValue()
  191. {
  192. $uri = 'tcp://10.10.10.10?foobar=a=b=c&persistent=1';
  193. $expected = array(
  194. 'scheme' => 'tcp',
  195. 'host' => '10.10.10.10',
  196. 'foobar' => 'a=b=c',
  197. 'persistent' => '1',
  198. );
  199. $this->assertSame($expected, Parameters::parse($uri));
  200. }
  201. /**
  202. * @group disconnected
  203. */
  204. public function testParsingURIWhenQueryStringHasBracketsInFieldnames()
  205. {
  206. $uri = 'tcp://10.10.10.10?persistent=1&metavars[]=foo&metavars[]=hoge';
  207. $expected = array(
  208. 'scheme' => 'tcp',
  209. 'host' => '10.10.10.10',
  210. 'persistent' => '1',
  211. 'metavars' => array('foo', 'hoge'),
  212. );
  213. $this->assertSame($expected, Parameters::parse($uri));
  214. }
  215. /**
  216. * @group disconnected
  217. * @expectedException \InvalidArgumentException
  218. * @expectedExceptionMessage Invalid parameters URI: tcp://invalid:uri
  219. */
  220. public function testParsingURIThrowOnInvalidURI()
  221. {
  222. Parameters::parse('tcp://invalid:uri');
  223. }
  224. // ******************************************************************** //
  225. // ---- HELPER METHODS ------------------------------------------------ //
  226. // ******************************************************************** //
  227. /**
  228. * Returns a named array with the default connection parameters and their values.
  229. *
  230. * @return array Default connection parameters.
  231. */
  232. protected function getDefaultParametersArray()
  233. {
  234. return array(
  235. 'scheme' => 'tcp',
  236. 'host' => '127.0.0.1',
  237. 'port' => 6379,
  238. );
  239. }
  240. /**
  241. * Returns an URI string representation of the specified connection parameters.
  242. *
  243. * @param array $parameters array of connection parameters.
  244. *
  245. * @return string URI string.
  246. */
  247. protected function getParametersString(array $parameters)
  248. {
  249. $defaults = $this->getDefaultParametersArray();
  250. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  251. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  252. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  253. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  254. $uriString = "$scheme://$host:$port/?";
  255. foreach ($parameters as $k => $v) {
  256. $uriString .= "$k=$v&";
  257. }
  258. return $uriString;
  259. }
  260. }