統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Console / ConsoleOptionParserTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (18.764 KB)

1 635eef61 spyder1211
<?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
                $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
                $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
 * @return void
262
 */
263
        public function testOptionThatDoesNotExist() {
264
                $parser = new ConsoleOptionParser('test', false);
265
                $parser->addOption('no-commit', array('boolean' => true));
266
267
                $parser->parse(array('--fail', 'other'));
268
        }
269
270
/**
271
 * test parsing short options that do not exist.
272
 *
273
 * @expectedException ConsoleException
274
 * @return void
275
 */
276
        public function testShortOptionThatDoesNotExist() {
277
                $parser = new ConsoleOptionParser('test', false);
278
                $parser->addOption('no-commit', array('boolean' => true));
279
280
                $parser->parse(array('-f'));
281
        }
282
283
/**
284
 * test that options with choices enforce them.
285
 *
286
 * @expectedException ConsoleException
287
 * @return void
288
 */
289
        public function testOptionWithChoices() {
290
                $parser = new ConsoleOptionParser('test', false);
291
                $parser->addOption('name', array('choices' => array('mark', 'jose')));
292
293
                $result = $parser->parse(array('--name', 'mark'));
294
                $expected = array('name' => 'mark', 'help' => false);
295
                $this->assertEquals($expected, $result[0], 'Got the correct value.');
296
297
                $parser->parse(array('--name', 'jimmy'));
298
        }
299
300
/**
301
 * Ensure that option values can start with -
302
 *
303
 * @return void
304
 */
305
        public function testOptionWithValueStartingWithMinus() {
306
                $parser = new ConsoleOptionParser('test', false);
307
                $parser->addOption('name')
308
                        ->addOption('age');
309
310
                $result = $parser->parse(array('--name', '-foo', '--age', 'old'));
311
                $expected = array('name' => '-foo', 'age' => 'old', 'help' => false);
312
                $this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
313
        }
314
315
/**
316
 * test positional argument parsing.
317
 *
318
 * @return void
319
 */
320
        public function testPositionalArgument() {
321
                $parser = new ConsoleOptionParser('test', false);
322
                $result = $parser->addArgument('name', array('help' => 'An argument'));
323
                $this->assertEquals($parser, $result, 'Should return this');
324
        }
325
326
/**
327
 * test addOption with an object.
328
 *
329
 * @return void
330
 */
331
        public function testAddArgumentObject() {
332
                $parser = new ConsoleOptionParser('test', false);
333
                $parser->addArgument(new ConsoleInputArgument('test'));
334
                $result = $parser->arguments();
335
                $this->assertCount(1, $result);
336
                $this->assertEquals('test', $result[0]->name());
337
        }
338
339
/**
340
 * Test adding arguments out of order.
341
 *
342
 * @return void
343
 */
344
        public function testAddArgumentOutOfOrder() {
345
                $parser = new ConsoleOptionParser('test', false);
346
                $parser->addArgument('name', array('index' => 1, 'help' => 'first argument'))
347
                        ->addArgument('bag', array('index' => 2, 'help' => 'second argument'))
348
                        ->addArgument('other', array('index' => 0, 'help' => 'Zeroth argument'));
349
350
                $result = $parser->arguments();
351
                $this->assertCount(3, $result);
352
                $this->assertEquals('other', $result[0]->name());
353
                $this->assertEquals('name', $result[1]->name());
354
                $this->assertEquals('bag', $result[2]->name());
355
                $this->assertSame(array(0, 1, 2), array_keys($result));
356
        }
357
358
/**
359
 * test overwriting positional arguments.
360
 *
361
 * @return void
362
 */
363
        public function testPositionalArgOverwrite() {
364
                $parser = new ConsoleOptionParser('test', false);
365
                $parser->addArgument('name', array('help' => 'An argument'))
366
                        ->addArgument('other', array('index' => 0));
367
368
                $result = $parser->arguments();
369
                $this->assertEquals(1, count($result), 'Overwrite did not occur');
370
        }
371
372
/**
373
 * test parsing arguments.
374
 *
375
 * @expectedException ConsoleException
376
 * @return void
377
 */
378
        public function testParseArgumentTooMany() {
379
                $parser = new ConsoleOptionParser('test', false);
380
                $parser->addArgument('name', array('help' => 'An argument'))
381
                        ->addArgument('other');
382
383
                $expected = array('one', 'two');
384
                $result = $parser->parse($expected);
385
                $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
386
387
                $parser->parse(array('one', 'two', 'three'));
388
        }
