code cleaning (no functional changes)
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpeclipse / tests / parser / PHPParserTestCase.java
1 package net.sourceforge.phpeclipse.tests.parser;
2
3 /*******************************************************************************
4  * Copyright (c) 2002 www.phpeclipse.de All rights
5  * reserved. This program and the accompanying materials are made available
6  * under the terms of the Common Public License v1.0 which accompanies this
7  * distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html
8  ******************************************************************************/
9 import net.sourceforge.phpdt.core.tests.util.AbstractCompilerTest;
10 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
11
12 /**
13  * Tests the php parser
14  */
15 public class PHPParserTestCase extends AbstractCompilerTest {
16         // Parser parser;
17         public PHPParserTestCase(String name) {
18                 super(name);
19         }
20
21         /**
22          * Test the PHP Parser with different PHP snippets
23          */
24         public void testPHPParser() {
25                 // checkPHP("i=10;"); // should get an error !
26                 checkPHP("$y=self::$x->huba;");
27                 checkPHP("self::x()->set();");
28                 checkPHP("$test=\"{4IP}/{$include}\";");
29                 checkPHP("$this->mRegex = \"/{$this->mBaseRegex}/{$case}\";");
30                 checkPHP("$schema_create .= \" DEFAULT \'$row[Default]\'\";");
31                 checkPHP("$stringVar=\"ein normaler $varText\";");
32                 checkPHP("$stringVar=\'ein normaler $varText\';");
33                 checkPHP("switch ($aItem[ELM_NAME]) {\r\n" + "                case \'channel\':\r\n"
34                                 + "                    $this->readChannel($aItem);\r\n" + "                    break;\r\n"
35                                 + "                case \'item\':\r\n" + "                    $this->readItem($aItem);\r\n"
36                                 + "                    break;\r\n" + "                default:\r\n" + "                    //printr($aItem);\r\n"
37                                 + "            }");
38                 checkPHP("try {echo $Stream->readAll(); } catch (Exception $e) {\r\n" + "            // Swallow exception\r\n" + "        }");
39
40                 checkHTML("<?php\n" + "   function overLib($path = \"\") {\n" + "\n" + "                if (strlen($path)) $this->ol_path = $path;\n"
41                                 + "\n" + "?>\n" + "\n" + "<nolink rel=\'stylesheet\' href=<?php echo \"\'$this->ol_path/overlib.css\' \"; ?> \n" + "\n"
42                                 + "      type=\'text/css\'>\n" + "\n"
43                                 + "<div id=\'overDiv\' style=\'position:absolute; visibility:hide; z-index: 1000;\'>\n" + "\n" + "</div>\n" + "\n"
44                                 + "<script language=\'javascript\' src=<?php echo \"\'$this->ol_path/overlib.js\'\"; ?>>\n" + "\n" + "</script>\n" + "\n"
45                                 + "<?php\n" + "\n" + "    } \n" + "?>");
46                 checkPHP("$t = \') {$ya[]=\'.$this->iFunc.\';$xa[]=\'.$this->iXFunc.\';}\';");
47                 checkPHP("$output .=  \n" + "            \"\\$_smarty_tpl_vars = \\$this->_tpl_vars;\\n\" . \n"
48                                 + "            \"\\$this->_smarty_include(\".$include_file.\", array(\".implode(\',\', (array)$arg_list).\"));\\n\" .\n"
49                                 + "            \"\\$this->_tpl_vars = \\$_smarty_tpl_vars;\\n\" .\n" + "            \"unset(\\$_smarty_tpl_vars);\\n\";");
50                 checkPHP("$test=\"=$post_id#$post_id\"");
51                 checkPHP("$comments .= \"        \\${$attrname}[\'xmlns\'] = \'{$this->namespaces[$_argtype[\'namespace\']]}\';\\n\";");
52                 checkPHP("$this->_raiseSoapFault(\"method \'{{$this->method_namespace}}$this->methodname\' not defined in service\",\'\',\'\',\'Server\');");
53
54                 // checkPHP("$emailer->assign_vars(array(\r\n" +
55                 // " \'U_TOPIC\' => $server_protocol . POST_POST_URL .
56                 // \"=$post_id#$post_id\",\r\n" +
57                 // " \'U_STOP_WATCHING_TOPIC\' => $server_protocol . $server_name .
58                 // $server_port . $script_name . \'&\' . POST_TOPIC_URL .
59                 // \"=$topic_id&unwatch=topic\")\r\n" +
60                 // " );");
61                 checkPHP("$_compile_data = \'<?php $_config_vars = unserialize(\\\'\' . str_replace(\'\\\'\',\'\\\\\\\'\', serialize($_config_vars)) . \'\\\'); return true; ?>\';");
62                 checkPHP("$output = \'<?php \';");
63                 checkPHP("$key_part = \'\';");
64                 checkPHP("if (isset($_REQUEST[\"xsize\"]) && is_numeric($_REQUEST[\"xsize\"])) {$sxsize=$_REQUEST[\"xsize\"];}");
65                 checkPHP("$text_blocks[$curr_tb] = str_replace(\'%%%SMARTYSP\'.$curr_sp.\'%%%\', \'<?php echo \\\'\'.str_replace(\"\'\", \"\\\'\", $sp_match[1][$curr_sp]).\'\\\'; ?>\'.\"\\n\", $text_blocks[$curr_tb]);");
66                 checkPHP("$repl.=\"<a href=\\\\\\\"javascript:param=doo_${word}_$i();replaceLimon(param);\\\\\\\">$sug</a><br/>\";");
67                 checkPHP("$heading=\'<div class=\"blogtitle\">{tr}Blog{/tr}: {$title}</div>\'.\"\\n\";");
68                 checkPHP("return SOAP_Base_Object::_raiseSoapFault(\"No Transport for {$urlparts[\'scheme\']}\");");
69                 checkPHP("$text_blocks[$curr_tb] = str_replace(\'%%%SMARTYSP\'.$curr_sp.\'%%%\', \'<?php echo \\\'\'.str_replace(\"\'\", \"\\\'\", $sp_match[1][$curr_sp]).\'\\\'; ?>\'.\"\\n\", $text_blocks[$curr_tb]);");
70                 checkPHP("$output = \'<?php \';\n" + "$section_name = $attrs['name'];");
71                 checkPHP("echo\"<center><a href=\\\"banners.php?op=click&amp;bid=$bid\\\" target=\\\"_blank\\\"><img src=\\\"$imageurl\\\" border=\\\"0\\\" alt=\'$alttext\' title=\'$alttext\'></a></center>\";\r\n"
72                                 + "    ");
73                 checkPHP("$aid = \"$admin[0]\";");
74                 checkPHP("$headers = '';");
75                 checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));");
76                 checkPHP("\"\\\"\";");
77                 checkPHP(" print \"$value\"; \n");
78                 checkPHP("if ($shape instanceof Rectangle) { \n" + "    print \'$shape is a Rectangle\'; \n" + "} ");
79                 checkPHP("$test=\"values(\'$user\',\'${res[\"name\"]}\'  \";");
80
81                 checkPHP("$this->raiseError(\"The auth mode: $mode isn\'t implemented\");");
82                 checkPHP("\'<img src=\"\'");
83                 checkPHP("$test=\"values(\'$user\',\'${res[\"name\"]}\',\'${res[\"position\"]}\',\'${res[\"ord\"]}\',\'${res[\"type\"]}\',\'${res[\"title\"]}\',\'${res[\"cache_time\"]}\',\'${res[\"rows\"]}\',\'${res[\"groups\"]}\',\'${res[\"params\"]}\')\";");
84                 checkPHP("class tiki_phpOpenTracker extends TikiLib, phpOpenTracker { }");
85                 checkPHP("$template_source = $prefilter[0]($template_source, $this);");
86                 checkPHP("$keyval[] = $this->{$this->_keycolumn[$i]};");
87                 checkPHP("$this->_reg_objects[$object] =\n" + "     array(&$object_impl, $allowed, $smarty_args);");
88                 checkPHP("echo <<< EOF\n" + "        <table border=\'1\'><tr><td>\n" + "EOF;");
89                 checkPHP("interface Shape { \n" + "    function draw(); \n" + "} \n" + "\n" + "class Rectangle implements Shape { \n"
90                                 + "    function draw() { \n" + "        print \"Drawing a rectangle\"; \n" + "    } \n" + "}");
91                 checkPHP("class MyClass { \n" + "    private $priv; \n" + "\n" + "    public function getVar() { \n"
92                                 + "        return $this->priv; \n" + "    } \n" + "} ");
93                 checkPHP("class Test { \n" + "    function __construct() { \n" + "        print \"Test   constructor\"; \n" + "    } \n" + "}");
94                 checkPHP("class Test { \n" + "    function __destruct() { \n" + "        print \"Destroying Test object\"; \n" + "    } \n"
95                                 + "}");
96
97                 checkPHP("class Test { \n" + "    final function doNotOverload() { \n" + "        return __CLASS__; \n" + "    } \n" + "}");
98                 checkPHP("final class Test { \n" + "} \n" + "\n" + "class DoNotInherit extends Test { \n" + "}");
99                 checkPHP("class Test { \n" + "    function __clone() { \n" + "        print \"Clone test object\"; \n" + "    } \n" + "} \n"
100                                 + "$test = new Test(); \n" + "clone $test; ");
101                 checkPHP("class Test { \n" + "    const SEMICOLON = \";\"; \n" + "    const QUESTIONMARK = \"?\"; \n" + "} \n"
102                                 + "print Test::SEMICOLON; ");
103                 checkPHP("class Singleton { \n" + "    static $instance = NULL; \n" + "    function getInstance() { \n"
104                                 + "        if ($this->instance == NULL) { \n" + "            $this->instance = new Singleton(); \n" + "        } \n"
105                                 + "        return $this->instance; \n" + "    } \n" + "} ");
106                 checkPHP("class Test { \n" + "    static function helloWorld() { \n" + "        print \"Hello, world\"; \n" + "    } \n"
107                                 + "} \n" + "Test::helloWorld();");
108                 checkPHP("abstract class Test { \n" + "    function draw() { \n" + "        print \"Inside draw()\"; \n" + "    } \n" + "} ");
109                 checkPHP("abstract class Test { \n" + "    abstract function draw(); \n" + "} ");
110                 checkPHP("function f1(Test $test) { \n" + "\n" + "}");
111                 checkPHP("$test->m1()->m2(); ");
112                 checkPHP("$test = new IteratorImpl(); \n" + "foreach ($test as $value) { \n" + "    print \"$value\"; \n" + "}");
113                 checkPHP("function __autoload($clazz) { \n" + "    include_once($clazz . \"php\"); \n" + "} \n" + "\n"
114                                 + "$obj  = new Test1(); \n" + "$obj2 = new Test2(); ");
115                 checkPHP("class SQLException extends Exception { \n" + "    public $problem; \n" + "    function __construct($problem) { \n"
116                                 + "        $this->problem = $problem; \n" + "    } \n" + "} \n" + "\n" + "try { \n"
117                                 + "    throw new SQLException(\"Couldn&#8217;t connect to database\"); \n" + "} catch (SQLException $e) { \n"
118                                 + "    print \"Caught an SQLException with problem $obj->problem\"; \n" + "} catch (Exception $e) { \n"
119                                 + "    print \"Caught unrecognized exception\"; \n" + "}");
120                 checkPHP("function my_func(&$arg = null) { \n" + "    if ($arg === NULL) { \n" + "        print \'$arg is empty\'; \n"
121                                 + "    } \n" + "} \n" + "my_func();");
122                 checkPHP("foreach ($array as &$value) { \n" + "    if ($value === \"NULL\") { \n" + "        $value = NULL; \n" + "    } \n"
123                                 + "}");
124                 checkPHP("$testxml = simplexml_load_file(\'test.xml\'); \n" + "foreach ($$testxml->client as $test) { \n"
125                                 + "    print \"$test->name has account number $test->account_number \"; \n" + "} ");
126                 checkHTML("<?php if ($a==$b) {\n" + "}\n" + "" + "?> ");
127                 checkHTML("<?php if ($a==$b) {?>\n" + "<b>test <?php echo \"test\";?> me</b>\n" + "<?php } \n" + "echo $bgcolor2 ?>");
128
129                 checkHTML("<?php echo $bgcolor2 ?>");
130                 checkPHP("function clean_words($mode, &$entry, &$stopword_list, &$synonym_list)\r\n"
131                                 + "{ static $drop_char_match = array(\'^\', \'$\'); }");
132
133                 checkPHP("if ($topic<1) { $topic = 1;}");
134                 checkPHP("$this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);");
135                 checkPHP("$db->sql_query($sql);");
136                 checkPHP("$val = $$add;");
137                 // checkPHP("if(!$result = mysql_query($sql)) return(array());");
138                 checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
139                 // Bugs item #690938
140                 checkPHP("$ebus_sql['sel_url_list'] = <<<EOS\n" + "select rtrim(URL_NAME) as url_name\n" + "    , rtrim(URL) as url\n"
141                                 + "     , rtrim(URL_DESC) as url_desc\n" + "from appl_url\n" + "where appl_instnc_sk = <<INSTNC>>\n"
142                                 + "and appl_sect_deftn_sk = <<SECT>>\n" + "order by url_ord\n" + "EOS;\n");
143                 checkPHP("foreach ($HTTP_GET_VARS as $secvalue) { }");
144
145                 checkPHP("\"\\[addsig]\"");
146                 checkPHP("$v->read();");
147                 checkPHP("$add = 'a'.$i;$val = $$add;");
148                 checkPHP("($a==\"b\") || (c($this->x)==\"d\");");
149                 checkPHP("(substr($this->file, 0, 2) == \"MM\");");
150                 checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
151                 checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
152                 checkPHP("$this->highlightfile->linkscripts{$category}");
153                 checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module)");
154                 checkPHP("$this->startmap[$startcurrtag]();");
155                 checkPHP("new $this->startmap[$startcurrtag]();");
156                 checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();");
157                 checkPHP("echo \"Test\", \"me\";");
158                 checkPHP("print (\"Test me\");");
159                 checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
160                 checkPHP("$a == 0 ? print \"true\" : print \"false\";");
161                 checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
162                 checkPHP("@$connect_function($dbhost, $user, $pw);");
163                 checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
164                 checkPHP("global ${$objectname}; ");
165                 // checkPHP("class DB_mssql extends DB_common { var $connection; var
166                 // $phptype, $dbsyntax; } ");
167                 checkPHP("unset($this->blockvariables[$block][$varname]);");
168                 checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
169                 checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
170                 checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
171                 checkPHP("static $last_run = 0;");
172                 checkPHP("unset($headers['Subject']);");
173                 checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
174                 checkPHP("function validateAndParseResponse($code, &$arguments) { }");
175                 checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
176                 checkPHP("$this->container = new $container_class($container_options);");
177                 // checkPHP("class Cmd extends PEAR { var $arrSetting = array(); }");
178                 // checkPHP("class Cmd extends PEAR { var $arrSetting = array(), $i=10; }");
179                 checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
180                 checkPHP("require_once(\"mainfile.php\");  ");
181                 checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
182                 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
183                 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n");
184                 checkPHP("$tipath = \"images/topics/\";");
185                 checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
186                 checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
187                 checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
188                 checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
189                 checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
190                 checkPHP("if (!$name) { \n }");
191                 checkPHP("mt_srand((double)microtime()*1000000);");
192                 checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
193                 checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
194                 checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
195                 checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
196                 checkPHP("if ($term{0}!=$firstChar) {}");
197                 checkPHP("echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
198                                 + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
199                                 + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
200                                 + ";");
201                 checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */");
202                 checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){  } ");
203                 checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
204                 checkPHP("if (!isset($message)){ \n" + "$message = $myrow[post_text];\n"
205                                 + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" .    $myrow[user_sig], $message); \n"
206                                 + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n" + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
207                 checkPHP("$ol = new Overlib();");
208                 checkPHP("$risultato = mysql_query($sql) or\n    die(mysql_error());");
209                 // checkHTML("\n\n\n\n <?php print \"Hello world\" ?>");
210                 // checkHTML("<?php phpinfo(); ?>");
211                 // checkHTML("<?php phpinfo()?>");
212                 // checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>");
213                 // checkHTML(" <?php //this is a line comment ?>");
214                 // checkHTML("<?php echo $module_name ?>");
215         }
216
217         private void checkPHP(String strEval) {
218                 if (Scanner.DEBUG) {
219                         System.out.println("\n------------------------------------");
220                         System.out.println(strEval);
221                 }
222                 checkParsePHP(strEval.toCharArray(), "");
223                 // parser.phpParserTester(strEval, 1);
224         }
225
226 //      private void checkHTML(String strEval) {
227 //              if (Scanner.DEBUG) {
228 //                      System.out.println("\n------------------------------------");
229 //                      System.out.println(strEval);
230 //              }
231 //              checkParseHTML(strEval.toCharArray(), "");
232 //              // parser.phpParserTester(strEval, 1);
233 //      }
234
235 }