PredisTestCase.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /**
  20. * Verifies that a Redis command is a valid Predis\Command\CommandInterface
  21. * instance with the specified ID and command arguments.
  22. *
  23. * @param string|CommandInterface $command Expected command or command ID.
  24. * @param array $arguments Expected command arguments.
  25. * @return RedisCommandConstraint
  26. */
  27. public function isRedisCommand($command = null, array $arguments = null)
  28. {
  29. return new RedisCommandConstraint($command, $arguments);
  30. }
  31. /**
  32. * Verifies that a Redis command is a valid Predis\Command\CommandInterface
  33. * instance with the specified ID and command arguments. The comparison does
  34. * not check for identity when passing a Predis\Command\CommandInterface
  35. * instance for $expected.
  36. *
  37. * @param array|string|CommandInterface $expected Expected command.
  38. * @param mixed $actual Actual command.
  39. * @param string $message Optional assertion message.
  40. */
  41. public function assertRedisCommand($expected, $actual, $message = '')
  42. {
  43. if (is_array($expected)) {
  44. @list($command, $arguments) = $expected;
  45. } else {
  46. $command = $expected;
  47. $arguments = null;
  48. }
  49. $this->assertThat($actual, new RedisCommandConstraint($command, $arguments), $message);
  50. }
  51. /**
  52. * Asserts that two arrays have the same values, even if with different order.
  53. *
  54. * @param array $expected Expected array.
  55. * @param array $actual Actual array.
  56. * @param string $message Optional assertion message.
  57. */
  58. public function assertSameValues(array $expected, array $actual, $message = '')
  59. {
  60. $this->assertThat($actual, new ArrayHasSameValuesConstraint($expected), $message);
  61. }
  62. /**
  63. * Returns a named array with the default connection parameters and their values.
  64. *
  65. * @return array Default connection parameters.
  66. */
  67. protected function getDefaultParametersArray()
  68. {
  69. return array(
  70. 'scheme' => 'tcp',
  71. 'host' => REDIS_SERVER_HOST,
  72. 'port' => REDIS_SERVER_PORT,
  73. 'database' => REDIS_SERVER_DBNUM,
  74. );
  75. }
  76. /**
  77. * Returns a named array with the default client options and their values.
  78. *
  79. * @return array Default connection parameters.
  80. */
  81. protected function getDefaultOptionsArray()
  82. {
  83. return array(
  84. 'profile' => REDIS_SERVER_VERSION,
  85. );
  86. }
  87. /**
  88. * Returns a named array with the default connection parameters merged with
  89. * the specified additional parameters.
  90. *
  91. * @param array $additional Additional connection parameters.
  92. * @return array Connection parameters.
  93. */
  94. protected function getParametersArray(array $additional)
  95. {
  96. return array_merge($this->getDefaultParametersArray(), $additional);
  97. }
  98. /**
  99. * Returns a new instance of connection parameters.
  100. *
  101. * @param array $additional Additional connection parameters.
  102. * @return Connection\Parameters Default connection parameters.
  103. */
  104. protected function getParameters($additional = array())
  105. {
  106. $parameters = array_merge($this->getDefaultParametersArray(), $additional);
  107. $parameters = new Connection\Parameters($parameters);
  108. return $parameters;
  109. }
  110. /**
  111. * Returns a new instance of server profile.
  112. *
  113. * @param string $version Redis profile.
  114. * @return Profile\ProfileInterface
  115. */
  116. protected function getProfile($version = null)
  117. {
  118. return Profile\Factory::get($version ?: REDIS_SERVER_VERSION);
  119. }
  120. /**
  121. * Returns a new client instance.
  122. *
  123. * @param array $parameters Additional connection parameters.
  124. * @param array $options Additional client options.
  125. * @param bool $flushdb Flush selected database before returning the client.
  126. * @return Client
  127. */
  128. protected function createClient(array $parameters = null, array $options = null, $flushdb = true)
  129. {
  130. $parameters = array_merge(
  131. $this->getDefaultParametersArray(),
  132. $parameters ?: array()
  133. );
  134. $options = array_merge(
  135. array(
  136. 'profile' => $this->getProfile(),
  137. ),
  138. $options ?: array()
  139. );
  140. $client = new Client($parameters, $options);
  141. $client->connect();
  142. if ($flushdb) {
  143. $client->flushdb();
  144. }
  145. return $client;
  146. }
  147. /**
  148. * @param string $expectedVersion Expected redis version.
  149. * @param string $operator Comparison operator.
  150. * @param callable $callback Callback for matching version.
  151. * @throws PHPUnit_Framework_SkippedTestError When expected redis version is not met
  152. */
  153. protected function executeOnRedisVersion($expectedVersion, $operator, $callback)
  154. {
  155. $client = $this->createClient(null, null, true);
  156. $info = array_change_key_case($client->info());
  157. if (isset($info['server']['redis_version'])) {
  158. // Redis >= 2.6
  159. $version = $info['server']['redis_version'];
  160. } elseif (isset($info['redis_version'])) {
  161. // Redis < 2.6
  162. $version = $info['redis_version'];
  163. } else {
  164. throw new RuntimeException('Unable to retrieve server info');
  165. }
  166. $comparation = version_compare($version, $expectedVersion);
  167. if ($match = eval("return $comparation $operator 0;")) {
  168. call_user_func($callback, $this, $version);
  169. }
  170. return $match;
  171. }
  172. /**
  173. * @param string $expectedVersion Expected redis version.
  174. * @param string $operator Comparison operator.
  175. * @param callable $callback Callback for matching version.
  176. * @throws PHPUnit_Framework_SkippedTestError When expected redis version is not met
  177. */
  178. protected function executeOnProfileVersion($expectedVersion, $operator, $callback)
  179. {
  180. $profile = $this->getProfile();
  181. $comparation = version_compare($version = $profile->getVersion(), $expectedVersion);
  182. if ($match = eval("return $comparation $operator 0;")) {
  183. call_user_func($callback, $this, $version);
  184. }
  185. return $match;
  186. }
  187. /**
  188. * @param string $expectedVersion Expected redis version.
  189. * @param string $message Optional message.
  190. * @param bool $remote Based on local profile or remote redis version.
  191. * @throws RuntimeException When unable to retrieve server info or redis version
  192. * @throws PHPUnit_Framework_SkippedTestError When expected redis version is not met
  193. */
  194. public function markTestSkippedOnRedisVersionBelow($expectedVersion, $message = '', $remote = true)
  195. {
  196. $callback = function ($test, $version) use ($message, $expectedVersion) {
  197. $test->markTestSkipped($message ?: "Test requires Redis $expectedVersion, current is $version.");
  198. };
  199. $method = $remote ? 'executeOnRedisVersion' : 'executeOnProfileVersion';
  200. $this->$method($expectedVersion, '<', $callback);
  201. }
  202. /**
  203. * Sleep the test case with microseconds resolution.
  204. *
  205. * @param float $seconds Seconds to sleep.
  206. */
  207. protected function sleep($seconds)
  208. {
  209. usleep($seconds * 1000000);
  210. }
  211. }