6091e18263066154d66c8b025cf1cbc4d1a73900
[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
4 All rights reserved. This program and the accompanying materials
5 are made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
8 **********************************************************************/
9
10 import junit.framework.*;
11 import net.sourceforge.phpdt.internal.compiler.parser.*;
12
13 import org.eclipse.core.runtime.*;
14
15 /**
16  *  Tests the php parser
17  */
18 public class PHPParserTestCase extends TestCase {
19
20   Parser parser;
21
22   public PHPParserTestCase(String name) {
23     super(name);
24   }
25
26   /**
27    *  Test the PHP Parser with different PHP snippets
28    */
29   public void testPHPParser() {
30     // Bugs item #690938
31     checkPHP(
32       "$ebus_sql['sel_url_list'] = <<<EOS\n"
33         + "select rtrim(URL_NAME) as url_name\n"
34         + "     , rtrim(URL) as url\n"
35         + "     , rtrim(URL_DESC) as url_desc\n"
36         + "from appl_url\n"
37         + "where appl_instnc_sk = <<INSTNC>>\n"
38         + "and appl_sect_deftn_sk = <<SECT>>\n"
39         + "order by url_ord\n"
40         + "EOS;\n");
41
42     checkPHP("foreach ($HTTP_GET_VARS as $secvalue) { }");
43     checkPHP("\"\\\"\";");
44     checkPHP("\"\\[addsig]\"");
45     checkPHP("$v->read();");
46     checkPHP("$add = 'a'.$i;$val = $$add;");
47     checkPHP("($a==\"b\") || (c($this->x)==\"d\");");
48     checkPHP("(substr($this->file, 0, 2) == \"MM\");");
49     checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
50     checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
51     checkPHP("$this->highlightfile->linkscripts{$category}");
52     checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module)");
53     checkPHP("$this->startmap[$startcurrtag]();");
54     checkPHP("new $this->startmap[$startcurrtag]();");
55     checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();");
56     checkPHP("echo \"Test\", \"me\";");
57     checkPHP("print (\"Test me\");");
58     checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
59     checkPHP("$a == 0 ? print \"true\" : print \"false\";");
60     checkPHP("if(!$result = mysql_query($sql)) return(array());");
61     checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
62     checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
63     checkPHP("@$connect_function($dbhost, $user, $pw);");
64     checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
65     checkPHP("global ${$objectname}; ");
66     checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; }  ");
67     checkPHP("unset($this->blockvariables[$block][$varname]);");
68     checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
69     checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
70     checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
71     checkPHP("static $last_run = 0;");
72     checkPHP("unset($headers['Subject']);");
73     checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
74     checkPHP("function validateAndParseResponse($code, &$arguments) { }");
75     checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
76     checkPHP("$this->container = new $container_class($container_options);");
77     checkPHP("class Cmd extends PEAR { var $arrSetting     = array(); }");
78     checkPHP("class Cmd extends PEAR { var $arrSetting     = array(), $i=10; }");
79     checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
80     checkPHP("require_once(\"mainfile.php\");  ");
81     checkPHP(
82       "if (eregi(\"footer.php\",$PHP_SELF)) {\n"
83         + "Header(\"Location: index.php\");\n"
84         + "die();\n"
85         + "}\n");
86     checkPHP(
87       "while (eregi(\"footer.php\",$PHP_SELF)) {\n"
88         + "Header(\"Location: index.php\");\n"
89         + "die();\n"
90         + "}\n");
91     checkPHP(
92       "while (eregi(\"footer.php\",$PHP_SELF)) :\n"
93         + "Header(\"Location: index.php\");\n"
94         + "die();\n"
95         + "endwhile;\n");
96     checkPHP("$tipath = \"images/topics/\";");
97     checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
98     checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
99     checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
100     checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
101     checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
102     checkPHP("if (!$name) { \n }");
103     checkPHP("mt_srand((double)microtime()*1000000);");
104     checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
105     checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
106     checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
107     checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
108     checkPHP("if ($term{0}!=$firstChar) {}");
109     checkPHP(
110       "echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
111         + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
112         + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
113         + ";");
114     checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */");
115     checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){  } ");
116     checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
117     checkPHP(
118       "if (!isset($message)){ \n"
119         + "$message = $myrow[post_text];\n"
120         + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" .    $myrow[user_sig], $message); \n"
121         + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
122         + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
123     checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));");
124     checkPHP("$ol = new Overlib();");
125     checkPHP("$risultato = mysql_query($sql) or\n    die(mysql_error());");
126
127     checkHTML("\n\n\n\n  <?php print \"Hello world\" ?>");
128     checkHTML("<?php phpinfo(); ?>");
129     checkHTML("<?php phpinfo()?>");
130     checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>");
131     checkHTML(" <?php //this is a line comment ?>");
132     checkHTML("<?php echo $module_name ?>");
133   }
134
135   private void checkPHP(String strEval) {
136     try {
137       if (Scanner.DEBUG) {
138         System.out.println("\n------------------------------------");
139         System.out.println(strEval);
140       }
141       parser.phpParserTester(strEval, 1);
142     } catch (CoreException e) {
143     }
144   }
145
146   private void checkHTML(String strEval) {
147     try {
148       if (Scanner.DEBUG) {
149         System.out.println("\n------------------------------------");
150         System.out.println(strEval);
151       }
152       parser.parse(strEval);
153     } catch (CoreException e) {
154     }
155   }
156
157   /**
158    *  The JUnit setup method
159    */
160   protected void setUp() {
161     parser = new Parser(null);
162   }
163
164 }