1 package net.sourceforge.phpeclipse.tests.parser;
 
   2 /*******************************************************************************
 
   3  * Copyright (c) 2002 Klaus Hartlage - www.eclipseproject.de All rights
 
   4  * reserved. This program and the accompanying materials are made available
 
   5  * under the terms of the Common Public License v1.0 which accompanies this
 
   6  * distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html
 
   7  ******************************************************************************/
 
   8 import net.sourceforge.phpdt.core.tests.util.AbstractCompilerTest;
 
   9 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 
  11  * Tests the php parser
 
  13 public class PHPParserTestCase extends AbstractCompilerTest {
 
  15   public PHPParserTestCase(String name) {
 
  19    * Test the PHP Parser with different PHP snippets
 
  21   public void testPHPParser() {
 
  22     checkPHP("$test=\"{4IP}/{$include}\";");
 
  23     checkPHP("$this->mRegex = \"/{$this->mBaseRegex}/{$case}\";");
 
  24     checkPHP("$schema_create .= \" DEFAULT \'$row[Default]\'\";");
 
  25     checkPHP("$stringVar=\"ein normaler $varText\";");
 
  26     checkPHP("$stringVar=\'ein normaler $varText\';");
 
  27     checkPHP("switch ($aItem[ELM_NAME]) {\r\n" +  
 
  28                 "                case \'channel\':\r\n" + 
 
  29                 "                    $this->readChannel($aItem);\r\n" + 
 
  31                 "                case \'item\':\r\n" + 
 
  32                 "                    $this->readItem($aItem);\r\n" + 
 
  35                 "                    //printr($aItem);\r\n" + 
 
  37     checkPHP("try {echo $Stream->readAll(); } catch (Exception $e) {\r\n" + 
 
  38                 "            // Swallow exception\r\n" + 
 
  42             "     function overLib($path = \"\") {\n" + 
 
  44                 "               if (strlen($path)) $this->ol_path = $path;\n" + 
 
  48                 "<nolink rel=\'stylesheet\' href=<?php echo \"\'$this->ol_path/overlib.css\' \"; ?> \n" + 
 
  50                 "      type=\'text/css\'>\n" + 
 
  52                 "<div id=\'overDiv\' style=\'position:absolute; visibility:hide; z-index: 1000;\'>\n" + 
 
  56                 "<script language=\'javascript\' src=<?php echo \"\'$this->ol_path/overlib.js\'\"; ?>>\n" + 
 
  64     checkPHP("$t = \') {$ya[]=\'.$this->iFunc.\';$xa[]=\'.$this->iXFunc.\';}\';");
 
  65     checkPHP("$output .=  \n" + 
 
  66                 "            \"\\$_smarty_tpl_vars = \\$this->_tpl_vars;\\n\" . \n" + 
 
  67                 "            \"\\$this->_smarty_include(\".$include_file.\", array(\".implode(\',\', (array)$arg_list).\"));\\n\" .\n" + 
 
  68                 "            \"\\$this->_tpl_vars = \\$_smarty_tpl_vars;\\n\" .\n" + 
 
  69                 "            \"unset(\\$_smarty_tpl_vars);\\n\";");
 
  70     checkPHP("$test=\"=$post_id#$post_id\"");   
 
  71     checkPHP("$comments .= \"        \\${$attrname}[\'xmlns\'] = \'{$this->namespaces[$_argtype[\'namespace\']]}\';\\n\";");
 
  72     checkPHP("$this->_raiseSoapFault(\"method \'{{$this->method_namespace}}$this->methodname\' not defined in service\",\'\',\'\',\'Server\');");
 
  74 //    checkPHP("$emailer->assign_vars(array(\r\n" + 
 
  75 //              " \'U_TOPIC\' => $server_protocol .  POST_POST_URL . \"=$post_id#$post_id\",\r\n" + 
 
  76 //              "                                                        \'U_STOP_WATCHING_TOPIC\' => $server_protocol . $server_name . $server_port . $script_name . \'&\' . POST_TOPIC_URL . \"=$topic_id&unwatch=topic\")\r\n" + 
 
  78     checkPHP("$_compile_data = \'<?php $_config_vars = unserialize(\\\'\' . str_replace(\'\\\'\',\'\\\\\\\'\', serialize($_config_vars)) . \'\\\'); return true; ?>\';");
 
  79     checkPHP("$output = \'<?php \';");
 
  80     checkPHP("$key_part = \'\';");
 
  81     checkPHP("if (isset($_REQUEST[\"xsize\"]) && is_numeric($_REQUEST[\"xsize\"])) {$sxsize=$_REQUEST[\"xsize\"];}");
 
  82     checkPHP("$text_blocks[$curr_tb] = str_replace(\'%%%SMARTYSP\'.$curr_sp.\'%%%\', \'<?php echo \\\'\'.str_replace(\"\'\", \"\\\'\", $sp_match[1][$curr_sp]).\'\\\'; ?>\'.\"\\n\", $text_blocks[$curr_tb]);");
 
  83     checkPHP("$repl.=\"<a href=\\\\\\\"javascript:param=doo_${word}_$i();replaceLimon(param);\\\\\\\">$sug</a><br/>\";");
 
  84     checkPHP("$heading=\'<div class=\"blogtitle\">{tr}Blog{/tr}: {$title}</div>\'.\"\\n\";");
 
  85     checkPHP("return SOAP_Base_Object::_raiseSoapFault(\"No Transport for {$urlparts[\'scheme\']}\");");
 
  86     checkPHP("$text_blocks[$curr_tb] = str_replace(\'%%%SMARTYSP\'.$curr_sp.\'%%%\', \'<?php echo \\\'\'.str_replace(\"\'\", \"\\\'\", $sp_match[1][$curr_sp]).\'\\\'; ?>\'.\"\\n\", $text_blocks[$curr_tb]);");
 
  87     checkPHP("$output = \'<?php \';\n"+
 
  88              "$section_name = $attrs['name'];");
 
  89     checkPHP("echo\"<center><a href=\\\"banners.php?op=click&bid=$bid\\\" target=\\\"_blank\\\"><img src=\\\"$imageurl\\\" border=\\\"0\\\" alt=\'$alttext\' title=\'$alttext\'></a></center>\";\r\n" + 
 
  91     checkPHP("$aid = \"$admin[0]\";");
 
  92     checkPHP("$headers = '';");
 
  93     checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));");
 
  94     checkPHP("\"\\\"\";");
 
  95     checkPHP(" print \"$value\"; \n");
 
  96     checkPHP("if ($shape instanceof Rectangle) { \n" + 
 
  97                 "    print \'$shape is a Rectangle\'; \n" + 
 
  99     checkPHP("$test=\"values(\'$user\',\'${res[\"name\"]}\'  \";");
 
 101     checkPHP("$this->raiseError(\"The auth mode: $mode isn\'t implemented\");");
 
 102     checkPHP("\'<img src=\"\'");
 
 103     checkPHP("$test=\"values(\'$user\',\'${res[\"name\"]}\',\'${res[\"position\"]}\',\'${res[\"ord\"]}\',\'${res[\"type\"]}\',\'${res[\"title\"]}\',\'${res[\"cache_time\"]}\',\'${res[\"rows\"]}\',\'${res[\"groups\"]}\',\'${res[\"params\"]}\')\";");
 
 104     checkPHP("class tiki_phpOpenTracker extends TikiLib, phpOpenTracker { }");
 
 105     checkPHP("$template_source = $prefilter[0]($template_source, $this);");
 
 106     checkPHP("$keyval[] = $this->{$this->_keycolumn[$i]};");
 
 107     checkPHP("$this->_reg_objects[$object] =\n" +
 
 108         "     array(&$object_impl, $allowed, $smarty_args);");
 
 109     checkPHP("echo <<< EOF\n" + 
 
 110                 "        <table border=\'1\'><tr><td>\n" + 
 
 112     checkPHP("interface Shape { \n" + 
 
 113                 "    function draw(); \n" + 
 
 116                 "class Rectangle implements Shape { \n" + 
 
 117                 "    function draw() { \n" + 
 
 118                 "        print \"Drawing a rectangle\"; \n" + 
 
 121     checkPHP("class MyClass { \n" + 
 
 122                 "    private $priv; \n" + 
 
 124                 "    public function getVar() { \n" + 
 
 125                 "        return $this->priv; \n" + 
 
 128     checkPHP("class Test { \n" + 
 
 129                 "    function __construct() { \n" + 
 
 130                 "        print \"Test    constructor\"; \n" + 
 
 133     checkPHP("class Test { \n" + 
 
 134                 "    function __destruct() { \n" + 
 
 135                 "        print \"Destroying Test object\"; \n" + 
 
 139     checkPHP("class Test { \n" + 
 
 140                 "    final function doNotOverload() { \n" + 
 
 141                 "        return __CLASS__; \n" + 
 
 144     checkPHP("final class Test { \n" + 
 
 147                 "class DoNotInherit extends Test { \n" + 
 
 149     checkPHP("class Test { \n" + 
 
 150                 "    function __clone() { \n" + 
 
 151                 "        print \"Clone test object\"; \n" + 
 
 154                 "$test = new Test(); \n" + 
 
 156     checkPHP("class Test { \n" + 
 
 157                 "    const SEMICOLON = \";\"; \n" + 
 
 158                 "    const QUESTIONMARK = \"?\"; \n" + 
 
 160                 "print Test::SEMICOLON; ");
 
 161     checkPHP("class Singleton { \n" + 
 
 162                 "    static $instance = NULL; \n" + 
 
 163                 "    function getInstance() { \n" + 
 
 164                 "        if ($this->instance == NULL) { \n" + 
 
 165                 "            $this->instance = new Singleton(); \n" + 
 
 167                 "        return $this->instance; \n" + 
 
 170     checkPHP("class Test { \n" + 
 
 171                 "    static function helloWorld() { \n" + 
 
 172                 "        print \"Hello, world\"; \n" + 
 
 175                 "Test::helloWorld();");
 
 176     checkPHP("abstract class Test { \n" + 
 
 177                 "    function draw() { \n" + 
 
 178                 "        print \"Inside draw()\"; \n" + 
 
 181     checkPHP("abstract class Test { \n" + 
 
 182                 "    abstract function draw(); \n" + 
 
 184     checkPHP("function f1(Test $test) { \n" + 
 
 187     checkPHP("$test->m1()->m2(); ");
 
 188     checkPHP("$test = new IteratorImpl(); \n" + 
 
 189                 "foreach ($test as $value) { \n" + 
 
 190                 "    print \"$value\"; \n" + 
 
 192     checkPHP("function __autoload($clazz) { \n" + 
 
 193                 "    include_once($clazz . \"php\"); \n" + 
 
 196                 "$obj  = new Test1(); \n" + 
 
 197                 "$obj2 = new Test2(); ");
 
 198     checkPHP("class SQLException extends Exception { \n" + 
 
 199                 "    public $problem; \n" + 
 
 200                 "    function __construct($problem) { \n" + 
 
 201                 "        $this->problem = $problem; \n" + 
 
 206                 "    throw new SQLException(\"Couldn’t connect to database\"); \n" + 
 
 207                 "} catch (SQLException $e) { \n" + 
 
 208                 "    print \"Caught an SQLException with problem $obj->problem\"; \n" + 
 
 209                 "} catch (Exception $e) { \n" + 
 
 210                 "    print \"Caught unrecognized exception\"; \n" + 
 
 212     checkPHP("function my_func(&$arg = null) { \n" + 
 
 213                 "    if ($arg === NULL) { \n" + 
 
 214                 "        print \'$arg is empty\'; \n" + 
 
 218     checkPHP("foreach ($array as &$value) { \n" + 
 
 219                 "    if ($value === \"NULL\") { \n" + 
 
 220                 "        $value = NULL; \n" + 
 
 223     checkPHP("$testxml = simplexml_load_file(\'test.xml\'); \n" + 
 
 224                 "foreach ($$testxml->client as $test) { \n" + 
 
 225                 "    print \"$test->name has account number $test->account_number \"; \n" + 
 
 227     checkHTML("<?php if ($a==$b) {\n" +
 
 231     checkHTML("<?php if ($a==$b) {?>\n" +
 
 232                 "<b>test <?php echo \"test\";?> me</b>\n" +
 
 234                 "echo $bgcolor2 ?>");
 
 236     checkHTML("<?php echo $bgcolor2 ?>");
 
 237     checkPHP("function clean_words($mode, &$entry, &$stopword_list, &$synonym_list)\r\n" + 
 
 238                 "{ static $drop_char_match = array(\'^\', \'$\'); }");
 
 240     checkPHP("if ($topic<1) { $topic = 1;}");
 
 241     checkPHP("$this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);");
 
 242     checkPHP("$db->sql_query($sql);");
 
 243     checkPHP("$val = $$add;");
 
 244     //  checkPHP("if(!$result = mysql_query($sql)) return(array());");
 
 245     checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
 
 247     checkPHP("$ebus_sql['sel_url_list'] = <<<EOS\n"
 
 248         + "select rtrim(URL_NAME) as url_name\n" + "    , rtrim(URL) as url\n"
 
 249         + "     , rtrim(URL_DESC) as url_desc\n" + "from appl_url\n"
 
 250         + "where appl_instnc_sk = <<INSTNC>>\n"
 
 251         + "and appl_sect_deftn_sk = <<SECT>>\n" + "order by url_ord\n"
 
 253     checkPHP("foreach ($HTTP_GET_VARS as $secvalue) { }");
 
 255     checkPHP("\"\\[addsig]\"");
 
 256     checkPHP("$v->read();");
 
 257     checkPHP("$add = 'a'.$i;$val = $$add;");
 
 258     checkPHP("($a==\"b\") || (c($this->x)==\"d\");");
 
 259     checkPHP("(substr($this->file, 0, 2) == \"MM\");");
 
 260     checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
 
 261     checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
 
 262     checkPHP("$this->highlightfile->linkscripts{$category}");
 
 263     checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module)");
 
 264     checkPHP("$this->startmap[$startcurrtag]();");
 
 265     checkPHP("new $this->startmap[$startcurrtag]();");
 
 266     checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();");
 
 267     checkPHP("echo \"Test\", \"me\";");
 
 268     checkPHP("print (\"Test me\");");
 
 269     checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
 
 270     checkPHP("$a == 0 ? print \"true\" : print \"false\";");
 
 271     checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
 
 272     checkPHP("@$connect_function($dbhost, $user, $pw);");
 
 273     checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
 
 274     checkPHP("global ${$objectname}; ");
 
 275    // checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; }  ");
 
 276     checkPHP("unset($this->blockvariables[$block][$varname]);");
 
 277     checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
 
 278     checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
 
 279     checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
 
 280     checkPHP("static $last_run = 0;");
 
 281     checkPHP("unset($headers['Subject']);");
 
 282     checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
 
 283     checkPHP("function validateAndParseResponse($code, &$arguments) { }");
 
 284     checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
 
 285     checkPHP("$this->container = new $container_class($container_options);");
 
 286    // checkPHP("class Cmd extends PEAR { var $arrSetting     = array(); }");
 
 287   //  checkPHP("class Cmd extends PEAR { var $arrSetting     = array(), $i=10; }");
 
 288     checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
 
 289     checkPHP("require_once(\"mainfile.php\");  ");
 
 290     checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n"
 
 291         + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
 
 292     checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n"
 
 293         + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
 
 294     checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n"
 
 295         + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n");
 
 296     checkPHP("$tipath = \"images/topics/\";");
 
 297     checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
 
 298     checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
 
 299     checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
 
 300     checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
 
 301     checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
 
 302     checkPHP("if (!$name) { \n }");
 
 303     checkPHP("mt_srand((double)microtime()*1000000);");
 
 304     checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
 
 305     checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
 
 306     checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
 
 307     checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
 
 308     checkPHP("if ($term{0}!=$firstChar) {}");
 
 309     checkPHP("echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
 
 310         + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
 
 311         + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
 
 313     checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */");
 
 314     checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){  } ");
 
 315     checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
 
 316     checkPHP("if (!isset($message)){ \n"
 
 317         + "$message = $myrow[post_text];\n"
 
 318         + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" .    $myrow[user_sig], $message); \n"
 
 319         + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
 
 320         + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
 
 321     checkPHP("$ol = new Overlib();");
 
 322     checkPHP("$risultato = mysql_query($sql) or\n    die(mysql_error());");
 
 323 //    checkHTML("\n\n\n\n  <?php print \"Hello world\" ?>");
 
 324 //    checkHTML("<?php phpinfo(); ?>");
 
 325 //    checkHTML("<?php phpinfo()?>");
 
 326 //    checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>");
 
 327 //    checkHTML(" <?php //this is a line comment ?>");
 
 328 //    checkHTML("<?php echo $module_name ?>");
 
 330   private void checkPHP(String strEval) {
 
 332       System.out.println("\n------------------------------------");
 
 333       System.out.println(strEval);
 
 336         strEval.toCharArray(),
 
 338 //    parser.phpParserTester(strEval, 1);
 
 340   private void checkHTML(String strEval) {
 
 342       System.out.println("\n------------------------------------");
 
 343       System.out.println(strEval);
 
 346         strEval.toCharArray(),
 
 348 //    parser.phpParserTester(strEval, 1);
 
 350 //  private void checkHTML(String strEval) {
 
 351 //    if (Scanner.DEBUG) {
 
 352 //      System.out.println("\n------------------------------------");
 
 353 //      System.out.println(strEval);
 
 355 //    parser.parse(strEval);
 
 358 //   * The JUnit setup method
 
 360 //  protected void setUp() {
 
 361 //    parser = new Parser(null);