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