PredisTestCase.php 9.2 KB

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