PredisTestCase.php 7.6 KB

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