Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 065f3d5

Browse filesBrowse files
committed
Introduce unit test for default command with options
1 parent 00b1d79 commit 065f3d5
Copy full SHA for 065f3d5

File tree

Expand file treeCollapse file tree

4 files changed

+67
-13
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+67
-13
lines changed

‎src/Symfony/Component/Console/Application.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Application.php
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,16 @@ public function doRun(InputInterface $input, OutputInterface $output)
179179

180180
if (!$name) {
181181
$name = $this->defaultCommand;
182-
$inputArgument = $this->definition->getArgument('command');
183-
$inputArgument->setMode(InputArgument::OPTIONAL);
184-
$inputArgument->setDefault($name);
182+
$this->definition->setArguments(array_merge(
183+
$this->definition->getArguments(),
184+
array(
185+
'command' => new InputArgument(
186+
'command',
187+
InputArgument::OPTIONAL,
188+
$this->definition->getArgument('command')->getDescription(),
189+
$name),
190+
)
191+
));
185192
}
186193

187194
// the command name MUST be the first element of the input

‎src/Symfony/Component/Console/Input/InputArgument.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/InputArgument.php
-10Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,6 @@ public function setDefault($default = null)
106106
$this->default = $default;
107107
}
108108

109-
/**
110-
* Sets argument's mode.
111-
*
112-
* @param int $mode
113-
*/
114-
public function setMode($mode)
115-
{
116-
$this->mode = $mode;
117-
}
118-
119109
/**
120110
* Returns the default value.
121111
*

‎src/Symfony/Component/Console/Tests/ApplicationTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/ApplicationTest.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static function setUpBeforeClass()
3939
{
4040
self::$fixturesPath = realpath(__DIR__.'/Fixtures/');
4141
require_once self::$fixturesPath.'/FooCommand.php';
42+
require_once self::$fixturesPath.'/FooOptCommand.php';
4243
require_once self::$fixturesPath.'/Foo1Command.php';
4344
require_once self::$fixturesPath.'/Foo2Command.php';
4445
require_once self::$fixturesPath.'/Foo3Command.php';
@@ -1135,6 +1136,21 @@ public function testSetRunCustomDefaultCommand()
11351136
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
11361137
}
11371138

1139+
public function testSetRunCustomDefaultCommandWithOption()
1140+
{
1141+
$command = new \FooOptCommand();
1142+
1143+
$application = new Application();
1144+
$application->setAutoExit(false);
1145+
$application->add($command);
1146+
$application->setDefaultCommand($command->getName());
1147+
1148+
$tester = new ApplicationTester($application);
1149+
$tester->run(array('--fooopt' => 'opt'));
1150+
1151+
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL.'opt'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1152+
}
1153+
11381154
/**
11391155
* @requires function posix_isatty
11401156
*/
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Input\InputOption;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class FooOptCommand extends Command
9+
{
10+
public $input;
11+
public $output;
12+
13+
protected function configure()
14+
{
15+
$this
16+
->setName('foo:bar')
17+
->setDescription('The foo:bar command')
18+
->setAliases(array('afoobar'))
19+
->addOption(
20+
'fooopt',
21+
'fo',
22+
InputOption::VALUE_OPTIONAL,
23+
'fooopt description'
24+
)
25+
;
26+
}
27+
28+
protected function interact(InputInterface $input, OutputInterface $output)
29+
{
30+
$output->writeln('interact called');
31+
}
32+
33+
protected function execute(InputInterface $input, OutputInterface $output)
34+
{
35+
$this->input = $input;
36+
$this->output = $output;
37+
38+
$output->writeln('called');
39+
$output->writeln($this->input->getOption('fooopt'));
40+
}
41+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.