Subversion Repositories Applications.gtt

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
Line 2... Line 2...
2
/**
2
/**
3
 * PEAR_Command_Test (run-tests)
3
 * PEAR_Command_Test (run-tests)
4
 *
4
 *
5
 * PHP versions 4 and 5
5
 * PHP versions 4 and 5
6
 *
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
7
 * @category   pear
14
 * @package    PEAR
8
 * @package    PEAR
15
 * @author     Stig Bakken <ssb@php.net>
9
 * @author     Stig Bakken <ssb@php.net>
16
 * @author     Martin Jansen <mj@php.net>
10
 * @author     Martin Jansen <mj@php.net>
17
 * @author     Greg Beaver <cellog@php.net>
11
 * @author     Greg Beaver <cellog@php.net>
18
 * @copyright  1997-2006 The PHP Group
12
 * @copyright  1997-2009 The Authors
19
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
13
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
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
14
 * @link       http://pear.php.net/package/PEAR
22
 * @since      File available since Release 0.1
15
 * @since      File available since Release 0.1
23
 */
16
 */
Line 24... Line 17...
24
 
17
 
Line 33... Line 26...
33
 * @category   pear
26
 * @category   pear
34
 * @package    PEAR
27
 * @package    PEAR
35
 * @author     Stig Bakken <ssb@php.net>
28
 * @author     Stig Bakken <ssb@php.net>
36
 * @author     Martin Jansen <mj@php.net>
29
 * @author     Martin Jansen <mj@php.net>
37
 * @author     Greg Beaver <cellog@php.net>
30
 * @author     Greg Beaver <cellog@php.net>
38
 * @copyright  1997-2006 The PHP Group
31
 * @copyright  1997-2009 The Authors
39
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
32
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
40
 * @version    Release: 1.5.1
33
 * @version    Release: 1.10.1
41
 * @link       http://pear.php.net/package/PEAR
34
 * @link       http://pear.php.net/package/PEAR
42
 * @since      Class available since Release 0.1
35
 * @since      Class available since Release 0.1
43
 */
36
 */
Line 44... Line 37...
44
 
37
 
45
class PEAR_Command_Test extends PEAR_Command_Common
38
class PEAR_Command_Test extends PEAR_Command_Common
46
{
-
 
47
    // {{{ properties
-
 
48
 
39
{
49
    var $commands = array(
40
    var $commands = array(
50
        'run-tests' => array(
41
        'run-tests' => array(
51
            'summary' => 'Run Regression Tests',
42
            'summary' => 'Run Regression Tests',
52
            'function' => 'doRunTests',
43
            'function' => 'doRunTests',
Line 77... Line 68...
77
                    'shortopt' => 'p',
68
                    'shortopt' => 'p',
78
                    'doc' => 'Treat parameters as installed packages from which to run tests',
69
                    'doc' => 'Treat parameters as installed packages from which to run tests',
79
                ),
70
                ),
80
                'phpunit' => array(
71
                'phpunit' => array(
81
                    'shortopt' => 'u',
72
                    'shortopt' => 'u',
82
                    'doc' => 'Search parameters for AllTests.php, and use that to run phpunit-based tests',
73
                    'doc' => 'Search parameters for AllTests.php, and use that to run phpunit-based tests
-
 
74
If none is found, all .phpt tests will be tried instead.',
83
                ),
75
                ),
84
                'tapoutput' => array(
76
                'tapoutput' => array(
85
                    'shortopt' => 't',
77
                    'shortopt' => 't',
86
                    'doc' => 'Output run-tests.log in TAP-compliant format',
78
                    'doc' => 'Output run-tests.log in TAP-compliant format',
87
                ),
79
                ),
88
                'cgi' => array(
80
                'cgi' => array(
89
                    'shortopt' => 'c',
81
                    'shortopt' => 'c',
90
                    'doc' => 'CGI php executable (needed for tests with POST/GET section)',
82
                    'doc' => 'CGI php executable (needed for tests with POST/GET section)',
91
                    'arg' => 'PHPCGI',
83
                    'arg' => 'PHPCGI',
92
                ),
84
                ),
-
 
85
                'coverage' => array(
-
 
86
                    'shortopt' => 'x',
-
 
87
                    'doc'      => 'Generate a code coverage report (requires Xdebug 2.0.0+)',
-
 
88
                ),
-
 
89
                'showdiff' => array(
-
 
90
                    'shortopt' => 'd',
-
 
91
                    'doc' => 'Output diff on test failure',
-
 
92
                ),
93
            ),
93
            ),
94
            'doc' => '[testfile|dir ...]
94
            'doc' => '[testfile|dir ...]
95
Run regression tests with PHP\'s regression testing script (run-tests.php).',
95
Run regression tests with PHP\'s regression testing script (run-tests.php).',
96
            ),
96
            ),
