From 6a4d498af4136a81e9cf8d1e9b735b0e2a618b9e Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 6 Feb 2005 13:35:09 +0000 Subject: [PATCH] - Initial release # Used for being able to run .phpt-files in a webserver environment # Incomplete and pretty basic but serves its purpose ATM --- ext/sybase_ct/tests/index.php | 215 ++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 ext/sybase_ct/tests/index.php diff --git a/ext/sybase_ct/tests/index.php b/ext/sybase_ct/tests/index.php new file mode 100644 index 0000000000..346e6d13be --- /dev/null +++ b/ext/sybase_ct/tests/index.php @@ -0,0 +1,215 @@ +expected= $expected; + } + + function matches($output) { } + } + // }}} + + // {{{ class PHPTRegexExpectancy + // Expectancy class for regular expressions + class PHPTRegexExpectancy extends PHPTExpectancy { + + function matches($output) { + return preg_match('°^'.strtr(preg_quote(rtrim($this->expected), '°'), array( + '%s' => '(.+)', + '%d' => '([0-9]+)' + )).'°', $output); + } + } + // }}} + + // {{{ class PHPTTest + // Represents a single .phpt-style test + class PHPTTest { + var + $name = '', + $description = '', + $skipif = '', + $code = '', + $expectancy = NULL, + $output = ''; + + function &fromFile($filename) { + $fd= fopen($filename, 'r'); + + $sections= array(); + $current= NULL; + while (!feof($fd)) { + $line= fgets($fd, 0xFFFF); + if (1 == sscanf($line, '--%[^-]--', $section)) { + $sections[$section]= ''; + $current= $section; + continue; + } + $sections[$current].= $line; + } + fclose($fd); + + // Create instance from read data and return it + $t= &new PHPTTest(); { + $t->name= substr(realpath($filename), 0, -1); + $t->description= rtrim($sections['TEST']); + $t->skipif= $sections['SKIPIF']; + $t->code= $sections['FILE']; + + if (isset($sections['EXPECTF'])) { + $t->expectancy= &new PHPTRegexExpectancy($sections['EXPECTF']); + } else { + // XXX TBI XXX + } + } + return $t; + } + + function onError($errno, $errstr, $errfile, $errline) { + static $names= array( + E_NOTICE => 'Notice', + E_WARNING => 'Warning' + ); + + printf( + "\n%s: %s in %s on line %d\n", + $names[$errno], + $errstr, + $this->name, + $errline + ); + } + + function run() { + + // Precondition check - will die if test needs to be skipped + eval('?>'.$this->skipif); + + set_error_handler(array(&$this, 'onError')); { + error_reporting(E_ALL); + + ob_start(); + eval('?>'.$this->code); + $this->output= rtrim(ob_get_contents()); + ob_end_clean(); + } restore_error_handler(); + + return $this->expectancy->matches($this->output); + } + } + // }}} + + // {{{ main + if (isset($_GET['phpinfo'])) { + phpinfo((int)$_GET['phpinfo']); + + echo 'Home'; + exit(); + } + + echo <<<__ + + + PHPT Test + + + +__; + + $test= basename($_SERVER['QUERY_STRING']); + if ($test && file_exists($test)) { + $t= &PHPTTest::fromFile($test); + echo '

'.basename($t->name), ': ', $t->description.'

'; + echo 'Back to test suite'; + flush(); + + // Run the test + $result= $t->run(); + + // Evaluate results + if ($result) { + echo '

Passed

'; + } else { + echo '

Failed


'; + + echo '

Actual output

'; + echo '', $t->output, '
'; + + echo '

Expectancy

'; + echo '', $t->expectancy->expected, ''; + } + + echo '
'; + exit(); + } + + echo '

Test suite

'; + + // phpinfo() links + echo 'phpinfo(): '; + foreach (array( + 1 => 'General', + 4 => 'Configuration', + 8 => 'Modules' + ) as $const => $name) { + printf('%s | ', $const, $name); + } + echo '(All)'; + + echo '

Select one to run

'; + echo '
'; + + echo <<<__ + + +__; + // }}} +?> -- 2.50.1