ParametersTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. $defaults = $this->getDefaultParametersArray();
  78. $overrides = array(
  79. 'port' => 7000,
  80. 'database' => 5,
  81. 'custom' => 'foobar',
  82. );
  83. $uriString = $this->getParametersString($overrides);
  84. $parameters = Parameters::create($uriString);
  85. $this->sharedTestsWithArrayParameters($parameters);
  86. $this->assertEquals($overrides['database'], $parameters->database);
  87. }
  88. /**
  89. * @group disconnected
  90. */
  91. public function testToArray()
  92. {
  93. $additional = array('port' => 7000, 'custom' => 'foobar');
  94. $parameters = new Parameters($additional);
  95. $this->assertEquals($this->getParametersArray($additional), $parameters->toArray());
  96. }
  97. /**
  98. * @group disconnected
  99. */
  100. public function testSerialization()
  101. {
  102. $parameters = new Parameters(array('port' => 7000, 'custom' => 'foobar'));
  103. $unserialized = unserialize(serialize($parameters));
  104. $this->assertEquals($parameters->scheme, $unserialized->scheme);
  105. $this->assertEquals($parameters->port, $unserialized->port);
  106. $this->assertTrue(isset($unserialized->custom));
  107. $this->assertEquals($parameters->custom, $unserialized->custom);
  108. $this->assertFalse(isset($unserialized->unknown));
  109. $this->assertNull($unserialized->unknown);
  110. }
  111. /**
  112. * @group disconnected
  113. */
  114. public function testParsingURI()
  115. {
  116. $uri = 'tcp://10.10.10.10:6400?timeout=0.5&persistent=1';
  117. $expected = array(
  118. 'scheme' => 'tcp',
  119. 'host' => '10.10.10.10',
  120. 'port' => 6400,
  121. 'timeout' => '0.5',
  122. 'persistent' => '1',
  123. );
  124. $this->assertSame($expected, Parameters::parse($uri));
  125. }
  126. /**
  127. * @group disconnected
  128. */
  129. public function testParsingUnixDomainURI()
  130. {
  131. $uri = 'unix:///tmp/redis.sock?timeout=0.5&persistent=1';
  132. $expected = array(
  133. 'scheme' => 'unix',
  134. 'host' => 'localhost',
  135. 'path' => '/tmp/redis.sock',
  136. 'timeout' => '0.5',
  137. 'persistent' => '1',
  138. );
  139. $this->assertSame($expected, Parameters::parse($uri));
  140. }
  141. /**
  142. * @group disconnected
  143. */
  144. public function testParsingURIWithIncompletePairInQueryString()
  145. {
  146. $uri = 'tcp://10.10.10.10?persistent=1&foo=&bar';
  147. $expected = array(
  148. 'scheme' => 'tcp',
  149. 'host' => '10.10.10.10',
  150. 'persistent' => '1',
  151. 'foo' => '',
  152. 'bar' => '',
  153. );
  154. $this->assertSame($expected, Parameters::parse($uri));
  155. }
  156. /**
  157. * @group disconnected
  158. */
  159. public function testParsingURIWithMoreThanOneEqualSignInQueryStringPairValue()
  160. {
  161. $uri = 'tcp://10.10.10.10?foobar=a=b=c&persistent=1';
  162. $expected = array(
  163. 'scheme' => 'tcp',
  164. 'host' => '10.10.10.10',
  165. 'foobar' => 'a=b=c',
  166. 'persistent' => '1',
  167. );
  168. $this->assertSame($expected, Parameters::parse($uri));
  169. }
  170. /**
  171. * @group disconnected
  172. */
  173. public function testParsingURIWhenQueryStringHasBracketsInFieldnames()
  174. {
  175. $uri = 'tcp://10.10.10.10?persistent=1&metavars[]=foo&metavars[]=hoge';
  176. $expected = array(
  177. 'scheme' => 'tcp',
  178. 'host' => '10.10.10.10',
  179. 'persistent' => '1',
  180. 'metavars' => array('foo', 'hoge'),
  181. );
  182. $this->assertSame($expected, Parameters::parse($uri));
  183. }
  184. /**
  185. * @group disconnected
  186. * @expectedException \InvalidArgumentException
  187. * @expectedExceptionMessage Invalid parameters URI: tcp://invalid:uri
  188. */
  189. public function testParsingURIThrowOnInvalidURI()
  190. {
  191. Parameters::parse('tcp://invalid:uri');
  192. }
  193. // ******************************************************************** //
  194. // ---- HELPER METHODS ------------------------------------------------ //
  195. // ******************************************************************** //
  196. /**
  197. * Returns a named array with the default connection parameters and their values.
  198. *
  199. * @return array Default connection parameters.
  200. */
  201. protected function getDefaultParametersArray()
  202. {
  203. return array(
  204. 'scheme' => 'tcp',
  205. 'host' => '127.0.0.1',
  206. 'port' => 6379,
  207. 'timeout' => 5.0,
  208. );
  209. }
  210. /**
  211. * Returns an URI string representation of the specified connection parameters.
  212. *
  213. * @param array $parameters array of connection parameters.
  214. * @return String URI string.
  215. */
  216. protected function getParametersString(array $parameters)
  217. {
  218. $defaults = $this->getDefaultParametersArray();
  219. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  220. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  221. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  222. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  223. $uriString = "$scheme://$host:$port/?";
  224. foreach ($parameters as $k => $v) {
  225. $uriString .= "$k=$v&";
  226. }
  227. return $uriString;
  228. }
  229. }