1 package junit.sourceforge.phpeclipse;
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 **********************************************************************/
11 import junit.framework.TestCase;
12 import test.PHPParser2;
13 import test.ParseException;
15 import java.io.CharArrayReader;
16 import java.io.InputStream;
18 import net.sourceforge.phpeclipse.phpeditor.phpparser.PHPParser;
19 import org.eclipse.core.runtime.CoreException;
22 * Tests the php parser
24 public class PHPParserTestCase2 extends TestCase {
28 public PHPParserTestCase2(String name) {
33 * Test the PHP Parser with different PHP snippets
35 public void testPHPParser() {
37 checkPHP("$b = ($c);");
38 checkPHP("$b = ($b) ? $a : $c;");
39 checkPHP("$b = $b.$a && $f.'a' || 'n '.$b;");
40 checkPHP("$toto = 0;");
41 checkPHP("$toto = \"tata\";");
42 checkPHP("$toto = 'tata';");
43 checkPHP("$$a = 'toto';");
44 checkPHP("$add = 'a'.$i;");
45 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->startmap[$startcurrtag]();");
52 checkPHP("new $this->startmap[$startcurrtag]();");
53 checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();");
54 checkPHP("echo \"Test\", \"me\";");
55 checkPHP("print (\"Test me\");");
56 // checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;"); invalid
57 checkPHP("$a == 0 ? print (\"true\") : print (\"false\");");
58 checkPHP("if(!$result = mysql_query($sql)) return(array());");
59 checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
60 checkPHP("@$connect_function($dbhost, $user, $pw);");
61 checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
62 checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; } ");
63 checkPHP("unset($this->blockvariables[$block][$varname]);");
64 checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
65 checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
66 checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
67 checkPHP("static $last_run = 0;");
68 checkPHP("unset($headers['Subject']);");
69 checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
70 checkPHP("function validateAndParseResponse($code, &$arguments) { }");
71 checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
72 checkPHP("$this->container = new $container_class($container_options);");
73 checkPHP("class Cmd extends PEAR { var $arrSetting = array(); }");
74 checkPHP("class Cmd extends PEAR { var $arrSetting = array(), $i=10; }");
75 checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
76 checkPHP("require_once(\"mainfile.php\"); ");
77 checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
78 checkPHP("$tipath = \"images/topics/\";");
79 checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
80 checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
81 checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
82 checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
83 checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
84 checkPHP("if (!$name) { \n }");
85 checkPHP("mt_srand((double)microtime()*1000000);");
86 checkPHP("\"\\\"\";");
87 checkPHP("$v->read();");
88 checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
89 checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
90 checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
91 checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
93 "echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
94 + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
95 + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
97 checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */");
98 checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){ } ");
99 checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
100 checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));");
101 checkPHP("$ol = new Overlib();");
102 checkPHP("$risultato = mysql_query($sql) or\n die(mysql_error());");
104 "if (!isset($message)){ \n"
105 + "$message = $myrow[post_text];\n"
106 + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" . $myrow[user_sig], $message); \n"
107 + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
108 + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
109 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
110 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n");
111 checkPHP("if ($term{0}!=$firstChar) {}");
112 checkPHP("$this->highlightfile->linkscripts{$category}");
113 checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module)");
114 checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
115 checkPHP("global ${$objectname}; ");
118 private void checkPHP(String strEval) {
120 phpparser.ReInit(new CharArrayReader(strEval.toCharArray()));
122 } catch (ParseException e) {
123 System.out.println(strEval);
129 * The JUnit setup method
131 protected void setUp() {
132 phpparser = new PHPParser2(new CharArrayReader("".toCharArray()));