1 package net.sourceforge.phpeclipse.tests.parser;
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;
12 * Tests the php parser
14 public class PHPParserTestCase extends AbstractCompilerTest {
16 public PHPParserTestCase(String name) {
21 * Test the PHP Parser with failing PHP snippets
24 public void testPHPParserFailingSyntax() {
28 + "1. ERROR in (at line 1)\n"
31 + "Parser error \"Assignment operator \'=\' not allowed after identifier \'i\' (use \'define(...)\' to define constants).\"\n"
36 * Test Interface Extending multiple Interfaces
38 * This is valid PHP5 syntax Bug 1431425
40 public void testMIInterfaceExtendsInterface() {
42 checkPHP("interface a {}\n" + "interface b {}\n" + "\n"
43 + "interface c extends a, b\n" + "{}\n");
47 * Test Interface implementing multiple Interfaces
49 * This is invalid PHP5 syntax Bug 1431425
51 public void testMIInterfaceImplementsInterface() {
54 "interface a {}\n" + "interface b {}\n" + "\n"
55 + "interface c implements a, b\n" + "{}\n",
57 + "1. ERROR in (at line 1)\n"
58 + " interface c implements a, b\n"
60 + "Parser error \"'{' expected at start of class body.\"\n"
62 + "2. ERROR in (at line 1)\n"
66 + "Parser error \"Too many closing '}'; end-of-file not reached.\"\n"
72 * Test Class Extending multiple Interfaces
74 * This is invalid PHP5 syntax Bug 1431425
76 public void testMIClassExtendsInterface() {
79 "interface a {}\n" + "interface b {}\n" + "\n"
80 + "class c extends a, b\n" + "{}\n",
82 + "1. ERROR in (at line 1)\n"
83 + " class c extends a, b\n"
85 + "Parser error \"No multiple inheritence allowed. Expected token \'implements\' or \'{\'.\"\n"
90 * Test Class implementing multiple Interfaces
92 * This is valid PHP5 syntax Bug 1431425
94 public void testMIClassImplementsInterfaces() {
96 checkPHP("interface a {}\n" + "interface b {}\n" + "\n"
97 + "class c implements a, b\n" + "{}\n");
101 * Test Class Implementing multiple classes
103 * This is invalid PHP5 syntax Bug 1431425
105 public void testMIClassImplementsClasses() {
107 checkPHP("class a {}\n" + "class b {}\n" + "\n"
108 + "class c implements a, b\n" + "{}\n",
109 "This should fail, but doesn't currently.");
113 * Test Class Extending multiple Classes
115 * This is invalid PHP5 syntax Bug 1431425
117 public void testMIClassExtendsClasses() {
120 "class a {}\n" + "class b {}\n" + "\n"
121 + "class c extends a, b\n" + "{}\n",
123 + "1. ERROR in (at line 1)\n"
124 + " class c extends a, b\n"
126 + "Parser error \"No multiple inheritence allowed. Expected token \'implements\' or \'{\'.\"\n"
131 * Test the PHP Parser with different PHP snippets
133 public void testPHPParserGoodSyntax() {
134 checkPHP("$y=self::$x->huba;");
135 checkPHP("self::x()->set();");
136 checkPHP("$test=\"{4IP}/{$include}\";");
137 checkPHP("$this->mRegex = \"/{$this->mBaseRegex}/{$case}\";");
138 checkPHP("$schema_create .= \" DEFAULT \'$row[Default]\'\";");
139 checkPHP("$stringVar=\"ein normaler $varText\";");
140 checkPHP("$stringVar=\'ein normaler $varText\';");
141 checkPHP("switch ($aItem[ELM_NAME]) {\r\n"
142 + " case \'channel\':\r\n"
143 + " $this->readChannel($aItem);\r\n"
145 + " case \'item\':\r\n"
146 + " $this->readItem($aItem);\r\n"
149 + " //printr($aItem);\r\n" + " }");
150 checkPHP("try {echo $Stream->readAll(); } catch (Exception $e) {\r\n"
151 + " // Swallow exception\r\n" + " }");
154 + " function overLib($path = \"\") {\n"
156 + " if (strlen($path)) $this->ol_path = $path;\n"
160 + "<nolink rel=\'stylesheet\' href=<?php echo \"\'$this->ol_path/overlib.css\' \"; ?> \n"
162 + " type=\'text/css\'>\n"
164 + "<div id=\'overDiv\' style=\'position:absolute; visibility:hide; z-index: 1000;\'>\n"
168 + "<script language=\'javascript\' src=<?php echo \"\'$this->ol_path/overlib.js\'\"; ?>>\n"
169 + "\n" + "</script>\n" + "\n" + "<?php\n" + "\n" + " } \n"
171 checkPHP("$t = \') {$ya[]=\'.$this->iFunc.\';$xa[]=\'.$this->iXFunc.\';}\';");
172 checkPHP("$output .= \n"
173 + " \"\\$_smarty_tpl_vars = \\$this->_tpl_vars;\\n\" . \n"
174 + " \"\\$this->_smarty_include(\".$include_file.\", array(\".implode(\',\', (array)$arg_list).\"));\\n\" .\n"
175 + " \"\\$this->_tpl_vars = \\$_smarty_tpl_vars;\\n\" .\n"
176 + " \"unset(\\$_smarty_tpl_vars);\\n\";");
177 checkPHP("$test=\"=$post_id#$post_id\"");
178 checkPHP("$comments .= \" \\${$attrname}[\'xmlns\'] = \'{$this->namespaces[$_argtype[\'namespace\']]}\';\\n\";");
179 checkPHP("$this->_raiseSoapFault(\"method \'{{$this->method_namespace}}$this->methodname\' not defined in service\",\'\',\'\',\'Server\');");
181 checkPHP("$emailer->assign_vars(array(\r\n"
182 + " \'U_TOPIC\' => $server_protocol . POST_POST_URL . \"=$post_id#$post_id\",\r\n"
183 + " \'U_STOP_WATCHING_TOPIC\' => $server_protocol . $server_name . $server_port . $script_name . \'&\' . POST_TOPIC_URL . \"=$topic_id&unwatch=topic\")\r\n"
185 checkPHP("$_compile_data = \'<?php $_config_vars = unserialize(\\\'\' . str_replace(\'\\\'\',\'\\\\\\\'\', serialize($_config_vars)) . \'\\\'); return true; ?>\';");
186 checkPHP("$output = \'<?php \';");
187 checkPHP("$key_part = \'\';");
188 checkPHP("if (isset($_REQUEST[\"xsize\"]) && is_numeric($_REQUEST[\"xsize\"])) {$sxsize=$_REQUEST[\"xsize\"];}");
189 checkPHP("$text_blocks[$curr_tb] = str_replace(\'%%%SMARTYSP\'.$curr_sp.\'%%%\', \'<?php echo \\\'\'.str_replace(\"\'\", \"\\\'\", $sp_match[1][$curr_sp]).\'\\\'; ?>\'.\"\\n\", $text_blocks[$curr_tb]);");
190 checkPHP("$repl.=\"<a href=\\\\\\\"javascript:param=doo_${word}_$i();replaceLimon(param);\\\\\\\">$sug</a><br/>\";");
191 checkPHP("$heading=\'<div class=\"blogtitle\">{tr}Blog{/tr}: {$title}</div>\'.\"\\n\";");
192 checkPHP("return SOAP_Base_Object::_raiseSoapFault(\"No Transport for {$urlparts[\'scheme\']}\");");
193 checkPHP("$text_blocks[$curr_tb] = str_replace(\'%%%SMARTYSP\'.$curr_sp.\'%%%\', \'<?php echo \\\'\'.str_replace(\"\'\", \"\\\'\", $sp_match[1][$curr_sp]).\'\\\'; ?>\'.\"\\n\", $text_blocks[$curr_tb]);");
194 checkPHP("$output = \'<?php \';\n" + "$section_name = $attrs['name'];");
195 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"
197 checkPHP("$aid = \"$admin[0]\";");
198 checkPHP("$headers = '';");
199 checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));");
200 checkPHP("\"\\\"\";");
201 checkPHP(" print \"$value\"; \n");
202 checkPHP("if ($shape instanceof Rectangle) { \n"
203 + " print \'$shape is a Rectangle\'; \n" + "} ");
204 checkPHP("$test=\"values(\'$user\',\'${res[\"name\"]}\' \";");
206 checkPHP("$this->raiseError(\"The auth mode: $mode isn\'t implemented\");");
207 checkPHP("\'<img src=\"\'");
208 checkPHP("$test=\"values(\'$user\',\'${res[\"name\"]}\',\'${res[\"position\"]}\',\'${res[\"ord\"]}\',\'${res[\"type\"]}\',\'${res[\"title\"]}\',\'${res[\"cache_time\"]}\',\'${res[\"rows\"]}\',\'${res[\"groups\"]}\',\'${res[\"params\"]}\')\";");
209 // checkPHP("class tiki_phpOpenTracker extends TikiLib, phpOpenTracker {
211 checkPHP("$template_source = $prefilter[0]($template_source, $this);");
212 checkPHP("$keyval[] = $this->{$this->_keycolumn[$i]};");
213 checkPHP("$this->_reg_objects[$object] =\n"
214 + " array(&$object_impl, $allowed, $smarty_args);");
215 checkPHP("echo <<< EOF\n" + " <table border=\'1\'><tr><td>\n"
217 checkPHP("interface Shape { \n" + " function draw(); \n" + "} \n"
218 + "\n" + "class Rectangle implements Shape { \n"
219 + " function draw() { \n"
220 + " print \"Drawing a rectangle\"; \n" + " } \n"
222 checkPHP("class MyClass { \n" + " private $priv; \n" + "\n"
223 + " public function getVar() { \n"
224 + " return $this->priv; \n" + " } \n" + "} ");
225 checkPHP("class Test { \n" + " function __construct() { \n"
226 + " print \"Test constructor\"; \n" + " } \n" + "}");
227 checkPHP("class Test { \n" + " function __destruct() { \n"
228 + " print \"Destroying Test object\"; \n" + " } \n"
231 checkPHP("class Test { \n" + " final function doNotOverload() { \n"
232 + " return __CLASS__; \n" + " } \n" + "}");
233 checkPHP("final class Test { \n" + "} \n" + "\n"
234 + "class DoNotInherit extends Test { \n" + "}");
235 checkPHP("class Test { \n" + " function __clone() { \n"
236 + " print \"Clone test object\"; \n" + " } \n"
237 + "} \n" + "$test = new Test(); \n" + "clone $test; ");
238 checkPHP("class Test { \n" + " const SEMICOLON = \";\"; \n"
239 + " const QUESTIONMARK = \"?\"; \n" + "} \n"
240 + "print Test::SEMICOLON; ");
241 checkPHP("class Singleton { \n" + " static $instance = NULL; \n"
242 + " function getInstance() { \n"
243 + " if ($this->instance == NULL) { \n"
244 + " $this->instance = new Singleton(); \n"
245 + " } \n" + " return $this->instance; \n"
247 checkPHP("class Test { \n" + " static function helloWorld() { \n"
248 + " print \"Hello, world\"; \n" + " } \n" + "} \n"
249 + "Test::helloWorld();");
250 checkPHP("abstract class Test { \n" + " function draw() { \n"
251 + " print \"Inside draw()\"; \n" + " } \n" + "} ");
252 checkPHP("abstract class Test { \n"
253 + " abstract function draw(); \n" + "} ");
254 checkPHP("function f1(Test $test) { \n" + "\n" + "}");
255 checkPHP("$test->m1()->m2(); ");
256 checkPHP("$test = new IteratorImpl(); \n"
257 + "foreach ($test as $value) { \n" + " print \"$value\"; \n"
259 checkPHP("function __autoload($clazz) { \n"
260 + " include_once($clazz . \"php\"); \n" + "} \n" + "\n"
261 + "$obj = new Test1(); \n" + "$obj2 = new Test2(); ");
262 checkPHP("class SQLException extends Exception { \n"
263 + " public $problem; \n"
264 + " function __construct($problem) { \n"
265 + " $this->problem = $problem; \n"
270 + " throw new SQLException(\"Couldn’t connect to database\"); \n"
271 + "} catch (SQLException $e) { \n"
272 + " print \"Caught an SQLException with problem $obj->problem\"; \n"
273 + "} catch (Exception $e) { \n"
274 + " print \"Caught unrecognized exception\"; \n" + "}");
275 checkPHP("function my_func(&$arg = null) { \n"
276 + " if ($arg === NULL) { \n"
277 + " print \'$arg is empty\'; \n" + " } \n" + "} \n"
279 checkPHP("foreach ($array as &$value) { \n"
280 + " if ($value === \"NULL\") { \n"
281 + " $value = NULL; \n" + " } \n" + "}");
282 checkPHP("$testxml = simplexml_load_file(\'test.xml\'); \n"
283 + "foreach ($$testxml->client as $test) { \n"
284 + " print \"$test->name has account number $test->account_number \"; \n"
286 checkHTML("<?php if ($a==$b) {\n" + "}\n" + "" + "?> ");
287 checkHTML("<?php if ($a==$b) {?>\n"
288 + "<b>test <?php echo \"test\";?> me</b>\n" + "<?php } \n"
289 + "echo $bgcolor2 ?>");
291 checkHTML("<?php echo $bgcolor2 ?>");
292 checkPHP("function clean_words($mode, &$entry, &$stopword_list, &$synonym_list)\r\n"
293 + "{ static $drop_char_match = array(\'^\', \'$\'); }");
295 checkPHP("if ($topic<1) { $topic = 1;}");
296 checkPHP("$this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);");
297 checkPHP("$db->sql_query($sql);");
298 checkPHP("$val = $$add;");
299 checkPHP("if(!$result = mysql_query($sql)) return(array());");
300 checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
302 checkPHP("$ebus_sql['sel_url_list'] = <<<EOS\n"
303 + "select rtrim(URL_NAME) as url_name\n"
304 + " , rtrim(URL) as url\n" + " , rtrim(URL_DESC) as url_desc\n"
305 + "from appl_url\n" + "where appl_instnc_sk = <<INSTNC>>\n"
306 + "and appl_sect_deftn_sk = <<SECT>>\n" + "order by url_ord\n"
308 checkPHP("foreach ($HTTP_GET_VARS as $secvalue) { }");
310 checkPHP("\"\\[addsig]\"");
311 checkPHP("$v->read();");
312 checkPHP("$add = 'a'.$i;$val = $$add;");
313 checkPHP("($a==\"b\") || (c($this->x)==\"d\");");
314 checkPHP("(substr($this->file, 0, 2) == \"MM\");");
315 checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
316 checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
317 checkPHP("$this->highlightfile->linkscripts{$category}");
318 checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module)");
319 checkPHP("$this->startmap[$startcurrtag]();");
320 checkPHP("new $this->startmap[$startcurrtag]();");
321 checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();");
322 checkPHP("echo \"Test\", \"me\";");
323 checkPHP("print (\"Test me\");");
324 checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
325 checkPHP("$a == 0 ? print \"true\" : print \"false\";");
326 checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
327 checkPHP("@$connect_function($dbhost, $user, $pw);");
328 checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
329 checkPHP("global ${$objectname}; ");
330 checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; } ");
331 checkPHP("unset($this->blockvariables[$block][$varname]);");
332 checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
333 checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
334 checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
335 checkPHP("static $last_run = 0;");
336 checkPHP("unset($headers['Subject']);");
337 checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
338 checkPHP("function validateAndParseResponse($code, &$arguments) { }");
339 checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
340 checkPHP("$this->container = new $container_class($container_options);");
341 checkPHP("class Cmd extends PEAR { var $arrSetting = array(); }");
342 checkPHP("class Cmd extends PEAR { var $arrSetting = array(), $i=10; }");
343 checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
344 checkPHP("require_once(\"mainfile.php\"); ");
345 checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n"
346 + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
347 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n"
348 + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
349 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n"
350 + "Header(\"Location: index.php\");\n" + "die();\n"
352 checkPHP("$tipath = \"images/topics/\";");
353 checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
354 checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
355 checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
356 checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
357 checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
358 checkPHP("if (!$name) { \n }");
359 checkPHP("mt_srand((double)microtime()*1000000);");
360 checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
361 checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
362 checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
363 checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
364 checkPHP("if ($term{0}!=$firstChar) {}");
365 checkPHP("echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
366 + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
367 + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
369 checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */");
370 checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){ } ");
371 checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
372 checkPHP("if (!isset($message)){ \n"
373 + "$message = $myrow[post_text];\n"
374 + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" . $myrow[user_sig], $message); \n"
375 + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
376 + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
377 checkPHP("$ol = new Overlib();");
378 checkPHP("$risultato = mysql_query($sql) or\n die(mysql_error());");
379 checkHTML("\n\n\n\n <?php print \"Hello world\" ?>");
380 checkHTML("<?php phpinfo(); ?>");
381 checkHTML("<?php phpinfo()?>");
382 checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>");
383 checkHTML(" <?php //this is a line comment ?>");
384 checkHTML("<?php echo $module_name ?>");