Пример #1
0
 public function testParse()
 {
     $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
     $this->assertEquals($input->getArguments(), array('name' => 'foo'), '->parse() parses required arguments');
     try {
         $input = new ArrayInput(array('foo' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
         $this->fail('->parse() throws an \\InvalidArgumentException exception if an invalid argument is passed');
     } catch (\RuntimeException $e) {
     }
     $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo'))));
     $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options');
     $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default'))));
     $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options with a default value');
     $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default'))));
     $this->assertEquals($input->getOptions(), array('foo' => 'default'), '->parse() parses long options with a default value');
     try {
         $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
         $this->fail('->parse() throws an \\InvalidArgumentException exception if a required option is passed without a value');
     } catch (\RuntimeException $e) {
     }
     try {
         $input = new ArrayInput(array('--foo' => 'foo'), new InputDefinition());
         $this->fail('->parse() throws an \\InvalidArgumentException exception if an invalid option is passed');
     } catch (\RuntimeException $e) {
     }
     $input = new ArrayInput(array('-f' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f'))));
     $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses short options');
     try {
         $input = new ArrayInput(array('-o' => 'foo'), new InputDefinition());
         $this->fail('->parse() throws an \\InvalidArgumentException exception if an invalid option is passed');
     } catch (\RuntimeException $e) {
     }
 }
Пример #2
0
 public function testSetFetInteractive()
 {
     $input = new ArrayInput(array());
     $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
     $input->setInteractive(false);
     $this->assertTrue(!$input->isInteractive(), '->setInteractive() changes the interactive flag');
 }
Пример #3
0
 public function testParse()
 {
     $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
     $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
     try {
         $input = new ArrayInput(array('foo' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
         $this->fail('->parse() throws an \\InvalidArgumentException exception if an invalid argument is passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->parse() throws an \\InvalidArgumentException exception if an invalid argument is passed');
         $this->assertEquals('The "foo" argument does not exist.', $e->getMessage(), '->parse() throws an \\InvalidArgumentException exception if an invalid argument is passed');
     }
     $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo'))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options');
     $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default'))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a default value');
     $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default'))));
     $this->assertEquals(array('foo' => 'default'), $input->getOptions(), '->parse() parses long options with a default value');
     try {
         $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
         $this->fail('->parse() throws an \\InvalidArgumentException exception if a required option is passed without a value');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->parse() throws an \\InvalidArgumentException exception if a required option is passed without a value');
         $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws an \\InvalidArgumentException exception if a required option is passed without a value');
     }
     try {
         $input = new ArrayInput(array('--foo' => 'foo'), new InputDefinition());
         $this->fail('->parse() throws an \\InvalidArgumentException exception if an invalid option is passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->parse() throws an \\InvalidArgumentException exception if an invalid option is passed');
         $this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws an \\InvalidArgumentException exception if an invalid option is passed');
     }
     $input = new ArrayInput(array('-f' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f'))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options');
     try {
         $input = new ArrayInput(array('-o' => 'foo'), new InputDefinition());
         $this->fail('->parse() throws an \\InvalidArgumentException exception if an invalid option is passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->parse() throws an \\InvalidArgumentException exception if an invalid option is passed');
         $this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws an \\InvalidArgumentException exception if an invalid option is passed');
     }
 }
Пример #4
0
 /**
  * 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)
 {
     $name = $input->getFirstArgument('command');
     if (true === $input->hasParameterOption(array('--color', '-c'))) {
         $output->setDecorated(true);
     }
     if (true === $input->hasParameterOption(array('--help', '-H'))) {
         if (!$name) {
             $name = 'help';
             $input = new ArrayInput(array('command' => 'help'));
         } else {
             $this->wantHelps = true;
         }
     }
     if (true === $input->hasParameterOption(array('--no-interaction', '-n'))) {
         $input->setInteractive(false);
     }
     if (true === $input->hasParameterOption(array('--quiet', '-q'))) {
         $output->setVerbosity(Output::VERBOSITY_QUIET);
     } elseif (true === $input->hasParameterOption(array('--verbose', '-v'))) {
         $output->setVerbosity(Output::VERBOSITY_VERBOSE);
     }
     if (true === $input->hasParameterOption(array('--version', '-V'))) {
         $output->writeln($this->getLongVersion());
         return 0;
     }
     if (!$name) {
         $name = 'list';
         $input = new ArrayInput(array('command' => 'list'));
     }
     // the command name MUST be the first element of the input
     $command = $this->findCommand($name);
     $this->runningCommand = $command;
     $statusCode = $command->run($input, $output);
     $this->runningCommand = null;
     return is_numeric($statusCode) ? $statusCode : 0;
 }
Пример #5
0
try
{
  $input->validate();
  $t->fail('->validate() throws a \RuntimeException if not enough arguments are given');
}
catch (\RuntimeException $e)
{
  $t->pass('->validate() throws a \RuntimeException if not enough arguments are given');
}

$input = new ArrayInput(array('name' => 'foo'));
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));

try
{
  $input->validate();
  $t->pass('->validate() does not throw a \RuntimeException if enough arguments are given');
}
catch (\RuntimeException $e)
{
  $t->fail('->validate() does not throw a \RuntimeException if enough arguments are given');
}

// ->setInteractive() ->isInteractive()
$t->diag('->setInteractive() ->isInteractive()');
$input = new ArrayInput(array());
$t->ok($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
$input->setInteractive(false);
$t->ok(!$input->isInteractive(), '->setInteractive() changes the interactive flag');
Пример #6
0
  $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
  $t->fail('->parse() throws an \InvalidArgumentException exception if a required option is passed without a value');
}
catch (\RuntimeException $e)
{
  $t->pass('->parse() throws an \InvalidArgumentException exception if a required option is passed without a value');
}

try
{
  $input = new ArrayInput(array('--foo' => 'foo'), new InputDefinition());
  $t->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
}
catch (\RuntimeException $e)
{
  $t->pass('->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
}

$input = new ArrayInput(array('-f' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f'))));
$t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses short options');

try
{
  $input = new ArrayInput(array('-o' => 'foo'), new InputDefinition());
  $t->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
}
catch (\RuntimeException $e)
{
  $t->pass('->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
}