PredisTestCase.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. protected $redisServerVersion = null;
  20. /**
  21. * Sleep the test case with microseconds resolution.
  22. *
  23. * @param float $seconds Seconds to sleep.
  24. */
  25. protected function sleep($seconds)
  26. {
  27. usleep($seconds * 1000000);
  28. }
  29. /**
  30. * Returns if the current runtime is HHVM.
  31. *
  32. * @return bool
  33. */
  34. protected function isHHVM()
  35. {
  36. return defined('HHVM_VERSION');
  37. }
  38. /**
  39. * Verifies that a Redis command is a valid Predis\Command\CommandInterface
  40. * instance with the specified ID and command arguments.
  41. *
  42. * @param string|Command\CommandInterface $command Expected command or command ID.
  43. * @param array $arguments Expected command arguments.
  44. *
  45. * @return RedisCommandConstraint
  46. */
  47. public function isRedisCommand($command = null, array $arguments = null)
  48. {
  49. return new RedisCommandConstraint($command, $arguments);
  50. }
  51. /**
  52. * Verifies that a Redis command is a valid Predis\Command\CommandInterface
  53. * instance with the specified ID and command arguments. The comparison does
  54. * not check for identity when passing a Predis\Command\CommandInterface
  55. * instance for $expected.
  56. *
  57. * @param array|string|Command\CommandInterface $expected Expected command.
  58. * @param mixed $actual Actual command.
  59. * @param string $message Optional assertion message.
  60. */
  61. public function assertRedisCommand($expected, $actual, $message = '')
  62. {
  63. if (is_array($expected)) {
  64. @list($command, $arguments) = $expected;
  65. } else {
  66. $command = $expected;
  67. $arguments = null;
  68. }
  69. $this->assertThat($actual, new RedisCommandConstraint($command, $arguments), $message);
  70. }
  71. /**
  72. * Asserts that two arrays have the same values, even if with different order.
  73. *
  74. * @param array $expected Expected array.
  75. * @param array $actual Actual array.
  76. * @param string $message Optional assertion message.
  77. */
  78. public function assertSameValues(array $expected, array $actual, $message = '')
  79. {
  80. $this->assertThat($actual, new ArrayHasSameValuesConstraint($expected), $message);
  81. }
  82. /**
  83. * Returns a named array with the default connection parameters and their values.
  84. *
  85. * @return array Default connection parameters.
  86. */
  87. protected function getDefaultParametersArray()
  88. {
  89. return array(
  90. 'scheme' => 'tcp',
  91. 'host' => REDIS_SERVER_HOST,
  92. 'port' => REDIS_SERVER_PORT,
  93. 'database' => REDIS_SERVER_DBNUM,
  94. );
  95. }
  96. /**
  97. * Returns a named array with the default client options and their values.
  98. *
  99. * @return array Default connection parameters.
  100. */
  101. protected function getDefaultOptionsArray()
  102. {
  103. return array(
  104. 'profile' => REDIS_SERVER_VERSION,
  105. );
  106. }
  107. /**
  108. * Returns a named array with the default connection parameters merged with
  109. * the specified additional parameters.
  110. *
  111. * @param array $additional Additional connection parameters.
  112. *
  113. * @return array Connection parameters.
  114. */
  115. protected function getParametersArray(array $additional)
  116. {
  117. return array_merge($this->getDefaultParametersArray(), $additional);
  118. }
  119. /**
  120. * Returns a new instance of connection parameters.
  121. *
  122. * @param array $additional Additional connection parameters.
  123. *
  124. * @return Connection\Parameters
  125. */
  126. protected function getParameters($additional = array())
  127. {
  128. $parameters = array_merge($this->getDefaultParametersArray(), $additional);
  129. $parameters = new Connection\Parameters($parameters);
  130. return $parameters;
  131. }
  132. /**
  133. * Returns a new instance of server profile.
  134. *
  135. * @param string $version Redis profile.
  136. *
  137. * @return Profile\ProfileInterface
  138. */
  139. protected function getProfile($version = null)
  140. {
  141. return Profile\Factory::get($version ?: REDIS_SERVER_VERSION);
  142. }
  143. /**
  144. * Returns a new client instance.
  145. *
  146. * @param array $parameters Additional connection parameters.
  147. * @param array $options Additional client options.
  148. * @param bool $flushdb Flush selected database before returning the client.
  149. *
  150. * @return Client
  151. */
  152. protected function createClient(array $parameters = null, array $options = null, $flushdb = true)
  153. {
  154. $parameters = array_merge(
  155. $this->getDefaultParametersArray(),
  156. $parameters ?: array()
  157. );
  158. $options = array_merge(
  159. array(
  160. 'profile' => $this->getProfile(),
  161. ),
  162. $options ?: array()
  163. );
  164. $client = new Client($parameters, $options);
  165. $client->connect();
  166. if ($flushdb) {
  167. $client->flushdb();
  168. }
  169. return $client;
  170. }
  171. /**
  172. * Returns the server version of the Redis instance used by the test suite.
  173. *
  174. * @throws RuntimeException When the client cannot retrieve the current server version
  175. *
  176. * @return string
  177. */
  178. protected function getRedisServerVersion()
  179. {
  180. if (isset($this->redisServerVersion)) {
  181. return $this->redisServerVersion;
  182. }
  183. $client = $this->createClient(null, null, true);
  184. $info = array_change_key_case($client->info());
  185. if (isset($info['server']['redis_version'])) {
  186. // Redis >= 2.6
  187. $version = $info['server']['redis_version'];
  188. } elseif (isset($info['redis_version'])) {
  189. // Redis < 2.6
  190. $version = $info['redis_version'];
  191. } else {
  192. throw new RuntimeException('Unable to retrieve server info');
  193. }
  194. $this->redisServerVersion = $version;
  195. return $version;
  196. }
  197. /**
  198. * Returns the Redis server version required to run a @connected test from
  199. * the @requiresRedisVersion annotation decorating a test method.
  200. *
  201. * @return string
  202. */
  203. protected function getRequiredRedisServerVersion()
  204. {
  205. $annotations = $this->getAnnotations();
  206. if (isset($annotations['method']['requiresRedisVersion'], $annotations['method']['group']) &&
  207. !empty($annotations['method']['requiresRedisVersion']) &&
  208. in_array('connected', $annotations['method']['group'])
  209. ) {
  210. return $annotations['method']['requiresRedisVersion'][0];
  211. }
  212. return;
  213. }
  214. /**
  215. * Compares the specified version string against the Redis server version in
  216. * use for integration tests.
  217. *
  218. * @param string $operator Comparison operator.
  219. * @param string $version Version to compare.
  220. *
  221. * @return bool
  222. */
  223. public function isRedisServerVersion($operator, $version)
  224. {
  225. $serverVersion = $this->getRedisServerVersion();
  226. $comparation = version_compare($serverVersion, $version);
  227. return (bool) eval("return $comparation $operator 0;");
  228. }
  229. /**
  230. * Checks that the Redis server version used to run integration tests mets
  231. * the requirements specified with the @requiresRedisVersion annotation.
  232. *
  233. * @throws \PHPUnit_Framework_SkippedTestError When expected Redis server version is not met.
  234. */
  235. protected function checkRequiredRedisServerVersion()
  236. {
  237. if (!$requiredVersion = $this->getRequiredRedisServerVersion()) {
  238. return;
  239. }
  240. $requiredVersion = explode(' ', $requiredVersion, 2);
  241. if (count($requiredVersion) === 1) {
  242. $reqOperator = '>=';
  243. $reqVersion = $requiredVersion[0];
  244. } else {
  245. $reqOperator = $requiredVersion[0];
  246. $reqVersion = $requiredVersion[1];
  247. }
  248. if (!$this->isRedisServerVersion($reqOperator, $reqVersion)) {
  249. $serverVersion = $this->getRedisServerVersion();
  250. $this->markTestSkipped(
  251. "This test requires Redis $reqOperator $reqVersion but the current version is $serverVersion."
  252. );
  253. }
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. protected function checkRequirements()
  259. {
  260. parent::checkRequirements();
  261. $this->checkRequiredRedisServerVersion();
  262. }
  263. }