2 namespace Consolidation\AnnotatedCommand;
4 use Consolidation\AnnotatedCommand\Help\HelpCommand;
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;
28 * Test our 'help' command.
30 class HelpTests extends \PHPUnit_Framework_TestCase
32 protected $application;
33 protected $commandFactory;
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 $eventDispatcher->addSubscriber($alterOptionsEventManager);
44 $this->application->setDispatcher($eventDispatcher);
45 $this->application->setAutoExit(false);
47 $discovery = new CommandFileDiscovery();
49 ->setSearchPattern('*CommandFile.php')
50 ->setIncludeFilesAtBase(false)
51 ->setSearchLocations(['alpha']);
54 $commandFiles = $discovery->discover('.', '\Consolidation\TestUtils');
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);
64 $this->commandFactory->setIncludeAllPublicMethods(false);
65 $this->addDiscoveredCommands($this->commandFactory, $commandFiles);
67 $helpCommandfile = new HelpCommand($this->application);
68 $commandList = $this->commandFactory->createCommandsFromClass($helpCommandfile);
69 foreach ($commandList as $command) {
70 $this->application->add($command);
74 public function addDiscoveredCommands($factory, $commandFiles) {
75 foreach ($commandFiles as $path => $commandClass) {
76 $this->assertFileExists($path);
77 if (!class_exists($commandClass)) {
80 $commandInstance = new $commandClass();
81 $commandList = $factory->createCommandsFromClass($commandInstance);
82 foreach ($commandList as $command) {
83 $this->application->add($command);
88 function assertRunCommandViaApplicationEquals($cmd, $expectedOutput, $expectedStatusCode = 0)
90 $input = new StringInput($cmd);
91 $output = new BufferedOutput();
93 $statusCode = $this->application->run($input, $output);
94 $commandOutput = trim($output->fetch());
96 $expectedOutput = $this->simplifyWhitespace($expectedOutput);
97 $commandOutput = $this->simplifyWhitespace($commandOutput);
99 $this->assertEquals($expectedOutput, $commandOutput);
100 $this->assertEquals($expectedStatusCode, $statusCode);
103 function simplifyWhitespace($data)
105 return trim(preg_replace('#[ \t]+$#m', '', $data));
110 $expectedXML = <<<EOT
111 <?xml version="1.0" encoding="UTF-8"?>
112 <command id="example:table" name="example:table">
114 <usage>example:table [--format [FORMAT]] [--fields [FIELDS]] [--field [FIELD]] [--] [<unused>]</usage>
118 <usage>example:table --format=yml</usage>
119 <description>Show the example table in yml format.</description>
122 <usage>example:table --fields=first,third</usage>
123 <description>Show only the first and third fields in the table.</description>
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>
130 <description>Test command with formatters</description>
132 <argument name="unused" is_required="0" is_array="0">
133 <description>An unused argument</description>
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>
141 <default>table</default>
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>
148 <option name="--field" shortcut="" accept_value="1" is_value_required="0" is_multiple="0">
149 <description>Select just one field, and force format to 'string'.</description>
152 <option name="--help" shortcut="-h" accept_value="0" is_value_required="0" is_multiple="0">
153 <description>Display this help message</description>
155 <option name="--quiet" shortcut="-q" accept_value="0" is_value_required="0" is_multiple="0">
156 <description>Do not output any message</description>
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>
161 <option name="--version" shortcut="-V" accept_value="0" is_value_required="0" is_multiple="0">
162 <description>Display this application version</description>
164 <option name="--ansi" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
165 <description>Force ANSI output</description>
167 <option name="--no-ansi" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
168 <description>Disable ANSI output</description>
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>
174 <help>Test command with formatters</help>
179 <topic>docs-tables</topic>
184 $this->assertRunCommandViaApplicationEquals('my-help --format=xml example:table', $expectedXML);
186 $expectedJSON = <<<EOT
188 "id": "example:table",
189 "name": "example:table",
191 "example:table [--format [FORMAT]] [--fields [FIELDS]] [--field [FIELD]] [--] [<unused>]"
195 "usage": "example:table --format=yml",
196 "description": "Show the example table in yml format."
199 "usage": "example:table --fields=first,third",
200 "description": "Show only the first and third fields in the table."
203 "usage": "example:table --fields=II,III",
204 "description": "Note that either the field ID or the visible field label may be used."
207 "description": "Test command with formatters",
213 "description": "An unused argument"
221 "is_value_required": "0",
223 "description": "Format the result data. Available formats: csv,json,list,php,print-r,sections,string,table,tsv,var_export,xml,yaml",
232 "is_value_required": "0",
234 "description": "Available fields: I (first), II (second), III (third)"
240 "is_value_required": "0",
242 "description": "Select just one field, and force format to 'string'."
248 "is_value_required": "0",
250 "description": "Display this help message"
256 "is_value_required": "0",
258 "description": "Do not output any message"
263 "shortcuts": "-v|-vv|-vvv",
265 "is_value_required": "0",
267 "description": "Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug"
273 "is_value_required": "0",
275 "description": "Display this application version"
281 "is_value_required": "0",
283 "description": "Force ANSI output"
289 "is_value_required": "0",
291 "description": "Disable ANSI output"
294 "name": "--no-interaction",
297 "is_value_required": "0",
299 "description": "Do not ask any interactive question"
302 "help": "Test command with formatters",
309 $this->assertRunCommandViaApplicationEquals('my-help --format=json example:table', $expectedJSON);