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