PredisTestCase.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. use Predis\Client;
  11. use Predis\Command;
  12. use Predis\Connection;
  13. use Predis\Profile;
  14. /**
  15. * Base test case class for the Predis test suite.
  16. */
  17. abstract class PredisTestCase extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * Verifies that a Redis command is a valid Predis\Command\CommandInterface
  21. * instance with the specified ID and command arguments.
  22. *
  23. * @param string|CommandInterface $command Expected command or command ID.
  24. * @param array $arguments Expected command arguments.
  25. */
  26. public function isRedisCommand($command = null, array $arguments = null)
  27. {
  28. return new RedisCommandConstraint($command, $arguments);
  29. }
  30. /**
  31. * Verifies that a Redis command is a valid Predis\Command\CommandInterface
  32. * instance with the specified ID and command arguments. The comparison does
  33. * not check for identity when passing a Predis\Command\CommandInterface
  34. * instance for $expected.
  35. *
  36. * @param array|string|CommandInterface $expected Expected command.
  37. * @param mixed $actual Actual command.
  38. */
  39. public function assertRedisCommand($expected, $actual)
  40. {
  41. if (is_array($expected)) {
  42. @list($command, $arguments) = $expected;
  43. } else {
  44. $command = $expected;
  45. $arguments = null;
  46. }
  47. $this->assertThat($actual, new RedisCommandConstraint($command, $arguments));
  48. }
  49. /**
  50. * Asserts that two arrays have the same values, even if with different order.
  51. *
  52. * @param array $expected Expected array.
  53. * @param array $actual Actual array.
  54. */
  55. public function assertSameValues(array $expected, array $actual)
  56. {
  57. $this->assertThat($actual, new ArrayHasSameValuesConstraint($expected));
  58. }
  59. /**
  60. * Returns a named array with the default connection parameters and their values.
  61. *
  62. * @return array Default connection parameters.
  63. */
  64. protected function getDefaultParametersArray()
  65. {
  66. return array(
  67. 'scheme' => 'tcp',
  68. 'host' => REDIS_SERVER_HOST,
  69. 'port' => REDIS_SERVER_PORT,
  70. 'database' => REDIS_SERVER_DBNUM,
  71. );
  72. }
  73. /**
  74. * Returns a named array with the default client options and their values.
  75. *
  76. * @return array Default connection parameters.
  77. */
  78. protected function getDefaultOptionsArray()
  79. {
  80. return array(
  81. 'profile' => REDIS_SERVER_VERSION,
  82. );
  83. }
  84. /**
  85. * Returns a named array with the default connection parameters merged with
  86. * the specified additional parameters.
  87. *
  88. * @param array $additional Additional connection parameters.
  89. * @return array Connection parameters.
  90. */
  91. protected function getParametersArray(array $additional)
  92. {
  93. return array_merge($this->getDefaultParametersArray(), $additional);
  94. }
  95. /**
  96. * Returns a new instance of connection parameters.
  97. *
  98. * @param array $additional Additional connection parameters.
  99. * @return Connection\Parameters Default connection parameters.
  100. */
  101. protected function getParameters($additional = array())
  102. {
  103. $parameters = array_merge($this->getDefaultParametersArray(), $additional);
  104. $parameters = new Connection\Parameters($parameters);
  105. return $parameters;
  106. }
  107. /**
  108. * Returns a new instance of server profile.
  109. *
  110. * @param array $additional Additional connection parameters.
  111. * @return Profile\ProfileInterface
  112. */
  113. protected function getProfile($version = null)
  114. {
  115. return Profile\Factory::get($version ?: REDIS_SERVER_VERSION);
  116. }
  117. /**
  118. * Returns a new client instance.
  119. *
  120. * @param bool $connect Flush selected database before returning the client.
  121. * @return Client
  122. */
  123. protected function createClient(array $parameters = null, array $options = null, $flushdb = true)
  124. {
  125. $parameters = array_merge(
  126. $this->getDefaultParametersArray(),
  127. $parameters ?: array()
  128. );
  129. $options = array_merge(
  130. array(
  131. 'profile' => $this->getProfile(),
  132. ),
  133. $options ?: array()
  134. );
  135. $client = new Client($parameters, $options);
  136. $client->connect();
  137. if ($flushdb) {
  138. $client->flushdb();
  139. }
  140. return $client;
  141. }
  142. /**
  143. * @param string $expectedVersion Expected redis version
  144. * @param string $operator Comparison operator.
  145. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  146. */
  147. protected function executeOnRedisVersion($expectedVersion, $operator, $callback)
  148. {
  149. $client = $this->createClient(null, null, true);
  150. $info = array_change_key_case($client->info());
  151. if (isset($info['server']['redis_version'])) {
  152. // Redis >= 2.6
  153. $version = $info['server']['redis_version'];
  154. } else if (isset($info['redis_version'])) {
  155. // Redis < 2.6
  156. $version = $info['redis_version'];
  157. } else {
  158. throw new RuntimeException('Unable to retrieve server info');
  159. }
  160. $comparation = version_compare($version, $expectedVersion);
  161. if ($match = eval("return $comparation $operator 0;")) {
  162. call_user_func($callback, $this, $version);
  163. }
  164. return $match;
  165. }
  166. /**
  167. * @param string $expectedVersion Expected redis version
  168. * @param string $operator Comparison operator.
  169. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  170. */
  171. protected function executeOnProfileVersion($expectedVersion, $operator, $callback)
  172. {
  173. $profile = $this->getProfile();
  174. $comparation = version_compare($profile->getVersion(), $expectedVersion);
  175. if ($match = eval("return $comparation $operator 0;")) {
  176. call_user_func($callback, $this, $version);
  177. }
  178. return $match;
  179. }
  180. /**
  181. * @param string $expectedVersion Expected redis version.
  182. * @param string $message Optional message.
  183. * @param bool $remote Based on local profile or remote redis version.
  184. * @throws RuntimeException when unable to retrieve server info or redis version
  185. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  186. */
  187. public function markTestSkippedOnRedisVersionBelow($expectedVersion, $message = '', $remote = true)
  188. {
  189. $callback = function ($test, $version) use ($message, $expectedVersion) {
  190. $test->markTestSkipped($message ?: "Test requires Redis $expectedVersion, current is $version.");
  191. };
  192. $method = $remote ? 'executeOnRedisVersion' : 'executeOnProfileVersion';
  193. $this->$method($expectedVersion, '<', $callback);
  194. }
  195. /**
  196. * Sleep the test case with microseconds resolution.
  197. *
  198. * @param float $seconds Seconds to sleep.
  199. */
  200. protected function sleep($seconds)
  201. {
  202. usleep($seconds * 1000000);
  203. }
  204. }