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