389
390
/**
391
 * test parsing arguments with 0 value.
392
 *
393
 * @return void
394
 */
395
        public function testParseArgumentZero() {
396
                $parser = new ConsoleOptionParser('test', false);
397
398
                $expected = array('one', 'two', 0, 'after', 'zero');
399
                $result = $parser->parse($expected);
400
                $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
401
        }
402
403
/**
404
 * test that when there are not enough arguments an exception is raised
405
 *
406
 * @expectedException ConsoleException
407
 * @return void
408
 */
409
        public function testPositionalArgNotEnough() {
410
                $parser = new ConsoleOptionParser('test', false);
411
                $parser->addArgument('name', array('required' => true))
412
                        ->addArgument('other', array('required' => true));
413
414
                $parser->parse(array('one'));
415
        }
416
417
/**
418
 * test that arguments with choices enforce them.
419
 *
420
 * @expectedException ConsoleException
421
 * @return void
422
 */
423
        public function testPositionalArgWithChoices() {
424
                $parser = new ConsoleOptionParser('test', false);
425
                $parser->addArgument('name', array('choices' => array('mark', 'jose')))
426
                        ->addArgument('alias', array('choices' => array('cowboy', 'samurai')))
427
                        ->addArgument('weapon', array('choices' => array('gun', 'sword')));
428
429
                $result = $parser->parse(array('mark', 'samurai', 'sword'));
430
                $expected = array('mark', 'samurai', 'sword');
431
                $this->assertEquals($expected, $result[1], 'Got the correct value.');
432
433
                $parser->parse(array('jose', 'coder'));
434
        }
435
436
/**
437
 * Test adding multiple arguments.
438
 *
439
 * @return void
440
 */
441
        public function testAddArguments() {
442
                $parser = new ConsoleOptionParser('test', false);
443
                $result = $parser->addArguments(array(
444
                        'name' => array('help' => 'The name'),
445
                        'other' => array('help' => 'The other arg')
446
                ));
447
                $this->assertEquals($parser, $result, 'addArguments is not chainable.');
448
449
                $result = $parser->arguments();
450
                $this->assertEquals(2, count($result), 'Not enough arguments');
451
        }
452
453
/**
454
 * test setting a subcommand up.
455
 *
456
 * @return void
457
 */
458
        public function testSubcommand() {
459
                $parser = new ConsoleOptionParser('test', false);
460
                $result = $parser->addSubcommand('initdb', array(
461
                        'help' => 'Initialize the database'
462
                ));
463
                $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
464
        }
465
466
/**
467
 * test addSubcommand with an object.
468
 *
469
 * @return void
470
 */
471
        public function testAddSubcommandObject() {
472
                $parser = new ConsoleOptionParser('test', false);
473
                $parser->addSubcommand(new ConsoleInputSubcommand('test'));
474
                $result = $parser->subcommands();
475
                $this->assertEquals(1, count($result));
476
                $this->assertEquals('test', $result['test']->name());
477
        }
478
479
/**
480
 * test removeSubcommand with an object.
481
 *
482
 * @return void
483
 */
484
        public function testRemoveSubcommand() {
485
                $parser = new ConsoleOptionParser('test', false);
486
                $parser->addSubcommand(new ConsoleInputSubcommand('test'));
487
                $result = $parser->subcommands();
488
                $this->assertEquals(1, count($result));
489
                $parser->removeSubcommand('test');
490
                $result = $parser->subcommands();
491
                $this->assertEquals(0, count($result), 'Remove a subcommand does not work');
492
        }
493
494
/**
495
 * test adding multiple subcommands
496
 *
497
 * @return void
498
 */
499
        public function testAddSubcommands() {
500
                $parser = new ConsoleOptionParser('test', false);
501
                $result = $parser->addSubcommands(array(
502
                        'initdb' => array('help' => 'Initialize the database'),
503
                        'create' => array('help' => 'Create something')
504
                ));
505
                $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
506
                $result = $parser->subcommands();
507
                $this->assertEquals(2, count($result), 'Not enough subcommands');
508
        }
509
510
/**
511
 * test that no exception is triggered when help is being generated
512
 *
513
 * @return void
514
 */
