PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file RunTest.php

Documentation is available at RunTest.php

  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. /**
  25.  * Simplified version of PHP's test suite
  26.  * -- EXPERIMENTAL --
  27.  
  28.  Try it with:
  29.  
  30.  $ php -r 'include "../PEAR/RunTest.php"; $t=new PEAR_RunTest; $o=$t->run("./pear_system.phpt");print_r($o);'
  31.  
  32.  
  33. TODO:
  34.  
  35. Actually finish the development and testing
  36.  
  37.  */
  38.  
  39. require_once 'PEAR.php';
  40. require_once 'PEAR/Config.php';
  41.  
  42. define('DETAILED'1);
  43. putenv("PHP_PEAR_RUNTESTS=1");
  44.  
  45. class PEAR_RunTest
  46. {
  47.     var $_logger;
  48.  
  49.     /**
  50.      * An object that supports the PEAR_Common->log() signature, or null
  51.      * @param PEAR_Common|null
  52.      */
  53.     function PEAR_RunTest($logger = null)
  54.     {
  55.         $this->_logger $logger;
  56.     }
  57.  
  58.     //
  59.     //  Run an individual test case.
  60.     //
  61.  
  62.     function run($file)
  63.     {
  64.         $cwd getcwd();
  65.         $conf &PEAR_Config::singleton();
  66.         $php $conf->get('php_bin');
  67.         //var_dump($php);exit;
  68.         global $log_format$info_params$ini_overwrites;
  69.  
  70.         $info_params '';
  71.         $log_format 'LEOD';
  72.  
  73.         // Load the sections of the test file.
  74.         $section_text = array(
  75.             'TEST'   => '(unnamed test)',
  76.             'SKIPIF' => '',
  77.             'GET'    => '',
  78.             'ARGS'   => '',
  79.         );
  80.  
  81.         if (!is_file($file|| !$fp fopen($file"r")) {
  82.             return PEAR::raiseError("Cannot open test file: $file");
  83.         }
  84.  
  85.         $section '';
  86.         while (!feof($fp)) {
  87.             $line fgets($fp);
  88.  
  89.             // Match the beginning of a section.
  90.             if (ereg('^--([A-Z]+)--',$line,$r)) {
  91.                 $section $r[1];
  92.                 $section_text[$section'';
  93.                 continue;
  94.             }
  95.  
  96.             // Add to the section text.
  97.             $section_text[$section.= $line;
  98.         }
  99.         fclose($fp);
  100.  
  101.         $shortname str_replace($cwd.'/'''$file);
  102.         $tested trim($section_text['TEST'])." [$shortname]";
  103.  
  104.         $tmp realpath(dirname($file));
  105.         $tmp_skipif $tmp uniqid('/phpt.');
  106.         $tmp_file   ereg_replace('\.phpt$','.php',$file);
  107.         $tmp_post   $tmp uniqid('/phpt.');
  108.  
  109.         @unlink($tmp_skipif);
  110.         @unlink($tmp_file);
  111.         @unlink($tmp_post);
  112.  
  113.         // unlink old test results
  114.         @unlink(ereg_replace('\.phpt$','.diff',$file));
  115.         @unlink(ereg_replace('\.phpt$','.log',$file));
  116.         @unlink(ereg_replace('\.phpt$','.exp',$file));
  117.         @unlink(ereg_replace('\.phpt$','.out',$file));
  118.  
  119.         // Check if test should be skipped.
  120.         $info '';
  121.         $warn = false;
  122.         if (array_key_exists('SKIPIF'$section_text)) {
  123.             if (trim($section_text['SKIPIF'])) {
  124.                 $this->save_text($tmp_skipif$section_text['SKIPIF']);
  125.                 //$extra = substr(PHP_OS, 0, 3) !== "WIN" ?
  126.                 //    "unset REQUEST_METHOD;": "";
  127.  
  128.                 //$output = `$extra $php $info_params -f $tmp_skipif`;
  129.                 $output = `$php $info_params -f $tmp_skipif`;
  130.                 unlink($tmp_skipif);
  131.                 if (eregi("^skip"trim($output))) {
  132.                     $skipreason = "SKIP $tested";
  133.                     $reason (eregi("^skip[[:space:]]*(.+)\$"trim($output))) eregi_replace("^skip[[:space:]]*(.+)\$""\\1"trim($output)) : FALSE;
  134.                     if ($reason{
  135.                         $skipreason .= " (reason: $reason)";
  136.                     }
  137.                     $this->_logger->log(0$skipreason);
  138.                     if (isset($old_php)) {
  139.                         $php $old_php;
  140.                     }
  141.                     return 'SKIPPED';
  142.                 }
  143.                 if (eregi("^info"trim($output))) {
  144.                     $reason (ereg("^info[[:space:]]*(.+)\$"trim($output))) ereg_replace("^info[[:space:]]*(.+)\$""\\1"trim($output)) : FALSE;
  145.                     if ($reason{
  146.                         $info = " (info: $reason)";
  147.                     }
  148.                 }
  149.                 if (eregi("^warn"trim($output))) {
  150.                     $reason (ereg("^warn[[:space:]]*(.+)\$"trim($output))) ereg_replace("^warn[[:space:]]*(.+)\$""\\1"trim($output)) : FALSE;
  151.                     if ($reason{
  152.                         $warn = true; /* only if there is a reason */
  153.                         $info = " (warn: $reason)";
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.  
  159.         // We've satisfied the preconditions - run the test!
  160.         $this->save_text($tmp_file,$section_text['FILE']);
  161.  
  162.         $args $section_text['ARGS'' -- '.$section_text['ARGS''';
  163.  
  164.         $ini_settings='';
  165.         $cmd = "$php$ini_settings -f $tmp_file$args 2>&1";
  166.         if (isset($this->_logger)) {
  167.             $this->_logger->log(2'Running command "' $cmd '"');
  168.         }
  169.         $out = `$cmd`;
  170.         @unlink($tmp_post);
  171.  
  172.         // Does the output match what is expected?
  173.         $output trim($out);
  174.         $output preg_replace('/\r\n/'"\n"$output);
  175.  
  176.         if (isset($section_text['EXPECTF']|| isset($section_text['EXPECTREGEX'])) {
  177.             if (isset($section_text['EXPECTF'])) {
  178.                 $wanted trim($section_text['EXPECTF']);
  179.             else {
  180.                 $wanted trim($section_text['EXPECTREGEX']);
  181.             }
  182.             $wanted_re preg_replace('/\r\n/',"\n",$wanted);
  183.             if (isset($section_text['EXPECTF'])) {
  184.                 $wanted_re preg_quote($wanted_re'/');
  185.                 // Stick to basics
  186.                 $wanted_re str_replace("%s"".+?"$wanted_re)//not greedy
  187.                 $wanted_re str_replace("%i""[+\-]?[0-9]+"$wanted_re);
  188.                 $wanted_re str_replace("%d""[0-9]+"$wanted_re);
  189.                 $wanted_re str_replace("%x""[0-9a-fA-F]+"$wanted_re);
  190.                 $wanted_re str_replace("%f""[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?"$wanted_re);
  191.                 $wanted_re str_replace("%c""."$wanted_re);
  192.                 // %f allows two points "-.0.0" but that is the best *simple* expression
  193.             }
  194.     /* DEBUG YOUR REGEX HERE
  195.             var_dump($wanted_re);
  196.             print(str_repeat('=', 80) . "\n");
  197.             var_dump($output);
  198.     */
  199.             if (preg_match("/^$wanted_re\$/s"$output)) {
  200.                 @unlink($tmp_file);
  201.                 $this->_logger->log(0"PASS $tested$info");
  202.                 if (isset($old_php)) {
  203.                     $php $old_php;
  204.                 }
  205.                 return 'PASSED';
  206.             }
  207.  
  208.         else {
  209.             $wanted trim($section_text['EXPECT']);
  210.             $wanted preg_replace('/\r\n/',"\n",$wanted);
  211.         // compare and leave on success
  212.             $ok (0 == strcmp($output,$wanted));
  213.             if ($ok{
  214.                 @unlink($tmp_file);
  215.                 $this->_logger->log(0"PASS $tested$info");
  216.                 if (isset($old_php)) {
  217.                     $php $old_php;
  218.                 }
  219.                 return 'PASSED';
  220.             }
  221.         }
  222.  
  223.         // Test failed so we need to report details.
  224.         if ($warn{
  225.             $this->_logger->log(0"WARN $tested$info");
  226.         else {
  227.             $this->_logger->log(0"FAIL $tested$info");
  228.         }
  229.  
  230.         $GLOBALS['__PHP_FAILED_TESTS__'][= array(
  231.                             'name' => $file,
  232.                             'test_name' => $tested,
  233.                             'output' => ereg_replace('\.phpt$','.log'$file),
  234.                             'diff'   => ereg_replace('\.phpt$','.diff'$file),
  235.                             'info'   => $info
  236.                             );
  237.  
  238.         // write .exp
  239.         if (strpos($log_format,'E'!== FALSE{
  240.             $logname ereg_replace('\.phpt$','.exp',$file);
  241.             if (!$log fopen($logname,'w')) {
  242.                 return PEAR::raiseError("Cannot create test log - $logname");
  243.             }
  244.             fwrite($log,$wanted);
  245.             fclose($log);
  246.         }
  247.  
  248.         // write .out
  249.         if (strpos($log_format,'O'!== FALSE{
  250.             $logname ereg_replace('\.phpt$','.out',$file);
  251.             if (!$log fopen($logname,'w')) {
  252.                 return PEAR::raiseError("Cannot create test log - $logname");
  253.             }
  254.             fwrite($log,$output);
  255.             fclose($log);
  256.         }
  257.  
  258.         // write .diff
  259.         if (strpos($log_format,'D'!== FALSE{
  260.             $logname ereg_replace('\.phpt$','.diff',$file);
  261.             if (!$log fopen($logname,'w')) {
  262.                 return PEAR::raiseError("Cannot create test log - $logname");
  263.             }
  264.             fwrite($log$this->generate_diff($wanted,$output));
  265.             fclose($log);
  266.         }
  267.  
  268.         // write .log
  269.         if (strpos($log_format,'L'!== FALSE{
  270.             $logname ereg_replace('\.phpt$','.log',$file);
  271.             if (!$log fopen($logname,'w')) {
  272.                 return PEAR::raiseError("Cannot create test log - $logname");
  273.             }
  274.             fwrite($log,"
  275. ---- EXPECTED OUTPUT
  276. $wanted
  277. ---- ACTUAL OUTPUT
  278. $output
  279. ---- FAILED
  280. ");
  281.             fclose($log);
  282.             //error_report($file,$logname,$tested);
  283.         }
  284.  
  285.         if (isset($old_php)) {
  286.             $php $old_php;
  287.         }
  288.  
  289.         return $warn 'WARNED' 'FAILED';
  290.     }
  291.  
  292.     function generate_diff($wanted,$output)
  293.     {
  294.         $w explode("\n"$wanted);
  295.         $o explode("\n"$output);
  296.         $w1 array_diff_assoc($w,$o);
  297.         $o1 array_diff_assoc($o,$w);
  298.         $w2 = array();
  299.         $o2 = array();
  300.         foreach($w1 as $idx => $val$w2[sprintf("%03d<",$idx)sprintf("%03d- "$idx+1).$val;
  301.         foreach($o1 as $idx => $val$o2[sprintf("%03d>",$idx)sprintf("%03d+ "$idx+1).$val;
  302.         $diff array_merge($w2$o2);
  303.         ksort($diff);
  304.         return implode("\r\n"$diff);
  305.     }
  306.  
  307.     //
  308.     //  Write the given text to a temporary file, and return the filename.
  309.     //
  310.  
  311.     function save_text($filename$text)
  312.     {
  313.         if (!$fp fopen($filename'w')) {
  314.             return PEAR::raiseError("Cannot open file '" $filename "' (save_text)");
  315.         }
  316.         fwrite($fp,$text);
  317.         fclose($fp);
  318.     if (1 < DETAILEDecho "
  319. FILE $filename {{{
  320. $text
  321. }}}
  322. ";
  323.     }
  324.  
  325. }
  326. ?>

Documentation generated on Mon, 11 Mar 2019 13:58:41 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.