Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 jpm 1
<?php
2
/**
3
 * PEAR_Command_Test (run-tests)
4
 *
5
 * PHP versions 4 and 5
6
 *
7
 * LICENSE: This source file is subject to version 3.0 of the PHP license
8
 * that is available through the world-wide-web at the following URI:
9
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
10
 * the PHP License and are unable to obtain it through the web, please
11
 * send a note to license@php.net so we can mail you a copy immediately.
12
 *
13
 * @category   pear
14
 * @package    PEAR
15
 * @author     Stig Bakken <ssb@php.net>
16
 * @author     Martin Jansen <mj@php.net>
17
 * @author     Greg Beaver <cellog@php.net>
18
 * @copyright  1997-2006 The PHP Group
19
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
20
 * @version    CVS: $Id: Test.php,v 1.15 2007/02/17 17:51:25 cellog Exp $
21
 * @link       http://pear.php.net/package/PEAR
22
 * @since      File available since Release 0.1
23
 */
24
 
25
/**
26
 * base class
27
 */
28
require_once 'PEAR/Command/Common.php';
29
 
30
/**
31
 * PEAR commands for login/logout
32
 *
33
 * @category   pear
34
 * @package    PEAR
35
 * @author     Stig Bakken <ssb@php.net>
36
 * @author     Martin Jansen <mj@php.net>
37
 * @author     Greg Beaver <cellog@php.net>
38
 * @copyright  1997-2006 The PHP Group
39
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
40
 * @version    Release: 1.5.1
41
 * @link       http://pear.php.net/package/PEAR
42
 * @since      Class available since Release 0.1
43
 */
44
 
45
class PEAR_Command_Test extends PEAR_Command_Common
46
{
47
    // {{{ properties
48
 
49
    var $commands = array(
50
        'run-tests' => array(
51
            'summary' => 'Run Regression Tests',
52
            'function' => 'doRunTests',
53
            'shortcut' => 'rt',
54
            'options' => array(
55
                'recur' => array(
56
                    'shortopt' => 'r',
57
                    'doc' => 'Run tests in child directories, recursively.  4 dirs deep maximum',
58
                ),
59
                'ini' => array(
60
                    'shortopt' => 'i',
61
                    'doc' => 'actual string of settings to pass to php in format " -d setting=blah"',
62
                    'arg' => 'SETTINGS'
63
                ),
64
                'realtimelog' => array(
65
                    'shortopt' => 'l',
66
                    'doc' => 'Log test runs/results as they are run',
67
                ),
68
                'quiet' => array(
69
                    'shortopt' => 'q',
70
                    'doc' => 'Only display detail for failed tests',
71
                ),
72
                'simple' => array(
73
                    'shortopt' => 's',
74
                    'doc' => 'Display simple output for all tests',
75
                ),
76
                'package' => array(
77
                    'shortopt' => 'p',
78
                    'doc' => 'Treat parameters as installed packages from which to run tests',
79
                ),
80
                'phpunit' => array(
81
                    'shortopt' => 'u',
82
                    'doc' => 'Search parameters for AllTests.php, and use that to run phpunit-based tests',
83
                ),
84
                'tapoutput' => array(
85
                    'shortopt' => 't',
86
                    'doc' => 'Output run-tests.log in TAP-compliant format',
87
                ),
88
                'cgi' => array(
89
                    'shortopt' => 'c',
90
                    'doc' => 'CGI php executable (needed for tests with POST/GET section)',
91
                    'arg' => 'PHPCGI',
92
                ),
93
            ),
94
            'doc' => '[testfile|dir ...]
95
Run regression tests with PHP\'s regression testing script (run-tests.php).',
96
            ),
97
        );
98
 
99
    var $output;
100
 
101
    // }}}
102
    // {{{ constructor
103
 
104
    /**
105
     * PEAR_Command_Test constructor.
106
     *
107
     * @access public
108
     */
109
    function PEAR_Command_Test(&$ui, &$config)
110
    {
111
        parent::PEAR_Command_Common($ui, $config);
112
    }
113
 
114
    // }}}
115
    // {{{ doRunTests()
116
 
117
    function doRunTests($command, $options, $params)
118
    {
119
        if (isset($options['phpunit']) && isset($options['tapoutput'])) {
120
            return $this->raiseError('ERROR: cannot use both --phpunit and --tapoutput at the same time');
121
        }
122
        require_once 'PEAR/Common.php';
123
        require_once 'PEAR/RunTest.php';
124
        require_once 'System.php';
125
        $log = new PEAR_Common;
126
        $log->ui = &$this->ui; // slightly hacky, but it will work
127
        $run = new PEAR_RunTest($log, $options);
128
        $tests = array();
129
        if (isset($options['recur'])) {
130
            $depth = 4;
131
        } else {
132
            $depth = 1;
133
        }
134
        if (!count($params)) {
135
            $params[] = '.';
136
        }
137
        if (isset($options['package'])) {
138
            $oldparams = $params;
139
            $params = array();
140
            $reg = &$this->config->getRegistry();
141
            foreach ($oldparams as $param) {
142
                $pname = $reg->parsePackageName($param, $this->config->get('default_channel'));
143
                if (PEAR::isError($pname)) {
144
                    return $this->raiseError($pname);
145
                }
146
                $package = &$reg->getPackage($pname['package'], $pname['channel']);
147
                if (!$package) {
148
                    return PEAR::raiseError('Unknown package "' .
149
                        $reg->parsedPackageNameToString($pname) . '"');
150
                }
151
                $filelist = $package->getFilelist();
152
                foreach ($filelist as $name => $atts) {
153
                    if (isset($atts['role']) && $atts['role'] != 'test') {
154
                        continue;
155
                    }
156
                    if (isset($options['phpunit'])) {
157
                        if (!preg_match('/AllTests\.php$/i', $name)) {
158
                            continue;
159
                        }
160
                    } else {
161
                        if (!preg_match('/\.phpt$/', $name)) {
162
                            continue;
163
                        }
164
                    }
165
                    $params[] = $atts['installed_as'];
166
                }
167
            }
168
        }
169
        foreach ($params as $p) {
170
            if (is_dir($p)) {
171
                if (isset($options['phpunit'])) {
172
                    $dir = System::find(array($p, '-type', 'f',
173
                                                '-maxdepth', $depth,
174
                                                '-name', 'AllTests.php'));
175
                } else {
176
                    $dir = System::find(array($p, '-type', 'f',
177
                                                '-maxdepth', $depth,
178
                                                '-name', '*.phpt'));
179
                }
180
                $tests = array_merge($tests, $dir);
181
            } else {
182
                if (isset($options['phpunit'])) {
183
                    if (!preg_match('/AllTests\.php$/i', $p)) {
184
                        continue;
185
                    }
186
                    $tests[] = $p;
187
                } else {
188
                    if (!file_exists($p)) {
189
                        if (!preg_match('/\.phpt$/', $p)) {
190
                            $p .= '.phpt';
191
                        }
192
                        $dir = System::find(array(dirname($p), '-type', 'f',
193
                                                    '-maxdepth', $depth,
194
                                                    '-name', $p));
195
                        $tests = array_merge($tests, $dir);
196
                    } else {
197
                        $tests[] = $p;
198
                    }
199
                }
200
            }
201
        }
202
        $ini_settings = '';
203
        if (isset($options['ini'])) {
204
            $ini_settings .= $options['ini'];
205
        }
206
        if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) {
207
            $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}";
208
        }
