create-command-test 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of the Predis package.
  5. *
  6. * (c) Daniele Alessandri <suppakilla@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. // -------------------------------------------------------------------------- //
  12. // This script can be used to automatically generate a file with the scheleton
  13. // of a test case to test a Redis command by specifying the name of the class
  14. // in the Predis\Command namespace (only classes in this namespace are valid).
  15. // For example, to generate a test case for SET (which is represented by the
  16. // Predis\Command\StringSet class):
  17. //
  18. // $ ./bin/generate-command-test.php --class=StringSet
  19. //
  20. // Here is a list of optional arguments:
  21. //
  22. // --realm: each command has its own realm (commands that operate on strings,
  23. // lists, sets and such) but while this realm is usually inferred from the name
  24. // of the specified class, sometimes it can be useful to override it with a
  25. // custom one.
  26. //
  27. // --output: write the generated test case to the specified path instead of
  28. // the default one.
  29. //
  30. // --overwrite: pre-existing test files are not overwritten unless this option
  31. // is explicitly specified.
  32. // -------------------------------------------------------------------------- //
  33. use Predis\Command\CommandInterface;
  34. use Predis\Command\PrefixableCommandInterface;
  35. class CommandTestCaseGenerator
  36. {
  37. private $options;
  38. public function __construct(Array $options)
  39. {
  40. if (!isset($options['class'])) {
  41. throw new RuntimeException("Missing 'class' option.");
  42. }
  43. $this->options = $options;
  44. }
  45. public static function fromCommandLine()
  46. {
  47. $parameters = array(
  48. 'c:' => 'class:',
  49. 'r::' => 'realm::',
  50. 'o::' => 'output::',
  51. 'x::' => 'overwrite::'
  52. );
  53. $getops = getopt(implode(array_keys($parameters)), $parameters);
  54. $options = array(
  55. 'overwrite' => false,
  56. 'tests' => __DIR__.'/../tests',
  57. );
  58. foreach ($getops as $option => $value) {
  59. switch ($option) {
  60. case 'c':
  61. case 'class':
  62. $options['class'] = $value;
  63. break;
  64. case 'r':
  65. case 'realm':
  66. $options['realm'] = $value;
  67. break;
  68. case 'o':
  69. case 'output':
  70. $options['output'] = $value;
  71. break;
  72. case 'x':
  73. case 'overwrite':
  74. $options['overwrite'] = true;
  75. break;
  76. }
  77. }
  78. if (!isset($options['class'])) {
  79. throw new RuntimeException("Missing 'class' option.");
  80. }
  81. $options['fqn'] = "Predis\\Command\\{$options['class']}";
  82. $options['path'] = "Predis/Command/{$options['class']}.php";
  83. $source = __DIR__.'/../lib/'.$options['path'];
  84. if (!file_exists($source)) {
  85. throw new RuntimeException("Cannot find class file for {$options['fqn']} in $source.");
  86. }
  87. if (!isset($options['output'])) {
  88. $options['output'] = sprintf("%s/%s", $options['tests'], str_replace('.php', 'Test.php', $options['path']));
  89. }
  90. return new self($options);
  91. }
  92. protected function getTestRealm()
  93. {
  94. if (isset($this->options['realm'])) {
  95. if (!$this->options['realm']) {
  96. throw new RuntimeException('Invalid value for realm has been sepcified (empty).');
  97. }
  98. return $this->options['realm'];
  99. }
  100. $fqnParts = explode('\\', $this->options['fqn']);
  101. $class = array_pop($fqnParts);
  102. list($realm,) = preg_split('/([[:upper:]][[:lower:]]+)/', $class, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  103. return strtolower($realm);
  104. }
  105. public function generate()
  106. {
  107. $reflection = new ReflectionClass($class = $this->options['fqn']);
  108. if (!$reflection->isInstantiable()) {
  109. throw new RuntimeException("Class $class must be instantiable, abstract classes or interfaces are not allowed.");
  110. }
  111. if (!$reflection->implementsInterface('Predis\Command\CommandInterface')) {
  112. throw new RuntimeException("Class $class must implement Predis\Command\CommandInterface.");
  113. }
  114. $instance = $reflection->newInstance();
  115. $buffer = $this->getTestCaseBuffer($instance);
  116. return $buffer;
  117. }
  118. public function save()
  119. {
  120. $options = $this->options;
  121. if (file_exists($options['output']) && !$options['overwrite']) {
  122. throw new RuntimeException("File {$options['output']} already exist. Specify the --overwrite option to overwrite the existing file.");
  123. }
  124. file_put_contents($options['output'], $this->generate());
  125. }
  126. protected function getTestCaseBuffer(CommandInterface $instance)
  127. {
  128. $id = $instance->getId();
  129. $fqn = get_class($instance);
  130. $fqnParts = explode('\\', $fqn);
  131. $class = array_pop($fqnParts) . "Test";
  132. $realm = $this->getTestRealm();
  133. $buffer =<<<PHP
  134. <?php
  135. /*
  136. * This file is part of the Predis package.
  137. *
  138. * (c) Daniele Alessandri <suppakilla@gmail.com>
  139. *
  140. * For the full copyright and license information, please view the LICENSE
  141. * file that was distributed with this source code.
  142. */
  143. namespace Predis\Command;
  144. /**
  145. * @group commands
  146. * @group realm-$realm
  147. */
  148. class $class extends PredisCommandTestCase
  149. {
  150. /**
  151. * {@inheritdoc}
  152. */
  153. protected function getExpectedCommand()
  154. {
  155. return '$fqn';
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. protected function getExpectedId()
  161. {
  162. return '$id';
  163. }
  164. /**
  165. * @group disconnected
  166. */
  167. public function testFilterArguments()
  168. {
  169. \$this->markTestIncomplete('This test has not been implemented yet.');
  170. \$arguments = array(/* add arguments */);
  171. \$expected = array(/* add arguments */);
  172. \$command = \$this->getCommand();
  173. \$command->setArguments(\$arguments);
  174. \$this->assertSame(\$expected, \$command->getArguments());
  175. }
  176. /**
  177. * @group disconnected
  178. */
  179. public function testParseResponse()
  180. {
  181. \$this->markTestIncomplete('This test has not been implemented yet.');
  182. \$raw = null;
  183. \$expected = null;
  184. \$command = \$this->getCommand();
  185. \$this->assertSame(\$expected, \$command->parseResponse(\$raw));
  186. }
  187. PHP;
  188. if ($instance instanceof PrefixableCommandInterface) {
  189. $buffer .=<<<PHP
  190. /**
  191. * @group disconnected
  192. */
  193. public function testPrefixKeys()
  194. {
  195. \$this->markTestIncomplete('This test has not been implemented yet.');
  196. \$arguments = array(/* add arguments */);
  197. \$expected = array(/* add arguments */);
  198. \$command = \$this->getCommandWithArgumentsArray(\$arguments);
  199. \$command->prefixKeys('prefix:');
  200. \$this->assertSame(\$expected, \$command->getArguments());
  201. }
  202. /**
  203. * @group disconnected
  204. */
  205. public function testPrefixKeysIgnoredOnEmptyArguments()
  206. {
  207. \$command = \$this->getCommand();
  208. \$command->prefixKeys('prefix:');
  209. \$this->assertSame(array(), \$command->getArguments());
  210. }
  211. PHP;
  212. }
  213. return "$buffer}\n";
  214. }
  215. }
  216. // ------------------------------------------------------------------------- //
  217. require __DIR__.'/../autoload.php';
  218. $generator = CommandTestCaseGenerator::fromCommandLine();
  219. $generator->save();