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 **********************************************************************/
12 import junit.framework.TestCase;
15 import java.io.FileNotFoundException;
17 import net.sourceforge.phpdt.internal.corext.Assert;
21 * Tests the php parser
23 public class PHPParserTestCase2 extends TestCase {
27 public PHPParserTestCase2(String name) {
32 * Test the PHP Parser with different PHP snippets
34 public void testPHPParser() {
35 checkPHP("$a = $$tata;", true);
36 checkPHP("$$tata=2;", true);
37 checkPHP("$tata;", true);
38 checkPHP("$tata = &new Tutu;", true);
39 checkPHP("$tata = $bobo;", true);
40 checkPHP("function test($tutu) {$id = $this->gogo($titi); echo $id;}", true);
41 checkPHP("function test() { $tata = $b; echo $b;}", true);
42 checkPHP("function test() { echo $b;}", true);
43 checkPHP("for(;;) {}", true);
44 //checkHTML(new File("class.adm_gestuser.php"));
45 checkHTML("<?php $szOpenImg = \"/$location[Treeview]/images/Open.gif\"; ?>", true);
46 checkHTML("<?php function f($a,$b) {" +
47 "echo $a; } ?>", true);
48 checkHTML("<?php ec ho 'coucou'; ?>" +
57 checkHTML("<?php echo 'coucou'; ?>" +
64 checkHTML("<html>sdfsdf <?php phpinfo(); ?>", true);
65 checkHTML("\n\n\n\n <?php print \"Hello world\"?> ", true);
66 checkHTML("<?php phpinfo()?>", true);
67 checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>", true);
68 checkHTML(" <?php //this is a line comment ?>",true);
69 //todo : fix this checkHTML("<?php mysql_query(\"CREATE DATABASE $DB_TABLE\" ) or print 'Error creating database<br>'; ?>",true);
70 checkHTML("<?php function func($a) {$v1 = $v23;}; ?>", true);
71 checkHTML("<?php $a = !$b; ?>", true);
72 checkHTML("<?php $a = @@!!@@@!@coucou(2); ?>", true);
73 checkHTML("<?php $aname= $out['Name'][$z] = $remote[$j]['Name']; ?>", true);
74 checkHTML("<?php +$b; ?>", true);
75 checkHTML("<?php $a = -$b; ?>", true);
77 checkPHP("$a = array();", true);
78 checkPHP("'caca';", true);
79 checkPHP("if $cac a) echo 'coucou';", false);
80 checkPHP("$oka dd = 'a'.$i;$val = $$add;", false);
81 checkPHP("($a==\"b\") || (c($this->x)==\"d\");", true);
82 checkPHP("(substr($this->file, 0, 2) == \"MM\");", true);
83 checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";", true);
84 checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";", true);
85 checkPHP("$this->highlightfile->linkscripts{$category};", true);
86 checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module);", true);
87 checkPHP("$this->startmap[$startcurrtag]();", true);
88 checkPHP("new $this->startmap[$startcurrtag]();", true);
89 checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();", true);
90 checkPHP("echo \"Test\", \"me\";", true);
91 checkPHP("print (\"Test me\");", true);
92 // checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
93 // checkPHP("$a == 0 ? print \"true\" : print \"false\";");
94 checkPHP("if(!$result = mysql_query($sql)) return(array());", true);
95 checkPHP("class test { " +
99 "function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) { }" +
101 checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);", true);
102 checkPHP("@$connect_function($dbhost, $user, $pw);", true);
103 checkPHP("$conn = @$connect_function($dbhost, $user, $pw);", true);
104 checkPHP("global ${$objectname}; ", true);
105 checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; } ", true);
106 checkPHP("unset($this->blockvariables[$block][$varname]);", true);
107 checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);", true);
108 checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));", true);
109 checkPHP("define('MAIL_MIME_CRLF', $crlf, true);", false);
110 checkPHP("static $last_run = 0;", true);
111 checkPHP("unset($headers['Subject']);", true);
112 checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }", true);
113 checkPHP("function validateAndParseResponse($code, &$arguments) { }", true);
114 checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");", true);
115 checkPHP("$this->container = new $container_class($container_options);", true);
116 checkPHP("class Cmd extends PEAR { var $arrSetting = array(); }", true);
117 checkPHP("class Cmd extends PEAR { var $arrSetting = array(), $i=10; }", true);
118 checkPHP("if (isset($test)) { } elseif (isset($lang)) { }", true);
119 checkPHP("require_once(\"mainfile.php\"); ", true);
120 checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n", true);
121 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n", true);
122 checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n", true);
123 checkPHP("$tipath = \"images/topics/\";", true);
124 checkPHP("$reasons = array(\"1\", \"2\",\"test\");", true);
125 checkPHP("if ($home == 1) { message_box(); blocks(Center);}", true);
126 checkPHP("$bresult = sql_query(\"select * from \".$functionName.\"_banner WHERE type='0' AND active='1'\", $dbi);", true);
127 checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }", true);
128 checkPHP("list ($catid) = sql_fetch_row($result, $dbi);", true);
129 checkPHP("if (!$name) { \n }", true);
130 checkPHP("mt_srand((double)microtime()*1000000);", true);
131 checkPHP("\"\\\"\";", true);
132 checkPHP("$v->read();", true);
133 checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);", true);
134 checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";", true);
135 checkPHP("if (!empty($pass) AND $pass==$passwd) { }", true);
136 checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);", true);
137 checkPHP("if ($term{0}!=$firstChar) {}", true);
139 "echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
140 + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
141 + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
143 checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */;", true);
144 checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){ } ", true);
145 checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ", true);
147 "if (!isset($message)){ \n"
148 + "$message = $myrow[post_text];\n"
149 + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" . $myrow[user_sig], $message); \n"
150 + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
151 + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ", true);
152 checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));", true);
153 checkPHP("$ol = new Overlib();", true);
154 checkPHP("$risultato = mysql_query($sql) or\n die(mysql_error());", true);
157 private void checkPHP(String strEval, boolean good) {
158 ParseException ex = null;
160 System.out.println("strEval = " + strEval);
161 PHPParser.phpParserTester(strEval);
162 } catch (ParseException e) {
167 Assert.isTrue(ex == null);
168 } catch (RuntimeException e) {
169 ex.printStackTrace();
173 Assert.isNotNull(ex);
177 private void checkHTML(String strEval, boolean good) {
178 ParseException ex = null;
180 System.out.println("strEval = " + strEval);
181 PHPParser.htmlParserTester(strEval);
182 } catch (ParseException e) {
187 Assert.isTrue(ex == null);
188 } catch (RuntimeException e) {
189 ex.printStackTrace();
193 Assert.isNotNull(ex);
197 private void checkHTML(File strEval, boolean good) {
198 ParseException ex = null;
200 System.out.println("strEval = " + strEval.toString());
201 PHPParser.htmlParserTester(strEval);
202 } catch (FileNotFoundException e) {
204 } catch (ParseException e) {
209 Assert.isTrue(ex == null);
210 } catch (RuntimeException e) {
211 ex.printStackTrace();
215 Assert.isNotNull(ex);
220 * The JUnit setup method
222 protected void setUp() {
223 parser = (test.PHPParser) PHPParserManager.getParser();