CommandTestCase.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. namespace Predis\Command;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Client;
  13. use Predis\Profile\ServerProfile;
  14. use Predis\Profile\ServerProfileInterface;
  15. /**
  16. *
  17. */
  18. abstract class CommandTestCase extends StandardTestCase
  19. {
  20. /**
  21. * Returns the expected command.
  22. *
  23. * @return CommandInterface|string Instance or FQN of the expected command.
  24. */
  25. protected abstract function getExpectedCommand();
  26. /**
  27. * Returns the expected command ID.
  28. *
  29. * @return string
  30. */
  31. protected abstract function getExpectedId();
  32. /**
  33. * Returns a new command instance.
  34. *
  35. * @return CommandInterface
  36. */
  37. public function getCommand()
  38. {
  39. $command = $this->getExpectedCommand();
  40. return $command instanceof CommandInterface ? $command : new $command();
  41. }
  42. /**
  43. * Return the server profile used during the tests.
  44. *
  45. * @return ServerProfileInterface
  46. */
  47. public function getProfile()
  48. {
  49. return ServerProfile::get(REDIS_SERVER_VERSION);
  50. }
  51. /**
  52. * Returns a new client instance.
  53. *
  54. * @param Boolean $connect Flush selected database before returning the client.
  55. * @return Client
  56. */
  57. public function getClient($flushdb = true)
  58. {
  59. $profile = $this->getProfile();
  60. if (!$profile->supportsCommand($id = $this->getExpectedId())) {
  61. $this->markTestSkipped("The profile {$profile->getVersion()} does not support command {$id}");
  62. }
  63. $parameters = array(
  64. 'host' => REDIS_SERVER_HOST,
  65. 'port' => REDIS_SERVER_PORT,
  66. );
  67. $options = array(
  68. 'profile' => $profile
  69. );
  70. $client = new Client($parameters, $options);
  71. $client->connect();
  72. $client->select(REDIS_SERVER_DBNUM);
  73. if ($flushdb) {
  74. $client->flushdb();
  75. }
  76. return $client;
  77. }
  78. /**
  79. * Returns wether the command is prefixable or not.
  80. *
  81. * @return Boolean
  82. */
  83. protected function isPrefixable()
  84. {
  85. return $this->getCommand() instanceof PrefixableCommandInterface;
  86. }
  87. /**
  88. * Returns a new command instance with the specified arguments.
  89. *
  90. * @param ... List of arguments for the command.
  91. * @return CommandInterface
  92. */
  93. protected function getCommandWithArguments(/* arguments */)
  94. {
  95. return $this->getCommandWithArgumentsArray(func_get_args());
  96. }
  97. /**
  98. * Returns a new command instance with the specified arguments.
  99. *
  100. * @param array $arguments Arguments for the command.
  101. * @return CommandInterface
  102. */
  103. protected function getCommandWithArgumentsArray(Array $arguments)
  104. {
  105. $command = $this->getCommand();
  106. $command->setArguments($arguments);
  107. return $command;
  108. }
  109. /**
  110. * Sleep the test case with microseconds resolution.
  111. *
  112. * @param float $seconds Seconds to sleep.
  113. */
  114. protected function sleep($seconds)
  115. {
  116. usleep($seconds * 1000000);
  117. }
  118. /**
  119. * Asserts that two arrays have the same values, even if with different order.
  120. *
  121. * @param Array $expected Expected array.
  122. * @param Array $actual Actual array.
  123. */
  124. protected function assertSameValues(Array $expected, Array $actual)
  125. {
  126. $this->assertThat($expected, new \ArrayHasSameValuesConstraint($actual));
  127. }
  128. /**
  129. * @group disconnected
  130. */
  131. public function testCommandId()
  132. {
  133. $command = $this->getCommand();
  134. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  135. $this->assertEquals($this->getExpectedId(), $command->getId());
  136. }
  137. /**
  138. * @param string $expectedVersion Expected redis version
  139. * @param string $operator Comparison operator.
  140. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  141. */
  142. protected function executeOnRedisVersion($expectedVersion, $operator, $callback)
  143. {
  144. $client = $this->getClient();
  145. $info = array_change_key_case($client->info());
  146. if (isset($info['server']['redis_version'])) {
  147. // Redis >= 2.6
  148. $version = $info['server']['redis_version'];
  149. } else if (isset($info['redis_version'])) {
  150. // Redis < 2.6
  151. $version = $info['redis_version'];
  152. } else {
  153. throw new \RuntimeException('Unable to retrieve server info');
  154. }
  155. $comparation = version_compare($version, $expectedVersion);
  156. if ($match = eval("return $comparation $operator 0;")) {
  157. call_user_func($callback, $this, $version);
  158. }
  159. return $match;
  160. }
  161. /**
  162. * @param string $expectedVersion Expected redis version
  163. * @param string $operator Comparison operator.
  164. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  165. */
  166. protected function executeOnProfileVersion($expectedVersion, $operator, $callback)
  167. {
  168. $profile = $this->getProfile();
  169. $comparation = version_compare($profile->getVersion(), $expectedVersion);
  170. if ($match = eval("return $comparation $operator 0;")) {
  171. call_user_func($callback, $this, $version);
  172. }
  173. return $match;
  174. }
  175. /**
  176. * @param string $expectedVersion Expected redis version.
  177. * @param string $message Optional message.
  178. * @param bool $remote Based on local profile or remote redis version.
  179. * @throws \RuntimeException when unable to retrieve server info or redis version
  180. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  181. */
  182. protected function markTestSkippedOnRedisVersionBelow($expectedVersion, $message = '', $remote = true)
  183. {
  184. $callback = function ($test, $version) use ($message, $expectedVersion) {
  185. $test->markTestSkipped($message ?: "Test requires Redis $expectedVersion, current is $version.");
  186. };
  187. $method = $remote ? 'executeOnRedisVersion' : 'executeOnProfileVersion';
  188. $this->$method($expectedVersion, '<', $callback);
  189. }
  190. /**
  191. * @group disconnected
  192. */
  193. public function testRawArguments()
  194. {
  195. $expected = array('1st', '2nd', '3rd', '4th');
  196. $command = $this->getCommand();
  197. $command->setRawArguments($expected);
  198. $this->assertSame($expected, $command->getArguments());
  199. }
  200. }