PredisTestCase.php 8.1 KB

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