Skip to content

Default command #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Application
private $autoExit;
private $definition;
private $helperSet;

private $defaultCommandName = null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS here (and docs)

/**
* Constructor.
*
Expand All @@ -80,6 +80,28 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
}
}

/**
* Sets the name of the default command, this command will be executed if
* no command is provided in the CLI arguments
* @param String $command
*/
public function setDefaultCommandName($command)
{
if ("string" != gettype($command)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_string may be simpler and faster

throw new \UnexpectedValueException('Default Command Name must be a String');
}
$this->defaultCommandName = $command;
}
/**
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, additional (empty) line required

* gets the name of the default command, this command will be executed if
* no command is provided in the CLI arguments

* @return String
*/
public function getDefaultCommandName()
{
return $this->defaultCommandName ;
}
/**
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, additional (empty) line required

* Runs the current application.
*
Expand All @@ -96,6 +118,15 @@ public function run(InputInterface $input = null, OutputInterface $output = null
{
if (null === $input) {
$input = new ArgvInput();
// if we have a default command and we can not decide which command
// to execute we execute the default one
if (! is_null($this->getDefaultcommandName())) {
if (! $this->has($this->getCommandName($input))) {
$argv = $_SERVER['argv'];
array_splice($argv, 1, 0, $this->getDefaultcommandName());
$input = new ArgvInput($argv);
}
}
}

if (null === $output) {
Expand Down
94 changes: 94 additions & 0 deletions Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,84 @@ public function testRun()
$this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
}

/*
* @depends testSetGetDefaultCommandName
*/
public function testRun_defaultCommand()
{
$application = new Application();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
$application->add($command = new \Foo1Command());
$application->setDefaultCommandName('foo:bar1');
$_SERVER['argv'] = array('cli.php');

ob_start();
$application->run();
ob_end_clean();

$this->assertSame('Symfony\Component\Console\Input\ArgvInput', get_class($command->input), '->run() creates an ArgvInput by default if none is given');
$this->assertSame('Symfony\Component\Console\Output\ConsoleOutput', get_class($command->output), '->run() creates a ConsoleOutput by default if none is given');

$application = new Application();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the following assertions (till row 495) really needed?

$application->setAutoExit(false);
$application->setCatchExceptions(false);

$this->ensureStaticCommandHelp($application);
$tester = new ApplicationTester($application);

$tester->run(array(), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the list command if no argument is passed');

$tester->run(array('--help' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the help command if --help is passed');

$tester->run(array('-h' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the help command if -h is passed');

$tester->run(array('command' => 'list', '--help' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the help if --help is passed');

$tester->run(array('command' => 'list', '-h' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the help if -h is passed');

$tester->run(array('--ansi' => true));
$this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --ansi is passed');

$tester->run(array('--no-ansi' => true));
$this->assertFalse($tester->getOutput()->isDecorated(), '->run() forces color output to be disabled if --no-ansi is passed');

$tester->run(array('--version' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the program version if --version is passed');

$tester->run(array('-V' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the program version if -v is passed');

$tester->run(array('command' => 'list', '--quiet' => true));
$this->assertSame('', $tester->getDisplay(), '->run() removes all output if --quiet is passed');

$tester->run(array('command' => 'list', '-q' => true));
$this->assertSame('', $tester->getDisplay(), '->run() removes all output if -q is passed');

$tester->run(array('command' => 'list', '--verbose' => true));
$this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if --verbose is passed');

$tester->run(array('command' => 'list', '-v' => true));
$this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');

$application = new Application();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
$application->add(new \FooCommand());
$tester = new ApplicationTester($application);

$tester->run(array('command' => 'foo:bar', '--no-interaction' => true), array('decorated' => false));
$this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if --no-interaction is passed');

$tester->run(array('command' => 'foo:bar', '-n' => true), array('decorated' => false));
$this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
}

/**
* @expectedException \LogicException
* @dataProvider getAddingAlreadySetDefinitionElementData
Expand Down Expand Up @@ -445,4 +523,20 @@ public function getAddingAlreadySetDefinitionElementData()
array(new InputOption('query', 'q', InputOption::VALUE_NONE)),
);
}

public function testSetGetDefaultCommandName()
{
$application = new Application();
$application->setDefaultCommandName('foo');
$this->assertEquals('foo', $application->getDefaultCommandName());
}

/**
* @expectedException \UnexpectedValueException
*/
public function testSetGetDefaultCommandName_InvalidParameter()
{
$application = new Application();
$application->setDefaultCommandName(new \stdClass);
}
}