ParametersTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 testParsingURIWithRedisSchemeMustPreserveRemainderOfPath()
  152. {
  153. $uri = 'redis://10.10.10.10/5/rest/of/path';
  154. $expected = array(
  155. 'scheme' => 'redis',
  156. 'host' => '10.10.10.10',
  157. 'path' => '/rest/of/path',
  158. 'database' => '5',
  159. );
  160. $parameters = Parameters::parse($uri);
  161. $this->assertSame($expected, $parameters);
  162. }
  163. /**
  164. * @group disconnected
  165. */
  166. public function testRedisSchemeOverridesPasswordAndDatabaseInQueryString()
  167. {
  168. $parameters = Parameters::parse('redis://:secret@10.10.10.10/5?password=ignored&database=4');
  169. $this->assertSame('secret', $parameters['password']);
  170. $this->assertSame('5', $parameters['database']);
  171. }
  172. /**
  173. * @group disconnected
  174. */
  175. public function testParsingURIWithUnixDomainSocket()
  176. {
  177. $uri = 'unix:///tmp/redis.sock?timeout=0.5&persistent=1';
  178. $expected = array(
  179. 'scheme' => 'unix',
  180. 'path' => '/tmp/redis.sock',
  181. 'timeout' => '0.5',
  182. 'persistent' => '1',
  183. );
  184. $this->assertSame($expected, Parameters::parse($uri));
  185. }
  186. /**
  187. * @group disconnected
  188. */
  189. public function testParsingURIWithUnixDomainSocketOldWay()
  190. {
  191. $uri = 'unix:/tmp/redis.sock?timeout=0.5&persistent=1';
  192. $expected = array(
  193. 'scheme' => 'unix',
  194. 'path' => '/tmp/redis.sock',
  195. 'timeout' => '0.5',
  196. 'persistent' => '1',
  197. );
  198. $this->assertSame($expected, Parameters::parse($uri));
  199. }
  200. /**
  201. * @group disconnected
  202. */
  203. public function testParsingURIWithIncompletePairInQueryString()
  204. {
  205. $uri = 'tcp://10.10.10.10?persistent=1&foo=&bar';
  206. $expected = array(
  207. 'scheme' => 'tcp',
  208. 'host' => '10.10.10.10',
  209. 'persistent' => '1',
  210. 'foo' => '',
  211. 'bar' => '',
  212. );
  213. $this->assertSame($expected, Parameters::parse($uri));
  214. }
  215. /**
  216. * @group disconnected
  217. */
  218. public function testParsingURIWithMoreThanOneEqualSignInQueryStringPairValue()
  219. {
  220. $uri = 'tcp://10.10.10.10?foobar=a=b=c&persistent=1';
  221. $expected = array(
  222. 'scheme' => 'tcp',
  223. 'host' => '10.10.10.10',
  224. 'foobar' => 'a=b=c',
  225. 'persistent' => '1',
  226. );
  227. $this->assertSame($expected, Parameters::parse($uri));
  228. }
  229. /**
  230. * @group disconnected
  231. */
  232. public function testParsingURIWhenQueryStringHasBracketsInFieldnames()
  233. {
  234. $uri = 'tcp://10.10.10.10?persistent=1&metavars[]=foo&metavars[]=hoge';
  235. $expected = array(
  236. 'scheme' => 'tcp',
  237. 'host' => '10.10.10.10',
  238. 'persistent' => '1',
  239. 'metavars' => array('foo', 'hoge'),
  240. );
  241. $this->assertSame($expected, Parameters::parse($uri));
  242. }
  243. /**
  244. * @group disconnected
  245. */
  246. public function testParsingURIWithEmbeddedIPV6AddressShouldStripBracketsFromHost()
  247. {
  248. $expected = array('scheme' => 'tcp', 'host' => '::1', 'port' => 7000);
  249. $this->assertSame($expected, Parameters::parse('tcp://[::1]:7000'));
  250. $expected = array('scheme' => 'tcp', 'host' => '2001:db8:0:f101::1', 'port' => 7000);
  251. $this->assertSame($expected, Parameters::parse('tcp://[2001:db8:0:f101::1]:7000'));
  252. }
  253. /**
  254. * @group disconnected
  255. * @expectedException \InvalidArgumentException
  256. * @expectedExceptionMessage Invalid parameters URI: tcp://invalid:uri
  257. */
  258. public function testParsingURIThrowOnInvalidURI()
  259. {
  260. Parameters::parse('tcp://invalid:uri');
  261. }
  262. /**
  263. * @group disconnected
  264. */
  265. public function testToStringWithDefaultParameters()
  266. {
  267. $parameters = new Parameters();
  268. $this->assertSame('tcp://127.0.0.1:6379', (string) $parameters);
  269. }
  270. /**
  271. * @group disconnected
  272. */
  273. public function testToStringWithUnixScheme()
  274. {
  275. $uri = 'unix:/path/to/redis.sock';
  276. $parameters = Parameters::create("$uri?foo=bar");
  277. $this->assertSame($uri, (string) $parameters);
  278. }
  279. /**
  280. * @group disconnected
  281. */
  282. public function testToStringWithIPv4()
  283. {
  284. $uri = 'tcp://127.0.0.1:6379';
  285. $parameters = Parameters::create("$uri?foo=bar");
  286. $this->assertSame($uri, (string) $parameters);
  287. }
  288. /**
  289. * @group disconnected
  290. */
  291. public function testToStringWithIPv6()
  292. {
  293. $uri = 'tcp://[::1]:6379';
  294. $parameters = Parameters::create("$uri?foo=bar");
  295. $this->assertSame($uri, (string) $parameters);
  296. }
  297. // ******************************************************************** //
  298. // ---- HELPER METHODS ------------------------------------------------ //
  299. // ******************************************************************** //
  300. /**
  301. * Returns a named array with the default connection parameters and their values.
  302. *
  303. * @return array Default connection parameters.
  304. */
  305. protected function getDefaultParametersArray()
  306. {
  307. return array(
  308. 'scheme' => 'tcp',
  309. 'host' => '127.0.0.1',
  310. 'port' => 6379,
  311. );
  312. }
  313. /**
  314. * Returns an URI string representation of the specified connection parameters.
  315. *
  316. * @param array $parameters array of connection parameters.
  317. *
  318. * @return string URI string.
  319. */
  320. protected function getParametersString(array $parameters)
  321. {
  322. $defaults = $this->getDefaultParametersArray();
  323. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  324. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  325. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  326. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  327. $uriString = "$scheme://$host:$port/?";
  328. foreach ($parameters as $k => $v) {
  329. $uriString .= "$k=$v&";
  330. }
  331. return $uriString;
  332. }
  333. }