ConnectionParametersTest.php 5.8 KB

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