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');
41 $input = new ArrayInput(array('--name' => 'foo', '--bar' => ''), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
42 $this->assertEquals('', $input->getOption('bar'), '->getOption() returns null for options explicitly passed without value (or an empty value)');
43 $this->assertEquals(array('name' => 'foo', 'bar' => ''), $input->getOptions(), '->getOptions() returns all option values.');
45 $input = new ArrayInput(array('--name' => 'foo', '--bar' => null), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
46 $this->assertNull($input->getOption('bar'), '->getOption() returns null for options explicitly passed without value (or an empty value)');
47 $this->assertEquals(array('name' => 'foo', 'bar' => null), $input->getOptions(), '->getOptions() returns all option values');
51 * @expectedException \InvalidArgumentException
52 * @expectedExceptionMessage The "foo" option does not exist.
54 public function testSetInvalidOption()
56 $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
57 $input->setOption('foo', 'bar');
61 * @expectedException \InvalidArgumentException
62 * @expectedExceptionMessage The "foo" option does not exist.
64 public function testGetInvalidOption()
66 $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
67 $input->getOption('foo');
70 public function testArguments()
72 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
73 $this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
75 $input->setArgument('name', 'bar');
76 $this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
77 $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->getArguments() returns all argument values');
79 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
80 $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
81 $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
85 * @expectedException \InvalidArgumentException
86 * @expectedExceptionMessage The "foo" argument does not exist.
88 public function testSetInvalidArgument()
90 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
91 $input->setArgument('foo', 'bar');
95 * @expectedException \InvalidArgumentException
96 * @expectedExceptionMessage The "foo" argument does not exist.
98 public function testGetInvalidArgument()
100 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
101 $input->getArgument('foo');
105 * @expectedException \RuntimeException
106 * @expectedExceptionMessage Not enough arguments (missing: "name").
108 public function testValidateWithMissingArguments()
110 $input = new ArrayInput(array());
111 $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
116 * @expectedException \RuntimeException
117 * @expectedExceptionMessage Not enough arguments (missing: "name").
119 public function testValidateWithMissingRequiredArguments()
121 $input = new ArrayInput(array('bar' => 'baz'));
122 $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL))));
126 public function testValidate()
128 $input = new ArrayInput(array('name' => 'foo'));
129 $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
131 $this->assertNull($input->validate());
134 public function testSetGetInteractive()
136 $input = new ArrayInput(array());
137 $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
138 $input->setInteractive(false);
139 $this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');
142 public function testSetGetStream()
144 $input = new ArrayInput(array());
145 $stream = fopen('php://memory', 'r+', false);
146 $input->setStream($stream);
147 $this->assertSame($stream, $input->getStream());