PredisTestCase.php 8.8 KB

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