PredisTestCase.php 9.1 KB

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