ParametersTest.php 9.6 KB

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