Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 2004 The PHP Group                                     |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 3.0 of the PHP license,       |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available through the world-wide-web at the following url:           |
11
// | http://www.php.net/license/3_0.txt.                                  |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Author: Bertrand Mansion <bmansion@mamasam.com>                      |
17
// +----------------------------------------------------------------------+
18
//
19
// $Id: Getargs_basic_testcase.php 304307 2010-10-11 11:28:02Z jespino $
20
if (!defined('PHPUnit_MAIN_METHOD')) {
21
    define('PHPUnit_MAIN_METHOD', 'Getargs_Basic_testCase::main');
22
}
23
 
24
require_once 'Console/Getargs.php';
25
require_once 'PHPUnit/Framework.php';
26
 
27
/**
28
 * Unit tests for Console_Getargs package.
29
 */
30
 
31
class Getargs_Basic_testCase extends PHPUnit_Framework_TestCase
32
{
33
    /**
34
     * Runs the test methods of this class.
35
     *
36
     * @access public
37
     * @static
38
     */
39
    public static function main() {
40
        require_once 'PHPUnit/TextUI/TestRunner.php';
41
 
42
        $suite  = new PHPUnit_Framework_TestSuite('Getargs_Basic_testCase');
43
        PHPUnit_TextUI_TestRunner::run($suite);
44
    }
45
 
46
 
47
 
48
    function testFixed1()
49
    {
50
        $config = array(
51
                    'name' => array(
52
                        'short' => 'n',
53
                        'min'   => 1,
54
                        'max'   => 1,
55
                        'desc'  => 'An argument.')
56
                       );
57
 
58
        $args = array('-n', 'arg1');
59
        $message = implode(' ', $args);
60
        $obj =& Console_Getargs::factory($config, $args);
61
 
62
        if (PEAR::isError($obj)) {
63
            $this->fail("'$message' ".$obj->getMessage());
64
        } else {
65
            $this->assertEquals($args[1], $obj->getValue('n'), "'$message' Incorrect value returned");
66
        }
67
 
68
        $args = array('--name', 'arg1');
69
        $message = implode(' ', $args);
70
        $obj =& Console_Getargs::factory($config, $args);
71
 
72
        if (PEAR::isError($obj)) {
73
            $this->fail("'$message' ".$obj->getMessage());
74
        } else {
75
            $this->assertEquals($args[1], $obj->getValue('name'), "'$message' Incorrect value returned");
76
        }
77
    }
78
 
79
    function testFixed2()
80
    {
81
        $config = array(
82
                    'name' => array(
83
                        'short' => 'n',
84
                        'min'   => 2,
85
                        'max'   => 2,
86
                        'desc'  => 'Two arguments.')
87
                       );
88
 
89
        $args = array('-n', 'arg1', 'arg2');
90
        $message = implode(' ', $args);
91
        $obj =& Console_Getargs::factory($config, $args);
92
 
93
        if (PEAR::isError($obj)) {
94
            $this->fail("'$message' ".$obj->getMessage());
95
        } else {
96
            $this->assertEquals(array($args[1], $args[2]), $obj->getValue('n'), "'$message' Incorrect value returned");
97
        }
98
 
99
        $args = array('--name', 'arg1', 'arg2');
100
        $message = implode(' ', $args);
101
        $obj =& Console_Getargs::factory($config, $args);
102
 
103
        if (PEAR::isError($obj)) {
104
            $this->fail("'$message' ".$obj->getMessage());
105
        } else {
106
            $this->assertEquals(array($args[1], $args[2]), $obj->getValue('name'), "'$message' Incorrect value returned");
107
        }
108
 
109
    }
110
 
111
    function testFixed1WithDefault()
112
    {
113
        $config = array(
114
                    'name' => array(
115
                       'min'     => 1,
116
                       'max'     => 1,
117
                       'desc'    => 'Fixed number with default value if not used.',
118
                       'default' => 'default1')
119
                       );
120
 
121
        $args = array('--name', 'arg1');
122
        $message = implode(' ', $args);
123
        $obj =& Console_Getargs::factory($config, $args);
124
 
125
        if (PEAR::isError($obj)) {
126
            $this->fail("'$message' ".$obj->getMessage());
127
        } else {
128
            $this->assertEquals($args[1], $obj->getValue('name'), "'$message' Incorrect value returned");
129
        }
130
 
131
        $args = array();
132
 
133
        $obj =& Console_Getargs::factory($config, $args);
134
 
135
        if (PEAR::isError($obj)) {
136
            $this->fail($obj->getMessage());
137
        } else {
138
            $this->assertEquals('default1', $obj->getValue('name'), "Incorrect value returned");
139
        }
140
 
141
    }
142
 
143
    function testFixed2WithDefault()
144
    {
145
        $config = array(
146
                    'names' => array(
147
                       'min'     => 2,
148
                       'max'     => 2,
149
                       'desc'    => 'Fixed number with default values if not used.',
150
                       'default' => array('default1', 'default2'))
151
                       );
152
 
153
        $args = array('--names', 'arg1', 'arg2');
154
        $message = implode(' ', $args);
155
        $obj =& Console_Getargs::factory($config, $args);
156
 
157
        if (PEAR::isError($obj)) {
158
            $this->fail("'$message' ".$obj->getMessage());
159
        } else {
160
            $this->assertEquals(array($args[1], $args[2]), $obj->getValue('names'), "'$message' Incorrect value returned");
161
        }
162
 
163
        $args = array();
164
 
165
        $obj =& Console_Getargs::factory($config, $args);
166
 
167
        if (PEAR::isError($obj)) {
168
            $this->fail($obj->getMessage());
169
        } else {
170
            $this->assertEquals(array('default1', 'default2'), $obj->getValue('names'), "Incorrect value returned");
171
        }
172
 
173
    }
174
 
175
    function testSwitch()
176
    {
177
        $config = array(
178
                    'switch' => array(
179
                        'short' => 's',
180
                        'max'   => 0,
181
                        'desc'  => 'A switch.')
182
                        );
183
 
184
        $args = array('-s');
185
        $message = implode(' ', $args);
186
        $obj =& Console_Getargs::factory($config, $args);
187
 
188
        if (PEAR::isError($obj)) {
189
            $this->fail("'$message' ".$obj->getMessage());
190
        } else {
191
            $this->assertTrue($obj->isDefined('s'), "'$message' Switch not defined");
192
            $this->assertTrue($obj->isDefined('switch'), "'$message' Switch not defined");
193
        }
194
 
195
        $args = array('--switch');
196
        $message = implode(' ', $args);
197
        $obj =& Console_Getargs::factory($config, $args);
198
 
199
        if (PEAR::isError($obj)) {
200
            $this->fail("'$message' ".$obj->getMessage());
201
        } else {
202
            $this->assertTrue($obj->isDefined('s'), "'$message' Switch not defined");
203
            $this->assertTrue($obj->isDefined('switch'), "'$message' Switch not defined");
204
        }
205
 
206
    }
207
 
208
    function testSwitchWithDefaultIfValueNotSet()
209
    {
210
        $config = array(
211
                    'default' => array(
212
                        'short'   => 'd',
213
                        'min'     => 0,
214
                        'max'     => 1,
215
                        'desc'    => 'A switch with a default value if no value set.',
216
                        'default' => 3)
217
                        );
218
 
219
        $args = array('-d');
220
        $message = implode(' ', $args);
221
        $obj =& Console_Getargs::factory($config, $args);
222
 
223
        if (PEAR::isError($obj)) {
224
            $this->fail("'$message' ".$obj->getMessage());
225
        } else {
226
            $this->assertEquals(3, $obj->getValue('d'), "'$message' Incorrect value returned");
227
            $this->assertEquals(3, $obj->getValue('default'), "'$message' Incorrect value returned");
228
        }
229
 
230
        $args = array('-d', 4);
231
        $message = implode(' ', $args);
232
        $obj =& Console_Getargs::factory($config, $args);
233
 
234
        if (PEAR::isError($obj)) {
235
            $this->fail("'$message' ".$obj->getMessage());
236
        } else {
237
            $this->assertEquals(4, $obj->getValue('d'), "'$message' Incorrect value returned");
238
            $this->assertEquals(4, $obj->getValue('default'), "'$message' Incorrect value returned");
239
        }
240
    }
241
 
242
    function testVariable1_3()
243
    {
244
        $config = array(
245
                    'name' => array(
246
                        'min' => 1,
247
                        'max' => 3,
248
                        'desc' => 'One to three values.')
249
                        );
250
 
251
        $args = array('--name', 'arg1');
252
        $message = implode(' ', $args);
253
        $obj =& Console_Getargs::factory($config, $args);
254
 
255
        if (PEAR::isError($obj)) {
256
            $this->fail("'$message' ".$obj->getMessage());
257
        } else {
258
            $this->assertEquals('arg1', $obj->getValue('name'), "'$message' Incorrect value returned");
259
        }
260
 
261
        $args = array('--name', 'arg1', 'arg2', 'arg3');
262
        $message = implode(' ', $args);
263
        $obj =& Console_Getargs::factory($config, $args);
264
 
265
        if (PEAR::isError($obj)) {
266
            $this->fail("'$message' ".$obj->getMessage());
267
        } else {
268
            $this->assertEquals(array('arg1', 'arg2', 'arg3'), $obj->getValue('name'), "'$message' Incorrect values returned");
269
        }
270
 
271
    }
272
 
273
    function testVariable1_3WithDefault()
274
    {
275
        // TODO : using min => 0 should maybe work too
276
        $config = array(
277
                    'name' => array(
278
                        'min'     => 1,
279
                        'max'     => 3,
280
                        'desc'    => 'Zero to three values using default if no values set.',
281
                        'default' => 'default1')
282
                        );
283
 
284
        $args = array('--name', 'arg1');
285
        $message = implode(' ', $args);
286
        $obj =& Console_Getargs::factory($config, $args);
287
 
288
        if (PEAR::isError($obj)) {
289
            $this->fail("'$message' ".$obj->getMessage());
290
        } else {
291
            $this->assertEquals('arg1', $obj->getValue('name'), "'$message' Incorrect value returned");
292
        }
293
 
294
        $args = array('--name', 'arg1', 'arg2', 'arg3');
295
        $message = implode(' ', $args);
296
        $obj =& Console_Getargs::factory($config, $args);
297
 
298
        if (PEAR::isError($obj)) {
299
            $this->fail("'$message' ".$obj->getMessage());
300
        } else {
301
            $this->assertEquals(array('arg1', 'arg2', 'arg3'), $obj->getValue('name'), "'$message' Incorrect values returned");
302
        }
303
 
304
        $args = array();
305
 
306
        $obj =& Console_Getargs::factory($config, $args);
307
 
308
        if (PEAR::isError($obj)) {
309
            $this->fail($obj->getMessage());
310
        } else {
311
            $this->assertEquals('default1', $obj->getValue('name'), "'$message' Incorrect values returned");
312
        }
313
 
314
    }
315
 
316
    function testNoLimit()
317
    {
318
        $config = array(
319
                    'name' => array(
320
                        'min'     => 1,
321
                        'max'     => -1,
322
                        'desc'    => 'Unlimited number of values.')
323
                        );
324
 
325
        $args = array('--name', 'arg1');
326
        $message = implode(' ', $args);
327
        $obj =& Console_Getargs::factory($config, $args);
328
 
329
        if (PEAR::isError($obj)) {
330
            $this->fail("'$message' ".$obj->getMessage());
331
        } else {
332
            $this->assertEquals('arg1', $obj->getValue('name'), "'$message' Incorrect value returned");
333
        }
334
 
335
        $args = array('--name', 'arg1', 'arg2', 'arg3');
336
        $message = implode(' ', $args);
337
        $obj =& Console_Getargs::factory($config, $args);
338
 
339
        if (PEAR::isError($obj)) {
340
            $this->fail($obj->getMessage());
341
        } else {
342
            $this->assertEquals(array('arg1', 'arg2', 'arg3'), $obj->getValue('name'), "'$message' Incorrect values returned");
343
        }
344
    }
345
 
346
    function testNoLimitWithDefault()
347
    {
348
        $config = array(
349
                    'name' => array(
350
                        'min'     => 1,
351
                        'max'     => -1,
352
                        'desc'    => 'Unlimited number of values.',
353
                        'default' => 'default1')
354
                        );
355
 
356
        $args = array();
357
 
358
        $obj =& Console_Getargs::factory($config, $args);
359
 
360
        if (PEAR::isError($obj)) {
361
            $this->fail($obj->getMessage());
362
        } else {
363
            $this->assertEquals('default1', $obj->getValue('name'), "Incorrect values returned");
364
        }
365
    }
366
 
367
    function testAlias()
368
    {
369
        $config = array(
370
                    'name|alias' => array(
371
                        'short'   => 'n|a',
372
                        'min'     => 0,
373
                        'max'     => 0,
374
                        'desc'    => 'Unlimited number of values with alias.')
375
                        );
376
 
377
        $args = array('-n', '-a', '--name', '--alias');
378
        foreach ($args as $arg) {
379
            $message = $arg;
380
            $obj =& Console_Getargs::factory($config, array($arg));
381
 
382
            if (PEAR::isError($obj)) {
383
                $this->fail($obj->getMessage());
384
            } else {
385
                $this->assertTrue($obj->isDefined('name'), "Value 'name' is not defined");
386
                $this->assertTrue($obj->isDefined('alias'), "Value 'alias' is not defined");
387
                $this->assertTrue($obj->isDefined('n'), "Value 'n' is not defined");
388
                $this->assertTrue($obj->isDefined('a'), "Value 'a' is not defined");
389
            }
390
        }
391
    }
392
 
393
 
394
 
395
    /**
396
    * isDefined should return true when the parameter is passed on cmdline
397
    */
398
    function testIsDefined()
399
    {
400
        $config = array(
401
            'name' => array(
402
                'short'   => 'n',
403
                'min'     => 1,
404
                'max'     => 1,
405
                'desc'    => 'An argument.',
406
                'default' => 'john'
407
            )
408
        );
409
 
410
        $args = array('-n', 'arg1');
411
        $obj  =& Console_Getargs::factory($config, $args);
412
 
413
        $this->assertFalse(PEAR::isError($obj));
414
        $this->assertTrue($obj->isDefined('n'));
415
        $this->assertTrue($obj->isDefined('name'));
416
    }
417
 
418
 
419
 
420
    /**
421
    * isDefined should return true when the parameter is passed on cmdline,
422
    * even when checking for the alias name
423
    */
424
    function testIsDefinedAlias()
425
    {
426
        $config = array(
427
            'name|alias' => array(
428
                'short'   => 'n',
429
                'min'     => 1,
430
                'max'     => 1,
431
                'desc'    => 'An argument.',
432
                'default' => 'john'
433
            )
434
        );
435
 
436
        $args = array('-n', 'arg1');
437
        $obj  =& Console_Getargs::factory($config, $args);
438
 
439
        $this->assertFalse(PEAR::isError($obj));
440
        $this->assertTrue($obj->isDefined('n'));
441
        $this->assertTrue($obj->isDefined('name'));
442
        $this->assertTrue($obj->isDefined('alias'));
443
    }
444
 
445
 
446
 
447
    /**
448
    * isDefined should return false when the parameter is not
449
    * given on cmdline - even if there is a default value
450
    */
451
    function testIsDefinedDefaultOnly()
452
    {
453
        $config = array(
454
            'name' => array(
455
                'short'   => 'n',
456
                'min'     => 1,
457
                'max'     => 1,
458
                'desc'    => 'An argument.',
459
                'default' => 'john'
460
            )
461
        );
462
 
463
        $args = array();
464
        $obj  =& Console_Getargs::factory($config, $args);
465
 
466
        $this->assertFalse(PEAR::isError($obj));
467
        $this->assertFalse($obj->isDefined('n'));
468
        $this->assertFalse($obj->isDefined('name'));
469
    }
470
 
471
 
472
}
473
 
474
if (PHPUnit_MAIN_METHOD == 'Getargs_Basic_testCase::main') {
475
    Getargs_Basic_testCase::main();
476
}
477
 
478
?>