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