StringInputTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  39. * @group legacy
  40. */
  41. public function testLegacyInputOptionDefinitionInConstructor()
  42. {
  43. $definition = new InputDefinition(
  44. array(new InputOption('foo', null, InputOption::VALUE_REQUIRED))
  45. );
  46. $input = new StringInput('--foo=bar', $definition);
  47. $this->assertEquals('bar', $input->getOption('foo'));
  48. }
  49. public function getTokenizeData()
  50. {
  51. return array(
  52. array('', array(), '->tokenize() parses an empty string'),
  53. array('foo', array('foo'), '->tokenize() parses arguments'),
  54. array(' foo bar ', array('foo', 'bar'), '->tokenize() ignores whitespaces between arguments'),
  55. array('"quoted"', array('quoted'), '->tokenize() parses quoted arguments'),
  56. array("'quoted'", array('quoted'), '->tokenize() parses quoted arguments'),
  57. array("'a\rb\nc\td'", array("a\rb\nc\td"), '->tokenize() parses whitespace chars in strings'),
  58. array("'a'\r'b'\n'c'\t'd'", array('a', 'b', 'c', 'd'), '->tokenize() parses whitespace chars between args as spaces'),
  59. array('\"quoted\"', array('"quoted"'), '->tokenize() parses escaped-quoted arguments'),
  60. array("\'quoted\'", array('\'quoted\''), '->tokenize() parses escaped-quoted arguments'),
  61. array('-a', array('-a'), '->tokenize() parses short options'),
  62. array('-azc', array('-azc'), '->tokenize() parses aggregated short options'),
  63. array('-awithavalue', array('-awithavalue'), '->tokenize() parses short options with a value'),
  64. array('-a"foo bar"', array('-afoo bar'), '->tokenize() parses short options with a value'),
  65. array('-a"foo bar""foo bar"', array('-afoo barfoo bar'), '->tokenize() parses short options with a value'),
  66. array('-a\'foo bar\'', array('-afoo bar'), '->tokenize() parses short options with a value'),
  67. array('-a\'foo bar\'\'foo bar\'', array('-afoo barfoo bar'), '->tokenize() parses short options with a value'),
  68. array('-a\'foo bar\'"foo bar"', array('-afoo barfoo bar'), '->tokenize() parses short options with a value'),
  69. array('--long-option', array('--long-option'), '->tokenize() parses long options'),
  70. array('--long-option=foo', array('--long-option=foo'), '->tokenize() parses long options with a value'),
  71. array('--long-option="foo bar"', array('--long-option=foo bar'), '->tokenize() parses long options with a value'),
  72. array('--long-option="foo bar""another"', array('--long-option=foo baranother'), '->tokenize() parses long options with a value'),
  73. array('--long-option=\'foo bar\'', array('--long-option=foo bar'), '->tokenize() parses long options with a value'),
  74. array("--long-option='foo bar''another'", array('--long-option=foo baranother'), '->tokenize() parses long options with a value'),
  75. array("--long-option='foo bar'\"another\"", array('--long-option=foo baranother'), '->tokenize() parses long options with a value'),
  76. array('foo -a -ffoo --long bar', array('foo', '-a', '-ffoo', '--long', 'bar'), '->tokenize() parses when several arguments and options'),
  77. );
  78. }
  79. public function testToString()
  80. {
  81. $input = new StringInput('-f foo');
  82. $this->assertEquals('-f foo', (string) $input);
  83. $input = new StringInput('-f --bar=foo "a b c d"');
  84. $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d'), (string) $input);
  85. $input = new StringInput('-f --bar=foo \'a b c d\' '."'A\nB\\'C'");
  86. $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
  87. }
  88. }