ConnectionParametersTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. * @todo ConnectionParameters::define();
  14. * @todo ConnectionParameters::undefine();
  15. */
  16. class ParametersTest 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['iterable_multibulk'], $parameters->iterable_multibulk);
  29. $this->assertEquals($defaults['timeout'], $parameters->timeout);
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testIsSet()
  35. {
  36. $parameters = new ConnectionParameters();
  37. $this->assertTrue(isset($parameters->scheme));
  38. $this->assertFalse(isset($parameters->unknown));
  39. }
  40. /**
  41. * @group disconnected
  42. */
  43. public function testUserDefinedParameters()
  44. {
  45. $parameters = new ConnectionParameters(array('port' => 7000, 'custom' => 'foobar'));
  46. $this->assertTrue(isset($parameters->scheme));
  47. $this->assertSame('tcp', $parameters->scheme);
  48. $this->assertTrue(isset($parameters->port));
  49. $this->assertSame(7000, $parameters->port);
  50. $this->assertTrue(isset($parameters->custom));
  51. $this->assertSame('foobar', $parameters->custom);
  52. $this->assertFalse(isset($parameters->unknown));
  53. $this->assertNull($parameters->unknown);
  54. }
  55. /**
  56. * @group disconnected
  57. */
  58. public function testConstructWithUriString()
  59. {
  60. $defaults = $this->getDefaultParametersArray();
  61. $overrides = array(
  62. 'port' => 7000,
  63. 'database' => 5,
  64. 'iterable_multibulk' => false,
  65. 'custom' => 'foobar',
  66. );
  67. $parameters = new ConnectionParameters($this->getParametersString($overrides));
  68. $this->assertEquals($defaults['scheme'], $parameters->scheme);
  69. $this->assertEquals($defaults['host'], $parameters->host);
  70. $this->assertEquals($overrides['port'], $parameters->port);
  71. $this->assertEquals($overrides['database'], $parameters->database);
  72. $this->assertEquals($overrides['iterable_multibulk'], $parameters->iterable_multibulk);
  73. $this->assertTrue(isset($parameters->custom));
  74. $this->assertEquals($overrides['custom'], $parameters->custom);
  75. $this->assertFalse(isset($parameters->unknown));
  76. $this->assertNull($parameters->unknown);
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testToArray()
  82. {
  83. $additional = array('port' => 7000, 'custom' => 'foobar');
  84. $parameters = new ConnectionParameters($additional);
  85. $this->assertEquals($this->getParametersArray($additional), $parameters->toArray());
  86. }
  87. /**
  88. * @group disconnected
  89. */
  90. public function testSerialization()
  91. {
  92. $parameters = new ConnectionParameters(array('port' => 7000, 'custom' => 'foobar'));
  93. $unserialized = unserialize(serialize($parameters));
  94. $this->assertEquals($parameters->scheme, $unserialized->scheme);
  95. $this->assertEquals($parameters->port, $unserialized->port);
  96. $this->assertTrue(isset($unserialized->custom));
  97. $this->assertEquals($parameters->custom, $unserialized->custom);
  98. $this->assertFalse(isset($unserialized->unknown));
  99. $this->assertNull($unserialized->unknown);
  100. }
  101. // ******************************************************************** //
  102. // ---- HELPER METHODS ------------------------------------------------ //
  103. // ******************************************************************** //
  104. /**
  105. * Returns a named array with the default connection parameters and their values.
  106. *
  107. * @return Array Default connection parameters.
  108. */
  109. protected function getDefaultParametersArray()
  110. {
  111. return array(
  112. 'scheme' => 'tcp',
  113. 'host' => '127.0.0.1',
  114. 'port' => 6379,
  115. 'timeout' => 5.0,
  116. 'iterable_multibulk' => false,
  117. );
  118. }
  119. /**
  120. * Returns a named array with the default connection parameters merged with
  121. * the specified additional parameters.
  122. *
  123. * @param Array $additional Additional connection parameters.
  124. * @return Array Connection parameters.
  125. */
  126. protected function getParametersArray(Array $additional)
  127. {
  128. return array_merge($this->getDefaultParametersArray(), $additional);
  129. }
  130. /**
  131. * Returns an URI string representation of the specified connection parameters.
  132. *
  133. * @param Array $parameters Array of connection parameters.
  134. * @return String URI string.
  135. */
  136. protected function getParametersString(Array $parameters)
  137. {
  138. $defaults = $this->getDefaultParametersArray();
  139. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  140. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  141. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  142. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  143. $uriString = "$scheme://$host:$port/?";
  144. foreach ($parameters as $k => $v) {
  145. $uriString .= "$k=$v&";
  146. }
  147. return $uriString;
  148. }
  149. }