515
        public function testHelpNoExceptionWhenGettingHelp() {
516
                $parser = new ConsoleOptionParser('mycommand', false);
517
                $parser->addOption('test', array('help' => 'A test option.'))
518
                        ->addArgument('model', array('help' => 'The model to make.', 'required' => true));
519
520
                $result = $parser->parse(array('--help'));
521
                $this->assertTrue($result[0]['help']);
522
        }
523
524
/**
525
 * test that help() with a command param shows the help for a subcommand
526
 *
527
 * @return void
528
 */
529
        public function testHelpSubcommandHelp() {
530
                $subParser = new ConsoleOptionParser('method', false);
531
                $subParser->addOption('connection', array('help' => 'Db connection.'));
532
533
                $parser = new ConsoleOptionParser('mycommand', false);
534
                $parser->addSubcommand('method', array(
535
                                'help' => 'This is another command',
536
                                'parser' => $subParser
537
                        ))
538
                        ->addOption('test', array('help' => 'A test option.'));
539
540
                $result = $parser->help('method');
541
                $expected = <<<TEXT
542
<info>Usage:</info>
543
cake mycommand method [-h] [--connection]
544

545
<info>Options:</info>
546

547
--help, -h        Display this help.
548
--connection      Db connection.
549

550
TEXT;
551
                $this->assertTextEquals($expected, $result, 'Help is not correct.');
552
        }
553
554
/**
555
 * test building a parser from an array.
556
 *
557
 * @return void
558
 */
559
        public function testBuildFromArray() {
560
                $spec = array(
561
                        'command' => 'test',
562
                        'arguments' => array(
563
                                'name' => array('help' => 'The name'),
564
                                'other' => array('help' => 'The other arg')
565
                        ),
566
                        'options' => array(
567
                                'name' => array('help' => 'The name'),
568
                                'other' => array('help' => 'The other arg')
569
                        ),
570
                        'subcommands' => array(
571
                                'initdb' => array('help' => 'make database')
572
                        ),
573
                        'description' => 'description text',
574
                        'epilog' => 'epilog text'
575
                );
576
                $parser = ConsoleOptionParser::buildFromArray($spec);
577
578
                $this->assertEquals($spec['description'], $parser->description());
579
                $this->assertEquals($spec['epilog'], $parser->epilog());
580
581
                $options = $parser->options();
582
                $this->assertTrue(isset($options['name']));
583
                $this->assertTrue(isset($options['other']));
584
585
                $args = $parser->arguments();
586
                $this->assertEquals(2, count($args));
587
588
                $commands = $parser->subcommands();
589
                $this->assertEquals(1, count($commands));
590
        }
591
592
/**
593
 * test that create() returns instances
594
 *
595
 * @return void
596
 */
597
        public function testCreateFactory() {
598
                $parser = ConsoleOptionParser::create('factory', false);
599
                $this->assertInstanceOf('ConsoleOptionParser', $parser);
600
                $this->assertEquals('factory', $parser->command());
601
        }
602
603
/**
604
 * test that command() inflects the command name.
605
 *
606
 * @return void
607
 */
608
        public function testCommandInflection() {
609
                $parser = new ConsoleOptionParser('CommandLine');
610
                $this->assertEquals('command_line', $parser->command());
611
        }
612
613
/**
614
 * test that parse() takes a subcommand argument, and that the subcommand parser
615
 * is used.
616
 *
617
 * @return void
618
 */
619
        public function testParsingWithSubParser() {
620
                $parser = new ConsoleOptionParser('test', false);
621
                $parser->addOption('primary')
622
                        ->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
623
                        ->addArgument('two', array('required' => true))
624
                        ->addSubcommand('sub', array(
625
                                'parser' => array(
626
                                        'options' => array(
627
                                                'secondary' => array('boolean' => true),
628
                                                'fourth' => array('help' => 'fourth option')
629
                                        ),
630
                                        'arguments' => array(
631
                                                'sub_arg' => array('choices' => array('c', 'd'))
632
                                        )
633
                                )
634
                        ));
635
636
                $result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
637
                $expected = array(array(
638
                        'secondary' => true,
639
                        'fourth' => '4',
640
                        'help' => false,
641
                        'verbose' => false,
642
                        'quiet' => false), array('c'));
643
                $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
644
        }
645
646
}