| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* ConsoleOptionParserTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
|
|
14 |
* @package Cake.Test.Case.Console
|
|
|
15 |
* @since CakePHP(tm) v 2.0
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('ConsoleOptionParser', 'Console');
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Class ConsoleOptionParserTest
|
|
|
23 |
*
|
|
|
24 |
* @package Cake.Test.Case.Console
|
|
|
25 |
*/
|
|
|
26 |
class ConsoleOptionParserTest extends CakeTestCase {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* test setting the console description
|
|
|
30 |
*
|
|
|
31 |
* @return void
|
|
|
32 |
*/
|
|
|
33 |
public function testDescription() {
|
|
|
34 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
35 |
$result = $parser->description('A test');
|
|
|
36 |
|
|
|
37 |
$this->assertEquals($parser, $result, 'Setting description is not chainable');
|
|
|
38 |
$this->assertEquals('A test', $parser->description(), 'getting value is wrong.');
|
|
|
39 |
|
|
|
40 |
$result = $parser->description(array('A test', 'something'));
|
|
|
41 |
$this->assertEquals("A test\nsomething", $parser->description(), 'getting value is wrong.');
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* test setting the console epilog
|
|
|
46 |
*
|
|
|
47 |
* @return void
|
|
|
48 |
*/
|
|
|
49 |
public function testEpilog() {
|
|
|
50 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
51 |
$result = $parser->epilog('A test');
|
|
|
52 |
|
|
|
53 |
$this->assertEquals($parser, $result, 'Setting epilog is not chainable');
|
|
|
54 |
$this->assertEquals('A test', $parser->epilog(), 'getting value is wrong.');
|
|
|
55 |
|
|
|
56 |
$result = $parser->epilog(array('A test', 'something'));
|
|
|
57 |
$this->assertEquals("A test\nsomething", $parser->epilog(), 'getting value is wrong.');
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* test adding an option returns self.
|
|
|
62 |
*
|
|
|
63 |
* @return void
|
|
|
64 |
*/
|
|
|
65 |
public function testAddOptionReturnSelf() {
|
|
|
66 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
67 |
$result = $parser->addOption('test');
|
|
|
68 |
$this->assertEquals($parser, $result, 'Did not return $this from addOption');
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* test adding an option and using the long value for parsing.
|
|
|
73 |
*
|
|
|
74 |
* @return void
|
|
|
75 |
*/
|
|
|
76 |
public function testAddOptionLong() {
|
|
|
77 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
78 |
$parser->addOption('test', array(
|
|
|
79 |
'short' => 't'
|
|
|
80 |
));
|
|
|
81 |
$result = $parser->parse(array('--test', 'value'));
|
|
|
82 |
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* test adding an option with a zero value
|
|
|
87 |
*
|
|
|
88 |
* @return void
|
|
|
89 |
*/
|
|
|
90 |
public function testAddOptionZero() {
|
|
|
91 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
92 |
$parser->addOption('count', array());
|
|
|
93 |
$result = $parser->parse(array('--count', '0'));
|
|
|
94 |
$this->assertEquals(array('count' => '0', 'help' => false), $result[0], 'Zero parameter did not parse out');
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* test addOption with an object.
|
|
|
99 |
*
|
|
|
100 |
* @return void
|
|
|
101 |
*/
|
|
|
102 |
public function testAddOptionObject() {
|
|
|
103 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
104 |
$parser->addOption(new ConsoleInputOption('test', 't'));
|
|
|
105 |
$result = $parser->parse(array('--test=value'));
|
|
|
106 |
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* test adding an option and using the long value for parsing.
|
|
|
111 |
*
|
|
|
112 |
* @return void
|
|
|
113 |
*/
|
|
|
114 |
public function testAddOptionLongEquals() {
|
|
|
115 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
116 |
$parser->addOption('test', array(
|
|
|
117 |
'short' => 't'
|
|
|
118 |
));
|
|
|
119 |
$result = $parser->parse(array('--test=value'));
|
|
|
120 |
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* test adding an option and using the default.
|
|
|
125 |
*
|
|
|
126 |
* @return void
|
|
|
127 |
*/
|
|
|
128 |
public function testAddOptionDefault() {
|
|
|
129 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
130 |
$parser->addOption('test', array(
|
|
|
131 |
'default' => 'default value',
|
|
|
132 |
));
|
|
|
133 |
$result = $parser->parse(array('--test'));
|
|
|
134 |
$this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');
|
|
|
135 |
|
|
|
136 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
137 |
$parser->addOption('test', array(
|
|
|
138 |
'default' => 'default value',
|
|
|
139 |
));
|
|
|
140 |
$result = $parser->parse(array());
|
|
|
141 |
$this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* test adding an option and using the short value for parsing.
|
|
|
146 |
*
|
|
|
147 |
* @return void
|
|
|
148 |
*/
|
|
|
149 |
public function testAddOptionShort() {
|
|
|
150 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
151 |
$parser->addOption('test', array(
|
|
|
152 |
'short' => 't'
|
|
|
153 |
));
|
|
|
154 |
$result = $parser->parse(array('-t', 'value'));
|
|
|
155 |
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Short parameter did not parse out');
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Test that adding an option using a two letter short value causes an exception.
|
|
|
160 |
* As they will not parse correctly.
|
|
|
161 |
*
|
|
|
162 |
* @expectedException ConsoleException
|
|
|
163 |
* @return void
|
|
|
164 |
*/
|
|
|
165 |
public function testAddOptionShortOneLetter() {
|
|
|
166 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
167 |
$parser->addOption('test', array('short' => 'te'));
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* test adding and using boolean options.
|
|
|
172 |
*
|
|
|
173 |
* @return void
|
|
|
174 |
*/
|
|
|
175 |
public function testAddOptionBoolean() {
|
|
|
176 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
177 |
$parser->addOption('test', array(
|
|
|
178 |
'boolean' => true,
|
|
|
179 |
));
|
|
|
180 |
|
|
|
181 |
$result = $parser->parse(array('--test', 'value'));
|
|
|
182 |
$expected = array(array('test' => true, 'help' => false), array('value'));
|
|
|
183 |
$this->assertEquals($expected, $result);
|
|
|
184 |
|
|
|
185 |
$result = $parser->parse(array('value'));
|
|
|
186 |
$expected = array(array('test' => false, 'help' => false), array('value'));
|
|
|
187 |
$this->assertEquals($expected, $result);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* test adding an multiple shorts.
|
|
|
192 |
*
|
|
|
193 |
* @return void
|
|
|
194 |
*/
|
|
|
195 |
public function testAddOptionMultipleShort() {
|
|
|
196 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
197 |
$parser->addOption('test', array('short' => 't', 'boolean' => true))
|
|
|
198 |
->addOption('file', array('short' => 'f', 'boolean' => true))
|
|
|
199 |
->addOption('output', array('short' => 'o', 'boolean' => true));
|
|
|
200 |
|
|
|
201 |
$result = $parser->parse(array('-o', '-t', '-f'));
|
|
|
202 |
$expected = array('file' => true, 'test' => true, 'output' => true, 'help' => false);
|
|
|
203 |
$this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
|
|
|
204 |
|
|
|
205 |
$result = $parser->parse(array('-otf'));
|
|
|
206 |
$this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* test multiple options at once.
|
|
|
211 |
*
|
|
|
212 |
* @return void
|
|
|
213 |
*/
|
|
|
214 |
public function testMultipleOptions() {
|
|
|
215 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
216 |
$parser->addOption('test')
|
|
|
217 |
->addOption('connection')
|
|
|
218 |
->addOption('table', array('short' => 't', 'default' => true));
|
|
|
219 |
|
|
|
220 |
$result = $parser->parse(array('--test', 'value', '-t', '--connection', 'postgres'));
|
|
|
221 |
$expected = array('test' => 'value', 'table' => true, 'connection' => 'postgres', 'help' => false);
|
|
|
222 |
$this->assertEquals($expected, $result[0], 'multiple options did not parse');
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* Test adding multiple options.
|
|
|
227 |
*
|
|
|
228 |
* @return void
|
|
|
229 |
*/
|
|
|
230 |
public function testAddOptions() {
|
|
|
231 |
$parser = new ConsoleOptionParser('something', false);
|
|
|
232 |
$result = $parser->addOptions(array(
|
|
|
233 |
'name' => array('help' => 'The name'),
|
|
|
234 |
'other' => array('help' => 'The other arg')
|
|
|
235 |
));
|
|
|
236 |
$this->assertEquals($parser, $result, 'addOptions is not chainable.');
|
|
|
237 |
|
|
|
238 |
$result = $parser->options();
|
|
|
239 |
$this->assertEquals(3, count($result), 'Not enough options');
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
/**
|
|
|
243 |
* test that boolean options work
|
|
|
244 |
*
|
|
|
245 |
* @return void
|
|
|
246 |
*/
|
|
|
247 |
public function testOptionWithBooleanParam() {
|
|
|
248 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
249 |
$parser->addOption('no-commit', array('boolean' => true))
|
|
|
250 |
->addOption('table', array('short' => 't'));
|
|
|
251 |
|
|
|
252 |
$result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2'));
|
|
|
253 |
$expected = array(array('table' => 'posts', 'no-commit' => true, 'help' => false), array('arg1', 'arg2'));
|
|
|
254 |
$this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* test parsing options that do not exist.
|
|
|
259 |
*
|
|
|
260 |
* @expectedException ConsoleException
|
|
|
261 |
*/
|
|
|
262 |
public function testOptionThatDoesNotExist() {
|
|
|
263 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
264 |
$parser->addOption('no-commit', array('boolean' => true));
|
|
|
265 |
|
|
|
266 |
$parser->parse(array('--fail', 'other'));
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
/**
|
|
|
270 |
* test parsing short options that do not exist.
|
|
|
271 |
*
|
|
|
272 |
* @expectedException ConsoleException
|
|
|
273 |
*/
|
|
|
274 |
public function testShortOptionThatDoesNotExist() {
|
|
|
275 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
276 |
$parser->addOption('no-commit', array('boolean' => true));
|
|
|
277 |
|
|
|
278 |
$parser->parse(array('-f'));
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* test that options with choices enforce them.
|
|
|
283 |
*
|
|
|
284 |
* @expectedException ConsoleException
|
|
|
285 |
* @return void
|
|
|
286 |
*/
|
|
|
287 |
public function testOptionWithChoices() {
|
|
|
288 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
289 |
$parser->addOption('name', array('choices' => array('mark', 'jose')));
|
|
|
290 |
|
|
|
291 |
$result = $parser->parse(array('--name', 'mark'));
|
|
|
292 |
$expected = array('name' => 'mark', 'help' => false);
|
|
|
293 |
$this->assertEquals($expected, $result[0], 'Got the correct value.');
|
|
|
294 |
|
|
|
295 |
$result = $parser->parse(array('--name', 'jimmy'));
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* Ensure that option values can start with -
|
|
|
300 |
*
|
|
|
301 |
* @return void
|
|
|
302 |
*/
|
|
|
303 |
public function testOptionWithValueStartingWithMinus() {
|
|
|
304 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
305 |
$parser->addOption('name')
|
|
|
306 |
->addOption('age');
|
|
|
307 |
|
|
|
308 |
$result = $parser->parse(array('--name', '-foo', '--age', 'old'));
|
|
|
309 |
$expected = array('name' => '-foo', 'age' => 'old', 'help' => false);
|
|
|
310 |
$this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
/**
|
|
|
314 |
* test positional argument parsing.
|
|
|
315 |
*
|
|
|
316 |
* @return void
|
|
|
317 |
*/
|
|
|
318 |
public function testPositionalArgument() {
|
|
|
319 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
320 |
$result = $parser->addArgument('name', array('help' => 'An argument'));
|
|
|
321 |
$this->assertEquals($parser, $result, 'Should return this');
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
/**
|
|
|
325 |
* test addOption with an object.
|
|
|
326 |
*
|
|
|
327 |
* @return void
|
|
|
328 |
*/
|
|
|
329 |
public function testAddArgumentObject() {
|
|
|
330 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
331 |
$parser->addArgument(new ConsoleInputArgument('test'));
|
|
|
332 |
$result = $parser->arguments();
|
|
|
333 |
$this->assertCount(1, $result);
|
|
|
334 |
$this->assertEquals('test', $result[0]->name());
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
/**
|
|
|
338 |
* Test adding arguments out of order.
|
|
|
339 |
*
|
|
|
340 |
* @return void
|
|
|
341 |
*/
|
|
|
342 |
public function testAddArgumentOutOfOrder() {
|
|
|
343 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
344 |
$parser->addArgument('name', array('index' => 1, 'help' => 'first argument'))
|
|
|
345 |
->addArgument('bag', array('index' => 2, 'help' => 'second argument'))
|
|
|
346 |
->addArgument('other', array('index' => 0, 'help' => 'Zeroth argument'));
|
|
|
347 |
|
|
|
348 |
$result = $parser->arguments();
|
|
|
349 |
$this->assertCount(3, $result);
|
|
|
350 |
$this->assertEquals('other', $result[0]->name());
|
|
|
351 |
$this->assertEquals('name', $result[1]->name());
|
|
|
352 |
$this->assertEquals('bag', $result[2]->name());
|
|
|
353 |
$this->assertSame(array(0, 1, 2), array_keys($result));
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
/**
|
|
|
357 |
* test overwriting positional arguments.
|
|
|
358 |
*
|
|
|
359 |
* @return void
|
|
|
360 |
*/
|
|
|
361 |
public function testPositionalArgOverwrite() {
|
|
|
362 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
363 |
$parser->addArgument('name', array('help' => 'An argument'))
|
|
|
364 |
->addArgument('other', array('index' => 0));
|
|
|
365 |
|
|
|
366 |
$result = $parser->arguments();
|
|
|
367 |
$this->assertEquals(1, count($result), 'Overwrite did not occur');
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
/**
|
|
|
371 |
* test parsing arguments.
|
|
|
372 |
*
|
|
|
373 |
* @expectedException ConsoleException
|
|
|
374 |
* @return void
|
|
|
375 |
*/
|
|
|
376 |
public function testParseArgumentTooMany() {
|
|
|
377 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
378 |
$parser->addArgument('name', array('help' => 'An argument'))
|
|
|
379 |
->addArgument('other');
|
|
|
380 |
|
|
|
381 |
$expected = array('one', 'two');
|
|
|
382 |
$result = $parser->parse($expected);
|
|
|
383 |
$this->assertEquals($expected, $result[1], 'Arguments are not as expected');
|
|
|
384 |
|
|
|
385 |
$result = $parser->parse(array('one', 'two', 'three'));
|
|
|
386 |
}
|
|
|
387 |
|
|
|
388 |
/**
|
|
|
389 |
* test parsing arguments with 0 value.
|
|
|
390 |
*
|
|
|
391 |
* @return void
|
|
|
392 |
*/
|
|
|
393 |
public function testParseArgumentZero() {
|
|
|
394 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
395 |
|
|
|
396 |
$expected = array('one', 'two', 0, 'after', 'zero');
|
|
|
397 |
$result = $parser->parse($expected);
|
|
|
398 |
$this->assertEquals($expected, $result[1], 'Arguments are not as expected');
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
/**
|
|
|
402 |
* test that when there are not enough arguments an exception is raised
|
|
|
403 |
*
|
|
|
404 |
* @expectedException ConsoleException
|
|
|
405 |
* @return void
|
|
|
406 |
*/
|
|
|
407 |
public function testPositionalArgNotEnough() {
|
|
|
408 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
409 |
$parser->addArgument('name', array('required' => true))
|
|
|
410 |
->addArgument('other', array('required' => true));
|
|
|
411 |
|
|
|
412 |
$parser->parse(array('one'));
|
|
|
413 |
}
|
|
|
414 |
|
|
|
415 |
/**
|
|
|
416 |
* test that arguments with choices enforce them.
|
|
|
417 |
*
|
|
|
418 |
* @expectedException ConsoleException
|
|
|
419 |
* @return void
|
|
|
420 |
*/
|
|
|
421 |
public function testPositionalArgWithChoices() {
|
|
|
422 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
423 |
$parser->addArgument('name', array('choices' => array('mark', 'jose')))
|
|
|
424 |
->addArgument('alias', array('choices' => array('cowboy', 'samurai')))
|
|
|
425 |
->addArgument('weapon', array('choices' => array('gun', 'sword')));
|
|
|
426 |
|
|
|
427 |
$result = $parser->parse(array('mark', 'samurai', 'sword'));
|
|
|
428 |
$expected = array('mark', 'samurai', 'sword');
|
|
|
429 |
$this->assertEquals($expected, $result[1], 'Got the correct value.');
|
|
|
430 |
|
|
|
431 |
$result = $parser->parse(array('jose', 'coder'));
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
/**
|
|
|
435 |
* Test adding multiple arguments.
|
|
|
436 |
*
|
|
|
437 |
* @return void
|
|
|
438 |
*/
|
|
|
439 |
public function testAddArguments() {
|
|
|
440 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
441 |
$result = $parser->addArguments(array(
|
|
|
442 |
'name' => array('help' => 'The name'),
|
|
|
443 |
'other' => array('help' => 'The other arg')
|
|
|
444 |
));
|
|
|
445 |
$this->assertEquals($parser, $result, 'addArguments is not chainable.');
|
|
|
446 |
|
|
|
447 |
$result = $parser->arguments();
|
|
|
448 |
$this->assertEquals(2, count($result), 'Not enough arguments');
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
/**
|
|
|
452 |
* test setting a subcommand up.
|
|
|
453 |
*
|
|
|
454 |
* @return void
|
|
|
455 |
*/
|
|
|
456 |
public function testSubcommand() {
|
|
|
457 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
458 |
$result = $parser->addSubcommand('initdb', array(
|
|
|
459 |
'help' => 'Initialize the database'
|
|
|
460 |
));
|
|
|
461 |
$this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
|
|
|
462 |
}
|
|
|
463 |
|
|
|
464 |
/**
|
|
|
465 |
* test addSubcommand with an object.
|
|
|
466 |
*
|
|
|
467 |
* @return void
|
|
|
468 |
*/
|
|
|
469 |
public function testAddSubcommandObject() {
|
|
|
470 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
471 |
$parser->addSubcommand(new ConsoleInputSubcommand('test'));
|
|
|
472 |
$result = $parser->subcommands();
|
|
|
473 |
$this->assertEquals(1, count($result));
|
|
|
474 |
$this->assertEquals('test', $result['test']->name());
|
|
|
475 |
}
|
|
|
476 |
|
|
|
477 |
/**
|
|
|
478 |
* test adding multiple subcommands
|
|
|
479 |
*
|
|
|
480 |
* @return void
|
|
|
481 |
*/
|
|
|
482 |
public function testAddSubcommands() {
|
|
|
483 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
484 |
$result = $parser->addSubcommands(array(
|
|
|
485 |
'initdb' => array('help' => 'Initialize the database'),
|
|
|
486 |
'create' => array('help' => 'Create something')
|
|
|
487 |
));
|
|
|
488 |
$this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
|
|
|
489 |
$result = $parser->subcommands();
|
|
|
490 |
$this->assertEquals(2, count($result), 'Not enough subcommands');
|
|
|
491 |
}
|
|
|
492 |
|
|
|
493 |
/**
|
|
|
494 |
* test that no exception is triggered when help is being generated
|
|
|
495 |
*
|
|
|
496 |
* @return void
|
|
|
497 |
*/
|
|
|
498 |
public function testHelpNoExceptionWhenGettingHelp() {
|
|
|
499 |
$parser = new ConsoleOptionParser('mycommand', false);
|
|
|
500 |
$parser->addOption('test', array('help' => 'A test option.'))
|
|
|
501 |
->addArgument('model', array('help' => 'The model to make.', 'required' => true));
|
|
|
502 |
|
|
|
503 |
$result = $parser->parse(array('--help'));
|
|
|
504 |
$this->assertTrue($result[0]['help']);
|
|
|
505 |
}
|
|
|
506 |
|
|
|
507 |
/**
|
|
|
508 |
* test that help() with a command param shows the help for a subcommand
|
|
|
509 |
*
|
|
|
510 |
* @return void
|
|
|
511 |
*/
|
|
|
512 |
public function testHelpSubcommandHelp() {
|
|
|
513 |
$subParser = new ConsoleOptionParser('method', false);
|
|
|
514 |
$subParser->addOption('connection', array('help' => 'Db connection.'));
|
|
|
515 |
|
|
|
516 |
$parser = new ConsoleOptionParser('mycommand', false);
|
|
|
517 |
$parser->addSubcommand('method', array(
|
|
|
518 |
'help' => 'This is another command',
|
|
|
519 |
'parser' => $subParser
|
|
|
520 |
))
|
|
|
521 |
->addOption('test', array('help' => 'A test option.'));
|
|
|
522 |
|
|
|
523 |
$result = $parser->help('method');
|
|
|
524 |
$expected = <<<TEXT
|
|
|
525 |
<info>Usage:</info>
|
|
|
526 |
cake mycommand method [-h] [--connection]
|
|
|
527 |
|
|
|
528 |
<info>Options:</info>
|
|
|
529 |
|
|
|
530 |
--help, -h Display this help.
|
|
|
531 |
--connection Db connection.
|
|
|
532 |
|
|
|
533 |
TEXT;
|
|
|
534 |
$this->assertTextEquals($expected, $result, 'Help is not correct.');
|
|
|
535 |
}
|
|
|
536 |
|
|
|
537 |
/**
|
|
|
538 |
* test building a parser from an array.
|
|
|
539 |
*
|
|
|
540 |
* @return void
|
|
|
541 |
*/
|
|
|
542 |
public function testBuildFromArray() {
|
|
|
543 |
$spec = array(
|
|
|
544 |
'command' => 'test',
|
|
|
545 |
'arguments' => array(
|
|
|
546 |
'name' => array('help' => 'The name'),
|
|
|
547 |
'other' => array('help' => 'The other arg')
|
|
|
548 |
),
|
|
|
549 |
'options' => array(
|
|
|
550 |
'name' => array('help' => 'The name'),
|
|
|
551 |
'other' => array('help' => 'The other arg')
|
|
|
552 |
),
|
|
|
553 |
'subcommands' => array(
|
|
|
554 |
'initdb' => array('help' => 'make database')
|
|
|
555 |
),
|
|
|
556 |
'description' => 'description text',
|
|
|
557 |
'epilog' => 'epilog text'
|
|
|
558 |
);
|
|
|
559 |
$parser = ConsoleOptionParser::buildFromArray($spec);
|
|
|
560 |
|
|
|
561 |
$this->assertEquals($spec['description'], $parser->description());
|
|
|
562 |
$this->assertEquals($spec['epilog'], $parser->epilog());
|
|
|
563 |
|
|
|
564 |
$options = $parser->options();
|
|
|
565 |
$this->assertTrue(isset($options['name']));
|
|
|
566 |
$this->assertTrue(isset($options['other']));
|
|
|
567 |
|
|
|
568 |
$args = $parser->arguments();
|
|
|
569 |
$this->assertEquals(2, count($args));
|
|
|
570 |
|
|
|
571 |
$commands = $parser->subcommands();
|
|
|
572 |
$this->assertEquals(1, count($commands));
|
|
|
573 |
}
|
|
|
574 |
|
|
|
575 |
/**
|
|
|
576 |
* test that create() returns instances
|
|
|
577 |
*
|
|
|
578 |
* @return void
|
|
|
579 |
*/
|
|
|
580 |
public function testCreateFactory() {
|
|
|
581 |
$parser = ConsoleOptionParser::create('factory', false);
|
|
|
582 |
$this->assertInstanceOf('ConsoleOptionParser', $parser);
|
|
|
583 |
$this->assertEquals('factory', $parser->command());
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
/**
|
|
|
587 |
* test that command() inflects the command name.
|
|
|
588 |
*
|
|
|
589 |
* @return void
|
|
|
590 |
*/
|
|
|
591 |
public function testCommandInflection() {
|
|
|
592 |
$parser = new ConsoleOptionParser('CommandLine');
|
|
|
593 |
$this->assertEquals('command_line', $parser->command());
|
|
|
594 |
}
|
|
|
595 |
|
|
|
596 |
/**
|
|
|
597 |
* test that parse() takes a subcommand argument, and that the subcommand parser
|
|
|
598 |
* is used.
|
|
|
599 |
*
|
|
|
600 |
* @return void
|
|
|
601 |
*/
|
|
|
602 |
public function testParsingWithSubParser() {
|
|
|
603 |
$parser = new ConsoleOptionParser('test', false);
|
|
|
604 |
$parser->addOption('primary')
|
|
|
605 |
->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
|
|
|
606 |
->addArgument('two', array('required' => true))
|
|
|
607 |
->addSubcommand('sub', array(
|
|
|
608 |
'parser' => array(
|
|
|
609 |
'options' => array(
|
|
|
610 |
'secondary' => array('boolean' => true),
|
|
|
611 |
'fourth' => array('help' => 'fourth option')
|
|
|
612 |
),
|
|
|
613 |
'arguments' => array(
|
|
|
614 |
'sub_arg' => array('choices' => array('c', 'd'))
|
|
|
615 |
)
|
|
|
616 |
)
|
|
|
617 |
));
|
|
|
618 |
|
|
|
619 |
$result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
|
|
|
620 |
$expected = array(array(
|
|
|
621 |
'secondary' => true,
|
|
|
622 |
'fourth' => '4',
|
|
|
623 |
'help' => false,
|
|
|
624 |
'verbose' => false,
|
|
|
625 |
'quiet' => false), array('c'));
|
|
|
626 |
$this->assertEquals($expected, $result, 'Sub parser did not parse request.');
|
|
|
627 |
}
|
|
|
628 |
|
|
|
629 |
}
|