PredisCommandTestCase.php 3.0 KB

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