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