PredisCommandTestCase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Redis;
  11. use Predis\Client;
  12. use Predis\Command;
  13. use Predis\Profile;
  14. use PredisTestCase;
  15. /**
  16. *
  17. */
  18. abstract class PredisCommandTestCase extends PredisTestCase
  19. {
  20. /**
  21. * Returns the expected command.
  22. *
  23. * @return CommandInterface|string Instance or FQN of the expected command.
  24. */
  25. abstract protected function getExpectedCommand();
  26. /**
  27. * Returns the expected command ID.
  28. *
  29. * @return string
  30. */
  31. abstract protected 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. * Returns a new client instance.
  44. *
  45. * @param bool $flushdb Flush selected database before returning the client.
  46. *
  47. * @return Client
  48. */
  49. public function getClient($flushdb = true)
  50. {
  51. $profile = $this->getProfile();
  52. if (!$profile->supportsCommand($id = $this->getExpectedId())) {
  53. $this->markTestSkipped(
  54. "The profile {$profile->getVersion()} does not support command {$id}"
  55. );
  56. }
  57. $client = $this->createClient(null, null, $flushdb);
  58. return $client;
  59. }
  60. /**
  61. * Returns wether the command is prefixable or not.
  62. *
  63. * @return bool
  64. */
  65. protected function isPrefixable()
  66. {
  67. return $this->getCommand() instanceof PrefixableCommandInterface;
  68. }
  69. /**
  70. * Returns a new command instance with the specified arguments.
  71. *
  72. * @param ... List of arguments for the command.
  73. *
  74. * @return CommandInterface
  75. */
  76. protected function getCommandWithArguments(/* arguments */)
  77. {
  78. return $this->getCommandWithArgumentsArray(func_get_args());
  79. }
  80. /**
  81. * Returns a new command instance with the specified arguments.
  82. *
  83. * @param array $arguments Arguments for the command.
  84. *
  85. * @return CommandInterface
  86. */
  87. protected function getCommandWithArgumentsArray(array $arguments)
  88. {
  89. $command = $this->getCommand();
  90. $command->setArguments($arguments);
  91. return $command;
  92. }
  93. /**
  94. * @group disconnected
  95. */
  96. public function testCommandId()
  97. {
  98. $command = $this->getCommand();
  99. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  100. $this->assertEquals($this->getExpectedId(), $command->getId());
  101. }
  102. /**
  103. * @group disconnected
  104. */
  105. public function testRawArguments()
  106. {
  107. $expected = array('1st', '2nd', '3rd', '4th');
  108. $command = $this->getCommand();
  109. $command->setRawArguments($expected);
  110. $this->assertSame($expected, $command->getArguments());
  111. }
  112. }