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

pictcode / lib / Cake / Test / Case / Console / HelpFormatterTest.php @ 0b1b8047

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

1 635eef61 spyder1211
<?php
2
/**
3
 * HelpFormatterTest 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
App::uses('HelpFormatter', 'Console');
21
22
/**
23
 * Class HelpFormatterTest
24
 *
25
 * @package       Cake.Test.Case.Console
26
 */
27
class HelpFormatterTest extends CakeTestCase {
28
29
/**
30
 * test that the console max width is respected when generating help.
31
 *
32
 * @return void
33
 */
34
        public function testWidthFormatting() {
35
                $parser = new ConsoleOptionParser('test', false);
36
                $parser->description('This is fifteen This is fifteen This is fifteen')
37
                        ->addOption('four', array('help' => 'this is help text this is help text'))
38
                        ->addArgument('four', array('help' => 'this is help text this is help text'))
39
                        ->addSubcommand('four', array('help' => 'this is help text this is help text'));
40
41
                $formatter = new HelpFormatter($parser);
42
                $result = $formatter->text(30);
43
                $expected = <<<TEXT
44
This is fifteen This is
45
fifteen This is fifteen
46

47
<info>Usage:</info>
48
cake test [subcommand] [-h] [--four] [<four>]
49

50
<info>Subcommands:</info>
51

52
four  this is help text this
53
      is help text
54

55
To see help on a subcommand use <info>`cake test [subcommand] --help`</info>
56

57
<info>Options:</info>
58

59
--help, -h  Display this help.
60
--four      this is help text
61
            this is help text
62

63
<info>Arguments:</info>
64

65
four  this is help text this
66
      is help text
67
      <comment>(optional)</comment>
68

69
TEXT;
70
                $this->assertTextEquals($expected, $result, 'Generated help is too wide');
71
        }
72
73
/**
74
 * test help() with options and arguments that have choices.
75
 *
76
 * @return void
77
 */
78
        public function testHelpWithChoices() {
79
                $parser = new ConsoleOptionParser('mycommand', false);
80
                $parser->addOption('test', array('help' => 'A test option.', 'choices' => array('one', 'two')))
81
                        ->addArgument('type', array(
82
                                'help' => 'Resource type.',
83
                                'choices' => array('aco', 'aro'),
84
                                'required' => true
85
                        ))
86
                        ->addArgument('other_longer', array('help' => 'Another argument.'));
87
88
                $formatter = new HelpFormatter($parser);
89
                $result = $formatter->text();
90
                $expected = <<<TEXT
91
<info>Usage:</info>
92
cake mycommand [-h] [--test one|two] <aco|aro> [<other_longer>]
93

94
<info>Options:</info>
95

96
--help, -h  Display this help.
97
--test      A test option. <comment>(choices: one|two)</comment>
98

99
<info>Arguments:</info>
100

101
type          Resource type. <comment>(choices: aco|aro)</comment>
102
other_longer  Another argument. <comment>(optional)</comment>
103

104
TEXT;
105
                $this->assertTextEquals($expected, $result, 'Help does not match');
106
        }
107
108
/**
109
 * test description and epilog in the help
110
 *
111
 * @return void
112
 */
113
        public function testHelpDescriptionAndEpilog() {
114
                $parser = new ConsoleOptionParser('mycommand', false);
115
                $parser->description('Description text')
116
                        ->epilog('epilog text')
117
                        ->addOption('test', array('help' => 'A test option.'))
118
                        ->addArgument('model', array('help' => 'The model to make.', 'required' => true));
119
120
                $formatter = new HelpFormatter($parser);
121
                $result = $formatter->text();
122
                $expected = <<<TEXT
123
Description text
124

125
<info>Usage:</info>
126
cake mycommand [-h] [--test] <model>
127

128
<info>Options:</info>
129

130
--help, -h  Display this help.
131
--test      A test option.
132

133
<info>Arguments:</info>
134

135
model  The model to make.
136

137
epilog text
138

139
TEXT;
140
                $this->assertTextEquals($expected, $result, 'Help is wrong.');
141
        }
142
143
/**
144
 * test that help() outputs subcommands.
145
 *
146
 * @return void
147
 */
148
        public function testHelpSubcommand() {
149
                $parser = new ConsoleOptionParser('mycommand', false);
150
                $parser->addSubcommand('method', array('help' => 'This is another command'))
151
                        ->addOption('test', array('help' => 'A test option.'));
152
153
                $formatter = new HelpFormatter($parser);
154
                $result = $formatter->text();
155
                $expected = <<<TEXT
156
<info>Usage:</info>
157
cake mycommand [subcommand] [-h] [--test]
158

159
<info>Subcommands:</info>
160

161
method  This is another command
162

163
To see help on a subcommand use <info>`cake mycommand [subcommand] --help`</info>
164

165
<info>Options:</info>
166

167
--help, -h  Display this help.
168
--test      A test option.
169

170
TEXT;
171
                $this->assertTextEquals($expected, $result, 'Help is not correct.');
172
        }
173
174
/**
175
 * test getting help with defined options.
176
 *
177
 * @return void
178
 */
179
        public function testHelpWithOptions() {
180
                $parser = new ConsoleOptionParser('mycommand', false);
181
                $parser->addOption('test', array('help' => 'A test option.'))
182
                        ->addOption('connection', array(
183
                                'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
184
                        ));
185
186
                $formatter = new HelpFormatter($parser);
187
                $result = $formatter->text();
188
                $expected = <<<TEXT
189
<info>Usage:</info>
190
cake mycommand [-h] [--test] [-c default]
191

192
<info>Options:</info>
193

194
--help, -h        Display this help.
195
--test            A test option.
196
--connection, -c  The connection to use. <comment>(default:
197
                  default)</comment>
198

199
TEXT;
200
                $this->assertTextEquals($expected, $result, 'Help does not match');
201
        }
202
203
/**
204
 * test getting help with defined options.
205
 *
206
 * @return void
207
 */
208
        public function testHelpWithOptionsAndArguments() {
209
                $parser = new ConsoleOptionParser('mycommand', false);
210
                $parser->addOption('test', array('help' => 'A test option.'))
211
                        ->addArgument('model', array('help' => 'The model to make.', 'required' => true))
212
                        ->addArgument('other_longer', array('help' => 'Another argument.'));
213
214
                $formatter = new HelpFormatter($parser);
215
                $result = $formatter->text();
216
                $expected = <<<TEXT
217
<info>Usage:</info>
218
cake mycommand [-h] [--test] <model> [<other_longer>]
219

220
<info>Options:</info>
221

222
--help, -h  Display this help.
223
--test      A test option.
224

225
<info>Arguments:</info>
226

227
model         The model to make.
228
other_longer  Another argument. <comment>(optional)</comment>
229

230
TEXT;
231
                $this->assertTextEquals($expected, $result, 'Help does not match');
232
        }
233
234
/**
235
 * Test that a long set of options doesn't make useless output.
236
 *
237
 * @return void
238
 */
239
        public function testHelpWithLotsOfOptions() {
240
                $parser = new ConsoleOptionParser('mycommand', false);
241
                $parser
242
                        ->addOption('test', array('help' => 'A test option.'))
243
                        ->addOption('test2', array('help' => 'A test option.'))
244
                        ->addOption('test3', array('help' => 'A test option.'))
245
                        ->addOption('test4', array('help' => 'A test option.'))
246
                        ->addOption('test5', array('help' => 'A test option.'))
247
                        ->addOption('test6', array('help' => 'A test option.'))
248
                        ->addOption('test7', array('help' => 'A test option.'))
249
                        ->addArgument('model', array('help' => 'The model to make.', 'required' => true))
250
                        ->addArgument('other_longer', array('help' => 'Another argument.'));
251
252
                $formatter = new HelpFormatter($parser);
253
                $result = $formatter->text();
254
                $expected = 'cake mycommand [options] <model> [<other_longer>]';
255
                $this->assertContains($expected, $result);
256
        }
257
258
/**
259
 * Test that a long set of arguments doesn't make useless output.
260
 *
261
 * @return void
262
 */
263
        public function testHelpWithLotsOfArguments() {
264
                $parser = new ConsoleOptionParser('mycommand', false);
265
                $parser
266
                        ->addArgument('test', array('help' => 'A test option.'))
267
                        ->addArgument('test2', array('help' => 'A test option.'))
268
                        ->addArgument('test3', array('help' => 'A test option.'))
269
                        ->addArgument('test4', array('help' => 'A test option.'))
270
                        ->addArgument('test5', array('help' => 'A test option.'))
271
                        ->addArgument('test6', array('help' => 'A test option.'))
272
                        ->addArgument('test7', array('help' => 'A test option.'))
273
                        ->addArgument('model', array('help' => 'The model to make.', 'required' => true))
274
                        ->addArgument('other_longer', array('help' => 'Another argument.'));
275
276
                $formatter = new HelpFormatter($parser);
277
                $result = $formatter->text();
278
                $expected = 'cake mycommand [-h] [arguments]';
279
                $this->assertContains($expected, $result);
280
        }
281
282
/**
283
 * test help() with options and arguments that have choices.
284
 *
285
 * @return void
286
 */
287
        public function testXmlHelpWithChoices() {
288
                $parser = new ConsoleOptionParser('mycommand', false);
289
                $parser->addOption('test', array('help' => 'A test option.', 'choices' => array('one', 'two')))
290
                        ->addArgument('type', array(
291
                                'help' => 'Resource type.',
292
                                'choices' => array('aco', 'aro'),
293
                                'required' => true
294
                        ))
295
                        ->addArgument('other_longer', array('help' => 'Another argument.'));
296
297
                $formatter = new HelpFormatter($parser);
298
                $result = $formatter->xml();
299
                $expected = <<<TEXT
300
<?xml version="1.0"?>
301
<shell>
302
<name>mycommand</name>
303
<description>Description text</description>
304
<subcommands />
305
<options>
306
        <option name="--help" short="-h" help="Display this help." boolean="1">
307
                <default></default>
308
                <choices></choices>
309
        </option>
310
        <option name="--test" short="" help="A test option." boolean="0">
311
                <default></default>
312
                <choices>
313
                        <choice>one</choice>
314
                        <choice>two</choice>
315
                </choices>
316
        </option>
317
</options>
318
<arguments>
319
        <argument name="type" help="Resource type." required="1">
320
                <choices>
321
                        <choice>aco</choice>
322
                        <choice>aro</choice>
323
                </choices>
324
        </argument>
325
</arguments>
326
<epilog>epilog text</epilog>
327
</shell>
328
TEXT;
329
                $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
330
        }
331
332
/**
333
 * test description and epilog in the help
334
 *
335
 * @return void
336
 */
337
        public function testXmlHelpDescriptionAndEpilog() {
338
                $parser = new ConsoleOptionParser('mycommand', false);
339
                $parser->description('Description text')
340
                        ->epilog('epilog text')
341
                        ->addOption('test', array('help' => 'A test option.'))
342
                        ->addArgument('model', array('help' => 'The model to make.', 'required' => true));
343
344
                $formatter = new HelpFormatter($parser);
345
                $result = $formatter->xml();
346
                $expected = <<<TEXT
347
<?xml version="1.0"?>
348
<shell>
349
<name>mycommand</name>
350
<description>Description text</description>
351
<subcommands />
352
<options>
353
        <option name="--help" short="-h" help="Display this help." boolean="1">
354
                <default></default>
355
                <choices></choices>
356
        </option>
357
        <option name="--test" short="" help="A test option." boolean="0">
358
                <default></default>
359
                <choices></choices>
360
        </option>
361
</options>
362
<arguments>
363
        <argument name="model" help="The model to make." required="1">
364
                <choices></choices>
365
        </argument>
366
</arguments>
367
<epilog>epilog text</epilog>
368
</shell>
369
TEXT;
370
                $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
371
        }
372
373
/**
374
 * test that help() outputs subcommands.
375
 *
376
 * @return void
377
 */
378
        public function testXmlHelpSubcommand() {
379
                $parser = new ConsoleOptionParser('mycommand', false);
380
                $parser->addSubcommand('method', array('help' => 'This is another command'))
381
                        ->addOption('test', array('help' => 'A test option.'));
382
383
                $formatter = new HelpFormatter($parser);
384
                $result = $formatter->xml();
385
                $expected = <<<TEXT
386
<?xml version="1.0"?>
387
<shell>
388
<name>mycommand</name>
389
<description/>
390
<subcommands>
391
        <command name="method" help="This is another command" />
392
</subcommands>
393
<options>
394
        <option name="--help" short="-h" help="Display this help." boolean="1">
395
                <default></default>
396
                <choices></choices>
397
        </option>
398
        <option name="--test" short="" help="A test option." boolean="0">
399
                <default></default>
400
                <choices></choices>
401
        </option>
402
</options>
403
<arguments/>
404
<epilog/>
405
</shell>
406
TEXT;
407
                $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
408
        }
409
410
/**
411
 * test getting help with defined options.
412
 *
413
 * @return void
414
 */
415
        public function testXmlHelpWithOptions() {
416
                $parser = new ConsoleOptionParser('mycommand', false);
417
                $parser->addOption('test', array('help' => 'A test option.'))
418
                        ->addOption('connection', array(
419
                                'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
420
                        ));
421
422
                $formatter = new HelpFormatter($parser);
423
                $result = $formatter->xml();
424
                $expected = <<<TEXT
425
<?xml version="1.0"?>
426
<shell>
427
<name>mycommand</name>
428
<description/>
429
<subcommands/>
430
<options>
431
        <option name="--help" short="-h" help="Display this help." boolean="1">
432
                <default></default>
433
                <choices></choices>
434
        </option>
435
        <option name="--test" short="" help="A test option." boolean="0">
436
                <default></default>
437
                <choices></choices>
438
        </option>
439
        <option name="--connection" short="-c" help="The connection to use." boolean="0">
440
                <default>default</default>
441
                <choices></choices>
442
        </option>
443
</options>
444
<arguments/>
445
<epilog/>
446
</shell>
447
TEXT;
448
                $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
449
        }
450
451
/**
452
 * test getting help with defined options.
453
 *
454
 * @return void
455
 */
456
        public function testXmlHelpWithOptionsAndArguments() {
457
                $parser = new ConsoleOptionParser('mycommand', false);
458
                $parser->addOption('test', array('help' => 'A test option.'))
459
                        ->addArgument('model', array('help' => 'The model to make.', 'required' => true))
460
                        ->addArgument('other_longer', array('help' => 'Another argument.'));
461
462
                $formatter = new HelpFormatter($parser);
463
                $result = $formatter->xml();
464
                $expected = <<<TEXT
465
<?xml version="1.0"?>
466
<shell>
467
        <name>mycommand</name>
468
        <description/>
469
        <subcommands/>
470
        <options>
471
                <option name="--help" short="-h" help="Display this help." boolean="1">
472
                        <default></default>
473
                        <choices></choices>
474
                </option>
475
                <option name="--test" short="" help="A test option." boolean="0">
476
                        <default></default>
477
                        <choices></choices>
478
                </option>
479
        </options>
480
        <arguments>
481
                <argument name="model" help="The model to make." required="1">
482
                        <choices></choices>
483
                </argument>
484
                <argument name="other_longer" help="Another argument." required="0">
485
                        <choices></choices>
486
                </argument>
487
        </arguments>
488
        <epilog/>
489
</shell>
490
TEXT;
491
                $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
492
        }
493
494
/**
495
 * Test xml help as object
496
 *
497
 * @return void
498
 */
499
        public function testXmlHelpAsObject() {
500
                $parser = new ConsoleOptionParser('mycommand', false);
501
                $parser->addOption('test', array('help' => 'A test option.'))
502
                        ->addArgument('model', array('help' => 'The model to make.', 'required' => true))
503
                        ->addArgument('other_longer', array('help' => 'Another argument.'));
504
505
                $formatter = new HelpFormatter($parser);
506
                $result = $formatter->xml(false);
507
                $this->assertInstanceOf('SimpleXmlElement', $result);
508
        }
509
}