PredisTestCase.php 8.5 KB

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