Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 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
 * @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
		$result = $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
		$result = $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
		$result = $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 adding multiple subcommands
481
 *
482
 * @return void
483
 */
484
	public function testAddSubcommands() {
485
		$parser = new ConsoleOptionParser('test', false);
486
		$result = $parser->addSubcommands(array(
487
			'initdb' => array('help' => 'Initialize the database'),
488
			'create' => array('help' => 'Create something')
489
		));
490
		$this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
491
		$result = $parser->subcommands();
492
		$this->assertEquals(2, count($result), 'Not enough subcommands');
493
	}
494
 
495
/**
496
 * test that no exception is triggered when help is being generated
497
 *
498
 * @return void
499
 */
500
	public function testHelpNoExceptionWhenGettingHelp() {
501
		$parser = new ConsoleOptionParser('mycommand', false);
502
		$parser->addOption('test', array('help' => 'A test option.'))
503
			->addArgument('model', array('help' => 'The model to make.', 'required' => true));
504
 
505
		$result = $parser->parse(array('--help'));
506
		$this->assertTrue($result[0]['help']);
507
	}
508
 
509
/**
510
 * test that help() with a command param shows the help for a subcommand
511
 *
512
 * @return void
513
 */
514
	public function testHelpSubcommandHelp() {
515
		$subParser = new ConsoleOptionParser('method', false);
516
		$subParser->addOption('connection', array('help' => 'Db connection.'));
517
 
518
		$parser = new ConsoleOptionParser('mycommand', false);
519
		$parser->addSubcommand('method', array(
520
				'help' => 'This is another command',
521
				'parser' => $subParser
522
			))
523
			->addOption('test', array('help' => 'A test option.'));
524
 
525
		$result = $parser->help('method');
526
		$expected = <<<TEXT
527
<info>Usage:</info>
528
cake mycommand method [-h] [--connection]
529
 
530
<info>Options:</info>
531
 
532
--help, -h        Display this help.
533
--connection      Db connection.
534
 
535
TEXT;
536
		$this->assertTextEquals($expected, $result, 'Help is not correct.');
537
	}
538
 
539
/**
540
 * test building a parser from an array.
541
 *
542
 * @return void
543
 */
544
	public function testBuildFromArray() {
545
		$spec = array(
546
			'command' => 'test',
547
			'arguments' => array(
548
				'name' => array('help' => 'The name'),
549
				'other' => array('help' => 'The other arg')
550
			),
551
			'options' => array(
552
				'name' => array('help' => 'The name'),
553
				'other' => array('help' => 'The other arg')
554
			),
555
			'subcommands' => array(
556
				'initdb' => array('help' => 'make database')
557
			),
558
			'description' => 'description text',
559
			'epilog' => 'epilog text'
560
		);
561
		$parser = ConsoleOptionParser::buildFromArray($spec);
562
 
563
		$this->assertEquals($spec['description'], $parser->description());
564
		$this->assertEquals($spec['epilog'], $parser->epilog());
565
 
566
		$options = $parser->options();
567
		$this->assertTrue(isset($options['name']));
568
		$this->assertTrue(isset($options['other']));
569
 
570
		$args = $parser->arguments();
571
		$this->assertEquals(2, count($args));
572
 
573
		$commands = $parser->subcommands();
574
		$this->assertEquals(1, count($commands));
575
	}
576
 
577
/**
578
 * test that create() returns instances
579
 *
580
 * @return void
581
 */
582
	public function testCreateFactory() {
583
		$parser = ConsoleOptionParser::create('factory', false);
584
		$this->assertInstanceOf('ConsoleOptionParser', $parser);
585
		$this->assertEquals('factory', $parser->command());
586
	}
587
 
588
/**
589
 * test that command() inflects the command name.
590
 *
591
 * @return void
592
 */
593
	public function testCommandInflection() {
594
		$parser = new ConsoleOptionParser('CommandLine');
595
		$this->assertEquals('command_line', $parser->command());
596
	}
597
 
598
/**
599
 * test that parse() takes a subcommand argument, and that the subcommand parser
600
 * is used.
601
 *
602
 * @return void
603
 */
604
	public function testParsingWithSubParser() {
605
		$parser = new ConsoleOptionParser('test', false);
606
		$parser->addOption('primary')
607
			->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
608
			->addArgument('two', array('required' => true))
609
			->addSubcommand('sub', array(
610
				'parser' => array(
611
					'options' => array(
612
						'secondary' => array('boolean' => true),
613
						'fourth' => array('help' => 'fourth option')
614
					),
615
					'arguments' => array(
616
						'sub_arg' => array('choices' => array('c', 'd'))
617
					)
618
				)
619
			));
620
 
621
		$result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
622
		$expected = array(array(
623
			'secondary' => true,
624
			'fourth' => '4',
625
			'help' => false,
626
			'verbose' => false,
627
			'quiet' => false), array('c'));
628
		$this->assertEquals($expected, $result, 'Sub parser did not parse request.');
629
	}
630
 
631
}