ParametersTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 testRedisSchemeOverridesPasswordAndDatabaseInQueryString()
  153. {
  154. $parameters = Parameters::parse('redis://:secret@10.10.10.10/5?password=ignored&database=4');
  155. $this->assertSame('secret', $parameters['password']);
  156. $this->assertSame('5', $parameters['database']);
  157. }
  158. /**
  159. * @group disconnected
  160. */
  161. public function testParsingURIWithUnixDomain()
  162. {
  163. $uri = 'unix:///tmp/redis.sock?timeout=0.5&persistent=1';
  164. $expected = array(
  165. 'scheme' => 'unix',
  166. 'host' => 'localhost',
  167. 'path' => '/tmp/redis.sock',
  168. 'timeout' => '0.5',
  169. 'persistent' => '1',
  170. );
  171. $this->assertSame($expected, Parameters::parse($uri));
  172. }
  173. /**
  174. * @group disconnected
  175. */
  176. public function testParsingURIWithIncompletePairInQueryString()
  177. {
  178. $uri = 'tcp://10.10.10.10?persistent=1&foo=&bar';
  179. $expected = array(
  180. 'scheme' => 'tcp',
  181. 'host' => '10.10.10.10',
  182. 'persistent' => '1',
  183. 'foo' => '',
  184. 'bar' => '',
  185. );
  186. $this->assertSame($expected, Parameters::parse($uri));
  187. }
  188. /**
  189. * @group disconnected
  190. */
  191. public function testParsingURIWithMoreThanOneEqualSignInQueryStringPairValue()
  192. {
  193. $uri = 'tcp://10.10.10.10?foobar=a=b=c&persistent=1';
  194. $expected = array(
  195. 'scheme' => 'tcp',
  196. 'host' => '10.10.10.10',
  197. 'foobar' => 'a=b=c',
  198. 'persistent' => '1',
  199. );
  200. $this->assertSame($expected, Parameters::parse($uri));
  201. }
  202. /**
  203. * @group disconnected
  204. */
  205. public function testParsingURIWhenQueryStringHasBracketsInFieldnames()
  206. {
  207. $uri = 'tcp://10.10.10.10?persistent=1&metavars[]=foo&metavars[]=hoge';
  208. $expected = array(
  209. 'scheme' => 'tcp',
  210. 'host' => '10.10.10.10',
  211. 'persistent' => '1',
  212. 'metavars' => array('foo', 'hoge'),
  213. );
  214. $this->assertSame($expected, Parameters::parse($uri));
  215. }
  216. /**
  217. * @group disconnected
  218. * @expectedException \InvalidArgumentException
  219. * @expectedExceptionMessage Invalid parameters URI: tcp://invalid:uri
  220. */
  221. public function testParsingURIThrowOnInvalidURI()
  222. {
  223. Parameters::parse('tcp://invalid:uri');
  224. }
  225. // ******************************************************************** //
  226. // ---- HELPER METHODS ------------------------------------------------ //
  227. // ******************************************************************** //
  228. /**
  229. * Returns a named array with the default connection parameters and their values.
  230. *
  231. * @return array Default connection parameters.
  232. */
  233. protected function getDefaultParametersArray()
  234. {
  235. return array(
  236. 'scheme' => 'tcp',
  237. 'host' => '127.0.0.1',
  238. 'port' => 6379,
  239. 'timeout' => 5.0,
  240. );
  241. }
  242. /**
  243. * Returns an URI string representation of the specified connection parameters.
  244. *
  245. * @param array $parameters array of connection parameters.
  246. *
  247. * @return string URI string.
  248. */
  249. protected function getParametersString(array $parameters)
  250. {
  251. $defaults = $this->getDefaultParametersArray();
  252. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  253. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  254. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  255. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  256. $uriString = "$scheme://$host:$port/?";
  257. foreach ($parameters as $k => $v) {
  258. $uriString .= "$k=$v&";
  259. }
  260. return $uriString;
  261. }
  262. }