ShellInputTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2017 Justin Hileman
  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 Psy\Tests\Input;
  11. use Psy\Input\CodeArgument;
  12. use Psy\Input\ShellInput;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputDefinition;
  15. use Symfony\Component\Console\Input\InputOption;
  16. class ShellInputTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @dataProvider getTokenizeData
  20. */
  21. public function testTokenize($input, $tokens, $message)
  22. {
  23. $input = new ShellInput($input);
  24. $r = new \ReflectionClass('Psy\Input\ShellInput');
  25. $p = $r->getProperty('tokenPairs');
  26. $p->setAccessible(true);
  27. $this->assertEquals($tokens, $p->getValue($input), $message);
  28. }
  29. public function testInputOptionWithGivenString()
  30. {
  31. $definition = new InputDefinition(array(
  32. new InputOption('foo', null, InputOption::VALUE_REQUIRED),
  33. new CodeArgument('code', null, InputOption::VALUE_REQUIRED),
  34. ));
  35. $input = new ShellInput('--foo=bar echo "baz\n";');
  36. $input->bind($definition);
  37. $this->assertEquals('bar', $input->getOption('foo'));
  38. $this->assertEquals('echo "baz\n";', $input->getArgument('code'));
  39. }
  40. public function testInputOptionWithoutCodeArguments()
  41. {
  42. $definition = new InputDefinition(array(
  43. new InputOption('foo', null, InputOption::VALUE_REQUIRED),
  44. new InputArgument('bar', null, InputOption::VALUE_REQUIRED),
  45. new InputArgument('baz', null, InputOption::VALUE_REQUIRED),
  46. ));
  47. $input = new ShellInput('--foo=foo bar "baz\n"');
  48. $input->bind($definition);
  49. $this->assertEquals('foo', $input->getOption('foo'));
  50. $this->assertEquals('bar', $input->getArgument('bar'));
  51. $this->assertEquals("baz\n", $input->getArgument('baz'));
  52. }
  53. public function getTokenizeData()
  54. {
  55. // Test all the cases from StringInput test, ensuring they have an appropriate $rest token.
  56. return array(
  57. array(
  58. '',
  59. array(),
  60. '->tokenize() parses an empty string',
  61. ),
  62. array(
  63. 'foo',
  64. array(array('foo', 'foo')),
  65. '->tokenize() parses arguments',
  66. ),
  67. array(
  68. ' foo bar ',
  69. array(array('foo', 'foo bar '), array('bar', 'bar ')),
  70. '->tokenize() ignores whitespaces between arguments',
  71. ),
  72. array(
  73. '"quoted"',
  74. array(array('quoted', '"quoted"')),
  75. '->tokenize() parses quoted arguments',
  76. ),
  77. array(
  78. "'quoted'",
  79. array(array('quoted', "'quoted'")),
  80. '->tokenize() parses quoted arguments',
  81. ),
  82. array(
  83. "'a\rb\nc\td'",
  84. array(array("a\rb\nc\td", "'a\rb\nc\td'")),
  85. '->tokenize() parses whitespace chars in strings',
  86. ),
  87. array(
  88. "'a'\r'b'\n'c'\t'd'",
  89. array(
  90. array('a', "'a'\r'b'\n'c'\t'd'"),
  91. array('b', "'b'\n'c'\t'd'"),
  92. array('c', "'c'\t'd'"),
  93. array('d', "'d'"),
  94. ),
  95. '->tokenize() parses whitespace chars between args as spaces',
  96. ),
  97. array(
  98. '\"quoted\"',
  99. array(array('"quoted"', '\"quoted\"')),
  100. '->tokenize() parses escaped-quoted arguments',
  101. ),
  102. array(
  103. "\'quoted\'",
  104. array(array('\'quoted\'', "\'quoted\'")),
  105. '->tokenize() parses escaped-quoted arguments',
  106. ),
  107. array(
  108. '-a',
  109. array(array('-a', '-a')),
  110. '->tokenize() parses short options',
  111. ),
  112. array(
  113. '-azc',
  114. array(array('-azc', '-azc')),
  115. '->tokenize() parses aggregated short options',
  116. ),
  117. array(
  118. '-awithavalue',
  119. array(array('-awithavalue', '-awithavalue')),
  120. '->tokenize() parses short options with a value',
  121. ),
  122. array(
  123. '-a"foo bar"',
  124. array(array('-afoo bar', '-a"foo bar"')),
  125. '->tokenize() parses short options with a value',
  126. ),
  127. array(
  128. '-a"foo bar""foo bar"',
  129. array(array('-afoo barfoo bar', '-a"foo bar""foo bar"')),
  130. '->tokenize() parses short options with a value',
  131. ),
  132. array(
  133. '-a\'foo bar\'',
  134. array(array('-afoo bar', '-a\'foo bar\'')),
  135. '->tokenize() parses short options with a value',
  136. ),
  137. array(
  138. '-a\'foo bar\'\'foo bar\'',
  139. array(array('-afoo barfoo bar', '-a\'foo bar\'\'foo bar\'')),
  140. '->tokenize() parses short options with a value',
  141. ),
  142. array(
  143. '-a\'foo bar\'"foo bar"',
  144. array(array('-afoo barfoo bar', '-a\'foo bar\'"foo bar"')),
  145. '->tokenize() parses short options with a value',
  146. ),
  147. array(
  148. '--long-option',
  149. array(array('--long-option', '--long-option')),
  150. '->tokenize() parses long options',
  151. ),
  152. array(
  153. '--long-option=foo',
  154. array(array('--long-option=foo', '--long-option=foo')),
  155. '->tokenize() parses long options with a value',
  156. ),
  157. array(
  158. '--long-option="foo bar"',
  159. array(array('--long-option=foo bar', '--long-option="foo bar"')),
  160. '->tokenize() parses long options with a value',
  161. ),
  162. array(
  163. '--long-option="foo bar""another"',
  164. array(array('--long-option=foo baranother', '--long-option="foo bar""another"')),
  165. '->tokenize() parses long options with a value',
  166. ),
  167. array(
  168. '--long-option=\'foo bar\'',
  169. array(array('--long-option=foo bar', '--long-option=\'foo bar\'')),
  170. '->tokenize() parses long options with a value',
  171. ),
  172. array(
  173. "--long-option='foo bar''another'",
  174. array(array('--long-option=foo baranother', "--long-option='foo bar''another'")),
  175. '->tokenize() parses long options with a value',
  176. ),
  177. array(
  178. "--long-option='foo bar'\"another\"",
  179. array(array('--long-option=foo baranother', "--long-option='foo bar'\"another\"")),
  180. '->tokenize() parses long options with a value',
  181. ),
  182. array(
  183. 'foo -a -ffoo --long bar',
  184. array(
  185. array('foo', 'foo -a -ffoo --long bar'),
  186. array('-a', '-a -ffoo --long bar'),
  187. array('-ffoo', '-ffoo --long bar'),
  188. array('--long', '--long bar'),
  189. array('bar', 'bar'),
  190. ),
  191. '->tokenize() parses when several arguments and options',
  192. ),
  193. );
  194. }
  195. }