scanner/tests/ParserTests.php
Correl Roush 26d9f61672 Polished up and enabled the undefined variable scan by default Mantis: 2691
git-svn-id: file:///srv/svn/scanner/trunk@20 a0501263-5b7a-4423-a8ba-1edf086583e7
2008-05-23 21:13:36 +00:00

138 lines
3.7 KiB
PHP

<?php
require_once 'PHPUnit/Framework.php';
require_once '../parser.php';
class ParserTests extends PHPUnit_Framework_TestCase {
private $objects;
public function __construct() {
$this->objects = array();
}
public function callback($object) {
$this->objects[] = $object;
}
private function parse_and_count_type($code, $type, $fetch_mode = PHPPARSER_FETCH_ALL) {
$this->objects = array();
$this->parser = new PHPParser($fetch_mode);
$this->parser->registerCallback(array($this, 'callback'));
$this->parser->parse($code);
$count = 0;
foreach ($this->objects as $o) {
if ($type == $o['type']) $count++;
}
return $count;
}
public function testFetchClassDefinitions() {
$code = '<?php
class Test {};
class Test2 {
class Test2a {};
};
?>';
$this->assertEquals(3, $this->parse_and_count_type($code, PHPPARSER_CLASS_DEF, PHPPARSER_FETCH_CLASSES));
}
public function testFetchFunctionDefinitions() {
$code = '<?php
function Function1() {}
function Function2($foo = "bar") {}
?>';
$this->assertEquals(2, $this->parse_and_count_type($code, PHPPARSER_FUNCTION_DEF, PHPPARSER_FETCH_FUNCTIONS));
}
public function testFetchMethodDefinitions() {
$code = '<?php
class Test2 {
class Test2a {};
private function _init() {}
static public function test() {}
function do_something($when) {}
};
?>';
$this->assertEquals(3, $this->parse_and_count_type($code, PHPPARSER_FUNCTION_DEF, PHPPARSER_FETCH_FUNCTIONS));
}
public function testFetchFunctionCalls() {
$code = '<?php
dosomething();
do_something_else ($val);
give_up(array(
$wife,
$kids,
$money));
?>';
$this->assertEquals(3, $this->parse_and_count_type($code, PHPPARSER_FUNCTION_CALL, PHPPARSER_FETCH_CALLS));
}
public function testFetchIncludes() {
$code = '<?php
include "file.php";
require \'tools/stuff.php\';
include_once ("session.php");
require_once( "templates.php" );
?>';
$this->assertEquals(4, $this->parse_and_count_type($code, PHPPARSER_INCLUDE, PHPPARSER_FETCH_INCLUDES));
}
public function testFetchConstructs() {
$code = '<?php
echo "saywhatnow";
echo("hey!");
eval($_GET["evil_input"]);
?>';
$this->assertEquals(3, $this->parse_and_count_type($code, PHPPARSER_LANGUAGE_CONSTRUCT, PHPPARSER_FETCH_CONSTRUCTS));
}
public function testFetchAssignments() {
/*
Function arguments count as an assignment (as they are assigned when the function is called)
Variables that are declared are considered assigned, even if no value is actually set.
List assignments are counted once for each item in the list
The final line here tests nested expressions, which should be solved from the inside out.
*/
$code = '<?php
function test($array) {
global $dbconn, $options;
$foo = "bar";
list($a, $b, $c) = $array;
$hash["answer"] = 42;
$data[$index[0]] = "complex";
$data[$index = 0] = "nested";
};
?>';
$this->parse_and_count_type($code, PHPPARSER_ASSIGNMENT, PHPPARSER_FETCH_EXPRESSIONS);
$assignments = array();
foreach ($this->objects as $object) {
if (PHPPARSER_ASSIGNMENT == $object['type'])
$assignments[] = $object['name'];
}
$expected = array(
'$array=$array',
'$dbconn=$dbconn',
'$options=$options',
'$foo="bar"',
'$a=$array',
'$b=$array',
'$c=$array',
'$hash["answer"]=42',
'$data[$index[0]]="complex"',
'$index=0',
'$data[0]="nested"',
);
$this->assertEquals($expected, $assignments);
}
public function testCommentedCode() {
$code = '<?php
// $var_1
/* $var_2 */
/*
$var_3
*/
/**
* $var_4
*/
?>';
$this->assertEquals(0, $this->parse_and_count_type($code, PHPPARSER_VARIABLE, PHPPARSER_FETCH_EXPRESSIONS));
}
}
?>