Yaffs site version 1.1
[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\InputDefinition;
17 use Symfony\Component\Console\Input\InputArgument;
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
42     /**
43      * @expectedException        \InvalidArgumentException
44      * @expectedExceptionMessage The "foo" option does not exist.
45      */
46     public function testSetInvalidOption()
47     {
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');
50     }
51
52     /**
53      * @expectedException        \InvalidArgumentException
54      * @expectedExceptionMessage The "foo" option does not exist.
55      */
56     public function testGetInvalidOption()
57     {
58         $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
59         $input->getOption('foo');
60     }
61
62     public function testArguments()
63     {
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');
66
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');
70
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');
74     }
75
76     /**
77      * @expectedException        \InvalidArgumentException
78      * @expectedExceptionMessage The "foo" argument does not exist.
79      */
80     public function testSetInvalidArgument()
81     {
82         $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
83         $input->setArgument('foo', 'bar');
84     }
85
86     /**
87      * @expectedException        \InvalidArgumentException
88      * @expectedExceptionMessage The "foo" argument does not exist.
89      */
90     public function testGetInvalidArgument()
91     {
92         $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
93         $input->getArgument('foo');
94     }
95
96     /**
97      * @expectedException        \RuntimeException
98      * @expectedExceptionMessage Not enough arguments (missing: "name").
99      */
100     public function testValidateWithMissingArguments()
101     {
102         $input = new ArrayInput(array());
103         $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
104         $input->validate();
105     }
106
107     /**
108      * @expectedException        \RuntimeException
109      * @expectedExceptionMessage Not enough arguments (missing: "name").
110      */
111     public function testValidateWithMissingRequiredArguments()
112     {
113         $input = new ArrayInput(array('bar' => 'baz'));
114         $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL))));
115         $input->validate();
116     }
117
118     public function testValidate()
119     {
120         $input = new ArrayInput(array('name' => 'foo'));
121         $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
122
123         $this->assertNull($input->validate());
124     }
125
126     public function testSetGetInteractive()
127     {
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');
132     }
133 }