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+ (Getopt Plus) 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 name of its contributors may not be used to endorse or promote products
16
 * 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: GetoptPlusTest.php 47 2008-01-10 11:03:38Z mcorne $
35
 * @link      http://pear.php.net/package/Console_GetoptPlus
36
 */
37
// Call tests_GetoptPlusTest::main() if this source file is executed directly
38
if (!defined("PHPUnit_MAIN_METHOD")) {
39
    define("PHPUnit_MAIN_METHOD", "tests_GetoptPlusTest::main");
40
}
41
 
42
require_once "PHPUnit/Framework/TestCase.php";
43
require_once "PHPUnit/Framework/TestSuite.php";
44
 
45
require_once 'Console/GetoptPlus.php';
46
 
47
/**
48
 * Test class for PHP_GetoptPlus.
49
 * Generated by PHPUnit_Util_Skeleton on 2007-05-18 at 18:49:09.
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_GetoptPlusTest extends PHPUnit_Framework_TestCase
60
{
61
    private $getoptplus;
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("PHP_GetoptPlusTest");
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->getoptplus = new Console_GetoptPlus();
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 checkOptionsConfig()
100
     */
101
    public function testCheckOptionsConfig()
102
    {
103
        // format: <options configuration>, <expected option configuration>
104
        $test = array(// /
105
            // valid options
106
 
107
                array(array('long' => 'foo', 'type' => 'noarg')),
108
                array(// /
109
                    array('long' => 'foo', 'type' => 'noarg'),
110
                    array('long' => 'help', 'type' => 'noarg', 'desc' => 'This help.', 'short' => 'h'))),
111
            1 => array(
112
                array(array('long' => 'foo', 'type' => 'noarg'), array('long' => 'bar')),
113
                array(// /
114
                    array('long' => 'foo', 'type' => 'noarg'),
115
                    array('long' => 'bar', 'type' => 'noarg'),
116
                    array('long' => 'help', 'type' => 'noarg', 'desc' => 'This help.', 'short' => 'h'))),
117
            2 => array(array(// /
118
                    array('long' => 'foo', 'type' => 'noarg'),
119
                    array('long' => 'help', 'desc' => 'Another help.')),
120
                array(// /
121
                    array('long' => 'foo', 'type' => 'noarg'),
122
                    array('long' => 'help', 'type' => 'noarg', 'desc' => 'Another help.', 'type' => 'noarg'))),
123
            3 => array(array(// /
124
                    array('long' => 'foo', 'type' => 'noarg', 'short' => 'h')),
125
                array(// /
126
                    array('long' => 'foo', 'type' => 'noarg', 'short' => 'h'),
127
                    array('long' => 'help', 'type' => 'noarg', 'desc' => 'This help.'))),
128
            // invalid options, in the order errors appear in createOptionsDef()
129
            10 => array(array(array('type' => 'noarg')), 30),
130
            11 => array(array(array('long' => 'foo', 'type' => '???')), 31),
131
            );
132
        foreach($test as $idx => $values) {
133
            list($optionsConfig, $expected) = $values;
134
            // checks the options configuration, possibly captures an exception
135
            try {
136
                $result = $this->getoptplus->checkOptionsConfig(array('options' => $optionsConfig));
137
                $msg = '';
138
            }
139
            catch(Console_GetoptPlus_Exception $e) {
140
                $result = $e->getCode();
141
                $msg = "\n" . $e->getMessage();
142
            }
143
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
144
        }
145
    }
146
 
147
    /**
148
     * Tests createOptionsDef()
149
     */
150
    public function testCreateOptionsDef()
151
    {
152
        // format: <options configuration>, <expected option definitions>
153
        $test = array(// /
154
            // valid options
155
 
156
                array(array('long' => 'foo', 'type' => 'noarg')),
157
                array('foo' => 'noarg')),
158
            1 => array(// /
159
                array(array('long' => 'foo', 'type' => 'noarg'), array('long' => 'bar', 'type' => 'noarg')),
160
                array('foo' => 'noarg', 'bar' => 'noarg')),
161
            // invalid options, in the order errors appear in createOptionsDef()
162
            10 => array(array(array('long' => 'foo?')), 21),
163
            11 => array(array(// /
164
                    array('long' => 'foo', 'type' => 'noarg'),
165
                    array('long' => 'bar', 'type' => 'noarg'),
166
                    array('long' => 'foo'), 'type' => 'noarg'),
167
                20),
168
            );