209
        if ($ini_settings) {
210
            $this->ui->outputData('Using INI settings: "' . $ini_settings . '"');
211
        }
212
        $skipped = $passed = $failed = array();
213
        $this->ui->outputData('Running ' . count($tests) . ' tests', $command);
214
        $start = time();
215
        if (isset($options['realtimelog'])) {
216
            if (file_exists('run-tests.log')) {
217
                unlink('run-tests.log');
218
            }
219
        }
220
        if (isset($options['tapoutput'])) {
221
            $tap = '1..' . count($tests) . "\n";
222
        }
223
        $i = 1;
224
        foreach ($tests as $t) {
225
            if (isset($options['realtimelog'])) {
226
                $fp = @fopen('run-tests.log', 'a');
227
                if ($fp) {
228
                    fwrite($fp, "Running test $t...");
229
                    fclose($fp);
230
                }
231
            }
232
            PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
233
            $result = $run->run($t, $ini_settings);
234
            PEAR::staticPopErrorHandling();
235
            if (PEAR::isError($result)) {
236
                $this->ui->log($result->getMessage());
237
                continue;
238
            }
239
            if (isset($options['tapoutput'])) {
240
                $tap .= $result[0] . ' ' . $i . $result[1] . "\n";
241
                $i++;
242
                continue;
243
            }
244
            if (isset($options['realtimelog'])) {
245
                $fp = @fopen('run-tests.log', 'a');
246
                if ($fp) {
247
                    fwrite($fp, "$result\n");
248
                    fclose($fp);
249
                }
250
            }
251
            if ($result == 'FAILED') {
252
            	$failed[] = $t;
253
            }
254
            if ($result == 'PASSED') {
255
            	$passed[] = $t;
256
            }
257
            if ($result == 'SKIPPED') {
258
            	$skipped[] = $t;
259
            }
260
        }
261
        $total = date('i:s', time() - $start);
262
        if (isset($options['tapoutput'])) {
263
            $fp = @fopen('run-tests.log', 'w');
264
            if ($fp) {
265
                fwrite($fp, $tap, strlen($tap));
266
                fclose($fp);
267
                $this->ui->outputData('wrote TAP-format log to "' .realpath('run-tests.log') .
268
                    '"', $command);
269
            }
270
        } else {
271
            if (count($failed)) {
272
                $output = "TOTAL TIME: $total\n";
273
                $output .= count($passed) . " PASSED TESTS\n";
274
                $output .= count($skipped) . " SKIPPED TESTS\n";
275
        		$output .= count($failed) . " FAILED TESTS:\n";
276
            	foreach ($failed as $failure) {
277
            		$output .= $failure . "\n";
278
            	}
279
                if (isset($options['realtimelog'])) {
280
                    $fp = @fopen('run-tests.log', 'a');
281
                } else {
282
                    $fp = @fopen('run-tests.log', 'w');
283
                }
284
                if ($fp) {
285
                    fwrite($fp, $output, strlen($output));
286
                    fclose($fp);
287
                    $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command);
288
                }
289
            } elseif (file_exists('run-tests.log') && !is_dir('run-tests.log')) {
290
                @unlink('run-tests.log');
291
            }
292
        }
293
        $this->ui->outputData('TOTAL TIME: ' . $total);
294
        $this->ui->outputData(count($passed) . ' PASSED TESTS', $command);
295
        $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command);
296
        if (count($failed)) {
297
    		$this->ui->outputData(count($failed) . ' FAILED TESTS:', $command);
298
        	foreach ($failed as $failure) {
299
        		$this->ui->outputData($failure, $command);
300
        	}
301
        }
302
 
303
        return true;
304
    }
305
    // }}}
306
}
307
 
308
?>