Ejemplo n.º 1
0
 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::PARAMETER_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());
     }
 }
Ejemplo n.º 2
0
// ->getOption() ->setOption() ->getOptions()
$t->diag('->getOption() ->setOption() ->getOptions()');
$input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'))));
$t->is($input->getOption('name'), 'foo', '->getOption() returns the value for the given option');

$input->setOption('name', 'bar');
$t->is($input->getOption('name'), 'bar', '->setOption() sets the value for a given option');
$t->is($input->getOptions(), array('name' => 'bar'), '->getOptions() returns all option values');

$input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::PARAMETER_OPTIONAL, '', 'default'))));
$t->is($input->getOption('bar'), 'default', '->getOption() returns the default value for optional options');
$t->is($input->getOptions(), array('name' => 'foo', 'bar' => 'default'), '->getOptions() returns all option values, even optional ones');

try
{
  $input->setOption('foo', 'bar');
  $t->fail('->setOption() throws a \InvalidArgumentException if the option does not exist');
}
catch (\InvalidArgumentException $e)
{
  $t->pass('->setOption() throws a \InvalidArgumentException if the option does not exist');
}

try
{
  $input->getOption('foo');
  $t->fail('->getOption() throws a \InvalidArgumentException if the option does not exist');
}
catch (\InvalidArgumentException $e)
{
  $t->pass('->getOption() throws a \InvalidArgumentException if the option does not exist');