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.3 2004/12/31 18:57:12 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$ini_settings '')
  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.         $cmd = "$php$ini_settings -f $tmp_file$args 2>&1";
  165.         if (isset($this->_logger)) {
  166.             $this->_logger->log(2'Running command "' $cmd '"');
  167.         }
  168.         $out = `$cmd`;
  169.         @unlink($tmp_post);
  170.  
  171.         // Does the output match what is expected?
  172.         $output trim($out);
  173.         $output preg_replace('/\r\n/'"\n"$output);
  174.  
  175.         if (isset($section_text['EXPECTF']|| isset($section_text['EXPECTREGEX'])) {
  176.             if (isset($section_text['EXPECTF'])) {
  177.                 $wanted trim($section_text['EXPECTF']);
  178.             else {
  179.                 $wanted trim($section_text['EXPECTREGEX']);
  180.             }
  181.             $wanted_re preg_replace('/\r\n/',"\n",$wanted);
  182.             if (isset($section_text['EXPECTF'])) {
  183.                 $wanted_re preg_quote($wanted_re'/');
  184.                 // Stick to basics
  185.                 $wanted_re str_replace("%s"".+?"$wanted_re)//not greedy
  186.                 $wanted_re str_replace("%i""[+\-]?[0-9]+"$wanted_re);
  187.                 $wanted_re str_replace("%d""[0-9]+"$wanted_re);
  188.                 $wanted_re str_replace("%x""[0-9a-fA-F]+"$wanted_re);
  189.                 $wanted_re str_replace("%f""[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?"$wanted_re);
  190.                 $wanted_re str_replace("%c""."$wanted_re);
  191.                 // %f allows two points "-.0.0" but that is the best *simple* expression
  192.             }
  193.     /* DEBUG YOUR REGEX HERE
  194.             var_dump($wanted_re);
  195.             print(str_repeat('=', 80) . "\n");
  196.             var_dump($output);
  197.     */
  198.             if (preg_match("/^$wanted_re\$/s"$output)) {
  199.                 @unlink($tmp_file);
  200.                 $this->_logger->log(0"PASS $tested$info");
  201.                 if (isset($old_php)) {
  202.                     $php $old_php;
  203.                 }
  204.                 return 'PASSED';
  205.             }
  206.  
  207.         else {
  208.             $wanted trim($section_text['EXPECT']);
  209.             $wanted preg_replace('/\r\n/',"\n",$wanted);
  210.         // compare and leave on success
  211.             $ok (0 == strcmp($output,$wanted));
  212.             if ($ok{
  213.                 @unlink($tmp_file);
  214.                 $this->_logger->log(0"PASS $tested$info");
  215.                 if (isset($old_php)) {
  216.                     $php $old_php;
  217.                 }
  218.                 return 'PASSED';
  219.             }
  220.         }
  221.  
  222.         // Test failed so we need to report details.
  223.         if ($warn{
  224.             $this->_logger->log(0"WARN $tested$info");
  225.         else {
  226.             $this->_logger->log(0"FAIL $tested$info");
  227.         }
  228.  
  229.         $GLOBALS['__PHP_FAILED_TESTS__'][= array(
  230.                             'name' => $file,
  231.                             'test_name' => $tested,
  232.                             'output' => ereg_replace('\.phpt$','.log'$file),
  233.                             'diff'   => ereg_replace('\.phpt$','.diff'$file),
  234.                             'info'   => $info
  235.                             );
  236.  
  237.         // write .exp
  238.         if (strpos($log_format,'E'!== FALSE{
  239.             $logname ereg_replace('\.phpt$','.exp',$file);
  240.             if (!$log fopen($logname,'w')) {
  241.                 return PEAR::raiseError("Cannot create test log - $logname");
  242.             }
  243.             fwrite($log,$wanted);
  244.             fclose($log);
  245.         }
  246.  
  247.         // write .out
  248.         if (strpos($log_format,'O'!== FALSE{
  249.             $logname ereg_replace('\.phpt$','.out',$file);
  250.             if (!$log fopen($logname,'w')) {
  251.                 return PEAR::raiseError("Cannot create test log - $logname");
  252.             }
  253.             fwrite($log,$output);
  254.             fclose($log);
  255.         }
  256.  
  257.         // write .diff
  258.         if (strpos($log_format,'D'!== FALSE{
  259.             $logname ereg_replace('\.phpt$','.diff',$file);
  260.             if (!$log fopen($logname,'w')) {
  261.                 return PEAR::raiseError("Cannot create test log - $logname");
  262.             }
  263.             fwrite($log$this->generate_diff($wanted,$output));
  264.             fclose($log);
  265.         }
  266.  
  267.         // write .log
  268.         if (strpos($log_format,'L'!== FALSE{
  269.             $logname ereg_replace('\.phpt$','.log',$file);
  270.             if (!$log fopen($logname,'w')) {
  271.                 return PEAR::raiseError("Cannot create test log - $logname");
  272.             }
  273.             fwrite($log,"
  274. ---- EXPECTED OUTPUT
  275. $wanted
  276. ---- ACTUAL OUTPUT
  277. $output
  278. ---- FAILED
  279. ");
  280.             fclose($log);
  281.             //error_report($file,$logname,$tested);
  282.         }
  283.  
  284.         if (isset($old_php)) {
  285.             $php $old_php;
  286.         }
  287.  
  288.         return $warn 'WARNED' 'FAILED';
  289.     }
  290.  
  291.     function generate_diff($wanted,$output)
  292.     {
  293.         $w explode("\n"$wanted);
  294.         $o explode("\n"$output);
  295.         $w1 array_diff_assoc($w,$o);
  296.         $o1 array_diff_assoc($o,$w);
  297.         $w2 = array();
  298.         $o2 = array();
  299.         foreach($w1 as $idx => $val$w2[sprintf("%03d<",$idx)sprintf("%03d- "$idx+1).$val;
  300.         foreach($o1 as $idx => $val$o2[sprintf("%03d>",$idx)sprintf("%03d+ "$idx+1).$val;
  301.         $diff array_merge($w2$o2);
  302.         ksort($diff);
  303.         return implode("\r\n"$diff);
  304.     }
  305.  
  306.     //
  307.     //  Write the given text to a temporary file, and return the filename.
  308.     //
  309.  
  310.     function save_text($filename$text)
  311.     {
  312.         if (!$fp fopen($filename'w')) {
  313.             return PEAR::raiseError("Cannot open file '" $filename "' (save_text)");
  314.         }
  315.         fwrite($fp,$text);
  316.         fclose($fp);
  317.     if (1 < DETAILEDecho "
  318. FILE $filename {{{
  319. $text
  320. }}}
  321. ";
  322.     }
  323.  
  324. }
  325. ?>

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