7cf1d244a3e4eb4518133f6f1b10403c118c03d1
[yaffs-website] / vendor / symfony / console / Tests / Input / InputTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Console\Tests\Input;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Input\ArrayInput;
16 use Symfony\Component\Console\Input\InputArgument;
17 use Symfony\Component\Console\Input\InputDefinition;
18 use Symfony\Component\Console\Input\InputOption;
19
20 class InputTest extends TestCase
21 {
22     public function testConstructor()
23     {
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');
26     }
27
28     public function testOptions()
29     {
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');
32
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');
36
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');
40
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.');
44
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');
48     }
49
50     /**
51      * @expectedException        \InvalidArgumentException
52      * @expectedExceptionMessage The "foo" option does not exist.
53      */
54     public function testSetInvalidOption()
55     {
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');
58     }
59
60     /**
61      * @expectedException        \InvalidArgumentException
62      * @expectedExceptionMessage The "foo" option does not exist.
63      */
64     public function testGetInvalidOption()
65     {
66         $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
67         $input->getOption('foo');
68     }
69
70     public function testArguments()
71     {
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');
74
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');
78
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');
82     }
83
84     /**
85      * @expectedException        \InvalidArgumentException
86      * @expectedExceptionMessage The "foo" argument does not exist.
87      */
88     public function testSetInvalidArgument()
89     {
90         $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
91         $input->setArgument('foo', 'bar');
92     }
93
94     /**
95      * @expectedException        \InvalidArgumentException
96      * @expectedExceptionMessage The "foo" argument does not exist.
97      */
98     public function testGetInvalidArgument()
99     {
100         $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
101         $input->getArgument('foo');
102     }
103
104     /**
105      * @expectedException        \RuntimeException
106      * @expectedExceptionMessage Not enough arguments (missing: "name").
107      */
108     public function testValidateWithMissingArguments()
109     {
110         $input = new ArrayInput(array());
111         $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
112         $input->validate();
113     }
114
115     /**
116      * @expectedException        \RuntimeException
117      * @expectedExceptionMessage Not enough arguments (missing: "name").
118      */
119     public function testValidateWithMissingRequiredArguments()
120     {
121         $input = new ArrayInput(array('bar' => 'baz'));
122         $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL))));
123         $input->validate();
124     }
125
126     public function testValidate()
127     {
128         $input = new ArrayInput(array('name' => 'foo'));
129         $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
130
131         $this->assertNull($input->validate());
132     }
133
134     public function testSetGetInteractive()
135     {
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');
140     }
141
142     public function testSetGetStream()
143     {
144         $input = new ArrayInput(array());
145         $stream = fopen('php://memory', 'r+', false);
146         $input->setStream($stream);
147         $this->assertSame($stream, $input->getStream());
148     }
149 }