Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Console Getopt tests
4
 *
5
 * PHP version 5
6
 *
7
 * All rights reserved.
8
 * Redistribution and use in source and binary forms, with or without modification,
9
 * are permitted provided that the following conditions are met:
10
 * + Redistributions of source code must retain the above copyright notice,
11
 * this list of conditions and the following disclaimer.
12
 * + Redistributions in binary form must reproduce the above copyright notice,
13
 * this list of conditions and the following disclaimer in the documentation and/or
14
 * other materials provided with the distribution.
15
 * + The names of its contributors may not be used to endorse or promote
16
 * products derived from this software without specific prior written permission.
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * @category  PHP
30
 * @package   Console_GetoptPlus
31
 * @author    Michel Corne <mcorne@yahoo.com>
32
 * @copyright 2008 Michel Corne
33
 * @license   http://www.opensource.org/licenses/bsd-license.php The BSD License
34
 * @version   SVN: $Id: GetoptTest.php 47 2008-01-10 11:03:38Z mcorne $
35
 * @link      http://pear.php.net/package/Console_GetoptPlus
36
 */
37
// Call tests_GetoptPlus_GetoptTest::main() if this source file is executed directly.
38
if (!defined("PHPUnit_MAIN_METHOD")) {
39
    define("PHPUnit_MAIN_METHOD", "tests_GetoptPlus_GetoptTest::main");
40
}
41
 
42
require_once "PHPUnit/Framework/TestCase.php";
43
require_once "PHPUnit/Framework/TestSuite.php";
44
 
45
require_once 'Console/GetoptPlus/Getopt.php';
46
 
47
/**
48
 * Test class for Console_GetoptPlus_Getopt.
49
 * Generated by PHPUnit_Util_Skeleton on 2007-05-17 at 11:00:39.
50
 *
51
 * @category  PHP
52
 * @package   Console_GetoptPlus
53
 * @author    Michel Corne <mcorne@yahoo.com>
54
 * @copyright 2008 Michel Corne
55
 * @license   http://www.opensource.org/licenses/bsd-license.php The BSD License
56
 * @version   Release:@package_version@
57
 * @link      http://pear.php.net/package/Console_GetoptPlus
58
 */
