public function testOptions() { $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name')))); $this->assertEquals('foo', $input->getOption('name'), '->getOption() returns the value for the given option'); $input->setOption('name', 'bar'); $this->assertEquals('bar', $input->getOption('name'), '->setOption() sets the value for a given option'); $this->assertEquals(array('name' => 'bar'), $input->getOptions(), '->getOptions() returns all option values'); $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')))); $this->assertEquals('default', $input->getOption('bar'), '->getOption() returns the default value for optional options'); $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getOptions(), '->getOptions() returns all option values, even optional ones'); try { $input->setOption('foo', 'bar'); $this->fail('->setOption() throws a \\InvalidArgumentException if the option does not exist'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->setOption() throws a \\InvalidArgumentException if the option does not exist'); $this->assertEquals('The "foo" option does not exist.', $e->getMessage()); } try { $input->getOption('foo'); $this->fail('->getOption() throws a \\InvalidArgumentException if the option does not exist'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->setOption() throws a \\InvalidArgumentException if the option does not exist'); $this->assertEquals('The "foo" option does not exist.', $e->getMessage()); } }
/** * Initialize the command and determine which path to take * * @param InputInterface $input * @param OutputInterface $output * @internal param $ Symfony\Component\Console\Input\InputInterface * @internal param $ Symfony\Component\Console\Output\OutputInterface * @return void */ protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); switch ($this->input->getArgument('key')) { case 'folder': $this->newFolder(); break; case 'site': $this->newSite(); break; case 'database': $this->newDatabase(); break; case 'variable': $this->newVariable(); break; case 'list': $this->listKeys(); break; default: $helpCommand = $this->getApplication()->find('config:new'); $input = new ArrayInput(['key' => 'list', '--file' => $input->getOption('file')]); $helpCommand->run($input, $output); break; } }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The "foo" option does not exist. */ public function testGetInvalidOption() { $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')))); $input->getOption('foo'); }
/** * @param ScheduledCommand $scheduledCommand * @param OutputInterface $output * @param InputInterface $input */ private function executeCommand(ScheduledCommand $scheduledCommand, OutputInterface $output, InputInterface $input) { $scheduledCommand = $this->em->merge($scheduledCommand); $scheduledCommand->setLastExecution(new \DateTime()); $scheduledCommand->setLocked(true); $this->em->flush(); try { $command = $this->getApplication()->find($scheduledCommand->getCommand()); } catch (\InvalidArgumentException $e) { $scheduledCommand->setLastReturnCode(-1); $output->writeln('<error>Cannot find ' . $scheduledCommand->getCommand() . '</error>'); return; } $input = new ArrayInput(array_merge(array('command' => $scheduledCommand->getCommand(), '--env' => $input->getOption('env')), $scheduledCommand->getArguments(true))); // Use a StreamOutput or NullOutput to redirect write() and writeln() in a log file if (false === $this->logPath && "" != $scheduledCommand->getLogFile()) { $logOutput = new NullOutput(); } else { $logOutput = new StreamOutput(fopen($this->logPath . $scheduledCommand->getLogFile(), 'a', false), $this->commandsVerbosity); } // Execute command and get return code try { $output->writeln('<info>Execute</info> : <comment>' . $scheduledCommand->getCommand() . ' ' . $scheduledCommand->getArguments() . '</comment>'); $result = $command->run($input, $logOutput); } catch (\Exception $e) { $logOutput->writeln($e->getMessage()); $logOutput->writeln($e->getTraceAsString()); $result = -1; } if (false === $this->em->isOpen()) { $output->writeln('<comment>Entity manager closed by the last command.</comment>'); $this->em = $this->em->create($this->em->getConnection(), $this->em->getConfiguration()); } $scheduledCommand = $this->em->merge($scheduledCommand); $scheduledCommand->setLastReturnCode($result); $scheduledCommand->setLocked(false); $scheduledCommand->setExecuteImmediately(false); $this->em->flush(); /* * This clear() is necessary to avoid conflict between commands and to be sure that none entity are managed * before entering in a new command */ $this->em->clear(); unset($command); gc_collect_cycles(); }
/** * @param OutputInterface $output * * @return int 0 on success */ protected function install(OutputInterface $output) { $io = $this->getIO(); $composer = $this->getComposer(true, false); $composer->getDownloadManager()->setOutputProgress(true); $input = new ArrayInput([], (new InstallCommand())->getDefinition()); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'install', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $install = Installer::create($io, $composer); $preferSource = false; $preferDist = false; $config = $composer->getConfig(); switch ($config->get('preferred-install')) { case 'source': $preferSource = true; break; case 'dist': $preferDist = true; break; case 'auto': default: // noop break; } $install->setDryRun(false)->setVerbose(false)->setPreferSource($preferSource)->setPreferDist($preferDist)->setDevMode(true)->setDumpAutoloader(true)->setRunScripts(!$input->getOption('no-scripts'))->setOptimizeAutoloader(false)->setClassMapAuthoritative(false)->setIgnorePlatformRequirements(false); return $install->run(); }