97
        );
97
        );
Line 98... Line 98...
98
 
98
 
Line 99... Line -...
99
    var $output;
-
 
100
 
-
 
101
    // }}}
-
 
102
    // {{{ constructor
99
    var $output;
103
 
100
 
104
    /**
101
    /**
105
     * PEAR_Command_Test constructor.
102
     * PEAR_Command_Test constructor.
106
     *
103
     *
107
     * @access public
104
     * @access public
108
     */
105
     */
109
    function PEAR_Command_Test(&$ui, &$config)
106
    function __construct(&$ui, &$config)
110
    {
107
    {
Line 111... Line -...
111
        parent::PEAR_Command_Common($ui, $config);
-
 
112
    }
-
 
113
 
-
 
114
    // }}}
108
        parent::__construct($ui, $config);
115
    // {{{ doRunTests()
109
    }
116
 
110
 
117
    function doRunTests($command, $options, $params)
111
    function doRunTests($command, $options, $params)
118
    {
112
    {
-
 
113
        if (isset($options['phpunit']) && isset($options['tapoutput'])) {
119
        if (isset($options['phpunit']) && isset($options['tapoutput'])) {
114
            return $this->raiseError('ERROR: cannot use both --phpunit and --tapoutput at the same time');
120
            return $this->raiseError('ERROR: cannot use both --phpunit and --tapoutput at the same time');
-
 
121
        }
115
        }
122
        require_once 'PEAR/Common.php';
116
 
123
        require_once 'PEAR/RunTest.php';
117
        require_once 'PEAR/Common.php';
124
        require_once 'System.php';
-
 
125
        $log = new PEAR_Common;
118
        require_once 'System.php';
126
        $log->ui = &$this->ui; // slightly hacky, but it will work
119
        $log = new PEAR_Common;
127
        $run = new PEAR_RunTest($log, $options);
-
 
128
        $tests = array();
-
 
129
        if (isset($options['recur'])) {
-
 
130
            $depth = 4;
120
        $log->ui = &$this->ui; // slightly hacky, but it will work
131
        } else {
121
        $tests = array();
132
            $depth = 1;
122
        $depth = isset($options['recur']) ? 14 : 1;
133
        }
123
 
-
 
124
        if (!count($params)) {
134
        if (!count($params)) {
125
            $params[] = '.';
135
            $params[] = '.';
126
        }
136
        }
127
 
137
        if (isset($options['package'])) {
128
        if (isset($options['package'])) {
138
            $oldparams = $params;
129
            $oldparams = $params;
139
            $params = array();
130
            $params = array();
140
            $reg = &$this->config->getRegistry();
131
            $reg = &$this->config->getRegistry();
141
            foreach ($oldparams as $param) {
132
            foreach ($oldparams as $param) {
142
                $pname = $reg->parsePackageName($param, $this->config->get('default_channel'));
133
                $pname = $reg->parsePackageName($param, $this->config->get('default_channel'));
-
 
134
                if (PEAR::isError($pname)) {
143
                if (PEAR::isError($pname)) {
135
                    return $this->raiseError($pname);
144
                    return $this->raiseError($pname);
136
                }
145
                }
137
 
146
                $package = &$reg->getPackage($pname['package'], $pname['channel']);
138
                $package = &$reg->getPackage($pname['package'], $pname['channel']);
147
                if (!$package) {
139
                if (!$package) {
-
 
140
                    return PEAR::raiseError('Unknown package "' .
148
                    return PEAR::raiseError('Unknown package "' .
141
                        $reg->parsedPackageNameToString($pname) . '"');
149
                        $reg->parsedPackageNameToString($pname) . '"');
142
                }
150
                }
143
 
151
                $filelist = $package->getFilelist();
144
                $filelist = $package->getFilelist();
152
                foreach ($filelist as $name => $atts) {
145
                foreach ($filelist as $name => $atts) {
153
                    if (isset($atts['role']) && $atts['role'] != 'test') {
-
 
-
 
146
                    if (isset($atts['role']) && $atts['role'] != 'test') {
154
                        continue;
147
                        continue;
155
                    }
148
                    }
156
                    if (isset($options['phpunit'])) {
149
 
157
                        if (!preg_match('/AllTests\.php$/i', $name)) {
-
 
158
                            continue;
150
                    if (isset($options['phpunit']) && preg_match('/AllTests\.php\\z/i', $name)) {
159
                        }
151
                        $params[] = $atts['installed_as'];
160
                    } else {
-
 
161
                        if (!preg_match('/\.phpt$/', $name)) {
152
                        continue;
162
                            continue;
153
                    } elseif (!preg_match('/\.phpt\\z/', $name)) {
163
                        }
154
                        continue;
164
                    }
155
                    }
165
                    $params[] = $atts['installed_as'];
156
                    $params[] = $atts['installed_as'];
-
 
157
                }
166
                }
158
            }
167
            }
159
        }
168
        }
160
 
169
        foreach ($params as $p) {
161
        foreach ($params as $p) {
170
            if (is_dir($p)) {
162
            if (is_dir($p)) {
171
                if (isset($options['phpunit'])) {
163
                if (isset($options['phpunit'])) {
172
                    $dir = System::find(array($p, '-type', 'f',
164
                    $dir = System::find(array($p, '-type', 'f',
173
                                                '-maxdepth', $depth,
165
                                                '-maxdepth', $depth,
174
                                                '-name', 'AllTests.php'));
166
                                                '-name', 'AllTests.php'));
-
 
167
                    if (count($dir)) {
-
 
168
                        foreach ($dir as $p) {
-
 
169
                            $p = realpath($p);
175
                } else {
170
                            if (!count($tests) ||
-
 
171
                                  (count($tests) && strlen($p) < strlen($tests[0]))) {
-
 
172
                                // this is in a higher-level directory, use this one instead.
-
 
173
                                $tests = array($p);
-
 
174
                            }
176
                    $dir = System::find(array($p, '-type', 'f',
175
                        }
-
 
176
                    }
177
                                                '-maxdepth', $depth,
177
                    continue;
178
                                                '-name', '*.phpt'));
178
                }
179
                }
179
 
180
                $tests = array_merge($tests, $dir);
180
                $args  = array($p, '-type', 'f', '-name', '*.phpt');
181
            } else {
181
            } else {
182
                if (isset($options['phpunit'])) {
-
 
183
                    if (!preg_match('/AllTests\.php$/i', $p)) {
182
                if (isset($options['phpunit'])) {
184
                        continue;
-
 
185
                    }
183
                    if (preg_match('/AllTests\.php\\z/i', $p)) {
186
                    $tests[] = $p;
184
                        $p = realpath($p);
187
                } else {
185
                        if (!count($tests) ||
188
                    if (!file_exists($p)) {
186
                              (count($tests) && strlen($p) < strlen($tests[0]))) {
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);
187
                            // this is in a higher-level directory, use this one instead.
-
 
188
                            $tests = array($p);
-
 
189
                        }
-
 
190
                    }
-
 
191
                    continue;
-
 
192
                }
-
 
193
 
196
                    } else {
194
                if (file_exists($p) && preg_match('/\.phpt$/', $p)) {
-
 
195
                    $tests[] = $p;
-
 
196
                    continue;
-
 
197
                }
-
 
198
 
-
 
199
                if (!preg_match('/\.phpt\\z/', $p)) {
-
 
200
                    $p .= '.phpt';
-
 
201
                }
-
 
202
 
-
 
203
                $args  = array(dirname($p), '-type', 'f', '-name', $p);
-
 
204
            }
-
 
205
 
197
                        $tests[] = $p;
206
            if (!isset($options['recur'])) {
-
 
207
                $args[] = '-maxdepth';
-
 
208
                $args[] = 1;
-
 
209
            }
198
                    }
210
 
-
 
211
            $dir   = System::find($args);
199
                }
212
            $tests = array_merge($tests, $dir);
200
            }
213
        }
201
        }
214
 
202
        $ini_settings = '';
215
        $ini_settings = '';
-
 
216
        if (isset($options['ini'])) {
203
        if (isset($options['ini'])) {
217
            $ini_settings .= $options['ini'];
204
            $ini_settings .= $options['ini'];
218
        }
205
        }
219
 
-
 
220
        if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) {
206
        if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) {
221
            $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}";