59
class tests_GetoptPlus_GetoptTest extends PHPUnit_Framework_TestCase
60
{
61
    private $getopt;
62
 
63
    /**
64
     * Runs the test methods of this class.
65
     *
66
     * @access public
67
     * @static
68
     */
69
    public static function main()
70
    {
71
        require_once "PHPUnit/TextUI/TestRunner.php";
72
 
73
        $suite = new PHPUnit_Framework_TestSuite("Console_GetoptPlus_GetoptTest");
74
        $result = PHPUnit_TextUI_TestRunner::run($suite);
75
    }
76
 
77
    /**
78
     * Sets up the fixture, for example, open a network connection.
79
     * This method is called before a test is executed.
80
     *
81
     * @access protected
82
     */
83
    protected function setUp()
84
    {
85
        $this->getopt = new Console_GetoptPlus_Getopt();
86
    }
87
 
88
    /**
89
     * Tears down the fixture, for example, close a network connection.
90
     * This method is called after a test is executed.
91
     *
92
     * @access protected
93
     */
94
    protected function tearDown()
95
    {
96
    }
97
 
98
    /**
99
     * Tests readPHPArgv()
100
     */
101
    public function testReadPHPArgv()
102
    {
103
        global $argv;
104
        // format: <$argv>, <$_SERVER['argv']>,
105
        // <$GLOBALS['HTTP_SERVER_VARS']['argv']>, <expected>
106
        $test = array(// /
107
 
108
            1 => array(null, array(), null, array()),
109
            2 => array(null, null, array(), array()),
110
            3 => array(null, null, null, 13),
111
            );
112
 
113
        foreach($test as $idx => $values) {
114
            list($argv, $_SERVER['argv'], $GLOBALS['HTTP_SERVER_VARS']['argv'], $expected) = $values;
115
            // process the arguments
116
            try {
117
                $result = $this->getopt->readPHPArgv();
118
                $msg = '';
119
            }
120
            catch(Console_GetoptPlus_Exception $e) {
121
                $result = $e->getCode();
122
                $msg = "\n" . $e->getMessage();
123
            }
124
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
125
        }
126
    }
127
 
128
    /**
129
     * Tests isOption()
130
     */
131
    public function testIsOption()
132
    {
133
        // format: <option>, true|false
134
        $test = array(// /
135
            // valid options
136
 
137
            1 => array('-f', true),
138
            // invalid options
139
            10 => array('--', false),
140
            11 => array('-- foo', false),
141
            12 => array('foo', false),
142
            13 => array('foo--foo', false),
143
            14 => array('-', false),
144
            15 => array('- foo', false),
145
            16 => array('f', false),
146
            17 => array('f-f', false),
147
            );
148
 
149
        foreach($test as $idx => $values) {
150
            list($option, $expected) = $values;
151
            // cheks if the option is a valid one
152
            $isValid = $this->getopt->isOption($option);
153
 
154
            $this->assertEquals($expected, $isValid, 'test #' . $idx);
155
        }
156
    }
157
 
158
    /**
159
     * Tests parseLongOptionsDef()
160
     */
161
    public function testParseLongOptionsDef()
162
    {
163
        // format: <options definition>, <parsed options or errors>
164
        $test = array(// /
165
            // valid options
166
 
167
            1 => array(null, array()),
168
            2 => array(array(), array()),
169
            3 => array('foo', array('foo' => 'noarg')),
170
            4 => array(array('foo='), array('foo' => 'mandatory')),
171
            5 => array(array('foo=='), array('foo' => 'optional')),
172
            6 => array(// /
173
                array('foo', 'bar=', 'blah=='),
174
                array('foo' => 'noarg', 'bar' => 'mandatory', 'blah' => 'optional')),
175
            // invalid options, in the order errors appear in parseLongOptionsDef()
176
            10 => array(array('foo==', 'bar', 'foo'), 20),
177
            11 => array('foo===', 21),
178
            12 => array('=foo', 21),
179
            13 => array('==foo', 21),
180
            14 => array('=foo=bar', 21),
181
            15 => array('=foo bar', 21),
182
            16 => array('foo:', 21),
183
            );
184
 
185
        foreach($test as $idx => $values) {
186
            list($options, $expected) = $values;
187
            // parses the options, possibly captures an exception
188
            try {
189
                $result = $this->getopt->parseLongOptionsDef($options);
190
                $msg = '';
191
            }
192
            catch(Console_GetoptPlus_Exception $e) {
193
                $result = $e->getCode();
194
                $msg = "\n" . $e->getMessage();
195
            }
196
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
197
        }
198
    }
199
 
200
    /**
201
     * Tests parseShortOptionsDef()
202
     */
203
    public function testParseShortOptionsDef()
204
    {
205
        // format: <options definition>, <parsed options or error>
206
        $test = array(// /
207
            // valid options
208
 
209
            1 => array('', array()),
210
            2 => array('f', array('f' => 'noarg')),
211
            3 => array('f:', array('f' => 'mandatory')),
212
            4 => array('f::', array('f' => 'optional')),
213
            5 => array('fb:c::', array('f' => 'noarg', 'b' => 'mandatory', 'c' => 'optional')),
214
            // invalid options, in the order errors appear in parseShortOptionsDef()
215
            10 => array(array('f'), 22),
216
            11 => array('f::bf', 20),
217
            12 => array('f:::', 23),
218
            13 => array(':f', 23),
219
            14 => array('::f', 23),
220
            15 => array('f b', 23),
221
            16 => array('f=', 23),
222
            );
223
 
224
        foreach($test as $idx => $values) {
225
            list($options, $expected) = $values;
226
            // parses the options, possibly captures an exception
227
            try {
228
                $result = $this->getopt->parseShortOptionsDef($options);
229
                $msg = '';
230
            }
231
            catch(Console_GetoptPlus_Exception $e) {
232
                $result = $e->getCode();
233
                $msg = "\n" . $e->getMessage();
234
            }
235
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
236
        }
237
    }
238
 
239
    /**
240
     * Tests createShorcuts()
241
     */
242
    public function testCreateShorcuts()
243
    {
244
        // format: <options definition>, <expected shortcuts and ambigous names>
245
        $test = array(// /
246
            // valid options
247
 
248
            1 => array(// /
249
                array('foo' => 'noarg'),
250
                array(array('f' => 'foo', 'fo' => 'foo', 'foo' => 'foo'), array())),
251
            2 => array(array('fo' => 'noarg', 'ba' => 'mandatory', 'bl' => 'optional'),
252
                array(
253
                    array('f' => 'fo', 'fo' => 'fo', 'ba' => 'ba', 'bl' => 'bl'),
254
                    array('b' => array('ba', 'bl')),
255
                    )),
256
            // invalid options, in the order errors appear in parseLongOptionsDef()
257
            10 => array(array('fo' => 'noarg', 'ba' => 'mandatory', 'bl' => 'optional', 'b' => 'optional'), 10),
258
 
259
            );
260
        foreach($test as $idx => $values) {
261
            list($options, $expected) = $values;
262
            // creates the shortcuts, possibly captures an exception
263
            try {
264
                $result = $this->getopt->createShorcuts($options, 'shortcuts');
265
                $msg = '';
266
            }
267
            catch(Console_GetoptPlus_Exception $e) {
268
                $result = $e->getCode();
269
                $msg = "\n" . $e->getMessage();
270
            }
271
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
272
        }
273
    }
274
 
275
    /**
276
     * Tests verifyNoAmbiguity()
277
     */
278
    public function testVerifyNoAmbiguity()
279
    {
280
        // format: <options definition>, <ambiguity>, <true or an exception>
281
        $test = array(// /
282
            // valid options
283
 
284
            1 => array(null, 'strict', true),
285
            2 => array(array(), 'strict', true),
286
            3 => array(array('foo' => 'noarg'), 'strict', true),
287
            4 => array(array('foo' => 'mandatory'), 'strict', true),
288
            5 => array(array('foo' => 'optional'), 'strict', true),
289
            6 => array(array('foo' => 'noarg', 'bar' => 'mandatory', 'blah' => 'optional'),
290
                'strict', true),
291
            7 => array(array('foo' => 'noarg', 'bar' => 'mandatory', 'blah' => 'optional'),
292
                'loose', true),
293
            8 => array(array('foo' => 'noarg', 'bar' => 'mandatory', 'fool' => 'optional'),
294
                'loose', true),
295
            // invalid options
296
            10 => array(array('foo' => 'optional', 'bar' => 'mandatory', 'fool' => 'optional',),
297
                'loose', 10),
298
            11 => array(array('foo' => 'noarg', 'bar' => 'mandatory', 'fool' => 'optional',),
299
                'strict', 10),
300
            );
301
        foreach($test as $idx => $values) {
302
            list($options, $ambiguity, $expected) = $values;
303
            // verifies there is no name ambiguity, possibly captures an exception
304
            try {
305
                $result = $this->getopt->verifyNoAmbiguity($options, $ambiguity);
306
                $msg = '';
307
            }
308
            catch(Console_GetoptPlus_Exception $e) {
309
                $result = $e->getCode();
310
                $msg = "\n" . $e->getMessage();
311
            }
312
            $this->assertSame($expected, $result , 'test #' . $idx . $msg);
313
        }
314
    }
315
 
316
    /**
317
     * Tests process()
318
     */
319
    public function testProcess()
320
    {
321
        // format: <version>, <arguments>, <short options definition>,
322
        // <long options definition>, <ambiguity>, <expected options and parameters>
323
        $test = array(// /
324
            // valid command
325
 
326
            1 => array(2, array('foo'), '', null, null, array(array(), array('foo'))),
327
            2 => array(2, array('--', '--foo', 'bar'), '', null, null, array(array(), array('--foo', 'bar'))),
328
            3 => array(2, array('--', '-f', 'bar'), '', null, null, array(array(), array('-f', 'bar'))),
329
            4 => array(2, array('-', '--foo', 'bar'), '', null, null, array(array(), array('-', '--foo', 'bar'))),
330
            5 => array(2, array('-', '-f', 'bar'), '', null, null, array(array(), array('-', '-f', 'bar'))),
331
            6 => array(2, array('--foo'), '', array(), null, array(array(), array('foo'))),
332
            7 => array(1, array('foo'), '', null, null, array(array(), array())),
333
            // valid command with 1 long option
334
            10 => array(2, array('--foo'), '', array('foo'), null, array(array(array('--foo', '')), array())),
335
            11 => array(2, array('--foo=bar'), '', array('foo='), null, array(array(array('--foo', 'bar')), array())),
336
            12 => array(2, array('--foo', 'bar'), '', array('foo='), null, array(array(array('--foo', 'bar')), array())),
337
            13 => array(2, array('--foo', 'bar'), '', array('foo=='), null, array(array(array('--foo', '')), array('bar'))),
338
            14 => array(2, array('--foo'), '', array('foo=='), null, array(array(array('--foo', '')), array())),
339
            // valid command with 2 long options
340
            20 => array(2, array('--foo'), '', array('foo', 'bar'), null, array(array(array('--foo', '')), array())),
341
            21 => array(2, array('--foo', '--bar'), '', array('foo', 'bar'), null,
342
                array(array(array('--foo', ''), array('--bar', '')), array())),
343
            22 => array(2, array('--foo=bar', '--blah'), '', array('foo=', 'blah'), null,
344
                array(array(array('--foo', 'bar'), array('--blah', '')), array())),
345
            23 => array(2, array('--foo', 'bar', '--blah'), '', array('foo=', 'blah'), null,
346
                array(array(array('--foo', 'bar'), array('--blah', '')), array())),
347
            24 => array(2, array('--foo=bar', '--blah'), '', array('foo==', 'blah'), null,
348
                array(array(array('--foo', 'bar'), array('--blah', '')), array())),
349
            25 => array(2, array('--foo', 'bar', '--blah'), '', array('foo==', 'blah'), null,
350
                array(array(array('--foo', '')), array('bar', '--blah'))),
351
            // valid command with 1 short option
352
            30 => array(2, array('-f'), 'f', null, null, array(array(array('f', '')), array())),
353
            31 => array(2, array('-fbar'), 'f:', '', null, array(array(array('f', 'bar')), array())),
354
            32 => array(2, array('-f', 'bar'), 'f:', '', null, array(array(array('f', 'bar')), array())),
355
            33 => array(2, array('-f', 'bar'), 'f::', '', null, array(array(array('f', '')), array('bar'))),
356
            34 => array(2, array('-f'), 'f::', '', null, array(array(array('f', '')), array())),
357
            // valid command with 2 short options
358
            40 => array(2, array('-f'), 'fb', '', null, array(array(array('f', '')), array())),
359
            41 => array(2, array('-f', '-b'), 'fb', '', null, array(array(array('f', ''), array('b', '')), array())),
360
            42 => array(2, array('-fbar', '-b'), 'f:b', '', null, array(array(array('f', 'bar'), array('b', '')), array())),
361
            43 => array(2, array('-f', 'bar', '-b'), 'f:b', '', null, array(array(array('f', 'bar'), array('b', '')), array())),
362
            44 => array(2, array('-fbar', '-b'), 'f::b', '', null, array(array(array('f', 'bar'), array('b', '')), array())),
363
            45 => array(2, array('-f', 'bar', '-b'), 'f::b', '', null, array(array(array('f', '')), array('bar', '-b'))),
364
            46 => array(2, array('-fb', 'bar'), 'fb', '', null, array(array(array('f', ''), array('b', '')), array('bar'))),
365
            47 => array(2, array('-fb', 'bar'), 'fb:', '', null, array(array(array('f', ''), array('b', 'bar')), array())),
366
            48 => array(2, array('-fb', 'bar'), 'fb::', '', null, array(array(array('f', ''), array('b', '')), array('bar'))),
367
            49 => array(2, array('-fbbar'), 'fb::', '', null, array(array(array('f', ''), array('b', 'bar')), array())),
368
            // valid command with 1 long + 1 short options
369
            50 => array(2, array('--foo'), 'fb', array('foo', 'bar'), null, array(array(array('--foo', '')), array())),
370
            51 => array(2, array('--foo', '-b'), 'fb', array('foo', 'bar'), null,
371
                array(array(array('--foo', ''), array('b', '')), array())),
372
            52 => array(2, array('--foo=bar', '-b'), 'f:b', array('foo=', 'blah'), null,
373
                array(array(array('--foo', 'bar'), array('b', '')), array())),
374
            53 => array(2, array('--foo', 'bar', '-b'), 'f:b', array('foo=', 'blah'), null,
375
                array(array(array('--foo', 'bar'), array('b', '')), array())),
376
            54 => array(2, array('--foo=bar', '-b'), 'f::b', array('foo==', 'blah'), null,
377
                array(array(array('--foo', 'bar'), array('b', '')), array())),
378
            55 => array(2, array('--foo', 'bar', '-b'), 'f::b', array('foo==', 'blah'), null,
379
                array(array(array('--foo', '')), array('bar', '-b'))),
380
            // valid command with 1 short + 1 long options
381
            60 => array(2, array('-f'), 'fb', array('foo', 'bar'), null, array(array(array('f', '')), array())),
382
            61 => array(2, array('-f', '--bar'), 'fb', array('foo', 'bar'), null,
383
                array(array(array('f', ''), array('--bar', '')), array())),
384
            62 => array(2, array('-f', 'bar', '--blah'), 'f:b', array('foo=', 'blah'), null,
385
                array(array(array('f', 'bar'), array('--blah', '')), array())),
386
            63 => array(2, array('-f', 'bar', '--blah'), 'f::b', array('foo==', 'blah'), null,
387
                array(array(array('f', '')), array('bar', '--blah'))),
388
            // integration test
389
            70 => array(2, // /
390
                array('--foo', '-f', '--bar=1', '-b2', '--cool=3', '-c4', 'abc'),
391
                'fb:c::', array('foo', 'bar=', 'cool=='), null,
392
                array(// /
393
                    array(array('--foo', ''), array('f', ''), array('--bar', '1'),
394
                        array('b', '2'), array('--cool', '3'), array('c', '4')),
395
                    array('abc'))),
396
            71 => array(2, // /
397
                array('--foo', '-fb2', '--bar', '1', '--cool=3', '-c', 'abc'),
398
                'fb:c::', array('foo', 'bar=', 'cool=='), null,
399
                array(// /
400
                    array(array('--foo', ''), array('f', ''), array('b', '2'),
401
                        array('--bar', '1'), array('--cool', '3'), array('c', '')),
402
                    array('abc'))),
403
            72 => array(2, // /
404
                array('--foo', '-fb2', '--bar', '1', '--', '--cool=3', '-c', 'abc'),
405
                'fb:c::', array('foo', 'bar=', 'cool=='), null,
406
                array(// /
407
                    array(array('--foo', ''), array('f', ''), array('b', '2'),
408
                        array('--bar', '1')),
409
                    array('--cool=3', '-c', 'abc'))),
410
            73 => array(2, // /
411
                array('--fo', '-fb2', '--ba', '1', '--co=3', 'abc'),
412
                'fb:c::', array('foo', 'bar=', 'cool=='), 'shortcuts',
413
                array(// /
414
                    array(array('--foo', ''), array('f', ''), array('b', '2'),
415
                        array('--bar', '1'), array('--cool' , '3')),
416
                    array('abc'))),
417
            // invalid options, in the order errors appear
418
            // in process(), parseLongOption(), parseShortOption()
419
            80 => array(2, array('--foo'), '', array('bar'), null, 14),
420
            81 => array(2, array('-f'), '', array(), null, 14),
421
            82 => array(2, array('-f'), 'b', array(), null, 14),
422
            83 => array(2, array('--foo'), '', array('foo='), null, 11),
423
            84 => array(2, array('--foo', '--bar'), '', array('foo=', 'bar'), null, 11),
424
            85 => array(2, array('--foo', '-b'), 'b', array('foo='), null, 11),
425
            86 => array(2, array('-f'), 'f:', array(), null, 11),
426
            87 => array(2, array('-f', '-b'), 'f:b', array(), null, 11),
427
            88 => array(2, array('-f', '--bar'), 'f:', array('bar'), null, 11),
428
            89 => array(2, array('--foo=bar'), '', array('foo'), null, 12),
429
            );
430
        foreach($test as $idx => $values) {
431
            list($version, $args, $short, $long, $ambiguity, $expected) = $values;
432
            // process the arguments
433
            try {
434
                $result = $this->getopt->process($args, $short, $long, $ambiguity, $version);
435
                $msg = '';
436
            }
437
            catch(Console_GetoptPlus_Exception $e) {
438
                $result = $e->getCode();
439
                $msg = "\n" . $e->getMessage();
440
            }
441
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
442
        }
443
    }
444
 
445
    /**
446
     * Tests doGetopt()
447
     */
448
    public function testDoGetopt()
449
    {
450
        $result = Console_GetoptPlus_Getopt::doGetopt();
451
        $this->assertEquals(array(array(), array()), $result);
452
    }
453
 
454
    /**
455
     * Tests getopt()
456
     */
457
    public function testGetopt()
458
    {
459
        $result = Console_GetoptPlus_Getopt::getopt();
460
        $this->assertEquals(array(array(), array()), $result);
461
    }
462
 
463
    /**
464
     * Tests getopt2()
465
     */
466
    public function testGetopt2()
467
    {
468
        $result = Console_GetoptPlus_Getopt::getopt2();
469
        $this->assertEquals(array(array(), array()), $result);
470
    }
471
}
472
// Call tests_GetoptPlus_GetoptTest::main() if this source file is executed directly.
473
if (PHPUnit_MAIN_METHOD == "tests_GetoptPlus_GetoptTest::main") {
474
    tests_GetoptPlus_GetoptTest::main();
475
}
476
 
477
?>