ConnectionParametersTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. * @todo ConnectionParameters::define();
  14. * @todo ConnectionParameters::undefine();
  15. */
  16. class ConnectionParametersTest extends StandardTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testDefaultValues()
  22. {
  23. $defaults = $this->getDefaultParametersArray();
  24. $parameters = new ConnectionParameters();
  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['throw_errors'], $parameters->throw_errors);
  29. $this->assertEquals($defaults['iterable_multibulk'], $parameters->iterable_multibulk);
  30. $this->assertEquals($defaults['connection_async'], $parameters->connection_async);
  31. $this->assertEquals($defaults['connection_persistent'], $parameters->connection_persistent);
  32. $this->assertEquals($defaults['connection_timeout'], $parameters->connection_timeout);
  33. $this->assertEquals($defaults['read_write_timeout'], $parameters->read_write_timeout);
  34. $this->assertEquals($defaults['database'], $parameters->database);
  35. $this->assertEquals($defaults['password'], $parameters->password);
  36. $this->assertEquals($defaults['alias'], $parameters->alias);
  37. $this->assertEquals($defaults['weight'], $parameters->weight);
  38. $this->assertEquals($defaults['path'], $parameters->path);
  39. }
  40. /**
  41. * @group disconnected
  42. */
  43. public function testIsSet()
  44. {
  45. $parameters = new ConnectionParameters();
  46. $this->assertTrue(isset($parameters->scheme));
  47. $this->assertFalse(isset($parameters->unknown));
  48. }
  49. /**
  50. * @group disconnected
  51. */
  52. public function testIsSetByUser()
  53. {
  54. $parameters = new ConnectionParameters(array('port' => 7000, 'custom' => 'foobar'));
  55. $this->assertTrue(isset($parameters->scheme));
  56. $this->assertFalse($parameters->isSetByUser('scheme'));
  57. $this->assertTrue(isset($parameters->port));
  58. $this->assertTrue($parameters->isSetByUser('port'));
  59. $this->assertTrue(isset($parameters->custom));
  60. $this->assertTrue($parameters->isSetByUser('custom'));
  61. $this->assertFalse(isset($parameters->unknown));
  62. $this->assertFalse($parameters->isSetByUser('unknown'));
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testConstructWithUriString()
  68. {
  69. $defaults = $this->getDefaultParametersArray();
  70. $overrides = array(
  71. 'port' => 7000,
  72. 'database' => 5,
  73. 'throw_errors' => false,
  74. 'custom' => 'foobar',
  75. );
  76. $parameters = new ConnectionParameters($this->getParametersString($overrides));
  77. $this->assertEquals($defaults['scheme'], $parameters->scheme);
  78. $this->assertEquals($defaults['host'], $parameters->host);
  79. $this->assertEquals($overrides['port'], $parameters->port);
  80. $this->assertEquals($overrides['database'], $parameters->database);
  81. $this->assertEquals($overrides['throw_errors'], $parameters->throw_errors);
  82. $this->assertTrue(isset($parameters->custom));
  83. $this->assertTrue($parameters->isSetByUser('custom'));
  84. $this->assertEquals($overrides['custom'], $parameters->custom);
  85. $this->assertFalse(isset($parameters->unknown));
  86. $this->assertFalse($parameters->isSetByUser('unknown'));
  87. }
  88. /**
  89. * @group disconnected
  90. */
  91. public function testToArray()
  92. {
  93. $additional = array('port' => 7000, 'custom' => 'foobar');
  94. $parameters = new ConnectionParameters($additional);
  95. $this->assertEquals($this->getParametersArray($additional), $parameters->toArray());
  96. }
  97. /**
  98. * @group disconnected
  99. */
  100. public function testToString()
  101. {
  102. $uri = 'tcp://localhost:7000/?database=15&custom=foobar&throw_errors=0';
  103. $parameters = new ConnectionParameters($uri);
  104. $this->assertEquals($uri, (string) $parameters);
  105. }
  106. /**
  107. * @group disconnected
  108. * @todo Does it actually make sense?
  109. */
  110. public function testToStringOmitPassword()
  111. {
  112. $uri = 'tcp://localhost:7000/?database=15&custom=foobar&throw_errors=0';
  113. $parameters = new ConnectionParameters($uri . '&password=foobar');
  114. $this->assertEquals($uri, (string) $parameters);
  115. }
  116. /**
  117. * @group disconnected
  118. */
  119. public function testSerialization()
  120. {
  121. $parameters = new ConnectionParameters(array('port' => 7000, 'custom' => 'foobar'));
  122. $unserialized = unserialize(serialize($parameters));
  123. $this->assertEquals($parameters->scheme, $unserialized->scheme);
  124. $this->assertEquals($parameters->port, $unserialized->port);
  125. $this->assertTrue(isset($unserialized->custom));
  126. $this->assertTrue($unserialized->isSetByUser('custom'));
  127. $this->assertEquals($parameters->custom, $unserialized->custom);
  128. $this->assertFalse(isset($unserialized->unknown));
  129. $this->assertFalse($unserialized->isSetByUser('unknown'));
  130. }
  131. // ******************************************************************** //
  132. // ---- HELPER METHODS ------------------------------------------------ //
  133. // ******************************************************************** //
  134. /**
  135. * Returns a named array with the default connection parameters and their values.
  136. *
  137. * @return Array Default connection parameters.
  138. */
  139. protected function getDefaultParametersArray()
  140. {
  141. return array(
  142. 'scheme' => 'tcp',
  143. 'host' => '127.0.0.1',
  144. 'port' => 6379,
  145. 'database' => null,
  146. 'password' => null,
  147. 'connection_async' => false,
  148. 'connection_persistent' => false,
  149. 'connection_timeout' => 5.0,
  150. 'read_write_timeout' => null,
  151. 'alias' => null,
  152. 'weight' => null,
  153. 'path' => null,
  154. 'iterable_multibulk' => false,
  155. 'throw_errors' => true,
  156. );
  157. }
  158. /**
  159. * Returns a named array with the default connection parameters merged with
  160. * the specified additional parameters.
  161. *
  162. * @param Array $additional Additional connection parameters.
  163. * @return Array Connection parameters.
  164. */
  165. protected function getParametersArray(Array $additional)
  166. {
  167. return array_merge($this->getDefaultParametersArray(), $additional);
  168. }
  169. /**
  170. * Returns an URI string representation of the specified connection parameters.
  171. *
  172. * @param Array $parameters Array of connection parameters.
  173. * @return String URI string.
  174. */
  175. protected function getParametersString(Array $parameters)
  176. {
  177. $defaults = $this->getDefaultParametersArray();
  178. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  179. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  180. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  181. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  182. $uriString = "$scheme://$host:$port/?";
  183. foreach ($parameters as $k => $v) {
  184. $uriString .= "$k=$v&";
  185. }
  186. return $uriString;
  187. }
  188. }