StringInputTest.php 4.9 KB

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