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 Symfony\Component\Console\Input\ArrayInput;
15 use Symfony\Component\Console\Input\InputDefinition;
16 use Symfony\Component\Console\Input\InputArgument;
17 use Symfony\Component\Console\Input\InputOption;
19 class InputTest extends \PHPUnit_Framework_TestCase
21 public function testConstructor()
23 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
24 $this->assertEquals('foo', $input->getArgument('name'), '->__construct() takes a InputDefinition as an argument');
27 public function testOptions()
29 $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'))));
30 $this->assertEquals('foo', $input->getOption('name'), '->getOption() returns the value for the given option');
32 $input->setOption('name', 'bar');
33 $this->assertEquals('bar', $input->getOption('name'), '->setOption() sets the value for a given option');
34 $this->assertEquals(array('name' => 'bar'), $input->getOptions(), '->getOptions() returns all option values');
36 $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
37 $this->assertEquals('default', $input->getOption('bar'), '->getOption() returns the default value for optional options');
38 $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getOptions(), '->getOptions() returns all option values, even optional ones');
41 $input->setOption('foo', 'bar');
42 $this->fail('->setOption() throws a \InvalidArgumentException if the option does not exist');
43 } catch (\Exception $e) {
44 $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
45 $this->assertEquals('The "foo" option does not exist.', $e->getMessage());
49 $input->getOption('foo');
50 $this->fail('->getOption() throws a \InvalidArgumentException if the option does not exist');
51 } catch (\Exception $e) {
52 $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
53 $this->assertEquals('The "foo" option does not exist.', $e->getMessage());
57 public function testArguments()
59 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
60 $this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
62 $input->setArgument('name', 'bar');
63 $this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
64 $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->getArguments() returns all argument values');
66 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
67 $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
68 $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
71 $input->setArgument('foo', 'bar');
72 $this->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist');
73 } catch (\Exception $e) {
74 $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
75 $this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
79 $input->getArgument('foo');
80 $this->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist');
81 } catch (\Exception $e) {
82 $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
83 $this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
87 public function testValidate()
89 $input = new ArrayInput(array());
90 $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
94 $this->fail('->validate() throws a \RuntimeException if not enough arguments are given');
95 } catch (\Exception $e) {
96 $this->assertInstanceOf('\RuntimeException', $e, '->validate() throws a \RuntimeException if not enough arguments are given');
97 $this->assertEquals('Not enough arguments.', $e->getMessage());
100 $input = new ArrayInput(array('name' => 'foo'));
101 $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
105 } catch (\RuntimeException $e) {
106 $this->fail('->validate() does not throw a \RuntimeException if enough arguments are given');
110 public function testSetFetInteractive()
112 $input = new ArrayInput(array());
113 $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
114 $input->setInteractive(false);
115 $this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');