PredisCommandTestCase.php 3.0 KB

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