CommandTestCase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. protected 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. protected 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. protected 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
  139. * @param string $message Optional message.
  140. * @throws \RuntimeException when unable to retrieve server info or redis version
  141. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  142. */
  143. protected function markTestSkippedOnRedisVersionBelow($expectedVersion, $message = '')
  144. {
  145. $client = $this->getClient();
  146. $info = array_change_key_case($client->info());
  147. if (isset($info['server']['redis_version'])) {
  148. // Redis >= 2.6
  149. $version = $info['server']['redis_version'];
  150. } else if (isset($info['redis_version'])) {
  151. // Redis < 2.6
  152. $version = $info['redis_version'];
  153. } else {
  154. throw new \RuntimeException('Unable to retrieve server info');
  155. }
  156. if (version_compare($version, $expectedVersion) <= -1) {
  157. $this->markTestSkipped($message ?: "Test requires Redis $expectedVersion, current is $version.");
  158. }
  159. }
  160. /**
  161. * @group disconnected
  162. */
  163. public function testRawArguments()
  164. {
  165. $expected = array('1st', '2nd', '3rd', '4th');
  166. $command = $this->getCommand();
  167. $command->setRawArguments($expected);
  168. $this->assertSame($expected, $command->getArguments());
  169. }
  170. }