TYPO3  7.6
StringInputTest.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 
17 
18 class StringInputTest extends \PHPUnit_Framework_TestCase
19 {
23  public function testTokenize($input, $tokens, $message)
24  {
25  $input = new StringInput($input);
26  $r = new \ReflectionClass('Symfony\Component\Console\Input\ArgvInput');
27  $p = $r->getProperty('tokens');
28  $p->setAccessible(true);
29  $this->assertEquals($tokens, $p->getValue($input), $message);
30  }
31 
33  {
34  $definition = new InputDefinition(
35  array(new InputOption('foo', null, InputOption::VALUE_REQUIRED))
36  );
37 
38  // call to bind
39  $input = new StringInput('--foo=bar');
40  $input->bind($definition);
41  $this->assertEquals('bar', $input->getOption('foo'));
42  }
43 
48  {
49  $definition = new InputDefinition(
50  array(new InputOption('foo', null, InputOption::VALUE_REQUIRED))
51  );
52 
53  $input = new StringInput('--foo=bar', $definition);
54  $this->assertEquals('bar', $input->getOption('foo'));
55  }
56 
57  public function getTokenizeData()
58  {
59  return array(
60  array('', array(), '->tokenize() parses an empty string'),
61  array('foo', array('foo'), '->tokenize() parses arguments'),
62  array(' foo bar ', array('foo', 'bar'), '->tokenize() ignores whitespaces between arguments'),
63  array('"quoted"', array('quoted'), '->tokenize() parses quoted arguments'),
64  array("'quoted'", array('quoted'), '->tokenize() parses quoted arguments'),
65  array("'a\rb\nc\td'", array("a\rb\nc\td"), '->tokenize() parses whitespace chars in strings'),
66  array("'a'\r'b'\n'c'\t'd'", array('a','b','c','d'), '->tokenize() parses whitespace chars between args as spaces'),
67  array('\"quoted\"', array('"quoted"'), '->tokenize() parses escaped-quoted arguments'),
68  array("\'quoted\'", array('\'quoted\''), '->tokenize() parses escaped-quoted arguments'),
69  array('-a', array('-a'), '->tokenize() parses short options'),
70  array('-azc', array('-azc'), '->tokenize() parses aggregated short options'),
71  array('-awithavalue', array('-awithavalue'), '->tokenize() parses short options with a value'),
72  array('-a"foo bar"', array('-afoo bar'), '->tokenize() parses short options with a value'),
73  array('-a"foo bar""foo bar"', array('-afoo barfoo bar'), '->tokenize() parses short options with a value'),
74  array('-a\'foo bar\'', array('-afoo bar'), '->tokenize() parses short options with a value'),
75  array('-a\'foo bar\'\'foo bar\'', array('-afoo barfoo bar'), '->tokenize() parses short options with a value'),
76  array('-a\'foo bar\'"foo bar"', array('-afoo barfoo bar'), '->tokenize() parses short options with a value'),
77  array('--long-option', array('--long-option'), '->tokenize() parses long options'),
78  array('--long-option=foo', array('--long-option=foo'), '->tokenize() parses long options with a value'),
79  array('--long-option="foo bar"', array('--long-option=foo bar'), '->tokenize() parses long options with a value'),
80  array('--long-option="foo bar""another"', array('--long-option=foo baranother'), '->tokenize() parses long options with a value'),
81  array('--long-option=\'foo bar\'', array('--long-option=foo bar'), '->tokenize() parses long options with a value'),
82  array("--long-option='foo bar''another'", array('--long-option=foo baranother'), '->tokenize() parses long options with a value'),
83  array("--long-option='foo bar'\"another\"", array('--long-option=foo baranother'), '->tokenize() parses long options with a value'),
84  array('foo -a -ffoo --long bar', array('foo', '-a', '-ffoo', '--long', 'bar'), '->tokenize() parses when several arguments and options'),
85  );
86  }
87 
88  public function testToString()
89  {
90  $input = new StringInput('-f foo');
91  $this->assertEquals('-f foo', (string) $input);
92 
93  $input = new StringInput('-f --bar=foo "a b c d"');
94  $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d'), (string) $input);
95 
96  $input = new StringInput('-f --bar=foo \'a b c d\' '."'A\nB\\'C'");
97  $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
98  }
99 }