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 softtabstop=4: */
3
 
4
/**
5
 * getValues Test case for Console_Getargs
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: This source file is subject to version 3.0 of the PHP license
10
 * that is available through the world-wide-web at the following URI:
11
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
12
 * the PHP License and are unable to obtain it through the web, please
13
 * send a note to license@php.net so we can mail you a copy immediately.
14
 *
15
 * @category   Console
16
 * @package    Console_Getargs
17
 * @subpackage Tests
18
 * @author     Stephan Schmidt <schst@php.net>
19
 * @copyright  1997-2005 The PHP Group
20
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
21
 * @version    CVS: $Id: Getargs_getValues_testcase.php 269139 2008-11-17 10:02:30Z clockwerx $
22
 * @link       http://pear.php.net/package/Console_Getargs
23
 */
24
if (!defined('PHPUnit_MAIN_METHOD')) {
25
    define('PHPUnit_MAIN_METHOD', 'Getargs_getValues_testCase::main');
26
}
27
 
28
require_once 'Console/Getargs.php';
29
require_once 'PHPUnit/Framework.php';
30
 
31
/**
32
 * getValues Test case for Console_Getargs
33
 *
34
 * @category   Console
35
 * @package    Console_Getargs
36
 * @subpackage Tests
37
 * @author     Stephan Schmidt <schst@php.net>
38
 * @copyright  1997-2005 The PHP Group
39
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
40
 * @version    Release: @package_version@
41
 * @link       http://pear.php.net/package/Console_Getargs
42
 */