207
            $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}";
222
        }
208
        }
223
 
-
 
224
        if ($ini_settings) {
209
        if ($ini_settings) {
225
            $this->ui->outputData('Using INI settings: "' . $ini_settings . '"');
-
 
226
        }
210
            $this->ui->outputData('Using INI settings: "' . $ini_settings . '"');
227
 
211
        }
228
        $skipped = $passed = $failed = array();
212
        $skipped = $passed = $failed = array();
229
        $tests_count = count($tests);
213
        $this->ui->outputData('Running ' . count($tests) . ' tests', $command);
-
 
214
        $start = time();
230
        $this->ui->outputData('Running ' . $tests_count . ' tests', $command);
215
        if (isset($options['realtimelog'])) {
-
 
216
            if (file_exists('run-tests.log')) {
231
        $start = time();
-
 
232
        if (isset($options['realtimelog']) && file_exists('run-tests.log')) {
217
                unlink('run-tests.log');
233
            unlink('run-tests.log');
218
            }
234
        }
219
        }
235
 
-
 
236
        if (isset($options['tapoutput'])) {
-
 
237
            $tap = '1..' . $tests_count . "\n";
-
 
238
        }
-
 
239
 
-
 
240
        require_once 'PEAR/RunTest.php';
-
 
241
        $run = new PEAR_RunTest($log, $options);
