2 namespace Consolidation\TestUtils;
4 use Consolidation\AnnotatedCommand\CommandData;
5 use Consolidation\AnnotatedCommand\AnnotationData;
6 use Consolidation\AnnotatedCommand\CommandError;
7 use Symfony\Component\Console\Command\Command;
8 use Symfony\Component\Console\Event\ConsoleCommandEvent;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Output\OutputInterface;
14 * Test file used in the Annotation Factory tests. It is also
15 * discovered in the testCommandDiscovery() test.
17 * The testCommandDiscovery test search base is the 'src' directory;
18 * any command files located immediately inside the search base are
19 * eligible for discovery, and will be included in the search results.
21 class ExampleCommandFile
26 public function __construct($state = '')
28 $this->state = $state;
31 public function setOutput($output)
33 $this->output = $output;
37 * This is the my:cat command
39 * This command will concatenate two parameters. If the --flip flag
40 * is provided, then the result is the concatenation of two and one.
42 * @param string $one The first parameter.
43 * @param string $two The other parameter.
44 * @option boolean $flip Whether or not the second parameter should come first in the result.
46 * @usage bet alpha --flip
47 * Concatenate "alpha" and "bet".
48 * @arbitrary This annotation is here merely as a marker used in testing.
50 public function myCat($one, $two = '', $options = ['flip' => false])
52 if ($options['flip']) {
53 return "{$two}{$one}";
55 return "{$one}{$two}";
61 public function myRepeat($one, $two = '', $options = ['repeat' => 1])
63 return str_repeat("{$one}{$two}", $options['repeat']);
67 * This is the my:join command
69 * This command will join its parameters together. It can also reverse and repeat its arguments.
73 * Join a and b to produce "a,b"
75 * Example with no parameters or options
77 public function myJoin(array $args, $options = ['flip' => false, 'repeat' => 1])
79 if ($options['flip']) {
80 $args = array_reverse($args);
82 $result = implode('', $args);
83 return str_repeat($result, $options['repeat']);
87 * This is a command with no options
89 * This command will concatenate two parameters.
91 * @param $one The first parameter.
92 * @param $two The other parameter.
95 * Concatenate "alpha" and "bet".
97 public function commandWithNoOptions($one, $two = 'default')
99 return "{$one}{$two}";
103 * This command has no arguments--only options
105 * Return a result only if not silent.
107 * @option silent Supress output.
109 public function commandWithNoArguments($opts = ['silent|s' => false])
111 if (!$opts['silent']) {
112 return "Hello, world";
117 * Shortcut on annotation
119 * This command defines the option shortcut on the annotation instead of in the options array.
121 * @param $opts The options
122 * @option silent|s Supress output.
124 public function shortcutOnAnnotation($opts = ['silent' => false])
126 if (!$opts['silent']) {
127 return "Hello, world";
132 * This is the test:arithmatic command
134 * This command will add one and two. If the --negate flag
135 * is provided, then the result is negated.
137 * @command test:arithmatic
138 * @param integer $one The first number to add.
139 * @param integer $two The other number to add.
140 * @option negate Whether or not the result should be negated.
141 * @aliases arithmatic
142 * @usage 2 2 --negate
143 * Add two plus two and then negate.
148 public function testArithmatic($one, $two = 2, $options = ['negate' => false, 'unused' => 'bob'])
150 $result = $one + $two;
151 if ($options['negate']) {
155 // Integer return codes are exit codes (errors), so
156 // return a the result as a string so that it will be printed.
161 * This is the test:state command
163 * This command tests to see if the state of the Commandfile instance
165 public function testState()
171 * This is the test:passthrough command
173 * This command takes a variable number of parameters as
174 * an array and returns them as a csv.
176 public function testPassthrough(array $params)
178 return implode(',', $params);
182 * This command wraps its parameter in []; its alter hook
183 * then wraps the result in <>.
185 public function testHook($parameter)
187 return "[$parameter]";
191 * Wrap the results of test:hook in <>.
193 * @hook alter test:hook
195 public function hookTestHook($result)
201 * This test is very similar to the preceding test, except
202 * it uses an annotation hook instead of a named-function hook.
208 public function testAnnotationHook($parameter)
210 return "($parameter)";
214 * Wrap the results of test:hook in whatever the @before and @after
215 * annotations contain.
217 * @hook alter @hookme
219 public function hookTestAnnotatedHook($result, CommandData $commandData)
221 $before = $commandData->annotationData()->get('before', '-');
222 $after = $commandData->annotationData()->get('after', '-');
223 return "$before$result$after";
227 * Alter the results of the hook with its command name.
229 * @hook alter @addmycommandname
231 public function hookAddCommandName($result, CommandData $commandData)
233 $annotationData = $commandData->annotationData();
234 return "$result from " . $annotationData['command'];
238 * Here is a hook with an explicit command annotation that we will alter
239 * with the preceeding hook
244 public function alterMe()
246 return "splendiferous";
250 * Here is another hook that has no command annotation that should be
251 * altered with the default value for the command name
255 public function alterMeToo()
257 return "fantabulous";
261 * @command test:replace-command
263 public function testReplaceCommand($value)
265 $this->output->writeln($value);
269 * @hook replace-command test:replace-command
271 public function hookTestReplaceCommandHook($value)
273 $this->output->writeln("bar");
277 * @hook pre-command test:post-command
279 public function hookTestPreCommandHook(CommandData $commandData)
281 // Use 'writeln' to detect order that hooks are called
282 $this->output->writeln("foo");
286 * @command test:post-command
288 public function testPostCommand($value)
290 $this->output->writeln($value);
294 * @hook post-command test:post-command
296 public function hookTestPostCommandHook($result, CommandData $commandData)
298 // Use 'writeln' to detect order that hooks are called
299 $this->output->writeln("baz");
302 public function testHello($who)
304 return "Hello, $who.";
307 public function testException($what)
309 throw new \Exception($what);
313 * @hook init test:hello
315 public function initializeTestHello($input, AnnotationData $annotationData)
317 $who = $input->getArgument('who');
319 $input->setArgument('who', 'Huey');
324 * @hook command-event test:hello
326 public function commandEventTestHello(ConsoleCommandEvent $event)
328 // Note that Symfony Console will not allow us to alter the
329 // input from this hook, so we'll just print something to
330 // show that this hook was executed.
331 $input = $event->getInput();
332 $who = $input->getArgument('who');
333 $this->output->writeln("Here comes $who!");
337 * @hook interact test:hello
339 public function interactTestHello($input, $output)
341 $who = $input->getArgument('who');
343 $input->setArgument('who', 'Goofey');
348 * @hook validate test:hello
350 public function validateTestHello($commandData)
352 $args = $commandData->arguments();
353 if ($args['who'] == 'Donald Duck') {
354 return new CommandError("I won't say hello to Donald Duck.");
356 if ($args['who'] == 'Drumph') {
357 throw new \Exception('Irrational value error.');
362 * Test default values in arguments
364 * @param string|null $one
365 * @param string|null $two
368 public function defaults($one = null, $two = null)
371 return "$one and $two";
376 return "nothing provided";
382 public function defaultOptionOne($options = ['foo' => '1'])
384 return "Foo is " . $options['foo'];
390 public function defaultOptionTwo($options = ['foo' => '2'])
392 return "Foo is " . $options['foo'];
398 public function defaultOptionNone($options = ['foo' => InputOption::VALUE_REQUIRED])
400 return "Foo is " . $options['foo'];
404 * This is the test:required-array-option command
406 * This command will print all the valused of passed option
411 public function testRequiredArrayOption($opts = ['arr|a' => []])
413 return implode(' ', $opts['arr']);
417 * This is the test:array-option command
419 * This command will print all the valused of passed option
424 public function testArrayOption($opts = ['arr|a' => ['1', '2', '3']])
426 return implode(' ', $opts['arr']);