PredisTestCase.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * Verifies that a Redis command is a valid Predis\Command\CommandInterface
  22. * instance with the specified ID and command arguments.
  23. *
  24. * @param string|Command\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|Command\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\Parameters Default connection parameters.
  104. */
  105. protected function getParameters($additional = array())
  106. {
  107. $parameters = array_merge($this->getDefaultParametersArray(), $additional);
  108. $parameters = new Connection\Parameters($parameters);
  109. return $parameters;
  110. }
  111. /**
  112. * Returns a new instance of server profile.
  113. *
  114. * @param string $version Redis profile.
  115. * @return Profile\ProfileInterface
  116. */
  117. protected function getProfile($version = null)
  118. {
  119. return Profile\Factory::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. * Returns the server version of the Redis instance used by the test suite.
  150. *
  151. * @return string
  152. * @throws RuntimeException When the client cannot retrieve the current server version
  153. */
  154. protected function getRedisServerVersion()
  155. {
  156. if (isset($this->redisServerVersion)) {
  157. return $this->redisServerVersion;
  158. }
  159. $client = $this->createClient(null, null, true);
  160. $info = array_change_key_case($client->info());
  161. if (isset($info['server']['redis_version'])) {
  162. // Redis >= 2.6
  163. $version = $info['server']['redis_version'];
  164. } elseif (isset($info['redis_version'])) {
  165. // Redis < 2.6
  166. $version = $info['redis_version'];
  167. } else {
  168. throw new RuntimeException('Unable to retrieve server info');
  169. }
  170. $this->redisServerVersion = $version;
  171. return $version;
  172. }
  173. /**
  174. * @param string $expectedVersion Expected redis version.
  175. * @param string $operator Comparison operator.
  176. * @param callable $callback Callback for matching version.
  177. * @return string
  178. * @throws \PHPUnit_Framework_SkippedTestError When expected redis version is not met
  179. */
  180. protected function executeOnRedisVersion($expectedVersion, $operator, $callback)
  181. {
  182. $version = $this->getRedisServerVersion();
  183. $comparation = version_compare($version, $expectedVersion);
  184. if ($match = eval("return $comparation $operator 0;")) {
  185. call_user_func($callback, $this, $version);
  186. }
  187. return $match;
  188. }
  189. /**
  190. * @param string $expectedVersion Expected redis version.
  191. * @param string $operator Comparison operator.
  192. * @param callable $callback Callback for matching version.
  193. * @return string
  194. * @throws \PHPUnit_Framework_SkippedTestError When expected redis version is not met
  195. */
  196. protected function executeOnProfileVersion($expectedVersion, $operator, $callback)
  197. {
  198. $profile = $this->getProfile();
  199. $comparation = version_compare($version = $profile->getVersion(), $expectedVersion);
  200. if ($match = eval("return $comparation $operator 0;")) {
  201. call_user_func($callback, $this, $version);
  202. }
  203. return $match;
  204. }
  205. /**
  206. * Sleep the test case with microseconds resolution.
  207. *
  208. * @param float $seconds Seconds to sleep.
  209. */
  210. protected function sleep($seconds)
  211. {
  212. usleep($seconds * 1000000);
  213. }
  214. /**
  215. *
  216. */
  217. protected function setRequiredRedisVersionFromAnnotation()
  218. {
  219. $annotations = $this->getAnnotations();
  220. if (isset($annotations['method']['requiresRedisVersion'], $annotations['method']['group']) &&
  221. !empty($annotations['method']['requiresRedisVersion']) &&
  222. in_array('connected', $annotations['method']['group'])
  223. ) {
  224. $this->required['requiresRedisVersion'] = $annotations['method']['requiresRedisVersion'][0];
  225. }
  226. }
  227. /**
  228. *
  229. */
  230. protected function checkRequiredRedisVersion()
  231. {
  232. if (!isset($this->required['requiresRedisVersion'])) {
  233. return;
  234. }
  235. $srvVersion = $this->getRedisServerVersion();
  236. $expectation = explode(' ', $this->required['requiresRedisVersion'], 2);
  237. if (count($expectation) === 1) {
  238. $expOperator = '>=';
  239. $expVersion = $expectation[0];
  240. } else {
  241. $expOperator = $expectation[0];
  242. $expVersion = $expectation[1];
  243. }
  244. $comparation = version_compare($srvVersion, $expVersion);
  245. if (!$match = eval("return $comparation $expOperator 0;")) {
  246. $this->markTestSkipped(
  247. "This test requires Redis $expOperator $expVersion but the current version is $srvVersion."
  248. );
  249. }
  250. }
  251. /**
  252. * {@inheritdoc}
  253. */
  254. protected function setRequirementsFromAnnotation()
  255. {
  256. parent::setRequirementsFromAnnotation();
  257. $this->setRequiredRedisVersionFromAnnotation();
  258. }
  259. /**
  260. * {@inheritdoc}
  261. */
  262. protected function checkRequirements()
  263. {
  264. parent::checkRequirements();
  265. $this->checkRequiredRedisVersion();
  266. }
  267. }