169
        foreach($test as $idx => $values) {
170
            list($optionsConfig, $expected) = $values;
171
            // creates the option definitions, possibly captures an exception
172
            try {
173
                $result = $this->getoptplus->createOptionsDef($optionsConfig, 'long', '~^\w+$~');
174
                $msg = '';
175
            }
176
            catch(Console_GetoptPlus_Exception $e) {
177
                $result = $e->getCode();
178
                $msg = "\n" . $e->getMessage();
179
            }
180
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
181
        }
182
    }
183
 
184
    /**
185
     * Tests parseLongOptionsDef()
186
     */
187
    public function testParseLongOptionsDef()
188
    {
189
        // format: <options configuration>, <expected option definitions>
190
        $test = array(// /
191
            // valid options
192
 
193
                array(array('long' => 'foo', 'type' => 'noarg')),
194
                array('foo' => 'noarg')),
195
            1 => array(// /
196
                array(array('long' => 'foo', 'type' => 'noarg'), array('short' => 'b', 'type' => 'noarg')),
197
                array('foo' => 'noarg')),
198
            // invalid options, in the order errors appear in createOptionsDef()
199
            10 => array(array(array('long' => 'foo?')), 21),
200
            );
201
        foreach($test as $idx => $values) {
202
            list($optionsConfig, $expected) = $values;
203
            // creates the option definitions, possibly captures an exception
204
            try {
205
                $result = $this->getoptplus->parseLongOptionsDef($optionsConfig);
206
                $msg = '';
207
            }
208
            catch(Console_GetoptPlus_Exception $e) {
209
                $result = $e->getCode();
210
                $msg = "\n" . $e->getMessage();
211
            }
212
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
213
        }
214
    }
215
 
216
    /**
217
     * Tests parseShortOptionsDef()
218
     */
219
    public function testParseShortOptionsDef()
220
    {
221
        // format: <options configuration>, <expected option definitions>
222
        $test = array(// /
223
            // valid options
224
 
225
                array(array('short' => 'f', 'type' => 'noarg')),
226
                array('f' => 'noarg')),
227
            1 => array(// /
228
                array(array('short' => 'f', 'type' => 'noarg'), array('long' => 'bar', 'type' => 'noarg')),
229
                array('f' => 'noarg')),
230
            // invalid options, in the order errors appear in createOptionsDef()
231
            10 => array(array(array('short' => 'f?')), 21),
232
            );
233
        foreach($test as $idx => $values) {
234
            list($optionsConfig, $expected) = $values;
235
            // creates the option definitions, possibly captures an exception
236
            try {
237
                $result = $this->getoptplus->parseShortOptionsDef($optionsConfig);
238
                $msg = '';
239
            }
240
            catch(Console_GetoptPlus_Exception $e) {
241
                $result = $e->getCode();
242
                $msg = "\n" . $e->getMessage();
243
            }
244
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
245
        }
246
    }
247
 
248
    /**
249
     * Tests tidyOptions()
250
     */
251
    public function testTidyOptions()
