4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Input;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Input\ArrayInput;
16 use Symfony\Component\Console\Input\InputDefinition;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputOption;
20 class InputTest extends TestCase
22 public function testConstructor()
24 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
25 $this->assertEquals('foo', $input->getArgument('name'), '->__construct() takes a InputDefinition as an argument');
28 public function testOptions()
30 $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'))));
31 $this->assertEquals('foo', $input->getOption('name'), '->getOption() returns the value for the given option');
33 $input->setOption('name', 'bar');
34 $this->assertEquals('bar', $input->getOption('name'), '->setOption() sets the value for a given option');
35 $this->assertEquals(array('name' => 'bar'), $input->getOptions(), '->getOptions() returns all option values');
37 $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
38 $this->assertEquals('default', $input->getOption('bar'), '->getOption() returns the default value for optional options');
39 $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getOptions(), '->getOptions() returns all option values, even optional ones');
43 * @expectedException \InvalidArgumentException
44 * @expectedExceptionMessage The "foo" option does not exist.
46 public function testSetInvalidOption()
48 $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
49 $input->setOption('foo', 'bar');
53 * @expectedException \InvalidArgumentException
54 * @expectedExceptionMessage The "foo" option does not exist.
56 public function testGetInvalidOption()
58 $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
59 $input->getOption('foo');
62 public function testArguments()
64 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
65 $this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
67 $input->setArgument('name', 'bar');
68 $this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
69 $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->getArguments() returns all argument values');
71 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
72 $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
73 $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
77 * @expectedException \InvalidArgumentException
78 * @expectedExceptionMessage The "foo" argument does not exist.
80 public function testSetInvalidArgument()
82 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
83 $input->setArgument('foo', 'bar');
87 * @expectedException \InvalidArgumentException
88 * @expectedExceptionMessage The "foo" argument does not exist.
90 public function testGetInvalidArgument()
92 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
93 $input->getArgument('foo');
97 * @expectedException \RuntimeException
98 * @expectedExceptionMessage Not enough arguments (missing: "name").
100 public function testValidateWithMissingArguments()
102 $input = new ArrayInput(array());
103 $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
108 * @expectedException \RuntimeException
109 * @expectedExceptionMessage Not enough arguments (missing: "name").
111 public function testValidateWithMissingRequiredArguments()
113 $input = new ArrayInput(array('bar' => 'baz'));
114 $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL))));
118 public function testValidate()
120 $input = new ArrayInput(array('name' => 'foo'));
121 $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
123 $this->assertNull($input->validate());
126 public function testSetGetInteractive()
128 $input = new ArrayInput(array());
129 $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
130 $input->setInteractive(false);
131 $this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');