TYPO3  7.6
ArrayInputTest.php
Go to the documentation of this file.
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 
18 
19 class ArrayInputTest extends \PHPUnit_Framework_TestCase
20 {
21  public function testGetFirstArgument()
22  {
23  $input = new ArrayInput(array());
24  $this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null if no argument were passed');
25  $input = new ArrayInput(array('name' => 'Fabien'));
26  $this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
27  $input = new ArrayInput(array('--foo' => 'bar', 'name' => 'Fabien'));
28  $this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
29  }
30 
31  public function testHasParameterOption()
32  {
33  $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
34  $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
35  $this->assertFalse($input->hasParameterOption('--bar'), '->hasParameterOption() returns false if an option is not present in the passed parameters');
36 
37  $input = new ArrayInput(array('--foo'));
38  $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
39  }
40 
41  public function testGetParameterOption()
42  {
43  $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
44  $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
45 
46  $input = new ArrayInput(array('Fabien', '--foo' => 'bar'));
47  $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
48  }
49 
50  public function testParseArguments()
51  {
52  $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
53 
54  $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
55  }
56 
60  public function testParseOptions($input, $options, $expectedOptions, $message)
61  {
62  $input = new ArrayInput($input, new InputDefinition($options));
63 
64  $this->assertEquals($expectedOptions, $input->getOptions(), $message);
65  }
66 
67  public function provideOptions()
68  {
69  return array(
70  array(
71  array('--foo' => 'bar'),
72  array(new InputOption('foo')),
73  array('foo' => 'bar'),
74  '->parse() parses long options',
75  ),
76  array(
77  array('--foo' => 'bar'),
78  array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
79  array('foo' => 'bar'),
80  '->parse() parses long options with a default value',
81  ),
82  array(
83  array('--foo' => null),
84  array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
85  array('foo' => 'default'),
86  '->parse() parses long options with a default value',
87  ),
88  array(
89  array('-f' => 'bar'),
90  array(new InputOption('foo', 'f')),
91  array('foo' => 'bar'),
92  '->parse() parses short options',
93  ),
94  );
95  }
96 
100  public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage)
101  {
102  $this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
103 
104  new ArrayInput($parameters, $definition);
105  }
106 
107  public function provideInvalidInput()
108  {
109  return array(
110  array(
111  array('foo' => 'foo'),
112  new InputDefinition(array(new InputArgument('name'))),
113  'The "foo" argument does not exist.',
114  ),
115  array(
116  array('--foo' => null),
117  new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
118  'The "--foo" option requires a value.',
119  ),
120  array(
121  array('--foo' => 'foo'),
122  new InputDefinition(),
123  'The "--foo" option does not exist.',
124  ),
125  array(
126  array('-o' => 'foo'),
127  new InputDefinition(),
128  'The "-o" option does not exist.',
129  ),
130  );
131  }
132 
133  public function testToString()
134  {
135  $input = new ArrayInput(array('-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo', 'test2' => "A\nB'C"));
136  $this->assertEquals('-f -b=bar --foo='.escapeshellarg('b a z').' --lala Foo '.escapeshellarg("A\nB'C"), (string) $input);
137  }
138 }