StringInputTest.php 4.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Input;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Input\InputDefinition;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Input\StringInput;
  15. class StringInputTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider getTokenizeData
  19. */
  20. public function testTokenize($input, $tokens, $message)
  21. {
  22. $input = new StringInput($input);
  23. $r = new \ReflectionClass('Symfony\Component\Console\Input\ArgvInput');
  24. $p = $r->getProperty('tokens');
  25. $p->setAccessible(true);
  26. $this->assertEquals($tokens, $p->getValue($input), $message);
  27. }
  28. public function testInputOptionWithGivenString()
  29. {
  30. $definition = new InputDefinition(
  31. array(new InputOption('foo', null, InputOption::VALUE_REQUIRED))
  32. );
  33. // call to bind
  34. $input = new StringInput('--foo=bar');
  35. $input->bind($definition);
  36. $this->assertEquals('bar', $input->getOption('foo'));
  37. }
  38. public function getTokenizeData()
  39. {
  40. return array(
  41. array('', array(), '->tokenize() parses an empty string'),
  42. array('foo', array('foo'), '->tokenize() parses arguments'),
  43. array(' foo bar ', array('foo', 'bar'), '->tokenize() ignores whitespaces between arguments'),
  44. array('"quoted"', array('quoted'), '->tokenize() parses quoted arguments'),
  45. array("'quoted'", array('quoted'), '->tokenize() parses quoted arguments'),
  46. array("'a\rb\nc\td'", array("a\rb\nc\td"), '->tokenize() parses whitespace chars in strings'),
  47. array("'a'\r'b'\n'c'\t'd'", array('a', 'b', 'c', 'd'), '->tokenize() parses whitespace chars between args as spaces'),
  48. array('\"quoted\"', array('"quoted"'), '->tokenize() parses escaped-quoted arguments'),
  49. array("\'quoted\'", array('\'quoted\''), '->tokenize() parses escaped-quoted arguments'),
  50. array('-a', array('-a'), '->tokenize() parses short options'),
  51. array('-azc', array('-azc'), '->tokenize() parses aggregated short options'),
  52. array('-awithavalue', array('-awithavalue'), '->tokenize() parses short options with a value'),
  53. array('-a"foo bar"', array('-afoo bar'), '->tokenize() parses short options with a value'),
  54. array('-a"foo bar""foo bar"', array('-afoo barfoo bar'), '->tokenize() parses short options with a value'),
  55. array('-a\'foo bar\'', array('-afoo bar'), '->tokenize() parses short options with a value'),
  56. array('-a\'foo bar\'\'foo bar\'', array('-afoo barfoo bar'), '->tokenize() parses short options with a value'),
  57. array('-a\'foo bar\'"foo bar"', array('-afoo barfoo bar'), '->tokenize() parses short options with a value'),
  58. array('--long-option', array('--long-option'), '->tokenize() parses long options'),
  59. array('--long-option=foo', array('--long-option=foo'), '->tokenize() parses long options with a value'),
  60. array('--long-option="foo bar"', array('--long-option=foo bar'), '->tokenize() parses long options with a value'),
  61. array('--long-option="foo bar""another"', array('--long-option=foo baranother'), '->tokenize() parses long options with a value'),
  62. array('--long-option=\'foo bar\'', array('--long-option=foo bar'), '->tokenize() parses long options with a value'),
  63. array("--long-option='foo bar''another'", array('--long-option=foo baranother'), '->tokenize() parses long options with a value'),
  64. array("--long-option='foo bar'\"another\"", array('--long-option=foo baranother'), '->tokenize() parses long options with a value'),
  65. array('foo -a -ffoo --long bar', array('foo', '-a', '-ffoo', '--long', 'bar'), '->tokenize() parses when several arguments and options'),
  66. );
  67. }
  68. public function testToString()
  69. {
  70. $input = new StringInput('-f foo');
  71. $this->assertEquals('-f foo', (string) $input);
  72. $input = new StringInput('-f --bar=foo "a b c d"');
  73. $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d'), (string) $input);
  74. $input = new StringInput('-f --bar=foo \'a b c d\' '."'A\nB\\'C'");
  75. $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
  76. }
  77. }