PredisTestCase.php 7.7 KB

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