comparison vendor/pear/console_commandline/tests/tests.inc.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * This file is part of the PEAR Console_CommandLine package.
7 *
8 * PHP version 5
9 *
10 * LICENSE: This source file is subject to the MIT license that is available
11 * through the world-wide-web at the following URI:
12 * http://opensource.org/licenses/mit-license.php
13 *
14 * @category Console
15 * @package Console_CommandLine
16 * @author David JEAN LOUIS <izimobil@gmail.com>
17 * @copyright 2007 David JEAN LOUIS
18 * @license http://opensource.org/licenses/mit-license.php MIT License
19 * @version CVS: $Id$
20 * @link http://pear.php.net/package/Console_CommandLine
21 * @since File available since release 0.1.0
22 */
23
24 if (php_sapi_name() != 'cli') {
25 // tests with php-cgi need this
26 ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR
27 . dirname(__FILE__) . '/../');
28 }
29
30 /**
31 * Required classes
32 */
33 require_once 'Console/CommandLine.php';
34 require_once 'Console/CommandLine/Renderer.php';
35 require_once 'Console/CommandLine/Outputter.php';
36 require_once 'Console/CommandLine/MessageProvider.php';
37
38 // rot13Callback() {{{
39
40 /**
41 * A dummy callback for tests purposes.
42 *
43 * @param mixed $value value provided by the user
44 * @param object $option the option instance
45 * @param object $result the result instance
46 * @param object $parser the parser instance
47 * @param array $params optional params array
48 *
49 * @return string
50 */
51 function rot13Callback($value, $option, $result, $parser, $params=array())
52 {
53 $ret = '';
54 if (isset($params['prefix'])) {
55 $ret .= $params['prefix'] . '__';
56 }
57 $ret .= str_rot13($value);
58 if (isset($params['suffix'])) {
59 $ret .= '__' . $params['suffix'];
60 }
61 return $ret;
62 }
63
64 // }}}
65 // buildParser1() {{{
66
67 /**
68 * Build a parser instance and return it.
69 *
70 * @return object Console_CommandLine instance
71 */
72 function buildParser1()
73 {
74 $parser = new Console_CommandLine();
75 $parser->name = 'some_program';
76 $parser->version = '0.1.0';
77 $parser->description = 'Description of our parser goes here...';
78
79 // add options
80 $parser->addOption('true', array(
81 'short_name' => '-t',
82 'long_name' => '--true',
83 'action' => 'StoreTrue',
84 'description' => 'test the StoreTrue action'
85 ));
86 $parser->addOption('false', array(
87 'short_name' => '-f',
88 'long_name' => '--false',
89 'action' => 'StoreFalse',
90 'description' => 'test the StoreFalse action'
91 ));
92 $parser->addOption('int', array(
93 'long_name' => '--int',
94 'action' => 'StoreInt',
95 'description' => 'test the StoreInt action',
96 'help_name' => 'INT',
97 'default' => 1
98 ));
99 $parser->addOption('float', array(
100 'long_name' => '--float',
101 'action' => 'StoreFloat',
102 'description' => 'test the StoreFloat action',
103 'help_name' => 'FLOAT',
104 'default' => 1.0
105 ));
106 $parser->addOption('string', array(
107 'short_name' => '-s',
108 'long_name' => '--string',
109 'action' => 'StoreString',
110 'description' => 'test the StoreString action',
111 'help_name' => 'STRING',
112 'choices' => array('foo', 'bar', 'baz')
113 ));
114 $parser->addOption('counter', array(
115 'short_name' => '-c',
116 'long_name' => '--counter',
117 'action' => 'Counter',
118 'description' => 'test the Counter action'
119 ));
120 $parser->addOption('callback', array(
121 'long_name' => '--callback',
122 'action' => 'Callback',
123 'description' => 'test the Callback action',
124 'callback' => 'rot13Callback',
125 'action_params' => array('prefix' => 'foo', 'suffix' => 'bar')
126 ));
127 $parser->addOption('array', array(
128 'short_name' => '-a',
129 'long_name' => '--array',
130 'default' => array('spam', 'egg'),
131 'action' => 'StoreArray',
132 'help_name' => 'ARRAY',
133 'description' => 'test the StoreArray action'
134 ));
135 $parser->addOption('password', array(
136 'short_name' => '-p',
137 'long_name' => '--password',
138 'action' => 'Password',
139 'description' => 'test the Password action'
140 ));
141 $parser->addArgument('simple', array(
142 'description' => 'test a simple argument'
143 ));
144 $parser->addArgument('multiple', array(
145 'description' => 'test a multiple argument',
146 'multiple' => true,
147 'optional' => true
148 ));
149 return $parser;
150 }
151
152 // }}}
153 // buildParser2() {{{
154
155 /**
156 * Build a parser instance and return it.
157 *
158 * @return object Console_CommandLine instance
159 */
160 function buildParser2()
161 {
162 $parser = new Console_CommandLine();
163 $parser->name = 'some_program';
164 $parser->version = '0.1.0';
165 $parser->description = 'Description of our parser goes here...';
166
167 // add general options
168 $parser->addOption('verbose', array(
169 'short_name' => '-v',
170 'long_name' => '--verbose',
171 'action' => 'StoreTrue',
172 'description' => 'verbose mode'
173 ));
174 $parser->addOption('logfile', array(
175 'short_name' => '-l',
176 'long_name' => '--logfile',
177 'action' => 'StoreString',
178 'description' => 'path to logfile'
179 ));
180
181 // install subcommand
182 $cmd1 = $parser->addCommand('install', array(
183 'description' => 'install given package',
184 'aliases' => array('inst', 'instbis'),
185 ));
186 $cmd1->addOption('force', array(
187 'short_name' => '-f',
188 'long_name' => '--force',
189 'action' => 'StoreTrue',
190 'description' => 'force installation'
191 ));
192 $cmd1->addArgument('package', array(
193 'description' => 'package to install'
194 ));
195
196 // uninstall subcommand
197 $cmd2 = $parser->addCommand('uninstall', array(
198 'description' => 'uninstall given package'
199 ));
200 $cmd2->addArgument('package', array(
201 'description' => 'package to uninstall'
202 ));
203 return $parser;
204 }
205
206 // }}}
207 // buildParser3() {{{
208
209 /**
210 * Build a parser instance and return it.
211 *
212 * @return object Console_CommandLine instance
213 */
214 function buildParser3()
215 {
216 $parser = new Console_CommandLine();
217 $parser->name = 'some_program';
218 $parser->version = '0.1.0';
219 $parser->description = 'Description of our parser goes here...';
220 // we force options default values
221 $parser->force_options_defaults = true;
222
223 // add options
224 $parser->addOption('true', array(
225 'short_name' => '-t',
226 'long_name' => '--true',
227 'action' => 'StoreTrue',
228 'description' => 'test the StoreTrue action',
229 ));
230 $parser->addOption('false', array(
231 'short_name' => '-f',
232 'long_name' => '--false',
233 'action' => 'StoreFalse',
234 'description' => 'test the StoreFalse action',
235 ));
236 $parser->addOption('int', array(
237 'long_name' => '--int',
238 'action' => 'StoreInt',
239 'description' => 'test the StoreInt action',
240 'help_name' => 'INT',
241 ));
242 $parser->addOption('float', array(
243 'long_name' => '--float',
244 'action' => 'StoreFloat',
245 'description' => 'test the StoreFloat action',
246 'help_name' => 'FLOAT',
247 ));
248 $parser->addOption('string', array(
249 'short_name' => '-s',
250 'long_name' => '--string',
251 'action' => 'StoreString',
252 'description' => 'test the StoreString action',
253 'help_name' => 'STRING',
254 'choices' => array('foo', 'bar', 'baz')
255 ));
256 $parser->addOption('counter', array(
257 'short_name' => '-c',
258 'long_name' => '--counter',
259 'action' => 'Counter',
260 'description' => 'test the Counter action'
261 ));
262 $parser->addOption('callback', array(
263 'long_name' => '--callback',
264 'action' => 'Callback',
265 'description' => 'test the Callback action',
266 'callback' => 'rot13Callback',
267 'action_params' => array('prefix' => 'foo', 'suffix' => 'bar')
268 ));
269 $parser->addOption('array', array(
270 'short_name' => '-a',
271 'long_name' => '--array',
272 'action' => 'StoreArray',
273 'help_name' => 'ARRAY',
274 'description' => 'test the StoreArray action'
275 ));
276 $parser->addOption('password', array(
277 'short_name' => '-p',
278 'long_name' => '--password',
279 'action' => 'Password',
280 'description' => 'test the Password action'
281 ));
282 return $parser;
283 }
284
285 // }}}
286 // {{{ buildParser4()
287
288 /**
289 * Build a parser instance and return it.
290 *
291 * For testing custom messages.
292 *
293 * @return object Console_CommandLine instance
294 */
295 function buildParser4()
296 {
297 $parser = new Console_CommandLine(array(
298 'messages' => array(
299 'INVALID_SUBCOMMAND' => 'Only "upgrade" is supported.',
300 ),
301 ));
302 $parser->name = 'some_program';
303 $parser->version = '0.1.0';
304 $parser->description = 'Description of our parser goes here...';
305
306 // some subcommand
307 $cmd1 = $parser->addCommand('upgrade', array(
308 'description' => 'upgrade given package',
309 'aliases' => array('up'),
310 'messages' => array(
311 'ARGUMENT_REQUIRED' => 'Package name is required.',
312 'OPTION_VALUE_REQUIRED' => 'Option requires value.',
313 'OPTION_VALUE_UNEXPECTED' => 'Option should not have a value.',
314 'OPTION_UNKNOWN' => 'Mysterious option encountered.',
315 ),
316 ));
317 // add option
318 $cmd1->addOption('state', array(
319 'short_name' => '-s',
320 'long_name' => '--state',
321 'action' => 'StoreString',
322 'choices' => array('stable', 'beta'),
323 'description' => 'accepted package states',
324 'messages' => array(
325 'OPTION_VALUE_NOT_VALID' => 'Valid states are "stable" and "beta".',
326 ),
327 ));
328 // add another option
329 $cmd1->addOption('dry_run', array(
330 'short_name' => '-d',
331 'long_name' => '--dry-run',
332 'action' => 'StoreTrue',
333 'description' => 'dry run',
334 ));
335 // add argument
336 $cmd1->addArgument('package', array(
337 'description' => 'package to upgrade'
338 ));
339
340 return $parser;
341 }
342
343 // }}}
344 // CustomRenderer() {{{
345
346 /**
347 * Some custom renderer for tests purposes.
348 *
349 * @category Console
350 * @package Console_CommandLine
351 * @author David JEAN LOUIS <izimobil@gmail.com>
352 * @copyright 2007 David JEAN LOUIS
353 * @license http://opensource.org/licenses/mit-license.php MIT License
354 * @version Release: @package_version@
355 * @link http://pear.php.net/package/Console_CommandLine
356 * @since File available since release 0.1.0
357 */
358 class CustomRenderer implements Console_CommandLine_Renderer
359 {
360 // usage() {{{
361
362 /**
363 * Return the full usage message
364 *
365 * @return string the usage message
366 * @access public
367 */
368 public function usage()
369 {
370 return __METHOD__ . '()';
371 }
372 // }}}
373 // error() {{{
374
375 /**
376 * Return a formatted error message
377 *
378 * @param string $error the error message to format
379 *
380 * @return string the error string
381 * @access public
382 */
383 public function error($error)
384 {
385 return __METHOD__ . "($error)";
386 }
387
388 // }}}
389 // version() {{{
390
391 /**
392 * Return the program version string
393 *
394 * @return string the version string
395 * @access public
396 */
397 public function version()
398 {
399 return __METHOD__ . '()';
400 }
401
402 // }}}
403 }
404
405 // }}}
406 // CustomOutputter() {{{
407
408 /**
409 * Some custom outputter for tests purposes.
410 *
411 * @category Console
412 * @package Console_CommandLine
413 * @author David JEAN LOUIS <izimobil@gmail.com>
414 * @copyright 2007 David JEAN LOUIS
415 * @license http://opensource.org/licenses/mit-license.php MIT License
416 * @version Release: @package_version@
417 * @link http://pear.php.net/package/Console_CommandLine
418 * @since File available since release 0.1.0
419 */
420 class CustomOutputter implements Console_CommandLine_Outputter
421 {
422 // stdout() {{{
423
424 /**
425 * Called for stdout messages.
426 *
427 * @param string $msg the message to output
428 *
429 * @return void
430 * @access public
431 */
432 public function stdout($msg)
433 {
434 echo "STDOUT >> $msg\n";
435 }
436
437 // }}}
438 // stderr() {{{
439
440 /**
441 * Called for stderr messages.
442 *
443 * @param string $msg the message to output
444 *
445 * @return void
446 * @access public
447 */
448 public function stderr($msg)
449 {
450 echo "STDERR >> $msg\n";
451 }
452
453 // }}}
454 }
455
456 // }}}
457 // CustomMessageProvider() {{{
458
459 /**
460 * Some custom message provider for tests purposes.
461 *
462 * @category Console
463 * @package Console_CommandLine
464 * @author David JEAN LOUIS <izimobil@gmail.com>
465 * @copyright 2007 David JEAN LOUIS
466 * @license http://opensource.org/licenses/mit-license.php MIT License
467 * @version Release: @package_version@
468 * @link http://pear.php.net/package/Console_CommandLine
469 * @since File available since release 0.1.0
470 */
471 class CustomMessageProvider implements Console_CommandLine_MessageProvider
472 {
473 // get() {{{
474
475 /**
476 * Retrieve the given string identifier corresponding message.
477 *
478 * @param string $code the string identifier of the message
479 * @param array $vars an array of template variables
480 *
481 * @return string
482 * @access public
483 */
484 public function get($code, $vars = array())
485 {
486 return $code;
487 }
488
489 // }}}
490 }
491
492 // }}}
493
494 ?>