43
class Getargs_getValues_testCase extends PHPUnit_Framework_TestCase
44
{
45
    /**
46
     * Runs the test methods of this class.
47
     *
48
     * @access public
49
     * @static
50
     */
51
    public static function main() {
52
        require_once 'PHPUnit/TextUI/TestRunner.php';
53
 
54
        $suite  = new PHPUnit_Framework_TestSuite('Getargs_getValues_testCase');
55
        PHPUnit_TextUI_TestRunner::run($suite);
56
    }
57
 
58
 
59
 
60
   /**
61
    * Test getValues('long') with one argument
62
    *
63
    * @access  public
64
    * @return  void
65
    */
66
    function testLong1()
67
    {
68
        $config = array(
69
                    'name' => array(
70
                        'short' => 'n',
71
                        'min'   => 1,
72
                        'max'   => 1,
73
                        'desc'  => 'An argument.')
74
                       );
75
 
76
        $args = array('-n', 'arg1');
77
        $message = implode(' ', $args);
78
        $obj =& Console_Getargs::factory($config, $args);
79
 
80
        if (PEAR::isError($obj)) {
81
            $this->fail("'$message' " . $obj->getMessage());
82
        } else {
83
            $this->assertEquals(array('name' => 'arg1'), $obj->getValues('long'), "'$message' Incorrect value returned");
84
        }
85
 
86
        $args = array('--name', 'arg1');
87
        $message = implode(' ', $args);
88
        $obj =& Console_Getargs::factory($config, $args);
89
 
90
        if (PEAR::isError($obj)) {
91
            $this->fail("'$message' " . $obj->getMessage());
92
        } else {
93
            $this->assertEquals(array('name' => 'arg1'), $obj->getValues('long'), "'$message' Incorrect value returned");
94
        }
95
    }
96
 
97
   /**
98
    * Test getValues('long') with one argument and two values
99
    *
100
    * @access  public
101
    * @return  void
102
    */
103
    function testLong2()
104
    {
105
        $config = array(
106
                    'name' => array(
107
                        'short' => 'n',
108
                        'min'   => 2,
109
                        'max'   => 2,
110
                        'desc'  => 'Two arguments.')
111
                       );
112
 
113
        $args = array('-n', 'arg1', 'arg2');
114
        $message = implode(' ', $args);
115
        $obj =& Console_Getargs::factory($config, $args);
116
 
117
        if (PEAR::isError($obj)) {
118
            $this->fail("'$message' " . $obj->getMessage());
119
        } else {
120
            $this->assertEquals(array('name' => array('arg1', 'arg2')), $obj->getValues('long'), "'$message' Incorrect value returned");
121
        }
122
 
123
        $args = array('--name', 'arg1', 'arg2');
124
        $message = implode(' ', $args);
125
        $obj =& Console_Getargs::factory($config, $args);
126
 
127
        if (PEAR::isError($obj)) {
128
            $this->fail("'$message' " . $obj->getMessage());
129
        } else {
130
            $this->assertEquals(array('name' => array('arg1', 'arg2')), $obj->getValues('long'), "'$message' Incorrect value returned");
131
        }
132
    }
133
 
134
   /**
135
    * Test getValues('short') with one argument
136
    *
137
    * @access  public
138
    * @return  void
139
    */
140
    function testShort()
141
    {
142
        $config = array(
143
                    'name' => array(
144
                        'short' => 'n',
145
                        'min'   => 1,
146
                        'max'   => 1,
147
                        'desc'  => 'An argument.')
148
                       );
149
 
150
        $args = array('-n', 'arg1');
151
        $message = implode(' ', $args);
152
        $obj =& Console_Getargs::factory($config, $args);
153
 
154
        if (PEAR::isError($obj)) {
155
            $this->fail("'$message' " . $obj->getMessage());
156
        } else {
157
            $this->assertEquals(array('n' => 'arg1'), $obj->getValues('short'), "'$message' Incorrect value returned");
158
        }
159
 
160
        $args = array('--name', 'arg1');
161
        $message = implode(' ', $args);
162
        $obj =& Console_Getargs::factory($config, $args);
163
 
164
        if (PEAR::isError($obj)) {
165
            $this->fail("'$message' " . $obj->getMessage());
166
        } else {
167
            $this->assertEquals(array('n' => 'arg1'), $obj->getValues('short'), "'$message' Incorrect value returned");
168
        }
169
    }
170
 
171
   /**
172
    * Test getValues('short') with one argument and two values
173
    *
174
    * @access  public
175
    * @return  void
176
    */
177
    function testShort2()
178
    {
179
        $config = array(
180
                    'name' => array(
181
                        'short' => 'n',
182
                        'min'   => 2,
183
                        'max'   => 2,
184
                        'desc'  => 'Two arguments.')
185
                       );
186
 
187
        $args = array('-n', 'arg1', 'arg2');
188
        $message = implode(' ', $args);
189
        $obj =& Console_Getargs::factory($config, $args);
190
 
191
        if (PEAR::isError($obj)) {
192
            $this->fail("'$message' " . $obj->getMessage());
193
        } else {
194
            $this->assertEquals(array('n' => array('arg1', 'arg2')), $obj->getValues('short'), "'$message' Incorrect value returned");
195
        }
196
 
197
        $args = array('--name', 'arg1', 'arg2');
198
        $message = implode(' ', $args);
199
        $obj =& Console_Getargs::factory($config, $args);
200
 
201
        if (PEAR::isError($obj)) {
202
            $this->fail("'$message' " . $obj->getMessage());
203
        } else {
204
            $this->assertEquals(array('n' => array('arg1', 'arg2')), $obj->getValues('short'), "'$message' Incorrect value returned");
205
        }
206
    }
207
 
208
   /**
209
    * Test getValues('short') and getValues('long') with a switch
210
    *
211
    * @access  public
212
    * @return  void
213
    */
214
    function testSwitch()
215
    {
216
        $config = array(
217
                    'switch' => array(
218
                        'short' => 's',
219
                        'max'   => 0,
220
                        'desc'  => 'A switch.')
221
                        );
222
 
223
        $args = array('-s');
224
        $message = implode(' ', $args);
225
        $obj =& Console_Getargs::factory($config, $args);
226
 
227
        if (PEAR::isError($obj)) {
228
            $this->fail("'$message' " . $obj->getMessage());
229
        } else {
230
            $this->assertTrue($obj->isDefined('s'), "'$message' Switch not defined");
231
            $this->assertTrue($obj->isDefined('switch'), "'$message' Switch not defined");
232
        }
233
 
234
        $args = array('--switch');
235
        $message = implode(' ', $args);
236
        $obj =& Console_Getargs::factory($config, $args);
237
 
238
 
239
        if (PEAR::isError($obj)) {
240
            $this->fail("'$message' " . $obj->getMessage());
241
        } else {
242
            $this->assertEquals(array('switch' => true), $obj->getValues('long'), "'$message' Switch not defined");
243
            $this->assertEquals(array('s' => true), $obj->getValues('short'), "'$message' Switch not defined");
244
        }
245
 
246
    }
247
 
248
   /**
249
    * Test getValues('long') and getValues('short') with two arguments
250
    *
251
    * @access  public
252
    * @return  void
253
    */
254
    function testMultiple()
255
    {
256
        $config = array(
257
                    'name' => array(
258
                        'short' => 'n',
259
                        'min'   => 1,
260
                        'max'   => 1,
261
                        'desc'  => 'One argument.'),
262
                    'email' => array(
263
                        'short' => 'e',
264
                        'min'   => 2,
265
                        'max'   => 2,
266
                        'desc'  => 'Two arguments.'),
267
                       );
268
 
269
        $args = array('-n', 'arg1', '-e', 'schst@php.net', 'wenz@php.net');
270
        $message = implode(' ', $args);
271
        $obj =& Console_Getargs::factory($config, $args);
272
 
273
        if (PEAR::isError($obj)) {
274
            $this->fail("'$message' " . $obj->getMessage());
275
        } else {
276
            $this->assertEquals(array('name' => $args[1], 'email' => array($args[3], $args[4])), $obj->getValues('long'), "'$message' Incorrect value returned");
277
            $this->assertEquals(array('n' => $args[1], 'e' => array($args[3], $args[4])), $obj->getValues('short'), "'$message' Incorrect value returned");
278
        }
279
 
280
        $args = array('--name', 'arg1', '--email', 'schst@php.net', 'wenz@php.net');
281
        $message = implode(' ', $args);
282
        $obj =& Console_Getargs::factory($config, $args);
283
 
284
        if (PEAR::isError($obj)) {
285
            $this->fail("'$message' " . $obj->getMessage());
286
        } else {
287
            $this->assertEquals(array('name' => $args[1], 'email' => array($args[3], $args[4])), $obj->getValues('long'), "'$message' Incorrect value returned");
288
            $this->assertEquals(array('n' => $args[1], 'e' => array($args[3], $args[4])), $obj->getValues('short'), "'$message' Incorrect value returned");
289
        }
290
    }
291
}
292
 
293
if (PHPUnit_MAIN_METHOD == 'Getargs_getValues_testCase::main') {
294
    Getargs_getValues_testCase::main();
295
}
296
 
297
?>