CommandTestCase.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 CommandTestCase 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. if (!$this->getProfile()->supportsCommand($id = $this->getExpectedId())) {
  50. $this->markTestSkipped(
  51. "The profile {$profile->getVersion()} does not support command {$id}"
  52. );
  53. }
  54. $client = $this->createClient(null, null, $flushdb);
  55. return $client;
  56. }
  57. /**
  58. * Returns wether the command is prefixable or not.
  59. *
  60. * @return Boolean
  61. */
  62. protected function isPrefixable()
  63. {
  64. return $this->getCommand() instanceof PrefixableCommandInterface;
  65. }
  66. /**
  67. * Returns a new command instance with the specified arguments.
  68. *
  69. * @param ... List of arguments for the command.
  70. * @return CommandInterface
  71. */
  72. protected function getCommandWithArguments(/* arguments */)
  73. {
  74. return $this->getCommandWithArgumentsArray(func_get_args());
  75. }
  76. /**
  77. * Returns a new command instance with the specified arguments.
  78. *
  79. * @param array $arguments Arguments for the command.
  80. * @return CommandInterface
  81. */
  82. protected function getCommandWithArgumentsArray(array $arguments)
  83. {
  84. $command = $this->getCommand();
  85. $command->setArguments($arguments);
  86. return $command;
  87. }
  88. /**
  89. * @group disconnected
  90. */
  91. public function testCommandId()
  92. {
  93. $command = $this->getCommand();
  94. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  95. $this->assertEquals($this->getExpectedId(), $command->getId());
  96. }
  97. /**
  98. * @group disconnected
  99. */
  100. public function testRawArguments()
  101. {
  102. $expected = array('1st', '2nd', '3rd', '4th');
  103. $command = $this->getCommand();
  104. $command->setRawArguments($expected);
  105. $this->assertSame($expected, $command->getArguments());
  106. }
  107. }