PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PHPUnit/PredisTestCase.php

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