PredisCommandTestCase.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /**
  14. *
  15. */
  16. abstract class PredisCommandTestCase extends PredisTestCase
  17. {
  18. /**
  19. * Returns the expected command.
  20. *
  21. * @return CommandInterface|string Instance or FQN of the expected command.
  22. */
  23. abstract protected function getExpectedCommand();
  24. /**
  25. * Returns the expected command ID.
  26. *
  27. * @return string
  28. */
  29. abstract protected function getExpectedId();
  30. /**
  31. * Returns a new command instance.
  32. *
  33. * @return CommandInterface
  34. */
  35. public function getCommand()
  36. {
  37. $command = $this->getExpectedCommand();
  38. return $command instanceof CommandInterface ? $command : new $command();
  39. }
  40. /**
  41. * Returns a new client instance.
  42. *
  43. * @param bool $flushdb Flush selected database before returning the client.
  44. * @return Client
  45. */
  46. public function getClient($flushdb = true)
  47. {
  48. $profile = $this->getProfile();
  49. if (!$profile->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 bool
  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. }