538248b95b9f77ebfd387b2c8a719787b11e0a4c
[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 junit.framework.TestCase;
13
14 import java.io.File;
15 import java.io.FileNotFoundException;
16
17 import net.sourceforge.phpdt.internal.corext.Assert;
18
19
20 /**
21  *  Tests the php parser
22  */
23 public class PHPParserTestCase2 extends TestCase {
24
25   PHPParser parser;
26
27   public PHPParserTestCase2(final String name) {
28     super(name);
29   }
30
31   public void testPHPParserWithFiles() {
32     final String folderName = "phpfiles";
33     final File dirName = new File(folderName);
34     Assert.isTrue(dirName.isDirectory() || dirName.exists());
35     testDirectory(dirName);
36   }
37
38
39   /**
40    *  Test the PHP Parser with different PHP snippets
41    */
42   public void testPHPParser() {
43     checkPHP("for($i;$i;){}", true);
44     checkPHP("echo $this->$a;", true);
45     checkPHP("for ($this->i = 0;$this->i<2;$this->i++) {}", true);
46     checkPHP("for ($i = 2;$i<3;$i+= 3) {}", true);
47     checkPHP("function c($a,$b) {}", true);
48     checkPHP("$a = $$tata;", true);
49     checkPHP("$$tata=2;", true);
50     checkPHP("$tata;", true);
51     checkPHP("$tata = &new Tutu;", true);
52     checkPHP("$tata = $bobo;", true);
53     checkPHP("function test($tutu) {$id = $this->gogo($titi); echo $id;}", true);
54     checkPHP("function test() { $tata = $b; echo $b;}", true);
55     checkPHP("function test() { echo $b;}", true);
56     checkPHP("for(;;) {}", true);
57     //checkHTML(new File("class.adm_gestuser.php"));
58     checkHTML("<?php $szOpenImg   = \"/$location[Treeview]/images/Open.gif\"; ?>", true);
59     checkHTML("<?php function f($a,$b) {" +
60             "echo $a; } ?>", true);
61     checkHTML("<?php ec ho 'coucou'; ?>" +
62             "\n dfgdfgfdfg" +
63             "\n" +
64             "\n" +
65             "<?php" +
66             "\necho ' caca';" +
67             "\n?>" +
68             "dfgdfg" +
69             "\ndsfgdf", false);
70     checkHTML("<?php echo 'coucou'; ?>" +
71             "\n dfgdfgfdfg" +
72             "\n" +
73             "\n" +
74             "<?php" +
75             "\necho ' caca';" +
76             "\n?>", true);
77     checkHTML("<html>sdfsdf  <?php phpinfo(); ?>", true);
78     checkHTML("\n\n\n\n  <?php print \"Hello world\"?> ", true);
79     checkHTML("<?php phpinfo()?>", true);
80     checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>", true);
81     checkHTML(" <?php //this is a line comment ?>", true);
82 //todo : fix this    checkHTML("<?php mysql_query(\"CREATE DATABASE $DB_TABLE\" ) or print 'Error creating database<br>'; ?>",true);
83     checkHTML("<?php function func($a) {$v1 = $v23;}; ?>", true);
84     checkHTML("<?php $a = !$b; ?>", true);
85     checkHTML("<?php $a = @@!!@@@!@coucou(2); ?>", true);
86     checkHTML("<?php $aname= $out['Name'][$z] = $remote[$j]['Name']; ?>", true);
87     checkHTML("<?php +$b; ?>", true);
88     checkHTML("<?php $a = -$b; ?>", true);
89
90     checkPHP("$a = array();", true);
91     checkPHP("'caca';", true);
92     checkPHP("if $cac a) echo 'coucou';", false);
93     checkPHP("$oka dd = 'a'.$i;$val = $$add;", false);
94     checkPHP("($a==\"b\") || (c($this->x)==\"d\");", true);
95     checkPHP("(substr($this->file, 0, 2) == \"MM\");", true);
96     checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";", true);
97     checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";", true);
98     checkPHP("$this->highlightfile->linkscripts{$category};", true);
99     checkPHP("$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module);", true);
100     checkPHP("$this->startmap[$startcurrtag]();", true);
101     checkPHP("new $this->startmap[$startcurrtag]();", true);
102     checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();", true);
103     checkPHP("echo \"Test\", \"me\";", true);
104     checkPHP("print (\"Test me\");", true);
105 //    checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
106 //    checkPHP("$a == 0 ? print \"true\" : print \"false\";");
107     checkPHP("if(!$result = mysql_query($sql)) return(array());", true);
108     checkPHP("class test { " +
109             "  var $t;" +
110             "  var $tutu,$toto;" +
111             "  var $a = 2;" +
112             "function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) {  }" +
113             "}", true);
114     checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);", true);
115     checkPHP("@$connect_function($dbhost, $user, $pw);", true);
116     checkPHP("$conn = @$connect_function($dbhost, $user, $pw);", true);
117     checkPHP("global ${$objectname}; ", true);
118     checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; }  ", true);
119     checkPHP("unset($this->blockvariables[$block][$varname]);", true);
120     checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);", true);
121     checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));", true);
122     checkPHP("define('MAIL_MIME_CRLF', $crlf, true);", false);
123     checkPHP("static $last_run = 0;", true);
124     checkPHP("unset($headers['Subject']);", true);
125     checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }", true);
126     checkPHP("function validateAndParseResponse($code, &$arguments) { }", true);
127     checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");", true);
128     checkPHP("$this->container = new $container_class($container_options);", true);
129     checkPHP("class Cmd extends PEAR { var $arrSetting     = array(); }", true);
130     checkPHP("class Cmd extends PEAR { var $arrSetting     = array(), $i=10; }", true);
131     checkPHP("if (isset($test)) { } elseif (isset($lang)) { }", true);
132     checkPHP("require_once(\"mainfile.php\");  ", true);
133     checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n", true);
134     checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n", true);
135     checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n", true);
136     checkPHP("$tipath = \"images/topics/\";", true);
137     checkPHP("$reasons = array(\"1\", \"2\",\"test\");", true);
138     checkPHP("if ($home == 1) { message_box(); blocks(Center);}", true);
139     checkPHP("$bresult = sql_query(\"select * from \".$functionName.\"_banner WHERE type='0' AND active='1'\", $dbi);", true);
140     checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }", true);
141     checkPHP("list ($catid) = sql_fetch_row($result, $dbi);", true);
142     checkPHP("if (!$name) { \n }", true);
143     checkPHP("mt_srand((double)microtime()*1000000);", true);
144     checkPHP("\"\\\"\";", true);
145     checkPHP("$v->read();", true);
146     checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);", true);
147     checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";", true);
148     checkPHP("if (!empty($pass) AND $pass==$passwd) { }", true);
149     checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);", true);
150     checkPHP("if ($term{0}!=$firstChar) {}", true);
151     checkPHP(
152             "echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
153             + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
154             + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
155             + ";", true);
156     checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */;", true);
157     checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){  } ", true);
158     checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ", true);
159     checkPHP(
160             "if (!isset($message)){ \n"
161             + "$message = $myrow[post_text];\n"
162             + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" .    $myrow[user_sig], $message); \n"
163             + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
164             + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ", true);
165     checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));", true);
166     checkPHP("$ol = new Overlib();", true);
167     checkPHP("$risultato = mysql_query($sql) or\n    die(mysql_error());", true);
168   }
169
170   private void checkPHP(final String strEval, final boolean good) {
171     ParseException ex = null;
172     try {
173       System.out.println("strEval = " + strEval);
174       PHPParser.phpParserTester(strEval);
175     } catch (ParseException e) {
176       ex = e;
177     }
178     if (good) {
179       try {
180         Assert.isTrue(ex == null);
181       } catch (RuntimeException e) {
182         ex.printStackTrace();
183         throw e;
184       }
185     } else {
186       Assert.isNotNull(ex);
187     }
188   }
189
190   private void checkHTML(final String strEval, final boolean good) {
191     ParseException ex = null;
192     try {
193       System.out.println("strEval = " + strEval);
194       PHPParser.htmlParserTester(strEval);
195     } catch (ParseException e) {
196       ex = e;
197     }
198     if (good) {
199       try {
200         Assert.isTrue(ex == null);
201       } catch (RuntimeException e) {
202         ex.printStackTrace();
203         throw e;
204       }
205     } else {
206       Assert.isNotNull(ex);
207     }
208   }
209
210   private void checkHTML(final File strEval, final boolean good) {
211     ParseException ex = null;
212     try {
213       System.out.println("strEval = " + strEval.toString());
214       PHPParser.htmlParserTester(strEval);
215     } catch (FileNotFoundException e) {
216       e.printStackTrace();
217     } catch (ParseException e) {
218       ex = e;
219     }
220     if (good) {
221       try {
222         Assert.isTrue(ex == null);
223       } catch (RuntimeException e) {
224         ex.printStackTrace();
225         throw e;
226       }
227     } else {
228       Assert.isNotNull(ex);
229     }
230   }
231
232   private void testDirectory(final File file) {
233     if (file.isDirectory()) {
234       final File[] files = file.listFiles();
235       for (int i = 0; i < files.length; i++) {
236         testDirectory(files[i]);
237       }
238     } else {
239       if (file.getName().toUpperCase().endsWith(".PHP")) {
240         checkHTML(file, true);
241       }
242     }
243   }
244
245   /**
246    *  The JUnit setup method
247    */
248   protected void setUp() {
249     parser = (test.PHPParser) PHPParserManager.getParser();
250   }
251
252 }