Bearsampp 2026.9.23
Loading...
Searching...
No Matches
RunTest.php File Reference

Go to the source code of this file.

Data Structures

class  PEAR_RunTest

Namespaces

namespace  PEAR

Functions

 _cleanupOldFiles ($file)
 _processHeaders ($text)
 _processUpload ($section_text, $file)
 _readFile ($file)
 _resetEnv ($section_text, $temp_file)
 _restorePHPBinary ()
 _runSkipIf ($section_text, $temp_skipif, $tested, $ini_settings)
 _savePHPBinary ()
 _stripHeadersCGI ($output)
 _testCleanup ($section_text, $temp_clean)
 _writeLog ($logname, $data)
 generate_diff ($wanted, $output, $rvalue, $wanted_re)
 save_text ($filename, $text)

Variables

const DETAILED 1

Function Documentation

◆ _cleanupOldFiles()

_cleanupOldFiles ( $file)

Definition at line 715 of file RunTest.php.

716 {
717 $temp_dir = realpath(dirname($file));
718 $mainFileName = basename($file, 'phpt');
719 $diff_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'diff';
720 $log_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'log';
721 $exp_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'exp';
722 $output_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'out';
723 $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'mem';
724 $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'php';
725 $temp_skipif = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'skip.php';
726 $temp_clean = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'clean.php';
727 $tmp_post = $temp_dir . DIRECTORY_SEPARATOR . uniqid('phpt.');
728
729 // unlink old test results
730 @unlink($diff_filename);
731 @unlink($log_filename);
732 @unlink($exp_filename);
733 @unlink($output_filename);
734 @unlink($memcheck_filename);
735 @unlink($temp_file);
736 @unlink($temp_skipif);
737 @unlink($tmp_post);
738 @unlink($temp_clean);
739 }

Referenced by PEAR_RunTest\run().

Here is the caller graph for this function:

◆ _processHeaders()

_processHeaders ( $text)

Return an array that can be used with array_diff() to compare headers

Parameters
string$text

Definition at line 798 of file RunTest.php.

799 {
800 $headers = array();
801 $rh = preg_split("/[\n\r]+/", $text);
802 foreach ($rh as $line) {
803 if (strpos($line, ':')!== false) {
804 $line = explode(':', $line, 2);
805 $headers[trim($line[0])] = trim($line[1]);
806 }
807 }
808 return $headers;
809 }

Referenced by _stripHeadersCGI(), and PEAR_RunTest\run().

Here is the caller graph for this function:

◆ _processUpload()

_processUpload ( $section_text,
$file )

Definition at line 898 of file RunTest.php.

899 {
900 if (array_key_exists('UPLOAD', $section_text) && !empty($section_text['UPLOAD'])) {
901 $upload_files = trim($section_text['UPLOAD']);
902 $upload_files = explode("\n", $upload_files);
903
904 $request = "Content-Type: multipart/form-data; boundary=---------------------------20896060251896012921717172737\n" .
905 "-----------------------------20896060251896012921717172737\n";
906 foreach ($upload_files as $fileinfo) {
907 $fileinfo = explode('=', $fileinfo);
908 if (count($fileinfo) != 2) {
909 return PEAR::raiseError("Invalid UPLOAD section in test file: $file");
910 }
911 if (!realpath(dirname($file) . '/' . $fileinfo[1])) {
912 return PEAR::raiseError("File for upload does not exist: $fileinfo[1] " .
913 "in test file: $file");
914 }
915 $file_contents = file_get_contents(dirname($file) . '/' . $fileinfo[1]);
916 $fileinfo[1] = basename($fileinfo[1]);
917 $request .= "Content-Disposition: form-data; name=\"$fileinfo[0]\"; filename=\"$fileinfo[1]\"\n";
918 $request .= "Content-Type: text/plain\n\n";
919 $request .= $file_contents . "\n" .
920 "-----------------------------20896060251896012921717172737\n";
921 }
922
923 if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) {
924 // encode POST raw
925 $post = trim($section_text['POST']);
926 $post = explode('&', $post);
927 foreach ($post as $i => $post_info) {
928 $post_info = explode('=', $post_info);
929 if (count($post_info) != 2) {
930 return PEAR::raiseError("Invalid POST data in test file: $file");
931 }
932 $post_info[0] = rawurldecode($post_info[0]);
933 $post_info[1] = rawurldecode($post_info[1]);
934 $post[$i] = $post_info;
935 }
936 foreach ($post as $post_info) {
937 $request .= "Content-Disposition: form-data; name=\"$post_info[0]\"\n\n";
938 $request .= $post_info[1] . "\n" .
939 "-----------------------------20896060251896012921717172737\n";
940 }
941 unset($section_text['POST']);
942 }
943 $section_text['POST_RAW'] = $request;
944 }
945
946 return $section_text;
947 }

Referenced by PEAR_RunTest\run().

