StringInputTest.php 4.3 KB

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. [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 [
  41. ['', [], '->tokenize() parses an empty string'],
  42. ['foo', ['foo'], '->tokenize() parses arguments'],
  43. [' foo bar ', ['foo', 'bar'], '->tokenize() ignores whitespaces between arguments'],
  44. ['"quoted"', ['quoted'], '->tokenize() parses quoted arguments'],
  45. ["'quoted'", ['quoted'], '->tokenize() parses quoted arguments'],
  46. ["'a\rb\nc\td'", ["a\rb\nc\td"], '->tokenize() parses whitespace chars in strings'],
  47. ["'a'\r'b'\n'c'\t'd'", ['a', 'b', 'c', 'd'], '->tokenize() parses whitespace chars between args as spaces'],
  48. ['\"quoted\"', ['"quoted"'], '->tokenize() parses escaped-quoted arguments'],
  49. ["\'quoted\'", ['\'quoted\''], '->tokenize() parses escaped-quoted arguments'],
  50. ['-a', ['-a'], '->tokenize() parses short options'],
  51. ['-azc', ['-azc'], '->tokenize() parses aggregated short options'],
  52. ['-awithavalue', ['-awithavalue'], '->tokenize() parses short options with a value'],
  53. ['-a"foo bar"', ['-afoo bar'], '->tokenize() parses short options with a value'],
  54. ['-a"foo bar""foo bar"', ['-afoo barfoo bar'], '->tokenize() parses short options with a value'],
  55. ['-a\'foo bar\'', ['-afoo bar'], '->tokenize() parses short options with a value'],
  56. ['-a\'foo bar\'\'foo bar\'', ['-afoo barfoo bar'], '->tokenize() parses short options with a value'],
  57. ['-a\'foo bar\'"foo bar"', ['-afoo barfoo bar'], '->tokenize() parses short options with a value'],
  58. ['--long-option', ['--long-option'], '->tokenize() parses long options'],
  59. ['--long-option=foo', ['--long-option=foo'], '->tokenize() parses long options with a value'],
  60. ['--long-option="foo bar"', ['--long-option=foo bar'], '->tokenize() parses long options with a value'],
  61. ['--long-option="foo bar""another"', ['--long-option=foo baranother'], '->tokenize() parses long options with a value'],
  62. ['--long-option=\'foo bar\'', ['--long-option=foo bar'], '->tokenize() parses long options with a value'],
  63. ["--long-option='foo bar''another'", ['--long-option=foo baranother'], '->tokenize() parses long options with a value'],
  64. ["--long-option='foo bar'\"another\"", ['--long-option=foo baranother'], '->tokenize() parses long options with a value'],
  65. ['foo -a -ffoo --long bar', ['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. }