PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/include/PEAR/RunTest.php

https://github.com/radicaldesigns/amp
PHP | 326 lines | 219 code | 37 blank | 70 comment | 38 complexity | a6bde47d4d46f530fab46e02987c9150 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Tomas V.V.Cox <cox@idecnet.com> |
  17. // | Greg Beaver <cellog@php.net> |
  18. // | |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: RunTest.php,v 1.3.2.2 2004/11/09 19:55:49 cellog Exp $
  22. //
  23. /**
  24. * Simplified version of PHP's test suite
  25. * -- EXPERIMENTAL --
  26. Try it with:
  27. $ php -r 'include "../PEAR/RunTest.php"; $t=new PEAR_RunTest; $o=$t->run("./pear_system.phpt");print_r($o);'
  28. TODO:
  29. Actually finish the development and testing
  30. */
  31. require_once 'PEAR.php';
  32. require_once 'PEAR/Config.php';
  33. define('DETAILED', 1);
  34. putenv("PHP_PEAR_RUNTESTS=1");
  35. class PEAR_RunTest
  36. {
  37. var $_logger;
  38. /**
  39. * An object that supports the PEAR_Common->log() signature, or null
  40. * @param PEAR_Common|null
  41. */
  42. function PEAR_RunTest($logger = null)
  43. {
  44. $this->_logger = $logger;
  45. }
  46. //
  47. // Run an individual test case.
  48. //
  49. function run($file)
  50. {
  51. $cwd = getcwd();
  52. $conf = &PEAR_Config::singleton();
  53. $php = $conf->get('php_bin');
  54. //var_dump($php);exit;
  55. global $log_format, $info_params, $ini_overwrites;
  56. $info_params = '';
  57. $log_format = 'LEOD';
  58. // Load the sections of the test file.
  59. $section_text = array(
  60. 'TEST' => '(unnamed test)',
  61. 'SKIPIF' => '',
  62. 'GET' => '',
  63. 'ARGS' => '',
  64. );
  65. if (!is_file($file) || !$fp = fopen($file, "r")) {
  66. return PEAR::raiseError("Cannot open test file: $file");
  67. }
  68. $section = '';
  69. while (!feof($fp)) {
  70. $line = fgets($fp);
  71. // Match the beginning of a section.
  72. if (ereg('^--([A-Z]+)--',$line,$r)) {
  73. $section = $r[1];
  74. $section_text[$section] = '';
  75. continue;
  76. }
  77. // Add to the section text.
  78. $section_text[$section] .= $line;
  79. }
  80. fclose($fp);
  81. $shortname = str_replace($cwd.'/', '', $file);
  82. $tested = trim($section_text['TEST'])." [$shortname]";
  83. $tmp = realpath(dirname($file));
  84. $tmp_skipif = $tmp . uniqid('/phpt.');
  85. $tmp_file = ereg_replace('\.phpt$','.php',$file);
  86. $tmp_post = $tmp . uniqid('/phpt.');
  87. @unlink($tmp_skipif);
  88. @unlink($tmp_file);
  89. @unlink($tmp_post);
  90. // unlink old test results
  91. @unlink(ereg_replace('\.phpt$','.diff',$file));
  92. @unlink(ereg_replace('\.phpt$','.log',$file));
  93. @unlink(ereg_replace('\.phpt$','.exp',$file));
  94. @unlink(ereg_replace('\.phpt$','.out',$file));
  95. // Check if test should be skipped.
  96. $info = '';
  97. $warn = false;
  98. if (array_key_exists('SKIPIF', $section_text)) {
  99. if (trim($section_text['SKIPIF'])) {
  100. $this->save_text($tmp_skipif, $section_text['SKIPIF']);
  101. //$extra = substr(PHP_OS, 0, 3) !== "WIN" ?
  102. // "unset REQUEST_METHOD;": "";
  103. //$output = `$extra $php $info_params -f $tmp_skipif`;
  104. $output = `$php $info_params -f $tmp_skipif`;
  105. unlink($tmp_skipif);
  106. if (eregi("^skip", trim($output))) {
  107. $skipreason = "SKIP $tested";
  108. $reason = (eregi("^skip[[:space:]]*(.+)\$", trim($output))) ? eregi_replace("^skip[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
  109. if ($reason) {
  110. $skipreason .= " (reason: $reason)";
  111. }
  112. $this->_logger->log(0, $skipreason);
  113. if (isset($old_php)) {
  114. $php = $old_php;
  115. }
  116. return 'SKIPPED';
  117. }
  118. if (eregi("^info", trim($output))) {
  119. $reason = (ereg("^info[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^info[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
  120. if ($reason) {
  121. $info = " (info: $reason)";
  122. }
  123. }
  124. if (eregi("^warn", trim($output))) {
  125. $reason = (ereg("^warn[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^warn[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
  126. if ($reason) {
  127. $warn = true; /* only if there is a reason */
  128. $info = " (warn: $reason)";
  129. }
  130. }
  131. }
  132. }
  133. // We've satisfied the preconditions - run the test!
  134. $this->save_text($tmp_file,$section_text['FILE']);
  135. $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : '';
  136. $ini_settings='';
  137. $cmd = "$php$ini_settings -f $tmp_file$args 2>&1";
  138. if (isset($this->_logger)) {
  139. $this->_logger->log(2, 'Running command "' . $cmd . '"');
  140. }
  141. $out = `$cmd`;
  142. @unlink($tmp_post);
  143. // Does the output match what is expected?
  144. $output = trim($out);
  145. $output = preg_replace('/\r\n/', "\n", $output);
  146. if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) {
  147. if (isset($section_text['EXPECTF'])) {
  148. $wanted = trim($section_text['EXPECTF']);
  149. } else {
  150. $wanted = trim($section_text['EXPECTREGEX']);
  151. }
  152. $wanted_re = preg_replace('/\r\n/',"\n",$wanted);
  153. if (isset($section_text['EXPECTF'])) {
  154. $wanted_re = preg_quote($wanted_re, '/');
  155. // Stick to basics
  156. $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy
  157. $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re);
  158. $wanted_re = str_replace("%d", "[0-9]+", $wanted_re);
  159. $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re);
  160. $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?", $wanted_re);
  161. $wanted_re = str_replace("%c", ".", $wanted_re);
  162. // %f allows two points "-.0.0" but that is the best *simple* expression
  163. }
  164. /* DEBUG YOUR REGEX HERE
  165. var_dump($wanted_re);
  166. print(str_repeat('=', 80) . "\n");
  167. var_dump($output);
  168. */
  169. if (preg_match("/^$wanted_re\$/s", $output)) {
  170. @unlink($tmp_file);
  171. $this->_logger->log(0, "PASS $tested$info");
  172. if (isset($old_php)) {
  173. $php = $old_php;
  174. }
  175. return 'PASSED';
  176. }
  177. } else {
  178. $wanted = trim($section_text['EXPECT']);
  179. $wanted = preg_replace('/\r\n/',"\n",$wanted);
  180. // compare and leave on success
  181. $ok = (0 == strcmp($output,$wanted));
  182. if ($ok) {
  183. @unlink($tmp_file);
  184. $this->_logger->log(0, "PASS $tested$info");
  185. if (isset($old_php)) {
  186. $php = $old_php;
  187. }
  188. return 'PASSED';
  189. }
  190. }
  191. // Test failed so we need to report details.
  192. if ($warn) {
  193. $this->_logger->log(0, "WARN $tested$info");
  194. } else {
  195. $this->_logger->log(0, "FAIL $tested$info");
  196. }
  197. $GLOBALS['__PHP_FAILED_TESTS__'][] = array(
  198. 'name' => $file,
  199. 'test_name' => $tested,
  200. 'output' => ereg_replace('\.phpt$','.log', $file),
  201. 'diff' => ereg_replace('\.phpt$','.diff', $file),
  202. 'info' => $info
  203. );
  204. // write .exp
  205. if (strpos($log_format,'E') !== FALSE) {
  206. $logname = ereg_replace('\.phpt$','.exp',$file);
  207. if (!$log = fopen($logname,'w')) {
  208. return PEAR::raiseError("Cannot create test log - $logname");
  209. }
  210. fwrite($log,$wanted);
  211. fclose($log);
  212. }
  213. // write .out
  214. if (strpos($log_format,'O') !== FALSE) {
  215. $logname = ereg_replace('\.phpt$','.out',$file);
  216. if (!$log = fopen($logname,'w')) {
  217. return PEAR::raiseError("Cannot create test log - $logname");
  218. }
  219. fwrite($log,$output);
  220. fclose($log);
  221. }
  222. // write .diff
  223. if (strpos($log_format,'D') !== FALSE) {
  224. $logname = ereg_replace('\.phpt$','.diff',$file);
  225. if (!$log = fopen($logname,'w')) {
  226. return PEAR::raiseError("Cannot create test log - $logname");
  227. }
  228. fwrite($log, $this->generate_diff($wanted,$output));
  229. fclose($log);
  230. }
  231. // write .log
  232. if (strpos($log_format,'L') !== FALSE) {
  233. $logname = ereg_replace('\.phpt$','.log',$file);
  234. if (!$log = fopen($logname,'w')) {
  235. return PEAR::raiseError("Cannot create test log - $logname");
  236. }
  237. fwrite($log,"
  238. ---- EXPECTED OUTPUT
  239. $wanted
  240. ---- ACTUAL OUTPUT
  241. $output
  242. ---- FAILED
  243. ");
  244. fclose($log);
  245. //error_report($file,$logname,$tested);
  246. }
  247. if (isset($old_php)) {
  248. $php = $old_php;
  249. }
  250. return $warn ? 'WARNED' : 'FAILED';
  251. }
  252. function generate_diff($wanted,$output)
  253. {
  254. $w = explode("\n", $wanted);
  255. $o = explode("\n", $output);
  256. $w1 = array_diff_assoc($w,$o);
  257. $o1 = array_diff_assoc($o,$w);
  258. $w2 = array();
  259. $o2 = array();
  260. foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1).$val;
  261. foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1).$val;
  262. $diff = array_merge($w2, $o2);
  263. ksort($diff);
  264. return implode("\r\n", $diff);
  265. }
  266. //
  267. // Write the given text to a temporary file, and return the filename.
  268. //
  269. function save_text($filename, $text)
  270. {
  271. if (!$fp = fopen($filename, 'w')) {
  272. return PEAR::raiseError("Cannot open file '" . $filename . "' (save_text)");
  273. }
  274. fwrite($fp,$text);
  275. fclose($fp);
  276. if (1 < DETAILED) echo "
  277. FILE $filename {{{
  278. $text
  279. }}}
  280. ";
  281. }
  282. }
  283. ?>