ParametersTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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';
  116. $expected = array(
  117. 'scheme' => 'tcp',
  118. 'host' => '10.10.10.10',
  119. 'port' => 6400,
  120. 'timeout' => '0.5',
  121. 'persistent' => '1',
  122. );
  123. $this->assertSame($expected, Parameters::parse($uri));
  124. }
  125. /**
  126. * @group disconnected
  127. */
  128. public function testParsingUnixDomainURI()
  129. {
  130. $uri = 'unix:///tmp/redis.sock?timeout=0.5&persistent=1';
  131. $expected = array(
  132. 'scheme' => 'unix',
  133. 'host' => 'localhost',
  134. 'path' => '/tmp/redis.sock',
  135. 'timeout' => '0.5',
  136. 'persistent' => '1',
  137. );
  138. $this->assertSame($expected, Parameters::parse($uri));
  139. }
  140. /**
  141. * @group disconnected
  142. */
  143. public function testParsingURIWithIncompletePairInQueryString()
  144. {
  145. $uri = 'tcp://10.10.10.10?persistent=1&foo=&bar';
  146. $expected = array(
  147. 'scheme' => 'tcp',
  148. 'host' => '10.10.10.10',
  149. 'persistent' => '1',
  150. 'foo' => '',
  151. 'bar' => '',
  152. );
  153. $this->assertSame($expected, Parameters::parse($uri));
  154. }
  155. /**
  156. * @group disconnected
  157. */
  158. public function testParsingURIWithMoreThanOneEqualSignInQueryStringPairValue()
  159. {
  160. $uri = 'tcp://10.10.10.10?foobar=a=b=c&persistent=1';
  161. $expected = array(
  162. 'scheme' => 'tcp',
  163. 'host' => '10.10.10.10',
  164. 'foobar' => 'a=b=c',
  165. 'persistent' => '1',
  166. );
  167. $this->assertSame($expected, Parameters::parse($uri));
  168. }
  169. /**
  170. * @group disconnected
  171. */
  172. public function testParsingURIWhenQueryStringHasBracketsInFieldnames()
  173. {
  174. $uri = 'tcp://10.10.10.10?persistent=1&metavars[]=foo&metavars[]=hoge';
  175. $expected = array(
  176. 'scheme' => 'tcp',
  177. 'host' => '10.10.10.10',
  178. 'persistent' => '1',
  179. 'metavars' => array('foo', 'hoge'),
  180. );
  181. $this->assertSame($expected, Parameters::parse($uri));
  182. }
  183. /**
  184. * @group disconnected
  185. * @expectedException \InvalidArgumentException
  186. * @expectedExceptionMessage Invalid parameters URI: tcp://invalid:uri
  187. */
  188. public function testParsingURIThrowOnInvalidURI()
  189. {
  190. Parameters::parse('tcp://invalid:uri');
  191. }
  192. // ******************************************************************** //
  193. // ---- HELPER METHODS ------------------------------------------------ //
  194. // ******************************************************************** //
  195. /**
  196. * Returns a named array with the default connection parameters and their values.
  197. *
  198. * @return array Default connection parameters.
  199. */
  200. protected function getDefaultParametersArray()
  201. {
  202. return array(
  203. 'scheme' => 'tcp',
  204. 'host' => '127.0.0.1',
  205. 'port' => 6379,
  206. 'timeout' => 5.0,
  207. );
  208. }
  209. /**
  210. * Returns an URI string representation of the specified connection parameters.
  211. *
  212. * @param array $parameters array of connection parameters.
  213. * @return String URI string.
  214. */
  215. protected function getParametersString(array $parameters)
  216. {
  217. $defaults = $this->getDefaultParametersArray();
  218. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  219. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  220. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  221. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  222. $uriString = "$scheme://$host:$port/?";
  223. foreach ($parameters as $k => $v) {
  224. $uriString .= "$k=$v&";
  225. }
  226. return $uriString;
  227. }
  228. }