252
    {
253
        // format <options>, <convert option names>, <convert to associative array>,
254
        // <expected tidied options>
255
        $test = array(// /
256
            // valid long options
257
 
258
            1 => array(array(array('--foo', null)), null, true, array('foo' => '')),
259
            2 => array(array(array('--foo', null)), 'long2short', false, array(array('f', null))),
260
            3 => array(array(array('--foo', null)), 'long2short', true, array('f' => '')),
261
            4 => array(array(array('--foo', null)), 'short2long', false, array(array('--foo', ''))),
262
            5 => array(array(array('--foo', null)), 'short2long', true, array('foo' => '')),
263
            6 => array(array(array('--blah', null)), 'long2short', false, array(array('--blah', null))),
264
            7 => array(array(array('--blah', null)), 'long2short', true, array('blah' => '')),
265
            // valid short options
266
            10 => array(array(array('f', null)), null, false, array(array('f', null))),
267
            11 => array(array(array('f', null)), null, true, array('f' => '')),
268
            12 => array(array(array('f', null)), 'short2long', false, array(array('--foo', ''))),
269
            13 => array(array(array('f', null)), 'short2long', true, array('foo' => '')),
270
            14 => array(array(array('f', null)), 'long2short', false, array(array('f', null))),
271
            15 => array(array(array('f', null)), 'long2short', true, array('f' => '')),
272
            16 => array(array(array('c', null)), 'short2long', false, array(array('c', null))),
273
            17 => array(array(array('c', null)), 'short2long', true, array('c' => '')),
274
            // integrated
275
            20 => array(array(array('--foo', null), array('b', 1), array('--blah', 2)),
276
                null, false, array(array('--foo', null), array('b', 1), array('--blah', 2))),
277
            21 => array(array(array('--foo', null), array('b', 1), array('--blah', 2)),
278
                null, true, array('foo' => '', 'b' => 1, 'blah' => 2)),
279
            22 => array(array(array('--foo', null), array('b', 1), array('--blah', 2)),
280
                'long2short', false, array(array('f', null), array('b', 1), array('--blah', 2))),
281
            23 => array(array(array('--foo', null), array('b', 1), array('--blah', 2)),
282
                'short2long', true, array('foo' => '', 'bar' => 1, 'blah' => 2)),
283
            // help
284
            30 => array(array(array('--foo', null), array('--help', null)), null, true, '--help'),
285
            31 => array(array(array('--foo', null), array('--help', null)), 'long2short', true, 'h'),
286
            32 => array(array(array('--foo', null), array('--help', null)), 'short2long', true, '--help'),
287
            33 => array(array(array('--foo', null), array('h', null)), 'short2long', true, '--help'),
288
            34 => array(array(array('--foo', null), array('h', null)), null, true, 'h'),
289
            // invalid options, in the order errors appear in tidyOptions()
290
            40 => array(array(array('--foo', null)), 'bar', false, 32),
291
            );
292
 
293
        $optionsConfig = array(// /
294
            array('long' => 'foo', 'short' => 'f'),
295
            array('long' => 'bar', 'short' => 'b'),
296
            array('long' => 'blah'),
297
            array('short' => 'c'),
298
            array('long' => 'help', 'short' => 'h'),
299
            );
300
        $this->getoptplus->checkOptionsConfig(array('options' => $optionsConfig));
301
 
302
        foreach($test as $idx => $values) {
303
            list($options, $convertName, $toAssocArray, $expected) = $values;
304
            // tidies the option, possibly captures an exception
305
            try {
306
                $result = $this->getoptplus->tidyOptions($options, $convertName, $toAssocArray);
307
                $msg = '';
308
            }
309
            catch(Console_GetoptPlus_Exception $e) {
310
                $result = $e->getCode();
311
                $msg = "\n" . $e->getMessage();
312
            }
313
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
314
        }
315
    }
316
 
317
    /**
318
     * Tests process()
319
     */
320
    public function testProcess()
321
    {
322
        global $argv;
323
        // format <argv>, <convert option names>, <return associative array>,
324
        // <expected processed options>
325
        $test = array(// /
326
            // valid long options
327
 
328
                array(array(array('--foo', ''), array('--bar', 'blah')), array('boo'))),
329
            1 => array(array('--foo', '--bar', 'blah', 'boo'), null, true,
330
                array(array('foo' => '', 'bar' => 'blah'), array('boo'))),
331
            2 => array(array('--foo', '-b', 'blah', 'boo'), 'long2short', false,
332
                array(array(array('f', ''), array('b' , 'blah')), array('boo'))),
333
            3 => array(array('--foo', '-b', 'blah', 'boo'), 'short2long', true,
334
                array(array('foo' => '', 'bar' => 'blah'), array('boo'))),
335
            4 => array(array('-h'), null, null, array(// /
336
                    array(array('h', "Usage: command [options]\nOptions:\n-f --foo\n-b --bar <value>\n-h --help")),
337
                    array())),
338
            // invalid options
339
            10 => array(array('--foo', null), 'bar', false, 32),
340
            );
341
 
342
        $config['options'] = array(// /
343
            array('long' => 'foo', 'short' => 'f'),
344
            array('long' => 'bar', 'short' => 'b', 'type' => 'mandatory', 'desc' => 'value'),
345
            array('long' => 'help', 'short' => 'h'),
346
            );
347
 
348
        foreach($test as $idx => $values) {
349
            list($argv, $convertName, $returnAssoc, $expected) = $values;
350
            array_unshift($argv, 'command'); // adds the command
351
            // processes the option, possibly captures an exception
352
            try {
353
                $result = $this->getoptplus->process($config, $convertName, $returnAssoc, null, false);
354
                $msg = '';
355
            }
356
            catch(Console_GetoptPlus_Exception $e) {
357
                $result = $e->getCode();
358
                $msg = "\n" . $e->getMessage();
359
            }
360
            $this->assertEquals($expected, $result , 'test #' . $idx . $msg);
361
        }
362
    }
363
 
364
    /**
365
     * Tests getoptplus()
366
     */
367
    public function testGetoptplus()
368
    {
369
        global $argv;
370
        $argv = array('command');
371
 
372
        $result = Console_GetoptPlus::getoptplus();
373
 
374
        $this->assertEquals(array(array(), array()), $result);
375
    }
376
}
377
// Call tests_GetoptPlusTest::main() if this source file is executed directly.
378
if (PHPUnit_MAIN_METHOD == "tests_GetoptPlusTest::main") {
379
    tests_GetoptPlusTest::main();
380
}
381
 
382
?>