public function testArguments() { $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name')))); $this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument'); $input->setArgument('name', 'bar'); $this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument'); $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->getArguments() returns all argument values'); $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')))); $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments'); $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones'); try { $input->setArgument('foo', 'bar'); $this->fail('->setArgument() throws a \\InvalidArgumentException if the argument does not exist'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->setOption() throws a \\InvalidArgumentException if the option does not exist'); $this->assertEquals('The "foo" argument does not exist.', $e->getMessage()); } try { $input->getArgument('foo'); $this->fail('->getArgument() throws a \\InvalidArgumentException if the argument does not exist'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->setOption() throws a \\InvalidArgumentException if the option does not exist'); $this->assertEquals('The "foo" argument does not exist.', $e->getMessage()); } }
protected function execute(InputInterface $input, OutputInterface $output) { $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); $listInput = new ArrayInput([], $listCommand->getDefinition()); $listInput->setArgument('user_id', $input->getArgument('user_id')); $listInput->setOption('output', 'json_pretty'); $listInput->setOption('show-password', true); $listInput->setOption('full', true); $listCommand->execute($listInput, $output); }
protected function getInput(Command $command, array $arguments = [], array $options = []) { $input = new ArrayInput([]); $input->bind($command->getDefinition()); foreach ($arguments as $key => $value) { $input->setArgument($key, $value); } foreach ($options as $key => $value) { $input->setOption($key, $value); } return $input; }
/** * @param InputInterface $input * @param $appDefinition * @return ArrayInput */ public function filterByOriginalDefinition(InputInterface $input, $appDefinition) { $newDefinition = new InputDefinition(); $newInput = new ArrayInput(array(), $newDefinition); foreach ($input->getArguments() as $name => $value) { if (!$appDefinition->hasArgument($name)) { $newDefinition->addArgument($this->getDefinition()->getArgument($name)); if (!empty($value)) { $newInput->setArgument($name, $value); } } } foreach ($input->getOptions() as $name => $value) { if (!$appDefinition->hasOption($name)) { $newDefinition->addOption($this->getDefinition()->getOption($name)); if (!empty($value)) { $newInput->setOption($name, $value); } } } return $newInput; }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The "foo" argument does not exist. */ public function testSetInvalidArgument() { $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')))); $input->setArgument('foo', 'bar'); }