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