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