PredisTestCase.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * @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\ConnectionParameters Default connection parameters.
  103. */
  104. protected function getParameters($additional = array())
  105. {
  106. $parameters = array_merge($this->getDefaultParametersArray(), $additional);
  107. $parameters = new ConnectionParameters($parameters);
  108. return $parameters;
  109. }
  110. /**
  111. * Returns a new instance of server profile.
  112. *
  113. * @param array $additional Additional connection parameters.
  114. * @return ServerProfileInterface
  115. */
  116. protected function getProfile($version = null)
  117. {
  118. return ServerProfile::get($version ?: REDIS_SERVER_VERSION);
  119. }
  120. /**
  121. * Returns a new client instance.
  122. *
  123. * @param bool $connect Flush selected database before returning the client.
  124. * @return Client
  125. */
  126. protected function createClient(array $parameters = null, array $options = null, $flushdb = true)
  127. {
  128. $parameters = array_merge(
  129. $this->getDefaultParametersArray(),
  130. $parameters ?: array()
  131. );
  132. $options = array_merge(
  133. array(
  134. 'profile' => $this->getProfile(),
  135. ),
  136. $options ?: array()
  137. );
  138. $client = new Client($parameters, $options);
  139. $client->connect();
  140. if ($flushdb) {
  141. $client->flushdb();
  142. }
  143. return $client;
  144. }
  145. /**
  146. * @param string $expectedVersion Expected redis version
  147. * @param string $operator Comparison operator.
  148. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  149. */
  150. protected function executeOnRedisVersion($expectedVersion, $operator, $callback)
  151. {
  152. $client = $this->createClient(null, null, true);
  153. $info = array_change_key_case($client->info());
  154. if (isset($info['server']['redis_version'])) {
  155. // Redis >= 2.6
  156. $version = $info['server']['redis_version'];
  157. } elseif (isset($info['redis_version'])) {
  158. // Redis < 2.6
  159. $version = $info['redis_version'];
  160. } else {
  161. throw new RuntimeException('Unable to retrieve server info');
  162. }
  163. $comparation = version_compare($version, $expectedVersion);
  164. if ($match = eval("return $comparation $operator 0;")) {
  165. call_user_func($callback, $this, $version);
  166. }
  167. return $match;
  168. }
  169. /**
  170. * @param string $expectedVersion Expected redis version
  171. * @param string $operator Comparison operator.
  172. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  173. */
  174. protected function executeOnProfileVersion($expectedVersion, $operator, $callback)
  175. {
  176. $profile = $this->getProfile();
  177. $comparation = version_compare($version = $profile->getVersion(), $expectedVersion);
  178. if ($match = eval("return $comparation $operator 0;")) {
  179. call_user_func($callback, $this, $version);
  180. }
  181. return $match;
  182. }
  183. /**
  184. * @param string $expectedVersion Expected redis version.
  185. * @param string $message Optional message.
  186. * @param bool $remote Based on local profile or remote redis version.
  187. * @throws RuntimeException when unable to retrieve server info or redis version
  188. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  189. */
  190. public function markTestSkippedOnRedisVersionBelow($expectedVersion, $message = '', $remote = true)
  191. {
  192. $callback = function ($test, $version) use ($message, $expectedVersion) {
  193. $test->markTestSkipped($message ?: "Test requires Redis $expectedVersion, current is $version.");
  194. };
  195. $method = $remote ? 'executeOnRedisVersion' : 'executeOnProfileVersion';
  196. $this->$method($expectedVersion, '<', $callback);
  197. }
  198. /**
  199. * Sleep the test case with microseconds resolution.
  200. *
  201. * @param float $seconds Seconds to sleep.
  202. */
  203. protected function sleep($seconds)
  204. {
  205. usleep($seconds * 1000000);
  206. }
  207. }