Here is the caller graph for this function:

◆ _readFile()

_readFile ( $file)

Definition at line 811 of file RunTest.php.

812 {
813 // Load the sections of the test file.
814 $section_text = array(
815 'TEST' => '(unnamed test)',
816 'SKIPIF' => '',
817 'GET' => '',
818 'COOKIE' => '',
819 'POST' => '',
820 'ARGS' => '',
821 'INI' => '',
822 'CLEAN' => '',
823 );
824
825 if (!is_file($file) || !$fp = fopen($file, "r")) {
826 return PEAR::raiseError("Cannot open test file: $file");
827 }
828
829 $section = '';
830 while (!feof($fp)) {
831 $line = fgets($fp);
832
833 // Match the beginning of a section.
834 if (preg_match('/^--([_A-Z]+)--/', $line, $r)) {
835 $section = $r[1];
836 $section_text[$section] = '';
837 continue;
838 } elseif (empty($section)) {
839 fclose($fp);
840 return PEAR::raiseError("Invalid sections formats in test file: $file");
841 }
842
843 // Add to the section text.
844 $section_text[$section] .= $line;
845 }
846 fclose($fp);
847
848 return $section_text;
849 }

Referenced by PEAR_RunTest\run().

Here is the caller graph for this function:

◆ _resetEnv()

_resetEnv ( $section_text,
$temp_file )

Definition at line 860 of file RunTest.php.

861 {
862 $env = $_ENV;
863 $env['REDIRECT_STATUS'] = '';
864 $env['QUERY_STRING'] = '';
865 $env['PATH_TRANSLATED'] = '';
866 $env['SCRIPT_FILENAME'] = '';
867 $env['REQUEST_METHOD'] = '';
868 $env['CONTENT_TYPE'] = '';
869 $env['CONTENT_LENGTH'] = '';
870 if (!empty($section_text['ENV'])) {
871 if (strpos($section_text['ENV'], '{PWD}') !== false) {
872 $section_text['ENV'] = str_replace('{PWD}', dirname($temp_file), $section_text['ENV']);
873 }
874 foreach (explode("\n", trim($section_text['ENV'])) as $e) {
875 $e = explode('=', trim($e), 2);
876 if (!empty($e[0]) && isset($e[1])) {
877 $env[$e[0]] = $e[1];
878 }
879 }
880 }
881 if (array_key_exists('GET', $section_text)) {
882 $env['QUERY_STRING'] = trim($section_text['GET']);
883 } else {
884 $env['QUERY_STRING'] = '';
885 }
886 if (array_key_exists('COOKIE', $section_text)) {
887 $env['HTTP_COOKIE'] = trim($section_text['COOKIE']);
888 } else {
889 $env['HTTP_COOKIE'] = '';
890 }
891 $env['REDIRECT_STATUS'] = '1';
892 $env['PATH_TRANSLATED'] = $temp_file;
893 $env['SCRIPT_FILENAME'] = $temp_file;
894
895 return $env;
896 }

Referenced by PEAR_RunTest\run().

Here is the caller graph for this function:

◆ _restorePHPBinary()

_restorePHPBinary ( )

Definition at line 971 of file RunTest.php.

972 {
973 if (isset($this->_savephp))
974 {
975 $this->_php = $this->_savephp;
976 unset($this->_savephp);
977 }
978 }

References PEAR_RunTest\$_savephp.

Referenced by _testCleanup(), and PEAR_RunTest\run().

Here is the caller graph for this function:

◆ _runSkipIf()

_runSkipIf ( $section_text,
$temp_skipif,
$tested,
$ini_settings )

Definition at line 741 of file RunTest.php.

742 {
743 $info = '';
744 $warn = false;
745 if (array_key_exists('SKIPIF', $section_text) && trim($section_text['SKIPIF'])) {
746 $this->save_text($temp_skipif, $section_text['SKIPIF']);
747 $output = $this->system_with_timeout("$this->_php$ini_settings -f \"$temp_skipif\"");
748 $output = $output[1];
749 $loutput = ltrim($output);
750 unlink($temp_skipif);
751 if (!strncasecmp('skip', $loutput, 4)) {
752 $skipreason = "SKIP $tested";
753 if (preg_match('/^\s*skip\s*(.+)\s*/i', $output, $m)) {
754 $skipreason .= '(reason: ' . $m[1] . ')';
755 }
756 if (!isset($this->_options['quiet'])) {
757 $this->_logger->log(0, $skipreason);
758 }
759 if (isset($this->_options['tapoutput'])) {
760 return array('ok', ' # skip ' . $reason);
761 }
762 return 'SKIPPED';
763 }
764
765 if (!strncasecmp('info', $loutput, 4)
766 && preg_match('/^\s*info\s*(.+)\s*/i', $output, $m)) {
767 $info = " (info: $m[1])";
768 }
769
770 if (!strncasecmp('warn', $loutput, 4)
771 && preg_match('/^\s*warn\s*(.+)\s*/i', $output, $m)) {
772 $warn = true; /* only if there is a reason */
773 $info = " (warn: $m[1])";
774 }
775 }
776
777 return array('warn' => $warn, 'info' => $info);
778 }
save_text($filename, $text)
Definition RunTest.php:701

