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