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