References save_text(), and PEAR_RunTest\system_with_timeout().

Referenced by PEAR_RunTest\run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _savePHPBinary()

_savePHPBinary ( )

Definition at line 966 of file RunTest.php.

967 {
968 $this->_savephp = $this->_php;
969 }

References PEAR_RunTest\$_php.

Referenced by PEAR_RunTest\run().

Here is the caller graph for this function:

◆ _stripHeadersCGI()

_stripHeadersCGI ( $output)

Definition at line 780 of file RunTest.php.

781 {
782 $this->headers = array();
783 if (!empty($this->_options['cgi']) &&
784 $this->_php == $this->_options['cgi'] &&
785 preg_match("/^(.*?)(?:\n\n(.*)|\\z)/s", $output, $match)) {
786 $output = isset($match[2]) ? trim($match[2]) : '';
787 $this->_headers = $this->_processHeaders($match[1]);
788 }
789
790 return $output;
791 }
_processHeaders($text)
Definition RunTest.php:798

References _processHeaders().

Referenced by PEAR_RunTest\run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _testCleanup()

_testCleanup ( $section_text,
$temp_clean )

Definition at line 949 of file RunTest.php.

950 {
951 if ($section_text['CLEAN']) {
952 $this->_restorePHPBinary();
953
954 // perform test cleanup
955 $this->save_text($temp_clean, $section_text['CLEAN']);
956 $output = $this->system_with_timeout("$this->_php $temp_clean 2>&1");
957 if (strlen($output[1])) {
958 echo "BORKED --CLEAN-- section! output:\n", $output[1];
959 }
960 if (file_exists($temp_clean)) {
961 unlink($temp_clean);
962 }
963 }
964 }
_restorePHPBinary()
Definition RunTest.php:971

References _restorePHPBinary(), save_text(), and PEAR_RunTest\system_with_timeout().

Referenced by PEAR_RunTest\run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _writeLog()

_writeLog ( $logname,
$data )

Definition at line 851 of file RunTest.php.

852 {
853 if (!$log = fopen($logname, 'w')) {
854 return PEAR::raiseError("Cannot create test log - $logname");
855 }
856 fwrite($log, $data);
857 fclose($log);
858 }

Referenced by PEAR_RunTest\run().

Here is the caller graph for this function:

◆ generate_diff()

generate_diff ( $wanted,
$output,
$rvalue,
$wanted_re )

Definition at line 674 of file RunTest.php.

675 {
676 $w = explode("\n", $wanted);
677 $o = explode("\n", $output);
678 $wr = explode("\n", $wanted_re);
679 $w1 = array_diff_assoc($w, $o);
680 $o1 = array_diff_assoc($o, $w);
681 $o2 = $w2 = array();
682 foreach ($w1 as $idx => $val) {
683 if (!$wanted_re || !isset($wr[$idx]) || !isset($o1[$idx]) ||
684 !preg_match('/^' . $wr[$idx] . '\\z/', $o1[$idx])) {
685 $w2[sprintf("%03d<", $idx)] = sprintf("%03d- ", $idx + 1) . $val;
686 }
687 }
688 foreach ($o1 as $idx => $val) {
689 if (!$wanted_re || !isset($wr[$idx]) ||
690 !preg_match('/^' . $wr[$idx] . '\\z/', $val)) {
691 $o2[sprintf("%03d>", $idx)] = sprintf("%03d+ ", $idx + 1) . $val;
692 }
693 }
694 $diff = array_merge($w2, $o2);
695 ksort($diff);
696 $extra = $rvalue ? "##EXPECTED: $rvalue[0]\r\n##RETURNED: $rvalue[1]" : '';
697 return implode("\r\n", $diff) . $extra;
698 }

Referenced by PEAR_RunTest\run().

Here is the caller graph for this function:

◆ save_text()

save_text ( $filename,
$text )

Definition at line 701 of file RunTest.php.

702 {
703 if (!$fp = fopen($filename, 'w')) {
704 return PEAR::raiseError("Cannot open file '" . $filename . "' (save_text)");
705 }
706 fwrite($fp, $text);
707 fclose($fp);
708 if (1 < DETAILED) echo "
709FILE $filename {{{
710$text
711}}}
712";
713 }
const DETAILED
Definition RunTest.php:23

References DETAILED.

Referenced by _runSkipIf(), _testCleanup(), and PEAR_RunTest\run().

Here is the caller graph for this function:

Variable Documentation

◆ DETAILED

const DETAILED 1

for error handling

Definition at line 23 of file RunTest.php.

Referenced by save_text().