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' ); if (!(error_reporting() & $errno)) return; printf( "\n%s: %s in %s on line %d\n", $names[$errno], $errstr, strstr($errfile, 'eval()\'d code') ? $this->name : $errfile, $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 <<<__ __; // }}} ?>