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 **********************************************************************/
11 import org.eclipse.core.runtime.CoreException;
12 import junit.framework.TestCase;
14 import java.io.CharArrayReader;
19 * Tests the php parser
21 public class PHPParserTestCase2 extends TestCase {
25 public PHPParserTestCase2(String name) {
30 * Test the PHP Parser with different PHP snippets
32 public void testPHPParser() {
33 //checkHTML(new File("class.adm_gestuser.php"));
34 checkHTML("<?php ec ho 'coucou'; ?>" +
43 checkHTML("<?php echo 'coucou'; ?>" +
50 checkHTML("<html>sdfsdf <?php phpinfo(); ?>");
51 checkHTML("\n\n\n\n <?php print \"Hello world\"?> ");
52 checkHTML("<?php phpinfo()?>");
53 checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>");
54 checkHTML(" <?php //this is a line comment ?>");
56 checkPHP("if $cac a) echo 'coucou';");
57 checkPHP("$oka dd = 'a'.$i;$val = $$add;");
58 checkPHP("($a==\"b\") || (c($this->x)==\"d\");");
59 checkPHP("(substr($this->file, 0, 2) == \"MM\");");
60 checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
61 checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
62 checkPHP("$this->highlightfile->linkscripts{$category};");
63 checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module);");
64 checkPHP("$this->startmap[$startcurrtag]();");
65 checkPHP("new $this->startmap[$startcurrtag]();");
66 checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();");
67 checkPHP("echo \"Test\", \"me\";");
68 checkPHP("print (\"Test me\");");
69 // checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
70 // checkPHP("$a == 0 ? print \"true\" : print \"false\";");
71 checkPHP("if(!$result = mysql_query($sql)) return(array());");
72 checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
73 checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
74 checkPHP("@$connect_function($dbhost, $user, $pw);");
75 checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
76 checkPHP("global ${$objectname}; ");
77 checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; } ");
78 checkPHP("unset($this->blockvariables[$block][$varname]);");
79 checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
80 checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
81 checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
82 checkPHP("static $last_run = 0;");
83 checkPHP("unset($headers['Subject']);");
84 checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
85 checkPHP("function validateAndParseResponse($code, &$arguments) { }");
86 checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
87 checkPHP("$this->container = new $container_class($container_options);");
88 checkPHP("class Cmd extends PEAR { var $arrSetting = array(); }");
89 checkPHP("class Cmd extends PEAR { var $arrSetting = array(), $i=10; }");
90 checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
91 checkPHP("require_once(\"mainfile.php\"); ");
92 checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
93 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
94 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n");
95 checkPHP("$tipath = \"images/topics/\";");
96 checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
97 checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
98 checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
99 checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
100 checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
101 checkPHP("if (!$name) { \n }");
102 checkPHP("mt_srand((double)microtime()*1000000);");
103 checkPHP("\"\\\"\";");
104 checkPHP("$v->read();");
105 checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
106 checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
107 checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
108 checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
109 checkPHP("if ($term{0}!=$firstChar) {}");
111 "echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
112 + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
113 + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
115 checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */;");
116 checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){ } ");
117 checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
119 "if (!isset($message)){ \n"
120 + "$message = $myrow[post_text];\n"
121 + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" . $myrow[user_sig], $message); \n"
122 + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
123 + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
124 checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));");
125 checkPHP("$ol = new Overlib();");
126 checkPHP("$risultato = mysql_query($sql) or\n die(mysql_error());");
129 private void checkPHP(String strEval) {
131 System.out.println("strEval = " + strEval);
132 parser.phpParserTester(strEval);
133 } catch (CoreException e) {
135 } catch (ParseException e) {
140 private void checkHTML(String strEval) {
142 System.out.println("strEval = " + strEval);
143 parser.htmlParserTester(strEval);
144 } catch (CoreException e) {
146 } catch (ParseException e) {
152 private void checkHTML(File strEval) {
154 System.out.println("strEval = " + strEval.toString());
155 parser.htmlParserTester(strEval);
156 } catch (CoreException e) {
158 } catch (ParseException e) {
164 * The JUnit setup method
166 protected void setUp() {
167 parser = (test.PHPParser) PHPParserManager.getParser();