PredisTestCase.php 9.4 KB

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