PredisTestCase.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. *
  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|Command\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. *
  95. * @return array Connection parameters.
  96. */
  97. protected function getParametersArray(array $additional)
  98. {
  99. return array_merge($this->getDefaultParametersArray(), $additional);
  100. }
  101. /**
  102. * Returns a new instance of connection parameters.
  103. *
  104. * @param array $additional Additional connection parameters.
  105. *
  106. * @return Connection\Parameters
  107. */
  108. protected function getParameters($additional = array())
  109. {
  110. $parameters = array_merge($this->getDefaultParametersArray(), $additional);
  111. $parameters = new Connection\Parameters($parameters);
  112. return $parameters;
  113. }
  114. /**
  115. * Returns a new instance of server profile.
  116. *
  117. * @param string $version Redis profile.
  118. *
  119. * @return Profile\ProfileInterface
  120. */
  121. protected function getProfile($version = null)
  122. {
  123. return Profile\Factory::get($version ?: REDIS_SERVER_VERSION);
  124. }
  125. /**
  126. * Returns a new client instance.
  127. *
  128. * @param array $parameters Additional connection parameters.
  129. * @param array $options Additional client options.
  130. * @param bool $flushdb Flush selected database before returning the client.
  131. *
  132. * @return Client
  133. */
  134. protected function createClient(array $parameters = null, array $options = null, $flushdb = true)
  135. {
  136. $parameters = array_merge(
  137. $this->getDefaultParametersArray(),
  138. $parameters ?: array()
  139. );
  140. $options = array_merge(
  141. array(
  142. 'profile' => $this->getProfile(),
  143. ),
  144. $options ?: array()
  145. );
  146. $client = new Client($parameters, $options);
  147. $client->connect();
  148. if ($flushdb) {
  149. $client->flushdb();
  150. }
  151. return $client;
  152. }
  153. /**
  154. * Returns the server version of the Redis instance used by the test suite.
  155. *
  156. * @return string
  157. *
  158. * @throws RuntimeException When the client cannot retrieve the current server version
  159. */
  160. protected function getRedisServerVersion()
  161. {
  162. if (isset($this->redisServerVersion)) {
  163. return $this->redisServerVersion;
  164. }
  165. $client = $this->createClient(null, null, true);
  166. $info = array_change_key_case($client->info());
  167. if (isset($info['server']['redis_version'])) {
  168. // Redis >= 2.6
  169. $version = $info['server']['redis_version'];
  170. } elseif (isset($info['redis_version'])) {
  171. // Redis < 2.6
  172. $version = $info['redis_version'];
  173. } else {
  174. throw new RuntimeException('Unable to retrieve server info');
  175. }
  176. $this->redisServerVersion = $version;
  177. return $version;
  178. }
  179. /**
  180. * @param string $expectedVersion Expected redis version.
  181. * @param string $operator Comparison operator.
  182. * @param callable $callback Callback for matching version.
  183. *
  184. * @return string
  185. *
  186. * @throws \PHPUnit_Framework_SkippedTestError When expected redis version is not met
  187. */
  188. protected function executeOnRedisVersion($expectedVersion, $operator, $callback)
  189. {
  190. $version = $this->getRedisServerVersion();
  191. $comparation = version_compare($version, $expectedVersion);
  192. if ($match = eval("return $comparation $operator 0;")) {
  193. call_user_func($callback, $this, $version);
  194. }
  195. return $match;
  196. }
  197. /**
  198. * @param string $expectedVersion Expected redis version.
  199. * @param string $operator Comparison operator.
  200. * @param callable $callback Callback for matching version.
  201. *
  202. * @return string
  203. *
  204. * @throws \PHPUnit_Framework_SkippedTestError When expected redis version is not met
  205. */
  206. protected function executeOnProfileVersion($expectedVersion, $operator, $callback)
  207. {
  208. $profile = $this->getProfile();
  209. $comparation = version_compare($version = $profile->getVersion(), $expectedVersion);
  210. if ($match = eval("return $comparation $operator 0;")) {
  211. call_user_func($callback, $this, $version);
  212. }
  213. return $match;
  214. }
  215. /**
  216. * Sleep the test case with microseconds resolution.
  217. *
  218. * @param float $seconds Seconds to sleep.
  219. */
  220. protected function sleep($seconds)
  221. {
  222. usleep($seconds * 1000000);
  223. }
  224. /**
  225. *
  226. */
  227. protected function setRequiredRedisVersionFromAnnotation()
  228. {
  229. $annotations = $this->getAnnotations();
  230. if (isset($annotations['method']['requiresRedisVersion'], $annotations['method']['group']) &&
  231. !empty($annotations['method']['requiresRedisVersion']) &&
  232. in_array('connected', $annotations['method']['group'])
  233. ) {
  234. $this->required['requiresRedisVersion'] = $annotations['method']['requiresRedisVersion'][0];
  235. }
  236. }
  237. /**
  238. *
  239. */
  240. protected function checkRequiredRedisVersion()
  241. {
  242. if (!isset($this->required['requiresRedisVersion'])) {
  243. return;
  244. }
  245. $srvVersion = $this->getRedisServerVersion();
  246. $expectation = explode(' ', $this->required['requiresRedisVersion'], 2);
  247. if (count($expectation) === 1) {
  248. $expOperator = '>=';
  249. $expVersion = $expectation[0];
  250. } else {
  251. $expOperator = $expectation[0];
  252. $expVersion = $expectation[1];
  253. }
  254. $comparation = version_compare($srvVersion, $expVersion);
  255. if (!$match = eval("return $comparation $expOperator 0;")) {
  256. $this->markTestSkipped(
  257. "This test requires Redis $expOperator $expVersion but the current version is $srvVersion."
  258. );
  259. }
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. protected function setRequirementsFromAnnotation()
  265. {
  266. parent::setRequirementsFromAnnotation();
  267. $this->setRequiredRedisVersionFromAnnotation();
  268. }
  269. /**
  270. * {@inheritdoc}
  271. */
  272. protected function checkRequirements()
  273. {
  274. parent::checkRequirements();
  275. $this->checkRequiredRedisVersion();
  276. }
  277. }