-
 
242
        $run->tests_count = $tests_count;
-
 
243
 
-
 
244
        if (isset($options['coverage']) && extension_loaded('xdebug')){
-
 
245
            $run->xdebug_loaded = true;
-
 
246
        } else {
220
        if (isset($options['tapoutput'])) {
247
            $run->xdebug_loaded = false;
221
            $tap = '1..' . count($tests) . "\n";
248
        }
222
        }
249
 
223
        $i = 1;
250
        $j = $i = 1;
224
        foreach ($tests as $t) {
251
        foreach ($tests as $t) {
225
            if (isset($options['realtimelog'])) {
252
            if (isset($options['realtimelog'])) {
226
                $fp = @fopen('run-tests.log', 'a');
253
                $fp = @fopen('run-tests.log', 'a');
227
                if ($fp) {
254
                if ($fp) {
228
                    fwrite($fp, "Running test $t...");
255
                    fwrite($fp, "Running test [$i / $tests_count] $t...");
229
                    fclose($fp);
256
                    fclose($fp);
-
 
257
                }
-
 
258
            }
-
 
259
            PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
230
                }
260
            if (isset($options['phpunit'])) {
-
 
261
                $result = $run->runPHPUnit($t, $ini_settings);
231
            }
262
            } else {
232
            PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
263
                $result = $run->run($t, $ini_settings, $j);
233
            $result = $run->run($t, $ini_settings);
264
            }
234
            PEAR::staticPopErrorHandling();
265
            PEAR::staticPopErrorHandling();
235
            if (PEAR::isError($result)) {
266
            if (PEAR::isError($result)) {
-
 
267
                $this->ui->log($result->getMessage());
236
                $this->ui->log($result->getMessage());
268
                continue;
237
                continue;
269
            }
238
            }
-
 
239
            if (isset($options['tapoutput'])) {
270
 
240
                $tap .= $result[0] . ' ' . $i . $result[1] . "\n";
271
            if (isset($options['tapoutput'])) {
-
 
272
                $tap .= $result[0] . ' ' . $i . $result[1] . "\n";
241
                $i++;
273
                continue;
242
                continue;
274
            }
243
            }
275
 
244
            if (isset($options['realtimelog'])) {
276
            if (isset($options['realtimelog'])) {
245
                $fp = @fopen('run-tests.log', 'a');
277
                $fp = @fopen('run-tests.log', 'a');
246
                if ($fp) {
278
                if ($fp) {
247
                    fwrite($fp, "$result\n");
279
                    fwrite($fp, "$result\n");
-
 
280
                    fclose($fp);
248
                    fclose($fp);
281
                }
249
                }
282
            }
250
            }
283
 
251
            if ($result == 'FAILED') {
284
            if ($result == 'FAILED') {
252
            	$failed[] = $t;
285
                $failed[] = $t;
253
            }
286
            }
254
            if ($result == 'PASSED') {
287
            if ($result == 'PASSED') {
255
            	$passed[] = $t;
288
                $passed[] = $t;
256
            }
289
            }
-
 
290
            if ($result == 'SKIPPED') {
-
 
291
                $skipped[] = $t;
257
            if ($result == 'SKIPPED') {
292
            }
-
 
293
 
258
            	$skipped[] = $t;
294
            $j++;
259
            }
295
        }
260
        }
