1 package net.sourceforge.phpeclipse.tests.parser;
2 /*******************************************************************************
3 * Copyright (c) 2002 Klaus Hartlage - www.eclipseproject.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 junit.framework.TestCase;
9 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
10 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
12 * Tests the php parser
14 public class PHPParserTestCase extends TestCase {
16 public PHPParserTestCase(String name) {
20 * Test the PHP Parser with different PHP snippets
22 public void testPHPParser() {
23 // checkPHP("if(!$result = mysql_query($sql)) return(array());");
24 checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
26 checkPHP("$ebus_sql['sel_url_list'] = <<<EOS\n"
27 + "select rtrim(URL_NAME) as url_name\n" + " , rtrim(URL) as url\n"
28 + " , rtrim(URL_DESC) as url_desc\n" + "from appl_url\n"
29 + "where appl_instnc_sk = <<INSTNC>>\n"
30 + "and appl_sect_deftn_sk = <<SECT>>\n" + "order by url_ord\n"
32 checkPHP("foreach ($HTTP_GET_VARS as $secvalue) { }");
33 checkPHP("\"\\\"\";");
34 checkPHP("\"\\[addsig]\"");
35 checkPHP("$v->read();");
36 checkPHP("$add = 'a'.$i;$val = $$add;");
37 checkPHP("($a==\"b\") || (c($this->x)==\"d\");");
38 checkPHP("(substr($this->file, 0, 2) == \"MM\");");
39 checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
40 checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
41 checkPHP("$this->highlightfile->linkscripts{$category}");
42 checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module)");
43 checkPHP("$this->startmap[$startcurrtag]();");
44 checkPHP("new $this->startmap[$startcurrtag]();");
45 checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();");
46 checkPHP("echo \"Test\", \"me\";");
47 checkPHP("print (\"Test me\");");
48 checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
49 checkPHP("$a == 0 ? print \"true\" : print \"false\";");
50 checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
51 checkPHP("@$connect_function($dbhost, $user, $pw);");
52 checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
53 checkPHP("global ${$objectname}; ");
54 checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; } ");
55 checkPHP("unset($this->blockvariables[$block][$varname]);");
56 checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
57 checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
58 checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
59 checkPHP("static $last_run = 0;");
60 checkPHP("unset($headers['Subject']);");
61 checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
62 checkPHP("function validateAndParseResponse($code, &$arguments) { }");
63 checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
64 checkPHP("$this->container = new $container_class($container_options);");
65 checkPHP("class Cmd extends PEAR { var $arrSetting = array(); }");
66 checkPHP("class Cmd extends PEAR { var $arrSetting = array(), $i=10; }");
67 checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
68 checkPHP("require_once(\"mainfile.php\"); ");
69 checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n"
70 + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
71 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n"
72 + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
73 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n"
74 + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n");
75 checkPHP("$tipath = \"images/topics/\";");
76 checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
77 checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
78 checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
79 checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
80 checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
81 checkPHP("if (!$name) { \n }");
82 checkPHP("mt_srand((double)microtime()*1000000);");
83 checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
84 checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
85 checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
86 checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
87 checkPHP("if ($term{0}!=$firstChar) {}");
88 checkPHP("echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
89 + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
90 + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
92 checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */");
93 checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){ } ");
94 checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
95 checkPHP("if (!isset($message)){ \n"
96 + "$message = $myrow[post_text];\n"
97 + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" . $myrow[user_sig], $message); \n"
98 + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
99 + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
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());");
103 checkHTML("\n\n\n\n <?php print \"Hello world\" ?>");
104 checkHTML("<?php phpinfo(); ?>");
105 checkHTML("<?php phpinfo()?>");
106 checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>");
107 checkHTML(" <?php //this is a line comment ?>");
108 checkHTML("<?php echo $module_name ?>");
110 private void checkPHP(String strEval) {
112 System.out.println("\n------------------------------------");
113 System.out.println(strEval);
115 parser.phpParserTester(strEval, 1);
117 private void checkHTML(String strEval) {
119 System.out.println("\n------------------------------------");
120 System.out.println(strEval);
122 parser.parse(strEval);
125 * The JUnit setup method
127 protected void setUp() {
128 parser = new Parser(null);