CommandTestCase.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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;
  14. /**
  15. *
  16. */
  17. abstract class CommandTestCase extends StandardTestCase
  18. {
  19. /**
  20. * Returns the expected command.
  21. *
  22. * @return CommandInterface|string Instance or FQN of the expected command.
  23. */
  24. protected abstract function getExpectedCommand();
  25. /**
  26. * Returns the expected command ID.
  27. *
  28. * @return string
  29. */
  30. protected abstract function getExpectedId();
  31. /**
  32. * Returns a new command instance.
  33. *
  34. * @return CommandInterface
  35. */
  36. protected function getCommand()
  37. {
  38. $command = $this->getExpectedCommand();
  39. return $command instanceof CommandInterface ? $command : new $command();
  40. }
  41. /**
  42. * Return the server profile used during the tests.
  43. *
  44. * @return Profile\ProfileInterface
  45. */
  46. protected function getProfile()
  47. {
  48. return Profile\Factory::get(REDIS_SERVER_VERSION);
  49. }
  50. /**
  51. * Returns a new client instance.
  52. *
  53. * @param Boolean $connect Flush selected database before returning the client.
  54. * @return Client
  55. */
  56. protected function getClient($flushdb = true)
  57. {
  58. $profile = $this->getProfile();
  59. if (!$profile->supportsCommand($id = $this->getExpectedId())) {
  60. $this->markTestSkipped("The profile {$profile->getVersion()} does not support command {$id}");
  61. }
  62. $parameters = array(
  63. 'host' => REDIS_SERVER_HOST,
  64. 'port' => REDIS_SERVER_PORT,
  65. );
  66. $options = array(
  67. 'profile' => $profile
  68. );
  69. $client = new Client($parameters, $options);
  70. $client->connect();
  71. $client->select(REDIS_SERVER_DBNUM);
  72. if ($flushdb) {
  73. $client->flushdb();
  74. }
  75. return $client;
  76. }
  77. /**
  78. * Returns wether the command is prefixable or not.
  79. *
  80. * @return Boolean
  81. */
  82. protected function isPrefixable()
  83. {
  84. return $this->getCommand() instanceof PrefixableCommandInterface;
  85. }
  86. /**
  87. * Returns a new command instance with the specified arguments.
  88. *
  89. * @param ... List of arguments for the command.
  90. * @return CommandInterface
  91. */
  92. protected function getCommandWithArguments(/* arguments */)
  93. {
  94. return $this->getCommandWithArgumentsArray(func_get_args());
  95. }
  96. /**
  97. * Returns a new command instance with the specified arguments.
  98. *
  99. * @param array $arguments Arguments for the command.
  100. * @return CommandInterface
  101. */
  102. protected function getCommandWithArgumentsArray(array $arguments)
  103. {
  104. $command = $this->getCommand();
  105. $command->setArguments($arguments);
  106. return $command;
  107. }
  108. /**
  109. * Sleep the test case with microseconds resolution.
  110. *
  111. * @param float $seconds Seconds to sleep.
  112. */
  113. protected function sleep($seconds)
  114. {
  115. usleep($seconds * 1000000);
  116. }
  117. /**
  118. * Asserts that two arrays have the same values, even if with different order.
  119. *
  120. * @param array $expected Expected array.
  121. * @param array $actual Actual array.
  122. */
  123. protected function assertSameValues(array $expected, array $actual)
  124. {
  125. $this->assertThat($expected, new \ArrayHasSameValuesConstraint($actual));
  126. }
  127. /**
  128. * @group disconnected
  129. */
  130. public function testCommandId()
  131. {
  132. $command = $this->getCommand();
  133. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  134. $this->assertEquals($this->getExpectedId(), $command->getId());
  135. }
  136. /**
  137. * @param string $expectedVersion
  138. * @param string $message Optional message.
  139. * @throws \RuntimeException when unable to retrieve server info or redis version
  140. * @throws \PHPUnit_Framework_SkippedTestError when expected redis version is not met
  141. */
  142. protected function markTestSkippedOnRedisVersionBelow($expectedVersion, $message = '')
  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. if (version_compare($version, $expectedVersion) <= -1) {
  156. $this->markTestSkipped($message ?: "Test requires Redis $expectedVersion, current is $version.");
  157. }
  158. }
  159. /**
  160. * @group disconnected
  161. */
  162. public function testRawArguments()
  163. {
  164. $expected = array('1st', '2nd', '3rd', '4th');
  165. $command = $this->getCommand();
  166. $command->setRawArguments($expected);
  167. $this->assertSame($expected, $command->getArguments());
  168. }
  169. }