/** * Constructor. * * If there is no readline support for the current PHP executable * a \RuntimeException exception is thrown. * * @param Application $application An application instance * * @throws \RuntimeException When Readline extension is not enabled */ public function __construct(Application $application) { if (!function_exists('readline')) { throw new \RuntimeException('Unable to start the shell as the Readline extension is not enabled.'); } $this->application = $application; $this->history = getenv('HOME') . '/.history_' . $application->getName(); $this->output = new ConsoleOutput(); }
public function testExecute() { $application = new Application(); $commandTester = new CommandTester($application->getCommand('list')); $commandTester->execute(array()); $this->assertRegExp('/help Displays help for a command/', $commandTester->getDisplay(), '->execute() returns a list of available commands'); $commandTester->execute(array('--xml' => true)); $this->assertRegExp('/<command id="list" namespace="_global" name="list">/', $commandTester->getDisplay(), '->execute() returns a list of available commands in XML if --xml is passed'); }
public function testExecute() { $command = new HelpCommand(); $command->setCommand(new ListCommand()); $commandTester = new CommandTester($command); $commandTester->execute(array()); $this->assertRegExp('/list \\[--xml\\] \\[namespace\\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command'); $commandTester->execute(array('--xml' => true)); $this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed'); $application = new Application(); $commandTester = new CommandTester($application->getCommand('help')); $commandTester->execute(array('command_name' => 'list')); $this->assertRegExp('/list \\[--xml\\] \\[namespace\\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command'); $commandTester->execute(array('command_name' => 'list', '--xml' => true)); $this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed'); }
/** * Runs the current application. * * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * * @return integer 0 if everything went fine, or an error code */ public function doRun(InputInterface $input, OutputInterface $output) { if (true === $input->hasParameterOption(array('--shell', '-s'))) { $shell = new Shell($this); $shell->run(); return 0; } return parent::doRun($input, $output); }
public function registerCommands(Application $application) { foreach ($application->getKernel()->getBundleDirs() as $dir) { $bundleBase = dirname(str_replace('\\', '/', get_class($this))); $commandDir = $dir . '/' . basename($bundleBase) . '/Command'; if (!is_dir($commandDir)) { continue; } // look for commands foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($commandDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) { if ($file->isDir() || substr($file, -4) !== '.php') { continue; } $class = str_replace('/', '\\', $bundleBase) . '\\Command\\' . str_replace(realpath($commandDir) . '/', '', basename(realpath($file), '.php')); $r = new \ReflectionClass($class); if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract()) { $application->addCommand(new $class()); } } } }
require_once __DIR__.'/../../../../bootstrap.php'; use Symfony\Components\Console\Tester\CommandTester; use Symfony\Components\Console\Command\HelpCommand; use Symfony\Components\Console\Command\ListCommand; use Symfony\Components\Console\Application; $t = new LimeTest(4); // ->execute() $t->diag('->execute()'); $command = new HelpCommand(); $command->setCommand(new ListCommand()); $commandTester = new CommandTester($command); $commandTester->execute(array()); $t->like($commandTester->getDisplay(), '/list \[--xml\] \[namespace\]/', '->execute() returns a text help for the given command'); $commandTester->execute(array('--xml' => true)); $t->like($commandTester->getDisplay(), '/<command/', '->execute() returns an XML help text if --xml is passed'); $application = new Application(); $commandTester = new CommandTester($application->getCommand('help')); $commandTester->execute(array('command_name' => 'list')); $t->like($commandTester->getDisplay(), '/list \[--xml\] \[namespace\]/', '->execute() returns a text help for the given command'); $commandTester->execute(array('command_name' => 'list', '--xml' => true)); $t->like($commandTester->getDisplay(), '/<command/', '->execute() returns an XML help text if --xml is passed');
/** * Constructor. */ public function __construct() { parent::__construct('Symfony', Kernel::VERSION); $this->addCommand(new InitApplicationCommand()); }
$t->is($command->getHelp(), 'help1', '->setHelp() sets the help'); // ->getAliases() ->setAliases() $t->diag('->getAliases() ->setAliases()'); $t->is($command->getAliases(), array('name'), '->getAliases() returns the aliases'); $ret = $command->setAliases(array('name1')); $t->is($ret, $command, '->setAliases() implements a fluent interface'); $t->is($command->getAliases(), array('name1'), '->setAliases() sets the aliases'); // ->getSynopsis() $t->diag('->getSynopsis()'); $t->is($command->getSynopsis(), '%s foobar:bar [--foo] [foo]', '->getSynopsis() returns the synopsis'); // ->mergeApplicationDefinition() $t->diag('->mergeApplicationDefinition()'); $application1 = new Application(); $application1->getDefinition()->addArguments(array(new InputArgument('foo'))); $application1->getDefinition()->addOptions(array(new InputOption('bar'))); $command = new TestCommand(); $command->setApplication($application1); $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo')))); $command->mergeApplicationDefinition(); $t->ok($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments'); $t->ok($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments'); $t->ok($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options'); $t->ok($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options'); $command->mergeApplicationDefinition(); $t->is($command->getDefinition()->getArgumentCount(), 3, '->mergeApplicationDefinition() does not try to merge twice the application arguments and options'); $command = new TestCommand();
public function testRun() { $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $application->addCommand($command = new \Foo1Command()); $_SERVER['argv'] = array('cli.php', 'foo:bar1'); ob_start(); $application->run(); ob_end_clean(); $this->assertEquals('Symfony\\Components\\Console\\Input\\ArgvInput', get_class($command->input), '->run() creates an ArgvInput by default if none is given'); $this->assertEquals('Symfony\\Components\\Console\\Output\\ConsoleOutput', get_class($command->output), '->run() creates a ConsoleOutput by default if none is given'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $tester = new ApplicationTester($application); $tester->run(array()); $this->assertStringEqualsFile(self::$fixturesPath . '/application_run1.txt', $tester->getDisplay(), '->run() runs the list command if no argument is passed'); $tester->run(array('--help' => true)); $this->assertStringEqualsFile(self::$fixturesPath . '/application_run2.txt', $tester->getDisplay(), '->run() runs the help command if --help is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $tester = new ApplicationTester($application); $tester->run(array('command' => 'list', '--help' => true)); $this->assertStringEqualsFile(self::$fixturesPath . '/application_run3.txt', $tester->getDisplay(), '->run() displays the help if --help is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $tester = new ApplicationTester($application); $tester->run(array('--color' => true)); $this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --color is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $tester = new ApplicationTester($application); $tester->run(array('--version' => true)); $this->assertStringEqualsFile(self::$fixturesPath . '/application_run4.txt', $tester->getDisplay(), '->run() displays the program version if --version is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $tester = new ApplicationTester($application); $tester->run(array('command' => 'list', '--quiet' => true)); $this->assertEquals('', $tester->getDisplay(), '->run() removes all output if --quiet is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $tester = new ApplicationTester($application); $tester->run(array('command' => 'list', '--verbose' => true)); $this->assertEquals(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose is --verbose is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $application->addCommand(new \FooCommand()); $tester = new ApplicationTester($application); $tester->run(array('command' => 'foo:bar', '--no-interaction' => true)); $this->assertEquals("called\n", $tester->getDisplay(), '->run() does not called interact() if --no-interaction is passed'); }
public function testMergeApplicationDefinition() { $application1 = new Application(); $application1->getDefinition()->addArguments(array(new InputArgument('foo'))); $application1->getDefinition()->addOptions(array(new InputOption('bar'))); $command = new \TestCommand(); $command->setApplication($application1); $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo')))); $command->mergeApplicationDefinition(); $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments'); $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments'); $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options'); $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options'); $command->mergeApplicationDefinition(); $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options'); $command = new \TestCommand(); $command->mergeApplicationDefinition(); }
<?php /* * This file is part of the symfony package. * (c) Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once __DIR__.'/../../../../bootstrap.php'; use Symfony\Components\Console\Tester\CommandTester; use Symfony\Components\Console\Application; $t = new LimeTest(2); $application = new Application(); // ->execute() $t->diag('->execute()'); $commandTester = new CommandTester($application->getCommand('list')); $commandTester->execute(array()); $t->like($commandTester->getDisplay(), '/help Displays help for a command/', '->execute() returns a list of available commands'); $commandTester->execute(array('--xml' => true)); $t->like($commandTester->getDisplay(), '/<command id="list" namespace="_global" name="list">/', '->execute() returns a list of available commands in XML if --xml is passed');
$t->ok($tester->getOutput()->isDecorated(), '->run() forces color output if --color is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $tester = new ApplicationTester($application); $tester->run(array('--version' => true)); $t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_run4.txt'), '->run() displays the program version if --version is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $tester = new ApplicationTester($application); $tester->run(array('command' => 'list', '--quiet' => true)); $t->is($tester->getDisplay(), '', '->run() removes all output if --quiet is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $tester = new ApplicationTester($application); $tester->run(array('command' => 'list', '--verbose' => true)); $t->is($tester->getOutput()->getVerbosity(), Output::VERBOSITY_VERBOSE, '->run() sets the output to verbose is --verbose is passed'); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); $application->addCommand(new FooCommand()); $tester = new ApplicationTester($application); $tester->run(array('command' => 'foo:bar', '--no-interaction' => true)); $t->is($tester->getDisplay(), "called\n", '->run() does not called interact() if --no-interaction is passed');
* This file is part of the symfony package. * (c) Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once __DIR__.'/../../../../bootstrap.php'; use Symfony\Components\Console\Application; use Symfony\Components\Console\Output\Output; use Symfony\Components\Console\Tester\ApplicationTester; $t = new LimeTest(6); $application = new Application(); $application->setAutoExit(false); $application->register('foo') ->addArgument('command') ->addArgument('foo') ->setCode(function ($input, $output) { $output->writeln('foo'); }) ; $tester = new ApplicationTester($application); $tester->run(array('command' => 'foo', 'foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE)); // ->run() $t->diag('->run()'); $t->is($tester->getInput()->isInteractive(), false, '->execute() takes an interactive option'); $t->is($tester->getOutput()->isDecorated(), false, '->execute() takes a decorated option'); $t->is($tester->getOutput()->getVerbosity(), Output::VERBOSITY_VERBOSE, '->execute() takes a verbosity option');
/** * Finds and registers commands for the current bundle. * * @param Symfony\Components\Console\Application $application An Application instance */ public function registerCommands(Application $application) { if (!($dir = realpath($this->getPath() . '/Command'))) { return; } $finder = new Finder(); $finder->files()->name('*Command.php')->in($dir); $prefix = $this->namespacePrefix . '\\' . $this->name . '\\Command'; foreach ($finder as $file) { $r = new \ReflectionClass($prefix . strtr($file->getPath(), array($dir => '', '/' => '\\')) . '\\' . basename($file, '.php')); if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract()) { $application->addCommand($r->newInstance()); } } }