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
 * Net_FTP main test file
6
 *
7
 * To run the tests either execute this from the top directory if checked out from
8
 * CVS:
9
 * $ pear run-tests -ur
10
 * or if you want to run the tests on an installed version, run from within any
11
 * directory:
12
 * $ pear run-tests -pu Net_FTP
13
 *
14
 * In both cases you need PHPUnit installed
15
 *
16
 * PHP version 5
17
 *
18
 * LICENSE: This source file is subject to version 3.0 of the PHP license
19
 * that is available through the world-wide-web at the following URI:
20
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
21
 * the PHP License and are unable to obtain it through the web, please
22
 * send a note to license@php.net so we can mail you a copy immediately.
23
 *
24
 * @category  Networking
25
 * @package   FTP
26
 * @author    Tobias Schlitt <toby@php.net>
27
 * @copyright 1997-2008 The PHP Group
28
 * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
29
 * @version   CVS: $Id: Net_FTPTest.php,v 1.7.2.5 2008/05/19 18:08:23 jschippers Exp $
30
 * @link      http://pear.php.net/package/Net_FTP
31
 * @link      http://www.phpunit.de PHPUnit
32
 * @since     File available since Release 1.3.3
33
 */
34
 
35
if (!defined('PHPUnit_MAIN_METHOD')) {
36
    define('PHPUnit_MAIN_METHOD', 'Net_FTPTest::main');
37
}
38
 
39
require_once 'PHPUnit/Framework.php';
40
require_once 'System.php';
41
 
42
chdir(dirname(__FILE__));
43
if (substr(dirname(__FILE__), -6) == DIRECTORY_SEPARATOR.'tests') {
44
    include_once '../Net/FTP.php';
45
} else {
46
    include_once 'Net/FTP.php';
47
}
48
 
49
/**
50
 * Unit test case for Net_FTP
51
 *
52
 * @category  Networking
53
 * @package   FTP
54
 * @author    Jorrit Schippers <jschippers@php.net>
55
 * @copyright 1997-2008 The PHP Group
56
 * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
57
 * @version   Release: 1.3.7
58
 * @link      http://pear.php.net/package/Net_FTP
59
 * @since     Class available since Release 1.3.3
60
 */
