PredisTestCase.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 the current server profile in use by the test suite.
  145. *
  146. * @return Profile\ProfileInterface
  147. */
  148. protected function getCurrentProfile()
  149. {
  150. static $profile;
  151. $profile = $this->getProfile();
  152. return $profile;
  153. }
  154. /**
  155. * Returns a new client instance.
  156. *
  157. * @param array $parameters Additional connection parameters.
  158. * @param array $options Additional client options.
  159. * @param bool $flushdb Flush selected database before returning the client.
  160. *
  161. * @return Client
  162. */
  163. protected function createClient(array $parameters = null, array $options = null, $flushdb = true)
  164. {
  165. $parameters = array_merge(
  166. $this->getDefaultParametersArray(),
  167. $parameters ?: array()
  168. );
  169. $options = array_merge(
  170. array(
  171. 'profile' => $this->getProfile(),
  172. ),
  173. $options ?: array()
  174. );
  175. $client = new Client($parameters, $options);
  176. $client->connect();
  177. if ($flushdb) {
  178. $client->flushdb();
  179. }
  180. return $client;
  181. }
  182. /**
  183. * Returns the server version of the Redis instance used by the test suite.
  184. *
  185. * @throws RuntimeException When the client cannot retrieve the current server version
  186. *
  187. * @return string
  188. */
  189. protected function getRedisServerVersion()
  190. {
  191. if (isset($this->redisServerVersion)) {
  192. return $this->redisServerVersion;
  193. }
  194. $client = $this->createClient(null, null, true);
  195. $info = array_change_key_case($client->info());
  196. if (isset($info['server']['redis_version'])) {
  197. // Redis >= 2.6
  198. $version = $info['server']['redis_version'];
  199. } elseif (isset($info['redis_version'])) {
  200. // Redis < 2.6
  201. $version = $info['redis_version'];
  202. } else {
  203. throw new RuntimeException('Unable to retrieve server info');
  204. }
  205. $this->redisServerVersion = $version;
  206. return $version;
  207. }
  208. /**
  209. * Returns the Redis server version required to run a @connected test from
  210. * the @requiresRedisVersion annotation decorating a test method.
  211. *
  212. * @return string
  213. */
  214. protected function getRequiredRedisServerVersion()
  215. {
  216. $annotations = $this->getAnnotations();
  217. if (isset($annotations['method']['requiresRedisVersion'], $annotations['method']['group']) &&
  218. !empty($annotations['method']['requiresRedisVersion']) &&
  219. in_array('connected', $annotations['method']['group'])
  220. ) {
  221. return $annotations['method']['requiresRedisVersion'][0];
  222. }
  223. return;
  224. }
  225. /**
  226. * Compares the specified version string against the Redis server version in
  227. * use for integration tests.
  228. *
  229. * @param string $operator Comparison operator.
  230. * @param string $version Version to compare.
  231. *
  232. * @return bool
  233. */
  234. public function isRedisServerVersion($operator, $version)
  235. {
  236. $serverVersion = $this->getRedisServerVersion();
  237. $comparation = version_compare($serverVersion, $version);
  238. return (bool) eval("return $comparation $operator 0;");
  239. }
  240. /**
  241. * Checks that the Redis server version used to run integration tests mets
  242. * the requirements specified with the @requiresRedisVersion annotation.
  243. *
  244. * @throws \PHPUnit_Framework_SkippedTestError When expected Redis server version is not met.
  245. */
  246. protected function checkRequiredRedisServerVersion()
  247. {
  248. if (!$requiredVersion = $this->getRequiredRedisServerVersion()) {
  249. return;
  250. }
  251. $requiredVersion = explode(' ', $requiredVersion, 2);
  252. if (count($requiredVersion) === 1) {
  253. $reqOperator = '>=';
  254. $reqVersion = $requiredVersion[0];
  255. } else {
  256. $reqOperator = $requiredVersion[0];
  257. $reqVersion = $requiredVersion[1];
  258. }
  259. if (!$this->isRedisServerVersion($reqOperator, $reqVersion)) {
  260. $serverVersion = $this->getRedisServerVersion();
  261. $this->markTestSkipped(
  262. "This test requires Redis $reqOperator $reqVersion but the current version is $serverVersion."
  263. );
  264. }
  265. }
  266. /**
  267. * {@inheritdoc}
  268. */
  269. protected function checkRequirements()
  270. {
  271. parent::checkRequirements();
  272. $this->checkRequiredRedisServerVersion();
  273. }
  274. }