StringInputTest.php 4.6 KB

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