Редакция 69 | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
69 | alex-w | 1 | <?php |
2 | /** |
||
3 | * PEAR_RunTest |
||
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 Tomas V.V.Cox <cox@idecnet.com> |
||
16 | * @author Greg Beaver <cellog@php.net> |
||
17 | * @copyright 1997-2008 The PHP Group |
||
18 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
||
19 | * @version CVS: $Id: RunTest.php,v 1.67 2008/05/14 02:30:16 cellog Exp $ |
||
20 | * @link http://pear.php.net/package/PEAR |
||
21 | * @since File available since Release 1.3.3 |
||
22 | */ |
||
23 | |||
24 | /** |
||
25 | * for error handling |
||
26 | */ |
||
27 | require_once 'PEAR.php'; |
||
28 | require_once 'PEAR/Config.php'; |
||
29 | |||
30 | define('DETAILED', 1); |
||
31 | putenv("PHP_PEAR_RUNTESTS=1"); |
||
32 | |||
33 | /** |
||
34 | * Simplified version of PHP's test suite |
||
35 | * |
||
36 | * Try it with: |
||
37 | * |
||
38 | * $ php -r 'include "../PEAR/RunTest.php"; $t=new PEAR_RunTest; $o=$t->run("./pear_system.phpt");print_r($o);' |
||
39 | * |
||
40 | * |
||
41 | * @category pear |
||
42 | * @package PEAR |
||
43 | * @author Tomas V.V.Cox <cox@idecnet.com> |
||
44 | * @author Greg Beaver <cellog@php.net> |
||
45 | * @copyright 1997-2008 The PHP Group |
||
46 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
||
47 | * @version Release: 1.7.2 |
||
48 | * @link http://pear.php.net/package/PEAR |
||
49 | * @since Class available since Release 1.3.3 |
||
50 | */ |
||
51 | class PEAR_RunTest |
||
52 | { |
||
53 | var $_headers = array(); |
||
54 | var $_logger; |
||
55 | var $_options; |
||
56 | var $_php; |
||
57 | var $tests_count; |
||
58 | var $xdebug_loaded; |
||
59 | /** |
||
60 | * Saved value of php executable, used to reset $_php when we |
||
61 | * have a test that uses cgi |
||
62 | * |
||
63 | * @var unknown_type |
||
64 | */ |
||
65 | var $_savephp; |
||
66 | var $ini_overwrites = array( |
||
67 | 'output_handler=', |
||
68 | 'open_basedir=', |
||
69 | 'safe_mode=0', |
||
70 | 'disable_functions=', |
||
71 | 'output_buffering=Off', |
||
72 | 'display_errors=1', |
||
73 | 'log_errors=0', |
||
74 | 'html_errors=0', |
||
75 | 'track_errors=1', |
||
76 | 'report_memleaks=0', |
||
77 | 'report_zend_debug=0', |
||
78 | 'docref_root=', |
||
79 | 'docref_ext=.html', |
||
80 | 'error_prepend_string=', |
||
81 | 'error_append_string=', |
||
82 | 'auto_prepend_file=', |
||
83 | 'auto_append_file=', |
||
84 | 'magic_quotes_runtime=0', |
||
85 | 'xdebug.default_enable=0', |
||
86 | 'allow_url_fopen=1', |
||
87 | ); |
||
88 | |||
89 | /** |
||
90 | * An object that supports the PEAR_Common->log() signature, or null |
||
91 | * @param PEAR_Common|null |
||
92 | */ |
||
93 | function PEAR_RunTest($logger = null, $options = array()) |
||
94 | { |
||
95 | if (!defined('E_DEPRECATED')) { |
||
96 | define('E_DEPRECATED', 0); |
||
97 | } |
||
98 | if (!defined('E_STRICT')) { |
||
99 | define('E_STRICT', 0); |
||
100 | } |
||
101 | $this->ini_overwrites[] = 'error_reporting=' . (E_ALL & ~(E_DEPRECATED | E_STRICT)); |
||
102 | if (is_null($logger)) { |
||
103 | require_once 'PEAR/Common.php'; |
||
104 | $logger = new PEAR_Common; |
||
105 | } |
||
106 | $this->_logger = $logger; |
||
107 | $this->_options = $options; |
||
108 | |||
109 | $conf = &PEAR_Config::singleton(); |
||
110 | $this->_php = $conf->get('php_bin'); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Taken from php-src/run-tests.php |
||
115 | * |
||
116 | * @param string $commandline command name |
||
117 | * @param array $env |
||
118 | * @param string $stdin standard input to pass to the command |
||
119 | * @return unknown |
||
120 | */ |
||
121 | function system_with_timeout($commandline, $env = null, $stdin = null) |
||
122 | { |
||
123 | $data = ''; |
||
124 | if (version_compare(phpversion(), '5.0.0', '<')) { |
||
125 | $proc = proc_open($commandline, array( |
||
126 | |||
127 | 1 => array('pipe', 'w'), |
||
128 | 2 => array('pipe', 'w') |
||
129 | ), $pipes); |
||
130 | } else { |
||
131 | $proc = proc_open($commandline, array( |
||
132 | |||
133 | 1 => array('pipe', 'w'), |
||
134 | 2 => array('pipe', 'w') |
||
135 | ), $pipes, null, $env, array('suppress_errors' => true)); |
||
136 | } |
||
137 | |||
138 | if (!$proc) { |
||
139 | return false; |
||
140 | } |
||
141 | |||
142 | if (is_string($stdin)) { |
||
143 | fwrite($pipes[0], $stdin); |
||
144 | } |
||
145 | fclose($pipes[0]); |
||
146 | |||
147 | while (true) { |
||
148 | /* hide errors from interrupted syscalls */ |
||
149 | $r = $pipes; |
||
150 | $e = $w = null; |
||
151 | $n = @stream_select($r, $w, $e, 60); |
||
152 | |||
153 | if ($n === 0) { |
||
154 | /* timed out */ |
||
155 | $data .= "\n ** ERROR: process timed out **\n"; |
||
156 | proc_terminate($proc); |
||
157 | return array(1234567890, $data); |
||
158 | } else if ($n > 0) { |
||
159 | $line = fread($pipes[1], 8192); |
||
160 | if (strlen($line) == 0) { |
||
161 | /* EOF */ |
||
162 | break; |
||
163 | } |
||
164 | $data .= $line; |
||
165 | } |
||
166 | } |
||
167 | if (function_exists('proc_get_status')) { |
||
168 | $stat = proc_get_status($proc); |
||
169 | if ($stat['signaled']) { |
||
170 | $data .= "\nTermsig=".$stat['stopsig']; |
||
171 | } |
||
172 | } |
||
173 | $code = proc_close($proc); |
||
174 | if (function_exists('proc_get_status')) { |
||
175 | $code = $stat['exitcode']; |
||
176 | } |
||
177 | return array($code, $data); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Turns a PHP INI string into an array |
||
182 | * |
||
183 | * Turns -d "include_path=/foo/bar" into this: |
||
184 | * array( |
||
185 | * 'include_path' => array( |
||
186 | * 'operator' => '-d', |
||
187 | * 'value' => '/foo/bar', |
||
188 | * ) |
||
189 | * ) |
||
190 | * Works both with quotes and without |
||
191 | * |
||
192 | * @param string an PHP INI string, -d "include_path=/foo/bar" |
||
193 | * @return array |
||
194 | */ |
||
195 | function iniString2array($ini_string) |
||
196 | { |
||
197 | if (!$ini_string) { |
||
198 | return array(); |
||
199 | } |
||
200 | $split = preg_split('/[\s]|=/', $ini_string, -1, PREG_SPLIT_NO_EMPTY); |
||
201 | $key = $split[1][0] == '"' ? substr($split[1], 1) : $split[1]; |
||
202 | $value = $split[2][strlen($split[2]) - 1] == '"' ? substr($split[2], 0, -1) : $split[2]; |
||
203 | // FIXME review if this is really the struct to go with |
||
204 | $array = array($key => array('operator' => $split[0], 'value' => $value)); |
||
205 | return $array; |
||
206 | } |
||
207 | |||
208 | function settings2array($settings, $ini_settings) |
||
209 | { |
||
210 | foreach ($settings as $setting) { |
||
211 | if (strpos($setting, '=') !== false) { |
||
212 | $setting = explode('=', $setting, 2); |
||
213 | $name = trim(strtolower($setting[0])); |
||
214 | $value = trim($setting[1]); |
||
215 | $ini_settings[$name] = $value; |
||
216 | } |
||
217 | } |
||
218 | return $ini_settings; |
||
219 | } |
||
220 | |||
221 | function settings2params($ini_settings) |
||
222 | { |
||
223 | $settings = ''; |
||
224 | foreach ($ini_settings as $name => $value) { |
||
225 | if (is_array($value)) { |
||
226 | $operator = $value['operator']; |
||
227 | $value = $value['value']; |
||
228 | } else { |
||
229 | $operator = '-d'; |
||
230 | } |
||
231 | $value = addslashes($value); |
||
232 | $settings .= " $operator \"$name=$value\""; |
||
233 | } |
||
234 | return $settings; |
||
235 | } |
||
236 | |||
237 | function runPHPUnit($file, $ini_settings = '') |
||
238 | { |
||
239 | if (!file_exists($file) && file_exists(getcwd() . DIRECTORY_SEPARATOR . $file)) { |
||
240 | $file = realpath(getcwd() . DIRECTORY_SEPARATOR . $file); |
||
241 | break; |
||
242 | } elseif (file_exists($file)) { |
||
243 | $file = realpath($file); |
||
244 | } |
||
245 | $cmd = "$this->_php$ini_settings -f $file"; |
||
246 | if (isset($this->_logger)) { |
||
247 | $this->_logger->log(2, 'Running command "' . $cmd . '"'); |
||
248 | } |
||
249 | |||
250 | $savedir = getcwd(); // in case the test moves us around |
||
251 | chdir(dirname($file)); |
||
252 | echo `$cmd`; |
||
253 | chdir($savedir); |
||
254 | return 'PASSED'; // we have no way of knowing this information so assume passing |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Runs an individual test case. |
||
259 | * |
||
260 | * @param string The filename of the test |
||
261 | * @param array|string INI settings to be applied to the test run |
||
262 | * @param integer Number what the current running test is of the |
||
263 | * whole test suite being runned. |
||
264 | * |
||
265 | * @return string|object Returns PASSED, WARNED, FAILED depending on how the |
||
266 | * test came out. |
||
267 | * PEAR Error when the tester it self fails |
||
268 | */ |
||
269 | function run($file, $ini_settings = array(), $test_number = 1) |
||
270 | { |
||
271 | if (isset($this->_savephp)) { |
||
272 | $this->_php = $this->_savephp; |
||
273 | unset($this->_savephp); |
||
274 | } |
||
275 | if (empty($this->_options['cgi'])) { |
||
276 | // try to see if php-cgi is in the path |
||
277 | $res = $this->system_with_timeout('php-cgi -v'); |
||
278 | if (false !== $res && !(is_array($res) && $res === array(127, ''))) { |
||
279 | $this->_options['cgi'] = 'php-cgi'; |
||
280 | } |
||
281 | } |
||
282 | if (1 < $len = strlen($this->tests_count)) { |
||
283 | $test_number = str_pad($test_number, $len, ' ', STR_PAD_LEFT); |
||
284 | $test_nr = "[$test_number/$this->tests_count] "; |
||
285 | } else { |
||
286 | $test_nr = ''; |
||
287 | } |
||
288 | |||
289 | $file = realpath($file); |
||
290 | $section_text = $this->_readFile($file); |
||
291 | if (PEAR::isError($section_text)) { |
||
292 | return $section_text; |
||
293 | } |
||
294 | |||
295 | if (isset($section_text['POST_RAW']) && isset($section_text['UPLOAD'])) { |
||
296 | return PEAR::raiseError("Cannot contain both POST_RAW and UPLOAD in test file: $file"); |
||
297 | } |
||
298 | |||
299 | $cwd = getcwd(); |
||
300 | |||
301 | $pass_options = ''; |
||
302 | if (!empty($this->_options['ini'])) { |
||
303 | $pass_options = $this->_options['ini']; |
||
304 | } |
||
305 | |||
306 | if (is_string($ini_settings)) { |
||
307 | $ini_settings = $this->iniString2array($ini_settings); |
||
308 | } |
||
309 | |||
310 | $ini_settings = $this->settings2array($this->ini_overwrites, $ini_settings); |
||
311 | if ($section_text['INI']) { |
||
312 | if (strpos($section_text['INI'], '{PWD}') !== false) { |
||
313 | $section_text['INI'] = str_replace('{PWD}', dirname($file), $section_text['INI']); |
||
314 | } |
||
315 | $ini = preg_split( "/[\n\r]+/", $section_text['INI']); |
||
316 | $ini_settings = $this->settings2array($ini, $ini_settings); |
||
317 | } |
||
318 | $ini_settings = $this->settings2params($ini_settings); |
||
319 | $shortname = str_replace($cwd . DIRECTORY_SEPARATOR, '', $file); |
||
320 | |||
321 | $tested = trim($section_text['TEST']); |
||
322 | $tested.= !isset($this->_options['simple']) ? "[$shortname]" : ' '; |
||
323 | |||
324 | if (!empty($section_text['POST']) || !empty($section_text['POST_RAW']) || |
||
325 | !empty($section_text['UPLOAD']) || !empty($section_text['GET']) || |
||
326 | !empty($section_text['COOKIE']) || !empty($section_text['EXPECTHEADERS'])) { |
||
327 | if (empty($this->_options['cgi'])) { |
||
328 | if (!isset($this->_options['quiet'])) { |
||
329 | $this->_logger->log(0, "SKIP $test_nr$tested (reason: --cgi option needed for this test, type 'pear help run-tests')"); |
||
330 | } |
||
331 | if (isset($this->_options['tapoutput'])) { |
||
332 | return array('ok', ' # skip --cgi option needed for this test, "pear help run-tests" for info'); |
||
333 | } |
||
334 | return 'SKIPPED'; |
||
335 | } |
||
336 | $this->_savephp = $this->_php; |
||
337 | $this->_php = $this->_options['cgi']; |
||
338 | } |
||
339 | |||
340 | $temp_dir = realpath(dirname($file)); |
||
341 | $main_file_name = basename($file, 'phpt'); |
||
342 | $diff_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'diff'; |
||
343 | $log_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'log'; |
||
344 | $exp_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'exp'; |
||
345 | $output_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'out'; |
||
346 | $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'mem'; |
||
347 | $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'php'; |
||
348 | $temp_skipif = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'skip.php'; |
||
349 | $temp_clean = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'clean.php'; |
||
350 | $tmp_post = $temp_dir . DIRECTORY_SEPARATOR . uniqid('phpt.'); |
||
351 | |||
352 | // unlink old test results |
||
353 | $this->_cleanupOldFiles($file); |
||
354 | |||
355 | // Check if test should be skipped. |
||
356 | $res = $this->_runSkipIf($section_text, $temp_skipif, $tested, $ini_settings); |
||
357 | if (count($res) != 2) { |
||
358 | return $res; |
||
359 | } |
||
360 | $info = $res['info']; |
||
361 | $warn = $res['warn']; |
||
362 | |||
363 | // We've satisfied the preconditions - run the test! |
||
364 | if (isset($this->_options['coverage']) && $this->xdebug_loaded) { |
||
365 | $len_f = 5; |
||
366 | if (substr($section_text['FILE'], 0, 5) != '<?php' |
||
367 | && substr($section_text['FILE'], 0, 2) == '<?') { |
||
368 | $len_f = 2; |
||
369 | } |
||
370 | |||
371 | $text = '<?php' . "\n" . 'xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);' . "\n"; |
||
372 | $new = substr($section_text['FILE'], $len_f, strlen($section_text['FILE'])); |
||
373 | $text .= substr($new, 0, strrpos($new, '?>')); |
||
374 | $xdebug_file = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'xdebug'; |
||
375 | $text .= "\n" . |
||
376 | "\n" . '$xdebug = var_export(xdebug_get_code_coverage(), true);'; |
||
377 | if (!function_exists('file_put_contents')) { |
||
378 | $text .= "\n" . '$fh = fopen(\'' . $xdebug_file . '\', "wb");' . |
||
379 | "\n" . 'if ($fh !== false) {' . |
||
380 | "\n" . ' fwrite($fh, $xdebug);' . |
||
381 | "\n" . ' fclose($fh);' . |
||
382 | "\n" . '}'; |
||
383 | } else { |
||
384 | $text .= "\n" . 'file_put_contents(\'' . $xdebug_file . '\', $xdebug);'; |
||
385 | } |
||
386 | $text .= "\n" . 'xdebug_stop_code_coverage();' . "\n" . '?>'; |
||
387 | |||
388 | $this->save_text($temp_file, $text); |
||
389 | } else { |
||
390 | $this->save_text($temp_file, $section_text['FILE']); |
||
391 | } |
||
392 | |||
393 | $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : ''; |
||
394 | $cmd = "$this->_php$ini_settings -f \"$temp_file\" $args 2>&1"; |
||
395 | if (isset($this->_logger)) { |
||
396 | $this->_logger->log(2, 'Running command "' . $cmd . '"'); |
||
397 | } |
||
398 | |||
399 | // Reset environment from any previous test. |
||
400 | $env = $this->_resetEnv($section_text, $temp_file); |
||
401 | |||
402 | $section_text = $this->_processUpload($section_text, $file); |
||
403 | if (PEAR::isError($section_text)) { |
||
404 | return $section_text; |
||
405 | } |
||
406 | |||
407 | if (array_key_exists('POST_RAW', $section_text) && !empty($section_text['POST_RAW'])) { |
||
408 | $post = trim($section_text['POST_RAW']); |
||
409 | $raw_lines = explode("\n", $post); |
||
410 | |||
411 | $request = ''; |
||
412 | $started = false; |
||
413 | foreach ($raw_lines as $i => $line) { |
||
414 | if (empty($env['CONTENT_TYPE']) && |
||
415 | preg_match('/^Content-Type:(.*)/i', $line, $res)) { |
||
416 | $env['CONTENT_TYPE'] = trim(str_replace("\r", '', $res[1])); |
||
417 | continue; |
||
418 | } |
||
419 | if ($started) { |
||
420 | $request .= "\n"; |
||
421 | } |
||
422 | $started = true; |
||
423 | $request .= $line; |
||
424 | } |
||
425 | |||
426 | $env['CONTENT_LENGTH'] = strlen($request); |
||
427 | $env['REQUEST_METHOD'] = 'POST'; |
||
428 | |||
429 | $this->save_text($tmp_post, $request); |
||
430 | $cmd = "$this->_php$pass_options$ini_settings \"$temp_file\" 2>&1 < $tmp_post"; |
||
431 | } elseif (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) { |
||
432 | $post = trim($section_text['POST']); |
||
433 | $this->save_text($tmp_post, $post); |
||
434 | $content_length = strlen($post); |
||
435 | |||
436 | $env['REQUEST_METHOD'] = 'POST'; |
||
437 | $env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'; |
||
438 | $env['CONTENT_LENGTH'] = $content_length; |
||
439 | |||
440 | $cmd = "$this->_php$pass_options$ini_settings \"$temp_file\" 2>&1 < $tmp_post"; |
||
441 | } else { |
||
442 | $env['REQUEST_METHOD'] = 'GET'; |
||
443 | $env['CONTENT_TYPE'] = ''; |
||
444 | $env['CONTENT_LENGTH'] = ''; |
||
445 | } |
||
446 | |||
447 | if (OS_WINDOWS && isset($section_text['RETURNS'])) { |
||
448 | ob_start(); |
||
449 | system($cmd, $return_value); |
||
450 | $out = ob_get_contents(); |
||
451 | ob_end_clean(); |
||
452 | $section_text['RETURNS'] = (int) trim($section_text['RETURNS']); |
||
453 | $returnfail = ($return_value != $section_text['RETURNS']); |
||
454 | } else { |
||
455 | $returnfail = false; |
||
456 | $stdin = isset($section_text['STDIN']) ? $section_text['STDIN'] : null; |
||
457 | $out = $this->system_with_timeout($cmd, $env, $stdin); |
||
458 | $return_value = $out[0]; |
||
459 | $out = $out[1]; |
||
460 | } |
||
461 | |||
462 | $output = preg_replace('/\r\n/', "\n", trim($out)); |
||
463 | |||
464 | if (isset($tmp_post) && realpath($tmp_post) && file_exists($tmp_post)) { |
||
465 | @unlink(realpath($tmp_post)); |
||
466 | } |
||
467 | chdir($cwd); // in case the test moves us around |
||
468 | |||
469 | $this->_testCleanup($section_text, $temp_clean); |
||
470 | |||
471 | /* when using CGI, strip the headers from the output */ |
||
472 | $output = $this->_stripHeadersCGI($output); |
||
473 | |||
474 | if (isset($section_text['EXPECTHEADERS'])) { |
||
475 | $testheaders = $this->_processHeaders($section_text['EXPECTHEADERS']); |
||
476 | $missing = array_diff_assoc($testheaders, $this->_headers); |
||
477 | $changed = ''; |
||
478 | foreach ($missing as $header => $value) { |
||
479 | if (isset($this->_headers[$header])) { |
||
480 | $changed .= "-$header: $value\n+$header: "; |
||
481 | $changed .= $this->_headers[$header]; |
||
482 | } else { |
||
483 | $changed .= "-$header: $value\n"; |
||
484 | } |
||
485 | } |
||
486 | if ($missing) { |
||
487 | // tack on failed headers to output: |
||
488 | $output .= "\n====EXPECTHEADERS FAILURE====:\n$changed"; |
||
489 | } |
||
490 | } |
||
491 | // Does the output match what is expected? |
||
492 | do { |
||
493 | if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) { |
||
494 | if (isset($section_text['EXPECTF'])) { |
||
495 | $wanted = trim($section_text['EXPECTF']); |
||
496 | } else { |
||
497 | $wanted = trim($section_text['EXPECTREGEX']); |
||
498 | } |
||
499 | $wanted_re = preg_replace('/\r\n/', "\n", $wanted); |
||
500 | if (isset($section_text['EXPECTF'])) { |
||
501 | $wanted_re = preg_quote($wanted_re, '/'); |
||
502 | // Stick to basics |
||
503 | $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy |
||
504 | $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re); |
||
505 | $wanted_re = str_replace("%d", "[0-9]+", $wanted_re); |
||
506 | $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re); |
||
507 | $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?", $wanted_re); |
||
508 | $wanted_re = str_replace("%c", ".", $wanted_re); |
||
509 | // %f allows two points "-.0.0" but that is the best *simple* expression |
||
510 | } |
||
511 | /* DEBUG YOUR REGEX HERE |
||
512 | var_dump($wanted_re); |
||
513 | print(str_repeat('=', 80) . "\n"); |
||
514 | var_dump($output); |
||
515 | */ |
||
516 | if (!$returnfail && preg_match("/^$wanted_re\$/s", $output)) { |
||
517 | if (file_exists($temp_file)) { |
||
518 | unlink($temp_file); |
||
519 | } |
||
520 | if (array_key_exists('FAIL', $section_text)) { |
||
521 | break; |
||
522 | } |
||
523 | if (!isset($this->_options['quiet'])) { |
||
524 | $this->_logger->log(0, "PASS $test_nr$tested$info"); |
||
525 | } |
||
526 | if (isset($this->_options['tapoutput'])) { |
||
527 | return array('ok', ' - ' . $tested); |
||
528 | } |
||
529 | return 'PASSED'; |
||
530 | } |
||
531 | } else { |
||
532 | if (isset($section_text['EXPECTFILE'])) { |
||
533 | $f = $temp_dir . '/' . trim($section_text['EXPECTFILE']); |
||
534 | if (!($fp = @fopen($f, 'rb'))) { |
||
535 | return PEAR::raiseError('--EXPECTFILE-- section file ' . |
||
536 | $f . ' not found'); |
||
537 | } |
||
538 | fclose($fp); |
||
539 | $section_text['EXPECT'] = file_get_contents($f); |
||
540 | } |
||
541 | $wanted = preg_replace('/\r\n/', "\n", trim($section_text['EXPECT'])); |
||
542 | // compare and leave on success |
||
543 | if (!$returnfail && 0 == strcmp($output, $wanted)) { |
||
544 | if (file_exists($temp_file)) { |
||
545 | unlink($temp_file); |
||
546 | } |
||
547 | if (array_key_exists('FAIL', $section_text)) { |
||
548 | break; |
||
549 | } |
||
550 | if (!isset($this->_options['quiet'])) { |
||
551 | $this->_logger->log(0, "PASS $test_nr$tested$info"); |
||
552 | } |
||
553 | if (isset($this->_options['tapoutput'])) { |
||
554 | return array('ok', ' - ' . $tested); |
||
555 | } |
||
556 | return 'PASSED'; |
||
557 | } |
||
558 | } |
||
559 | } while (false); |
||
560 | |||
561 | if (array_key_exists('FAIL', $section_text)) { |
||
562 | // we expect a particular failure |
||
563 | // this is only used for testing PEAR_RunTest |
||
564 | $expectf = isset($section_text['EXPECTF']) ? $wanted_re : null; |
||
565 | $faildiff = $this->generate_diff($wanted, $output, null, $expectf); |
||
566 | $faildiff = preg_replace('/\r/', '', $faildiff); |
||
567 | $wanted = preg_replace('/\r/', '', trim($section_text['FAIL'])); |
||
568 | if ($faildiff == $wanted) { |
||
569 | if (!isset($this->_options['quiet'])) { |
||
570 | $this->_logger->log(0, "PASS $test_nr$tested$info"); |
||
571 | } |
||
572 | if (isset($this->_options['tapoutput'])) { |
||
573 | return array('ok', ' - ' . $tested); |
||
574 | } |
||
575 | return 'PASSED'; |
||
576 | } |
||
577 | unset($section_text['EXPECTF']); |
||
578 | $output = $faildiff; |
||
579 | if (isset($section_text['RETURNS'])) { |
||
580 | return PEAR::raiseError('Cannot have both RETURNS and FAIL in the same test: ' . |
||
581 | $file); |
||
582 | } |
||
583 | } |
||
584 | |||
585 | // Test failed so we need to report details. |
||
586 | $txt = $warn ? 'WARN ' : 'FAIL '; |
||
587 | $this->_logger->log(0, $txt . $test_nr . $tested . $info); |
||
588 | |||
589 | // write .exp |
||
590 | $res = $this->_writeLog($exp_filename, $wanted); |
||
591 | if (PEAR::isError($res)) { |
||
592 | return $res; |
||
593 | } |
||
594 | |||
595 | // write .out |
||
596 | $res = $this->_writeLog($output_filename, $output); |
||
597 | if (PEAR::isError($res)) { |
||
598 | return $res; |
||
599 | } |
||
600 | |||
601 | // write .diff |
||
602 | $returns = isset($section_text['RETURNS']) ? |
||
603 | array(trim($section_text['RETURNS']), $return_value) : null; |
||
604 | $expectf = isset($section_text['EXPECTF']) ? $wanted_re : null; |
||
605 | $data = $this->generate_diff($wanted, $output, $returns, $expectf); |
||
606 | $res = $this->_writeLog($diff_filename, $data); |
||
607 | if (PEAR::isError($res)) { |
||
608 | return $res; |
||
609 | } |
||
610 | |||
611 | // write .log |
||
612 | $data = " |
||
613 | ---- EXPECTED OUTPUT |
||
614 | $wanted |
||
615 | ---- ACTUAL OUTPUT |
||
616 | $output |
||
617 | ---- FAILED |
||
618 | "; |
||
619 | |||
620 | if ($returnfail) { |
||
621 | $data .= " |
||
622 | ---- EXPECTED RETURN |
||
623 | $section_text[RETURNS] |
||
624 | ---- ACTUAL RETURN |
||
625 | $return_value |
||
626 | "; |
||
627 | } |
||
628 | |||
629 | $res = $this->_writeLog($log_filename, $data); |
||
630 | if (PEAR::isError($res)) { |
||
631 | return $res; |
||
632 | } |
||
633 | |||
634 | if (isset($this->_options['tapoutput'])) { |
||
635 | $wanted = explode("\n", $wanted); |
||
636 | $wanted = "# Expected output:\n#\n#" . implode("\n#", $wanted); |
||
637 | $output = explode("\n", $output); |
||
638 | $output = "#\n#\n# Actual output:\n#\n#" . implode("\n#", $output); |
||
639 | return array($wanted . $output . 'not ok', ' - ' . $tested); |
||
640 | } |
||
641 | return $warn ? 'WARNED' : 'FAILED'; |
||
642 | } |
||
643 | |||
644 | function generate_diff($wanted, $output, $rvalue, $wanted_re) |
||
645 | { |
||
646 | $w = explode("\n", $wanted); |
||
647 | $o = explode("\n", $output); |
||
648 | $wr = explode("\n", $wanted_re); |
||
649 | $w1 = array_diff_assoc($w, $o); |
||
650 | $o1 = array_diff_assoc($o, $w); |
||
651 | $o2 = $w2 = array(); |
||
652 | foreach ($w1 as $idx => $val) { |
||
653 | if (!$wanted_re || !isset($wr[$idx]) || !isset($o1[$idx]) || |
||
654 | !preg_match('/^' . $wr[$idx] . '\\z/', $o1[$idx])) { |
||
655 | $w2[sprintf("%03d<", $idx)] = sprintf("%03d- ", $idx + 1) . $val; |
||
656 | } |
||
657 | } |
||
658 | foreach ($o1 as $idx => $val) { |
||
659 | if (!$wanted_re || !isset($wr[$idx]) || |
||
660 | !preg_match('/^' . $wr[$idx] . '\\z/', $val)) { |
||
661 | $o2[sprintf("%03d>", $idx)] = sprintf("%03d+ ", $idx + 1) . $val; |
||
662 | } |
||
663 | } |
||
664 | $diff = array_merge($w2, $o2); |
||
665 | ksort($diff); |
||
666 | $extra = $rvalue ? "##EXPECTED: $rvalue[0]\r\n##RETURNED: $rvalue[1]" : ''; |
||
667 | return implode("\r\n", $diff) . $extra; |
||
668 | } |
||
669 | |||
670 | // Write the given text to a temporary file, and return the filename. |
||
671 | function save_text($filename, $text) |
||
672 | { |
||
673 | if (!$fp = fopen($filename, 'w')) { |
||
674 | return PEAR::raiseError("Cannot open file '" . $filename . "' (save_text)"); |
||
675 | } |
||
676 | fwrite($fp, $text); |
||
677 | fclose($fp); |
||
678 | if (1 < DETAILED) echo " |
||
679 | FILE $filename {{{ |
||
680 | $text |
||
681 | }}} |
||
682 | "; |
||
683 | } |
||
684 | |||
685 | function _cleanupOldFiles($file) |
||
686 | { |
||
687 | $temp_dir = realpath(dirname($file)); |
||
688 | $mainFileName = basename($file, 'phpt'); |
||
689 | $diff_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'diff'; |
||
690 | $log_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'log'; |
||
691 | $exp_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'exp'; |
||
692 | $output_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'out'; |
||
693 | $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'mem'; |
||
694 | $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'php'; |
||
695 | $temp_skipif = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'skip.php'; |
||
696 | $temp_clean = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'clean.php'; |
||
697 | $tmp_post = $temp_dir . DIRECTORY_SEPARATOR . uniqid('phpt.'); |
||
698 | |||
699 | // unlink old test results |
||
700 | @unlink($diff_filename); |
||
701 | @unlink($log_filename); |
||
702 | @unlink($exp_filename); |
||
703 | @unlink($output_filename); |
||
704 | @unlink($memcheck_filename); |
||
705 | @unlink($temp_file); |
||
706 | @unlink($temp_skipif); |
||
707 | @unlink($tmp_post); |
||
708 | @unlink($temp_clean); |
||
709 | } |
||
710 | |||
711 | function _runSkipIf($section_text, $temp_skipif, $tested, $ini_settings) |
||
712 | { |
||
713 | $info = ''; |
||
714 | $warn = false; |
||
715 | if (array_key_exists('SKIPIF', $section_text) && trim($section_text['SKIPIF'])) { |
||
716 | $this->save_text($temp_skipif, $section_text['SKIPIF']); |
||
717 | $output = $this->system_with_timeout("$this->_php$ini_settings -f \"$temp_skipif\""); |
||
718 | $output = $output[1]; |
||
719 | $loutput = ltrim($output); |
||
720 | unlink($temp_skipif); |
||
721 | if (!strncasecmp('skip', $loutput, 4)) { |
||
722 | $skipreason = "SKIP $tested"; |
||
723 | if (preg_match('/^\s*skip\s*(.+)\s*/i', $output, $m)) { |
||
724 | $skipreason .= '(reason: ' . $m[1] . ')'; |
||
725 | } |
||
726 | if (!isset($this->_options['quiet'])) { |
||
727 | $this->_logger->log(0, $skipreason); |
||
728 | } |
||
729 | if (isset($this->_options['tapoutput'])) { |
||
730 | return array('ok', ' # skip ' . $reason); |
||
731 | } |
||
732 | return 'SKIPPED'; |
||
733 | } |
||
734 | |||
735 | if (!strncasecmp('info', $loutput, 4) |
||
736 | && preg_match('/^\s*info\s*(.+)\s*/i', $output, $m)) { |
||
737 | $info = " (info: $m[1])"; |
||
738 | } |
||
739 | |||
740 | if (!strncasecmp('warn', $loutput, 4) |
||
741 | && preg_match('/^\s*warn\s*(.+)\s*/i', $output, $m)) { |
||
742 | $warn = true; /* only if there is a reason */ |
||
743 | $info = " (warn: $m[1])"; |
||
744 | } |
||
745 | } |
||
746 | |||
747 | return array('warn' => $warn, 'info' => $info); |
||
748 | } |
||
749 | |||
750 | function _stripHeadersCGI($output) |
||
751 | { |
||
752 | $this->headers = array(); |
||
753 | if (!empty($this->_options['cgi']) && |
||
754 | $this->_php == $this->_options['cgi'] && |
||
755 | preg_match("/^(.*?)(?:\n\n(.*)|\\z)/s", $output, $match)) { |
||
756 | $output = isset($match[2]) ? trim($match[2]) : ''; |
||
757 | $this->_headers = $this->_processHeaders($match[1]); |
||
758 | } |
||
759 | |||
760 | return $output; |
||
761 | } |
||
762 | |||
763 | /** |
||
764 | * Return an array that can be used with array_diff() to compare headers |
||
765 | * |
||
766 | * @param string $text |
||
767 | */ |
||
768 | function _processHeaders($text) |
||
769 | { |
||
770 | $headers = array(); |
||
771 | $rh = preg_split("/[\n\r]+/", $text); |
||
772 | foreach ($rh as $line) { |
||
773 | if (strpos($line, ':')!== false) { |
||
774 | $line = explode(':', $line, 2); |
||
775 | $headers[trim($line[0])] = trim($line[1]); |
||
776 | } |
||
777 | } |
||
778 | return $headers; |
||
779 | } |
||
780 | |||
781 | function _readFile($file) |
||
782 | { |
||
783 | // Load the sections of the test file. |
||
784 | $section_text = array( |
||
785 | 'TEST' => '(unnamed test)', |
||
786 | 'SKIPIF' => '', |
||
787 | 'GET' => '', |
||
788 | 'COOKIE' => '', |
||
789 | 'POST' => '', |
||
790 | 'ARGS' => '', |
||
791 | 'INI' => '', |
||
792 | 'CLEAN' => '', |
||
793 | ); |
||
794 | |||
795 | if (!is_file($file) || !$fp = fopen($file, "r")) { |
||
796 | return PEAR::raiseError("Cannot open test file: $file"); |
||
797 | } |
||
798 | |||
799 | $section = ''; |
||
800 | while (!feof($fp)) { |
||
801 | $line = fgets($fp); |
||
802 | |||
803 | // Match the beginning of a section. |
||
804 | if (preg_match('/^--([_A-Z]+)--/', $line, $r)) { |
||
805 | $section = $r[1]; |
||
806 | $section_text[$section] = ''; |
||
807 | continue; |
||
808 | } elseif (empty($section)) { |
||
809 | fclose($fp); |
||
810 | return PEAR::raiseError("Invalid sections formats in test file: $file"); |
||
811 | } |
||
812 | |||
813 | // Add to the section text. |
||
814 | $section_text[$section] .= $line; |
||
815 | } |
||
816 | fclose($fp); |
||
817 | |||
818 | return $section_text; |
||
819 | } |
||
820 | |||
821 | function _writeLog($logname, $data) |
||
822 | { |
||
823 | if (!$log = fopen($logname, 'w')) { |
||
824 | return PEAR::raiseError("Cannot create test log - $logname"); |
||
825 | } |
||
826 | fwrite($log, $data); |
||
827 | fclose($log); |
||
828 | } |
||
829 | |||
830 | function _resetEnv($section_text, $temp_file) |
||
831 | { |
||
832 | $env = $_ENV; |
||
833 | $env['REDIRECT_STATUS'] = ''; |
||
834 | $env['QUERY_STRING'] = ''; |
||
835 | $env['PATH_TRANSLATED'] = ''; |
||
836 | $env['SCRIPT_FILENAME'] = ''; |
||
837 | $env['REQUEST_METHOD'] = ''; |
||
838 | $env['CONTENT_TYPE'] = ''; |
||
839 | $env['CONTENT_LENGTH'] = ''; |
||
840 | if (!empty($section_text['ENV'])) { |
||
841 | foreach (explode("\n", trim($section_text['ENV'])) as $e) { |
||
842 | $e = explode('=', trim($e), 2); |
||
843 | if (!empty($e[0]) && isset($e[1])) { |
||
844 | $env[$e[0]] = $e[1]; |
||
845 | } |
||
846 | } |
||
847 | } |
||
848 | if (array_key_exists('GET', $section_text)) { |
||
849 | $env['QUERY_STRING'] = trim($section_text['GET']); |
||
850 | } else { |
||
851 | $env['QUERY_STRING'] = ''; |
||
852 | } |
||
853 | if (array_key_exists('COOKIE', $section_text)) { |
||
854 | $env['HTTP_COOKIE'] = trim($section_text['COOKIE']); |
||
855 | } else { |
||
856 | $env['HTTP_COOKIE'] = ''; |
||
857 | } |
||
858 | $env['REDIRECT_STATUS'] = '1'; |
||
859 | $env['PATH_TRANSLATED'] = $temp_file; |
||
860 | $env['SCRIPT_FILENAME'] = $temp_file; |
||
861 | |||
862 | return $env; |
||
863 | } |
||
864 | |||
865 | function _processUpload($section_text, $file) |
||
866 | { |
||
867 | if (array_key_exists('UPLOAD', $section_text) && !empty($section_text['UPLOAD'])) { |
||
868 | $upload_files = trim($section_text['UPLOAD']); |
||
869 | $upload_files = explode("\n", $upload_files); |
||
870 | |||
871 | $request = "Content-Type: multipart/form-data; boundary=---------------------------20896060251896012921717172737\n" . |
||
872 | "-----------------------------20896060251896012921717172737\n"; |
||
873 | foreach ($upload_files as $fileinfo) { |
||
874 | $fileinfo = explode('=', $fileinfo); |
||
875 | if (count($fileinfo) != 2) { |
||
876 | return PEAR::raiseError("Invalid UPLOAD section in test file: $file"); |
||
877 | } |
||
878 | if (!realpath(dirname($file) . '/' . $fileinfo[1])) { |
||
879 | return PEAR::raiseError("File for upload does not exist: $fileinfo[1] " . |
||
880 | "in test file: $file"); |
||
881 | } |
||
882 | $file_contents = file_get_contents(dirname($file) . '/' . $fileinfo[1]); |
||
883 | $fileinfo[1] = basename($fileinfo[1]); |
||
884 | $request .= "Content-Disposition: form-data; name=\"$fileinfo[0]\"; filename=\"$fileinfo[1]\"\n"; |
||
885 | $request .= "Content-Type: text/plain\n\n"; |
||
886 | $request .= $file_contents . "\n" . |
||
887 | "-----------------------------20896060251896012921717172737\n"; |
||
888 | } |
||
889 | |||
890 | if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) { |
||
891 | // encode POST raw |
||
892 | $post = trim($section_text['POST']); |
||
893 | $post = explode('&', $post); |
||
894 | foreach ($post as $i => $post_info) { |
||
895 | $post_info = explode('=', $post_info); |
||
896 | if (count($post_info) != 2) { |
||
897 | return PEAR::raiseError("Invalid POST data in test file: $file"); |
||
898 | } |
||
899 | $post_info[0] = rawurldecode($post_info[0]); |
||
900 | $post_info[1] = rawurldecode($post_info[1]); |
||
901 | $post[$i] = $post_info; |
||
902 | } |
||
903 | foreach ($post as $post_info) { |
||
904 | $request .= "Content-Disposition: form-data; name=\"$post_info[0]\"\n\n"; |
||
905 | $request .= $post_info[1] . "\n" . |
||
906 | "-----------------------------20896060251896012921717172737\n"; |
||
907 | } |
||
908 | unset($section_text['POST']); |
||
909 | } |
||
910 | $section_text['POST_RAW'] = $request; |
||
911 | } |
||
912 | |||
913 | return $section_text; |
||
914 | } |
||
915 | |||
916 | function _testCleanup($section_text, $temp_clean) |
||
917 | { |
||
918 | if ($section_text['CLEAN']) { |
||
919 | // perform test cleanup |
||
920 | $this->save_text($temp_clean, $section_text['CLEAN']); |
||
921 | $this->system_with_timeout("$this->_php $temp_clean"); |
||
922 | if (file_exists($temp_clean)) { |
||
923 | unlink($temp_clean); |
||
924 | } |
||
925 | } |
||
926 | } |
||
927 | } |