61
class Net_FTPTest extends PHPUnit_Framework_TestCase
62
{
63
    protected $ftp;
64
    protected $ftpdir;
65
    protected $setupError;
66
 
67
    /**
68
     * Runs the test methods of this class.
69
     *
70
     * @access public
71
     * @static
72
     * @return void
73
     */
74
    public static function main()
75
    {
76
        include_once 'PHPUnit/TextUI/TestRunner.php';
77
 
78
        $suite  = new PHPUnit_Framework_TestSuite('Net_FTPTest');
79
        $result = PHPUnit_TextUI_TestRunner::run($suite);
80
    }
81
 
82
    /**
83
     * Sets up the fixture, for example, opens a network connection.
84
     * This method is called before a test is executed.
85
     *
86
     * @access protected
87
     * @return void
88
     */
89
    protected function setUp()
90
    {
91
        if (!file_exists('config.php')) {
92
            $this->setupError = 'config.php does not exist in '.getcwd();
93
            return;
94
        }
95
 
96
        include_once 'config.php';
97
 
98
        if (!defined('FTPHOST') || !defined('FTPPORT') || !defined('FTPUSER')
99
            || !defined('FTPPASSWORD')) {
100
            $this->setupError = 'Some required constants are not defined';
101
            return;
102
        }
103
 
104
        $this->ftp = new Net_FTP(FTPHOST, FTPPORT, 30);
105
        $res       = $this->ftp->connect();
106
 
107
        if (PEAR::isError($res)) {
108
            $this->setupError = 'Could not connect to the FTP server';
109
            $this->ftp        = null;
110
            return;
111
        }
112
 
113
        $res = $this->ftp->login(FTPUSER, FTPPASSWORD);
114
 
115
        if (PEAR::isError($res)) {
116
            $this->setupError = 'Could not login to the FTP server';
117
            $this->ftp        = null;
118
            return;
119
        }
120
 
121
        if (defined('FTPDIR') && '' !== FTPDIR) {
122
            $res = $this->ftp->cd(FTPDIR);
123
 
124
            if (PEAR::isError($res)) {
125
                $this->setupError = 'Could switch to directory '.FTPDIR;
126
                $this->ftp        = null;
127
                return;
128
            }
129
        }
130
 
131
        $res = $this->ftp->pwd();
132
 
133
        if (PEAR::isError($res)) {
134
            $this->setupError = 'Could not get current directory';
135
            $this->ftp        = null;
136
            return;
137
        }
138
 
139
        $this->ftpdir = $res;
140
 
141
        $res = $this->ftp->mkdir('test');
142
 
143
        if (PEAR::isError($res)) {
144
            $this->setupError = 'Could not create a test directory';
145
            $this->ftp        = null;
146
            return;
147
        }
148
 
149
        $res = $this->ftp->cd('test');
150
 
151
        if (PEAR::isError($res)) {
152
            $this->setupError = 'Could not change to the test directory';
153
            $this->ftp        = null;
154
            return;
155
        }
156
    }
157
 
158
    /**
159
     * Tears down the fixture, for example, closes a network connection.
160
     * This method is called after a test is executed.
161
     *
162
     * @access protected
163
     * @return void
164
     */
165
    protected function tearDown()
166
    {
167
        if ($this->ftp != null) {
168
            $this->ftp->cd($this->ftpdir);
169
            $this->ftp->rm('test/', true);
170
            $this->ftp->disconnect();
171
 
172
            $this->ftpdir     = null;
173
            $this->ftp        = null;
174
            $this->setupError = null;
175
        }
176
    }
177
 
178
    /**
179
     * Tests functionality of Net_FTP::mkdir()
180
     *
181
     * @return void
182
     * @see Net_FTP::mkdir()
183
     */
184
    public function testMkdir()
185
    {
186
        if ($this->ftp == null) {
187
            $this->fail('This test requires a working FTP connection. Setup '.
188
            'config.php with proper configuration parameters. ('.
189
            $this->setupError.')');
190
        }
191
        $this->ftp->mkdir('dir1', false);
192
        $this->ftp->mkdir('dir1/dir2/dir3/dir4', true);
193
        $this->assertTrue($this->ftp->cd('dir1/dir2/dir3/dir4'));
194
    }
195
 
196
    /**
197
     * Tests functionality of Net_FTP::mkdir()
198
     *
199
     * @return void
200
     * @see Net_FTP::mkdir()
201
     */
202
    public function testRename()
203
    {
204
        if ($this->ftp == null) {
205
            $this->fail('This test requires a working FTP connection. Setup '.
206
            'config.php with proper configuration parameters. ('.
207
            $this->setupError.')');
208
        }
209
        $this->ftp->put('testfile.dat', 'testfile.dat', FTP_ASCII);
210
        $this->assertTrue($this->ftp->rename('testfile.dat', 'testfile2.dat'));
211
    }
212
 
213
    /**
214
     * Tests functionality of Net_FTP::rm()
215
     *
216
     * @return void
217
     * @see Net_FTP::rm()
218
     */
219
    public function testRm()
220
    {
221
        if ($this->ftp == null) {
222
            $this->fail('This test requires a working FTP connection. Setup '.
223
            'config.php with proper configuration parameters. ('.
224
            $this->setupError.')');
225
        }
226
        $list1 = $this->ftp->ls();
227
 
228
        $this->ftp->put('testfile.dat', 'testfile.dat', FTP_ASCII);
229
        $this->ftp->mkdir('dir1/dir2/dir3/dir4', true);
230
 
231
        $this->ftp->rm('dir1/', true);
232
        $this->ftp->rm('testfile.dat');
233
 
234
        $list2 = $this->ftp->ls();
235
 
236
        $this->assertEquals($list1, $list2, 'Directory listing before creation and'.
237
            ' after creation are not equal');
238
    }
239
 
240
    /**
241
     * Tests functionality of Net_FTP::putRecursive()
242
     *
243
     * @return void
244
     * @see Net_FTP::putRecursive()
245
     */
246
    public function testPutRecursive()
247
    {
248
        if ($this->ftp == null) {
249
            $this->fail('This test requires a working FTP connection. Setup '.
250
            'config.php with proper configuration parameters. ('.
251
            $this->setupError.')');
252
        }
253
 
254
        $tmpdir    = array();
255
        $tmpfile   = array();
256
        $tmpdir[]  = System::mktemp(array('-d', 'pearnetftptest'));
257
        $tmpdir[]  = System::mktemp(array('-t', $tmpdir[0], '-d'));
258
        $tmpdir[]  = System::mktemp(array('-t', $tmpdir[1], '-d'));
259
        $tmpfile[] = System::mktemp(array('-t', $tmpdir[0]));
260
        $tmpfile[] = System::mktemp(array('-t', $tmpdir[1]));
261
        $tmpfile[] = System::mktemp(array('-t', $tmpdir[2]));
262
 
263
        $local  = $tmpdir[0].DIRECTORY_SEPARATOR;
264
        $remote = './'.$this->_getLastPart($tmpdir[0]).'/';
265
 
266
        $ret = $this->ftp->putRecursive($local, $remote);
267
        $this->assertFalse(PEAR::isError($ret));
268
 
269
        for ($i = 0; $i < 3; $i++) {
270
            $ret = $this->ftp->cd($this->_getLastPart($tmpdir[$i]).'/');
271
            $this->assertFalse(PEAR::isError($ret));
272
 
273
            $dirlist = $this->ftp->ls();
274
            $this->assertFalse(PEAR::isError($dirlist));
275
 
276
            $dirlist   = $this->_getNames($dirlist);
277
            $dirlistok = array($this->_getLastPart($tmpfile[$i]), '.', '..');
278
            if ($i < 2) {
279
                $dirlistok[] = $this->_getLastPart($tmpdir[$i+1]);
280
            }
281
 
282
            sort($dirlist);
283
            sort($dirlistok);
284
 
285
            $this->assertEquals($dirlist, $dirlistok);
286
        }
287
    }
288
 
289
    /**
290
     * Tests functionality of Net_FTP::_makeDirPermissions()
291
     *
292
     * @return void
293
     * @see Net_FTP::_makeDirPermissions()
294
     */
295
    public function testMakeDirPermissions()
296
    {
297
        if ($this->ftp == null) {
298
            $this->fail('This test requires a working FTP connection. Setup '.
299
            'config.php with proper configuration parameters. ('.
300
            $this->setupError.')');
301
        }
302
        $tests = array(
303
            '111' => '111',
304
            '110' => '110',
305
            '444' => '555',
306
            '412' => '512',
307
            '641' => '751',
308
            '666' => '777',
309
            '400' => '500',
310
            '040' => '050',
311
            '004' => '005',
312
        );
313
 
314
        foreach ($tests AS $in => $out) {
315
            $this->assertEquals($this->ftp->_makeDirPermissions($in), $out);
316
        }
317
    }
318
 
319
    /**
320
     * Tests functionality of Net_FTP::size()
321
     *
322
     * @return void
323
     * @see Net_FTP::size()
324
     */
325
    public function testSize()
326
    {
327
        if ($this->ftp == null) {
328
            $this->fail('This test requires a working FTP connection. Setup '.
329
            'config.php with proper configuration parameters. ('.
330
            $this->setupError.')');
331
        }
332
        // upload in binary to avoid addition/removal of characters
333
        $this->ftp->put('testfile.dat', 'testfile.dat', FTP_BINARY);
334
        $this->assertEquals($this->ftp->size('testfile.dat'),
335
            filesize('testfile.dat'));
336
    }
337
 
338
    /**
339
     * Tests functionality of Net_FTP::setMode(), Net_FTP::checkFileExtension(),
340
     * Net_FTP::addExtension() and Net_FTP::removeExtension()
341
     *
342
     * @return void
343
     * @see Net_FTP::checkFileExtension(), Net_FTP::addExtension(),
344
     *      Net_FTP::removeExtension(), Net_FTP::setMode()
345
     */
346
    public function testExtensions()
347
    {
348
        if ($this->ftp == null) {
349
            $this->fail('This test requires a working FTP connection. Setup '.
350
            'config.php with proper configuration parameters. ('.
351
            $this->setupError.')');
352
        }
353
        $this->ftp->setMode(FTP_ASCII);
354
        $this->ftp->addExtension(FTP_BINARY, 'tst');
355
        $this->assertEquals($this->ftp->checkFileExtension('test.tst'), FTP_BINARY);
356
        $this->ftp->removeExtension('tst');
357
        $this->assertEquals($this->ftp->checkFileExtension('test.tst'), FTP_ASCII);
358
        $this->ftp->setMode(FTP_BINARY);
359
        $this->assertEquals($this->ftp->checkFileExtension('test.tst'), FTP_BINARY);
360
    }
361
 
362
    /**
363
     * Tests functionality of Net_FTP::getExtensionsFile()
364
     *
365
     * @return void
366
     * @see Net_FTP::getExtensionsFile()
367
     */
368
    public function testGetExtensionsFile()
369
    {
370
        if ($this->ftp == null) {
371
            $this->fail('This test requires a working FTP connection. Setup '.
372
            'config.php with proper configuration parameters. ('.
373
            $this->setupError.')');
374
        }
375
        $res = $this->ftp->getExtensionsFile('extensions.ini');
376
        $this->assertFalse(PEAR::isError($res), 'Test extensions file could be'.
377
            'loaded');
378
 
379
        $this->ftp->setMode(FTP_BINARY);
380
        $this->assertEquals($this->ftp->checkFileExtension('test.asc'), FTP_ASCII);
381
        $this->ftp->setMode(FTP_ASCII);
382
        $this->assertEquals($this->ftp->checkFileExtension('test.gif'), FTP_BINARY);
383
    }
384
 
385
    /**
386
     * Tests _determineOSMatch
387
     *
388
     * @return void
389
     * @see Net_FTP::_determineOSMatch()
390
     */
391
    public function testDetermineOSMatch()
392
    {
393
        if ($this->ftp == null) {
394
            $this->fail('This test requires a working FTP connection. Setup '.
395
            'config.php with proper configuration parameters. ('.
396
            $this->setupError.')');
397
        }
398
        $dirlist = array(
399
            'drwxrwsr-x  75 upl.oad  (?).         3008 Oct 30 21:09 ftp1',
400
        );
401
 
402
        $res = $this->ftp->_determineOSMatch($dirlist);
403
        $this->assertFalse(PEAR::isError($res),
404
            'The directory listing should be recognized');
405
        $this->assertEquals($res['pattern'],
406
            $this->ftp->_ls_match['unix']['pattern'],
407
            'The input should be parsed by the unix pattern');
408
    }
409
 
410
    /**
411
     * Return all name keys in the elements of an array
412
     *
413
     * @param array $in Multidimensional array
414
     *
415
     * @return array Array containing name keys
416
     */
417
    function _getNames($in)
418
    {
419
        $return = array();
420
        foreach ($in as $v) {
421
            $return[] = $v['name'];
422
        }
423
        return $return;
424
    }
425
 
426
    /**
427
     * Return the last element of a local path
428
     *
429
     * @param string $in Path
430
     *
431
     * @return array Last part of path
432
     */
433
    function _getLastPart($in)
434
    {
435
        $start = strrpos($in, DIRECTORY_SEPARATOR) + 1;
436
        return substr($in, $start);
437
    }
438
}
439
 
440
// Call Net_FTPTest::main() if this source file is executed directly.
441
if (PHPUnit_MAIN_METHOD == 'Net_FTPTest::main') {
442
    Net_FTPTest::main();
443
}
444
?>