296
 
261
        $total = date('i:s', time() - $start);
297
        $total = date('i:s', time() - $start);
262
        if (isset($options['tapoutput'])) {
298
        if (isset($options['tapoutput'])) {
Line 270... Line 306...
270
        } else {
306
        } else {
271
            if (count($failed)) {
307
            if (count($failed)) {
272
                $output = "TOTAL TIME: $total\n";
308
                $output = "TOTAL TIME: $total\n";
273
                $output .= count($passed) . " PASSED TESTS\n";
309
                $output .= count($passed) . " PASSED TESTS\n";
274
                $output .= count($skipped) . " SKIPPED TESTS\n";
310
                $output .= count($skipped) . " SKIPPED TESTS\n";
275
        		$output .= count($failed) . " FAILED TESTS:\n";
311
                $output .= count($failed) . " FAILED TESTS:\n";
276
            	foreach ($failed as $failure) {
312
                foreach ($failed as $failure) {
277
            		$output .= $failure . "\n";
313
                    $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
                }
314
                }
-
 
315
 
-
 
316
                $mode = isset($options['realtimelog']) ? 'a' : 'w';
-
 
317
                $fp   = @fopen('run-tests.log', $mode);
-
 
318
 
284
                if ($fp) {
319
                if ($fp) {
285
                    fwrite($fp, $output, strlen($output));
320
                    fwrite($fp, $output, strlen($output));
286
                    fclose($fp);
321
                    fclose($fp);
287
                    $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command);
322
                    $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command);
288
                }
323
                }
Line 292... Line 327...
292
        }
327
        }
293
        $this->ui->outputData('TOTAL TIME: ' . $total);
328
        $this->ui->outputData('TOTAL TIME: ' . $total);
294
        $this->ui->outputData(count($passed) . ' PASSED TESTS', $command);
329
        $this->ui->outputData(count($passed) . ' PASSED TESTS', $command);
295
        $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command);
330
        $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command);
296
        if (count($failed)) {
331
        if (count($failed)) {
297
    		$this->ui->outputData(count($failed) . ' FAILED TESTS:', $command);
332
            $this->ui->outputData(count($failed) . ' FAILED TESTS:', $command);
298
        	foreach ($failed as $failure) {
333
            foreach ($failed as $failure) {
299
        		$this->ui->outputData($failure, $command);
334
                $this->ui->outputData($failure, $command);
300
        	}
335
            }
301
        }
336
        }
Line -... Line 337...
-
 
337
 
302
 
338
        if (count($failed) == 0) {
-
 
339
            return true;
-
 
340
        }
303
        return true;
341
        return $this->raiseError('Some tests failed');
304
    }
-
 
305
    // }}}
342
    }
306
}
-
 
307
 
-