Drupal investigation

testHelp.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace Consolidation\AnnotatedCommand;
  3. use Consolidation\AnnotatedCommand\Help\HelpCommand;
  4. use Consolidation\AnnotatedCommand\AnnotationData;
  5. use Consolidation\AnnotatedCommand\CommandData;
  6. use Consolidation\AnnotatedCommand\CommandProcessor;
  7. use Consolidation\AnnotatedCommand\Hooks\AlterResultInterface;
  8. use Consolidation\AnnotatedCommand\Hooks\ExtractOutputInterface;
  9. use Consolidation\AnnotatedCommand\Hooks\HookManager;
  10. use Consolidation\AnnotatedCommand\Hooks\ProcessResultInterface;
  11. use Consolidation\AnnotatedCommand\Hooks\StatusDeterminerInterface;
  12. use Consolidation\AnnotatedCommand\Hooks\ValidatorInterface;
  13. use Consolidation\AnnotatedCommand\Options\AlterOptionsCommandEvent;
  14. use Consolidation\AnnotatedCommand\Parser\CommandInfo;
  15. use Consolidation\OutputFormatters\FormatterManager;
  16. use Symfony\Component\Console\Application;
  17. use Symfony\Component\Console\Command\Command;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Input\StringInput;
  20. use Symfony\Component\Console\Output\BufferedOutput;
  21. use Symfony\Component\Console\Output\OutputInterface;
  22. use Consolidation\TestUtils\ApplicationWithTerminalWidth;
  23. use Consolidation\AnnotatedCommand\Options\PrepareTerminalWidthOption;
  24. /**
  25. * Test our 'help' command.
  26. */
  27. class HelpTests extends \PHPUnit_Framework_TestCase
  28. {
  29. protected $application;
  30. protected $commandFactory;
  31. function setup()
  32. {
  33. $this->application = new ApplicationWithTerminalWidth('TestApplication', '0.0.0');
  34. $this->commandFactory = new AnnotatedCommandFactory();
  35. // $factory->addListener(...);
  36. $alterOptionsEventManager = new AlterOptionsCommandEvent($this->application);
  37. $eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
  38. $eventDispatcher->addSubscriber($this->commandFactory->commandProcessor()->hookManager());
  39. $eventDispatcher->addSubscriber($alterOptionsEventManager);
  40. $this->application->setDispatcher($eventDispatcher);
  41. $this->application->setAutoExit(false);
  42. $discovery = new CommandFileDiscovery();
  43. $discovery
  44. ->setSearchPattern('*CommandFile.php')
  45. ->setIncludeFilesAtBase(false)
  46. ->setSearchLocations(['alpha']);
  47. chdir(__DIR__);
  48. $commandFiles = $discovery->discover('.', '\Consolidation\TestUtils');
  49. $formatter = new FormatterManager();
  50. $formatter->addDefaultFormatters();
  51. $formatter->addDefaultSimplifiers();
  52. $terminalWidthOption = new PrepareTerminalWidthOption();
  53. $terminalWidthOption->setApplication($this->application);
  54. $this->commandFactory->commandProcessor()->setFormatterManager($formatter);
  55. $this->commandFactory->commandProcessor()->addPrepareFormatter($terminalWidthOption);
  56. $this->commandFactory->setIncludeAllPublicMethods(false);
  57. $this->addDiscoveredCommands($this->commandFactory, $commandFiles);
  58. $helpCommandfile = new HelpCommand($this->application);
  59. $commandList = $this->commandFactory->createCommandsFromClass($helpCommandfile);
  60. foreach ($commandList as $command) {
  61. $this->application->add($command);
  62. }
  63. }
  64. public function addDiscoveredCommands($factory, $commandFiles) {
  65. foreach ($commandFiles as $path => $commandClass) {
  66. $this->assertFileExists($path);
  67. if (!class_exists($commandClass)) {
  68. include $path;
  69. }
  70. $commandInstance = new $commandClass();
  71. $commandList = $factory->createCommandsFromClass($commandInstance);
  72. foreach ($commandList as $command) {
  73. $this->application->add($command);
  74. }
  75. }
  76. }
  77. function assertRunCommandViaApplicationEquals($cmd, $expectedOutput, $expectedStatusCode = 0)
  78. {
  79. $input = new StringInput($cmd);
  80. $output = new BufferedOutput();
  81. $statusCode = $this->application->run($input, $output);
  82. $commandOutput = trim($output->fetch());
  83. $expectedOutput = $this->simplifyWhitespace($expectedOutput);
  84. $commandOutput = $this->simplifyWhitespace($commandOutput);
  85. $this->assertEquals($expectedOutput, $commandOutput);
  86. $this->assertEquals($expectedStatusCode, $statusCode);
  87. }
  88. function simplifyWhitespace($data)
  89. {
  90. return trim(preg_replace('#[ \t]+$#m', '', $data));
  91. }
  92. function testHelp()
  93. {
  94. $expectedXML = <<<EOT
  95. <?xml version="1.0" encoding="UTF-8"?>
  96. <command id="example:table" name="example:table">
  97. <usages>
  98. <usage>example:table [--format [FORMAT]] [--fields [FIELDS]] [--field [FIELD]] [--] [&lt;unused&gt;]</usage>
  99. </usages>
  100. <examples>
  101. <example>
  102. <usage>example:table --format=yml</usage>
  103. <description>Show the example table in yml format.</description>
  104. </example>
  105. <example>
  106. <usage>example:table --fields=first,third</usage>
  107. <description>Show only the first and third fields in the table.</description>
  108. </example>
  109. <example>
  110. <usage>example:table --fields=II,III</usage>
  111. <description>Note that either the field ID or the visible field label may be used.</description>
  112. </example>
  113. </examples>
  114. <description>Test command with formatters</description>
  115. <arguments>
  116. <argument name="unused" is_required="0" is_array="0">
  117. <description>An unused argument</description>
  118. <defaults/>
  119. </argument>
  120. </arguments>
  121. <options>
  122. <option name="--format" shortcut="" accept_value="1" is_value_required="0" is_multiple="0">
  123. <description>Format the result data. Available formats: csv,json,list,php,print-r,sections,string,table,tsv,var_export,xml,yaml</description>
  124. <defaults>
  125. <default>table</default>
  126. </defaults>
  127. </option>
  128. <option name="--fields" shortcut="" accept_value="1" is_value_required="0" is_multiple="0">
  129. <description>Available fields: I (first), II (second), III (third)</description>
  130. <defaults/>
  131. </option>
  132. <option name="--field" shortcut="" accept_value="1" is_value_required="0" is_multiple="0">
  133. <description>Select just one field, and force format to 'string'.</description>
  134. <defaults/>
  135. </option>
  136. <option name="--help" shortcut="-h" accept_value="0" is_value_required="0" is_multiple="0">
  137. <description>Display this help message</description>
  138. </option>
  139. <option name="--quiet" shortcut="-q" accept_value="0" is_value_required="0" is_multiple="0">
  140. <description>Do not output any message</description>
  141. </option>
  142. <option name="--verbose" shortcut="-v" shortcuts="-v|-vv|-vvv" accept_value="0" is_value_required="0" is_multiple="0">
  143. <description>Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug</description>
  144. </option>
  145. <option name="--version" shortcut="-V" accept_value="0" is_value_required="0" is_multiple="0">
  146. <description>Display this application version</description>
  147. </option>
  148. <option name="--ansi" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
  149. <description>Force ANSI output</description>
  150. </option>
  151. <option name="--no-ansi" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
  152. <description>Disable ANSI output</description>
  153. </option>
  154. <option name="--no-interaction" shortcut="-n" accept_value="0" is_value_required="0" is_multiple="0">
  155. <description>Do not ask any interactive question</description>
  156. </option>
  157. </options>
  158. <help>Test command with formatters</help>
  159. <aliases>
  160. <alias>extab</alias>
  161. </aliases>
  162. <topics>
  163. <topic>docs-tables</topic>
  164. </topics>
  165. </command>
  166. EOT;
  167. $this->assertRunCommandViaApplicationEquals('my-help --format=xml example:table', $expectedXML);
  168. $expectedJSON = <<<EOT
  169. {
  170. "id": "example:table",
  171. "name": "example:table",
  172. "usages": [
  173. "example:table [--format [FORMAT]] [--fields [FIELDS]] [--field [FIELD]] [--] [<unused>]"
  174. ],
  175. "examples": [
  176. {
  177. "usage": "example:table --format=yml",
  178. "description": "Show the example table in yml format."
  179. },
  180. {
  181. "usage": "example:table --fields=first,third",
  182. "description": "Show only the first and third fields in the table."
  183. },
  184. {
  185. "usage": "example:table --fields=II,III",
  186. "description": "Note that either the field ID or the visible field label may be used."
  187. }
  188. ],
  189. "description": "Test command with formatters",
  190. "arguments": {
  191. "unused": {
  192. "name": "unused",
  193. "is_required": "0",
  194. "is_array": "0",
  195. "description": "An unused argument"
  196. }
  197. },
  198. "options": {
  199. "format": {
  200. "name": "--format",
  201. "shortcut": "",
  202. "accept_value": "1",
  203. "is_value_required": "0",
  204. "is_multiple": "0",
  205. "description": "Format the result data. Available formats: csv,json,list,php,print-r,sections,string,table,tsv,var_export,xml,yaml",
  206. "defaults": [
  207. "table"
  208. ]
  209. },
  210. "fields": {
  211. "name": "--fields",
  212. "shortcut": "",
  213. "accept_value": "1",
  214. "is_value_required": "0",
  215. "is_multiple": "0",
  216. "description": "Available fields: I (first), II (second), III (third)"
  217. },
  218. "field": {
  219. "name": "--field",
  220. "shortcut": "",
  221. "accept_value": "1",
  222. "is_value_required": "0",
  223. "is_multiple": "0",
  224. "description": "Select just one field, and force format to 'string'."
  225. },
  226. "help": {
  227. "name": "--help",
  228. "shortcut": "-h",
  229. "accept_value": "0",
  230. "is_value_required": "0",
  231. "is_multiple": "0",
  232. "description": "Display this help message"
  233. },
  234. "quiet": {
  235. "name": "--quiet",
  236. "shortcut": "-q",
  237. "accept_value": "0",
  238. "is_value_required": "0",
  239. "is_multiple": "0",
  240. "description": "Do not output any message"
  241. },
  242. "verbose": {
  243. "name": "--verbose",
  244. "shortcut": "-v",
  245. "shortcuts": "-v|-vv|-vvv",
  246. "accept_value": "0",
  247. "is_value_required": "0",
  248. "is_multiple": "0",
  249. "description": "Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug"
  250. },
  251. "version": {
  252. "name": "--version",
  253. "shortcut": "-V",
  254. "accept_value": "0",
  255. "is_value_required": "0",
  256. "is_multiple": "0",
  257. "description": "Display this application version"
  258. },
  259. "ansi": {
  260. "name": "--ansi",
  261. "shortcut": "",
  262. "accept_value": "0",
  263. "is_value_required": "0",
  264. "is_multiple": "0",
  265. "description": "Force ANSI output"
  266. },
  267. "no-ansi": {
  268. "name": "--no-ansi",
  269. "shortcut": "",
  270. "accept_value": "0",
  271. "is_value_required": "0",
  272. "is_multiple": "0",
  273. "description": "Disable ANSI output"
  274. },
  275. "no-interaction": {
  276. "name": "--no-interaction",
  277. "shortcut": "-n",
  278. "accept_value": "0",
  279. "is_value_required": "0",
  280. "is_multiple": "0",
  281. "description": "Do not ask any interactive question"
  282. }
  283. },
  284. "help": "Test command with formatters",
  285. "alias": "extab",
  286. "topics": [
  287. "docs-tables"
  288. ]
  289. }
  290. EOT;
  291. $this->assertRunCommandViaApplicationEquals('my-help --format=json example:table', $expectedJSON);
  292. }
  293. }