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