Skip to content
Snippets Groups Projects
Commit f1711751 authored by Jared Hancock's avatar Jared Hancock
Browse files

Rebase test suite to be more modular

parent e30390b7
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env php
<?php
if (php_sapi_name() != 'cli') exit();
function get_osticket_root_path() {
# Hop up to the root folder
$start = dirname(__file__);
for (;;) {
if (file_exists($start . '/main.inc.php')) break;
$start .= '/..';
}
return realpath($start);
}
$root = get_osticket_root_path();
# Check PHP syntax across all php files
function glob_recursive($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files,
glob_recursive($dir.'/'.basename($pattern), $flags));
}
return $files;
}
echo "PHP Syntax Errors: ";
ob_start();
$scripts=glob_recursive("$root/*.php");
$exit=0;
$syntax_errors="";
foreach ($scripts as $s) {
system("php -l $s", $exit);
$line = ob_get_contents();
ob_clean();
if ($exit !== 0)
$syntax_errors .= $line;
}
ob_end_clean();
if (strlen($syntax_errors)) {
$syntax_errors=str_replace("$root/", '', $syntax_errors);
echo "FAIL\n";
echo "-------------------------------------------------------\n";
echo "$syntax_errors";
exit();
} else {
echo "pass\n";
}
function line_number_for_offset($filename, $offset) {
$lines = file($filename);
$bytes = $line = 0;
while ($bytes < $offset) {
$bytes += strlen(array_shift($lines));
$line += 1;
}
return $line;
}
echo "Short open tags: ";
$fails = array();
foreach ($scripts as $s) {
$matches = array();
if (preg_match_all('/<\?\s*(?!php|xml).*$/m', file_get_contents($s), $matches,
PREG_OFFSET_CAPTURE) > 0) {
foreach ($matches[0] as $match)
$fails[] = array(
str_replace($root.'/', '', $s),
$match[0],
line_number_for_offset($s, $match[1]));
}
}
if (count($fails)) {
echo "FAIL\n";
echo "-------------------------------------------------------\n";
foreach ($fails as $f)
echo sprintf("In %s, line %d: %s\n", $f[0], $f[2],
str_replace("\n", " ", $f[1]));
echo "\n";
} else {
echo "pass\n";
}
# Run phplint across all php files
echo "Access to unitialized variables: ";
ob_start();
# XXX: This won't run well on Windoze
system("$root/setup/test/lib/phplint.tcl ".implode(" ", $scripts));
$lint_errors = ob_get_clean();
if (strlen($lint_errors)) {
$lint_errors=str_replace("$root/", '', $lint_errors);
echo "FAIL\n";
echo "-------------------------------------------------------\n";
echo "$lint_errors";
} else {
echo "\n";
}
function find_function_calls($scripts) {
$calls=array();
foreach ($scripts as $s) {
$lines = explode("\n", file_get_contents($s));
$lineno=0;
foreach (explode("\n", file_get_contents($s)) as $line) {
$lineno++; $matches=array();
preg_match_all('/-[>]([a-zA-Z0-9]*)\(/', $line, $matches,
PREG_SET_ORDER);
foreach ($matches as $m) {
$calls[] = array($s, $lineno, $line, $m[1]);
}
}
}
return $calls;
}
$php_script_content='';
foreach ($scripts as $s) {
$php_script_content .= file_get_contents($s);
}
echo "Access to undefined object methods: ";
ob_start();
foreach (find_function_calls($scripts) as $call) {
list($file, $no, $line, $func) = $call;
if (!preg_match('/^\s*(\/\*[^*]*\*\/)?'."\s*function\s+&?\s*$func\\(/m",
$php_script_content)) {
print "$func: Definitely undefined, from $file:$no\n";
}
}
$undef_func_errors = ob_get_clean();
if (strlen($undef_func_errors)) {
$undef_func_errors=str_replace("$root/", '', $undef_func_errors);
echo "FAIL\n";
echo "-------------------------------------------------------\n";
echo "$undef_func_errors";
exit();
} else {
echo "\n";
}
?>
#!/usr/bin/env php
<?php
if (php_sapi_name() != 'cli') exit();
require_once "tests/class.test.php";
function get_osticket_root_path() {
# Hop up to the root folder
$start = dirname(__file__);
for (;;) {
if (file_exists($start . '/main.inc.php')) break;
$start .= '/..';
}
return realpath($start);
}
$root = get_osticket_root_path();
# Check PHP syntax across all php files
function glob_recursive($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files,
glob_recursive($dir.'/'.basename($pattern), $flags));
}
return $files;
}
$fails = array();
foreach (glob_recursive(dirname(__file__)."/tests/test.*.php") as $t) {
if (strpos($t,"class.") !== false)
continue;
$class = (include $t);
if (!is_string($class))
continue;
$test = new $class();
echo "Running: " . $test->name . "\n";
$test->run();
$fails = array_merge($fails, $test->fails);
echo " ok\n";
}
if ($fails) {
echo count($fails) . " FAIL(s)\n";
echo "-------------------------------------------------------\n";
foreach ($fails as $f) {
list($test, $script, $line, $message) = $f;
$script = str_replace($root.'/', '', $script);
print("$test: $message @ $script:$line\n");
}
}
?>
<?php
class Test {
var $fails = array();
var $name = "";
var $third_party_paths = array(
'/include/JSON.php',
'/include/htmLawed.php',
'/include/PasswordHash.php',
'/include/pear/',
);
function Test() {
call_user_func_array(array($this, '__construct'), func_get_args());
}
function __construct() {
assert_options(ASSERT_CALLBACK, array($this, 'fail'));
error_reporting(E_ALL & ~E_WARNING);
}
function setup() {
}
function teardown() {
}
/*static*/ function getAllScripts() {
$root = get_osticket_root_path();
$scripts = array();
foreach (glob_recursive("$root/*.php") as $s) {
$found = false;
foreach ($this->third_party_paths as $p) {
if (strpos($s, $p) !== false) {
$found = true;
break;
}
}
if (!$found)
$scripts[] = $s;
}
return $scripts;
}
function fail($script, $line, $message) {
$this->fails[] = array(get_class($this), $script, $line, $message);
fputs(STDOUT, 'F');
}
function pass() {
fputs(STDOUT, ".");
}
function run() {
$rc = new ReflectionClass(get_class($this));
foreach ($rc->getMethods() as $m) {
if (stripos($m->name, 'test') === 0) {
$this->setup();
call_user_func(array($this, $m->name));
$this->teardown();
}
}
}
}
?>
File moved
<?php
require_once "class.test.php";
class ShortOpenTag extends Test {
var $name = "PHP Short Open Checks";
function testFindShortOpens() {
foreach ($this->getAllScripts() as $s) {
$matches = array();
if (preg_match_all('/<\?\s*(?!php|xml).*$/m',
file_get_contents($s), $matches,
PREG_OFFSET_CAPTURE) > 0) {
foreach ($matches[0] as $match)
$this->fail(
$s,
line_number_for_offset($s, $match[1]),
$match[0]);
}
else $this->pass();
}
}
}
function line_number_for_offset($filename, $offset) {
$lines = file($filename);
$bytes = $line = 0;
while ($bytes < $offset) {
$bytes += strlen(array_shift($lines));
$line += 1;
}
return $line;
}
return 'ShortOpenTag';
?>
<?php
require_once "class.test.php";
class SyntaxTest extends Test {
var $name = "PHP Syntax Checks";
function testCompileErrors() {
$exit = 0;
foreach ($this->getAllScripts() as $s) {
ob_start();
system("php -l $s", $exit);
$line = ob_get_contents();
ob_end_clean();
if ($exit != 0)
$this->fail($s, 0, $line);
else
$this->pass();
}
}
}
return 'SyntaxTest';
?>
<?php
require_once "class.test.php";
class UndefinedMethods extends Test {
var $name = "Access to undefined object methods";
function testFindShortOpen() {
$scripts = $this->getAllScripts();
$php_script_content='';
foreach ($scripts as $s) {
$php_script_content .= file_get_contents($s);
}
foreach (find_function_calls($scripts) as $call) {
list($file, $no, $line, $func) = $call;
if (!preg_match('/^\s*(\/\*[^*]*\*\/)?'."\s*function\s+&?\s*$func\\(/m",
$php_script_content))
$this->fail($file, $no, "$func: Definitely undefined");
else
$this->pass();
}
}
}
function find_function_calls($scripts) {
$calls=array();
foreach ($scripts as $s) {
$lines = explode("\n", file_get_contents($s));
$lineno=0;
foreach ($lines as $line) {
$lineno++; $matches=array();
preg_match_all('/-[>]([a-zA-Z0-9]*)\(/', $line, $matches,
PREG_SET_ORDER);
foreach ($matches as $m) {
$calls[] = array($s, $lineno, $line, $m[1]);
}
}
}
return $calls;
}
return 'UndefinedMethods';
?>
<?php
require_once "class.test.php";
class UnitializedVars extends Test {
var $name = "Access to unitialized variables";
function testUnitializedUsage() {
$scripts = $this->getAllScripts();
$matches = array();
foreach (range(0, count($scripts), 40) as $start) {
$slice = array_slice($scripts, $start, 40);
ob_start();
# XXX: This won't run well on Windoze
system(dirname(__file__)."/lib/phplint.tcl ".implode(" ", $slice));
$lint_errors = ob_get_clean();
preg_match_all("/\* In (.*) line (\d+): access to uninitialized var '([^']+)'/m",
$lint_errors, $matches, PREG_SET_ORDER);
foreach ($matches as $match)
$this->fail($match[1], $match[2], "'\${$match[3]}'");
}
}
}
return 'UnitializedVars';
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment