PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/PHPParser/tags/PHPParser-1.3.1/test/gatchan/phpparser/parser/PHPParserTester.java

#
Java | 349 lines | 291 code | 22 blank | 36 comment | 5 complexity | 9dbf8c8ca0fd788717429aeabdbb68be MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * PHPParserTester.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2003, 2010 Matthieu Casanova
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package gatchan.phpparser.parser;
  22. import junit.framework.Assert;
  23. import junit.framework.TestCase;
  24. import java.io.File;
  25. import java.io.FileNotFoundException;
  26. /**
  27. * The PHP Parser test case.
  28. *
  29. * @author Matthieu Casanova
  30. * @version $Id: PHPParserTester.java 18522 2010-09-08 21:22:36Z kpouer $
  31. */
  32. public class PHPParserTester extends TestCase implements PHPParserListener
  33. {
  34. private PHPParser phpParser;
  35. public void testNew()
  36. {
  37. }
  38. public void testParserSuccess()
  39. {
  40. checkPHP("function objclone() { return clone($z); }");
  41. checkPHP("function objclone() { return clone $z; }");
  42. checkHTML("<? toto();?>");
  43. checkHTML("<? toto($this->highlightfile->linkscripts{$category});?>");
  44. checkHTML("<? while (true) : ?>coucou<? endwhile;?>");
  45. checkHTML("<? if (true) : ?>coucou<? endif;?>");
  46. checkHTML("<? if (true) : ?>coucou<? elseif (true) :?>coucou<? elseif (true) :?>coucou<? else :?>coucou<? endif;?>");
  47. checkHTML("<? while (true) : ?>coucou<? endwhile;?>");
  48. checkHTML("<? foreach ($a as $b) : ?>coucou<? endforeach;?>");
  49. checkHTML("<? for (;;) : ?>coucou<? endfor;?>");
  50. checkPHP("fn()->toto;");
  51. checkPHP("$a = float;");
  52. checkPHP("$link= mysql_connect($this->mysqlHost, $this->mysqlUser, $this->mysqlPassword)\n" +
  53. "or $errMsg= 'Could not connect: ' . mysql_error();");
  54. checkPHP("function method(array $array) {\n}");
  55. checkPHP("if (true or $b = 2) echo 'coucou';");
  56. checkPHP("!feof($fin) && $data = fread($fin, 8096);");
  57. checkPHP("if ($foo = bar()) echo 'coucou';");
  58. checkPHP("echo \"foo$\";");
  59. checkPHP("echo \"$1\";");
  60. checkPHP("echo \"$\";");
  61. checkPHP("echo \"$ foo\";");
  62. checkPHP("$b[1];");
  63. checkPHP("$b[1]->test;");
  64. checkPHP("$b[1]->test();");
  65. checkPHP("$b[1]->test[1];");
  66. checkPHP("$b[1]->test[1]->tata();");
  67. checkPHP("array('a' => float);");
  68. checkPHP("$a = @require 'b';");
  69. checkPHP("@list($sFormatted, $sExt) = explode(' ', $sFormatted, 2);");
  70. checkPHP("$tpl->define(array());");
  71. checkPHP("if ($a && !empty($c)) {echo 'coucou';}");
  72. checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
  73. checkPHP("($plant_loc == \"KV\") ? PRINT \"<TH>OEE %</TH>\" : PRINT \"<TH>UEE %</TH>\";");
  74. checkPHP("$a == 0 ? print \"true\" : print \"false\";");
  75. checkPHP("if(!$result = mysql_query($sql)) return(array());");
  76. checkPHP("!list($a) = $b;");
  77. checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
  78. checkPHP("1 == 1 ? $twovar = $onevar: $twovar = $threevar;");
  79. checkPHP("list($a,$b);");
  80. checkHTML("<?php\n" +
  81. "$heredoc = <<<EOF\n" +
  82. "?>\n" +
  83. "EOF;\n" +
  84. "?>");
  85. checkPHP("if ($some xor $thing) { }");
  86. checkHTML("<?=\"toto\"?>");
  87. checkPHP("$ a = <<<ca\n" +
  88. "\n" +
  89. "toto\n" +
  90. "\n" +
  91. "ca;");
  92. checkPHP("$ebus_sql['sel_url_list'] = <<<EOS\n"
  93. + "select rtrim(URL_NAME) as url_name\n" + " , rtrim(URL) as url\n"
  94. + " , rtrim(URL_DESC) as url_desc\n" + "from appl_url\n"
  95. + "where appl_instnc_sk = <<INSTNC>>\n"
  96. + "and appl_sect_deftn_sk = <<SECT>>\n" + "order by url_ord\n"
  97. + "EOS;\n");
  98. checkHTML("<?php echo $bgcolor2?>");
  99. checkPHP("if ($topic<1) { $topic = 1;}");
  100. checkPHP("$this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);");
  101. checkPHP("$db->sql_query($sql);");
  102. checkPHP("$val = $$add;");
  103. checkPHP("foreach ($HTTP_GET_VARS as $secvalue) { }");
  104. checkPHP("\"\\\"\";");
  105. checkPHP("$v->read();");
  106. checkPHP("$add = 'a'.$i;$val = $$add;");
  107. checkPHP("($a==\"b\") || (c($this->x)==\"d\");");
  108. checkPHP("(substr($this->file, 0, 2) == \"MM\");");
  109. checkPHP("(substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
  110. checkPHP("return (substr($this->file, 0, 2) == \"MM\") || substr($this->file, 0, 2) == \"II\";");
  111. checkPHP("$this->highlightfile->linkscripts{$category};");
  112. checkPHP(
  113. "$code = call_user_method($this->highlightfile->linkscripts{$category}, $this->highlightfile, $oldword, $this->output_module);");
  114. checkPHP("$this->startmap[$startcurrtag]();");
  115. checkPHP("new $this->startmap[$startcurrtag]();");
  116. checkPHP("$this->highlightfile = new $this->startmap[$startcurrtag]();");
  117. checkPHP("echo \"Test\", \"me\";");
  118. checkPHP("print (\"Test me\");");
  119. checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;");
  120. checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
  121. checkPHP("$connect_function($dbhost, $user, $pw);");
  122. checkPHP("@$connect_function($dbhost, $user, $pw);");
  123. checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
  124. checkPHP("global ${$objectname}; ");
  125. checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; } ");
  126. checkPHP("unset($this->blockvariables[$block][$varname]);");
  127. checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
  128. checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
  129. // checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
  130. checkPHP("static $last_run = 0;");
  131. checkPHP("unset($headers['Subject']);");
  132. checkPHP(
  133. "switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
  134. checkPHP("function validateAndParseResponse($code, &$arguments) { }");
  135. checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
  136. checkPHP("$this->container = new $container_class($container_options);");
  137. checkPHP("class Cmd extends PEAR { var $arrSetting = array(); }");
  138. checkPHP("class Cmd extends PEAR { var $arrSetting = array(), $i=10; }");
  139. checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
  140. checkPHP("require_once(\"mainfile.php\"); ");
  141. checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n"
  142. + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
  143. checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n"
  144. + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
  145. checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n"
  146. + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n");
  147. checkPHP("$tipath = \"images/topics/\";");
  148. checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
  149. checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
  150. checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
  151. checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
  152. checkPHP("if (!$name) { \n }");
  153. checkPHP("mt_srand((double)microtime()*1000000);");
  154. checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
  155. checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
  156. checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
  157. checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
  158. checkPHP("if ($term{0}!=$firstChar) {}");
  159. checkPHP("echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
  160. + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
  161. + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n;");
  162. checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */");
  163. checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){ } ");
  164. checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
  165. checkPHP("if (!isset($message)){ \n"
  166. + "$message = $myrow[post_text];\n"
  167. + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" . $myrow[user_sig], $message); \n"
  168. + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
  169. + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
  170. checkPHP(
  171. "do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));");
  172. checkPHP("$ol = new Overlib();");
  173. checkPHP("$risultato = mysql_query($sql) or\n die(mysql_error());");
  174. checkHTML("\n\n\n\n <?php print \"Hello world\" ?>");
  175. checkHTML("<?php phpinfo(); ?>");
  176. checkHTML("<?php phpinfo()?>");
  177. checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>");
  178. checkHTML(" <?php //this is a line comment ?>");
  179. checkHTML("<?php echo $module_name ?>");
  180. checkHTML("<?php $x = function() { echo 'hello'; }; ?>");
  181. checkPHP("$foo = \"{$_POST}\";");
  182. checkPHP("$foo = \"$_POST\";");
  183. checkPHP("$foo = \"$_POST['some name']\";");
  184. checkPHP("$foo = \"{$_POST['some name']}\";");
  185. checkPHP("TR_TreeAction::getInstance('containers')->isRoot(5);");
  186. }
  187. public void testParserSinglePHP5()
  188. {
  189. checkPHP("interface Test { function tutu(); }");
  190. }
  191. public void testParserSuccessPHP5SpecialSyntax()
  192. {
  193. checkPHP("TR_TreeAction::getInstance('containers');");
  194. checkPHP("TR_TreeAction::getInstance('containers')->isRoot(5);");
  195. checkPHP("function method(array $array) {\n}");
  196. checkPHP("abstract class Test {}");
  197. checkPHP("abstract class Test { var $toto,$tata;}");
  198. checkPHP("abstract class Test { $toto;}");
  199. checkPHP("abstract class Test { private $toto;}");
  200. checkPHP("abstract class Test { const toto = 3;}");
  201. checkPHP("abstract class Test { function tutu() {} }");
  202. checkPHP("abstract class Test { private function tutu() {} }");
  203. checkPHP("abstract class Test { abstract function tutu(); }");
  204. checkPHP("abstract class Test { abstract protected function tutu(); }");
  205. checkPHP("abstract class Test { protected abstract function tutu(); }");
  206. checkPHP("abstract class Test { final function tutu() {} }");
  207. checkPHP("abstract class Test { private final function tutu() {} }");
  208. checkPHP("abstract class Test { final private function tutu() {} }");
  209. checkPHP("interface Test { function tutu(); }");
  210. checkPHP("interface Test { const tata = 3; }");
  211. checkPHP("interface Test extends Tata { function tutu(); }");
  212. checkPHP("interface Test extends Tata, Toto { function tutu(); }");
  213. checkPHP("class Test extends Tata { protected static function tutu() {} }");
  214. checkPHP("class Test extends Tata { protected final static function tutu() {} }");
  215. checkPHP("class Test extends Tata { final protected static function tutu() {} }");
  216. checkPHP("class Test implements Toto { }");
  217. checkPHP("class Test implements Toto, Tata { }");
  218. checkPHP("function tutu(Toto $a) { }");
  219. checkPHP("try { } catch(MyException $a) {}");
  220. checkPHP("throw new Toto();");
  221. checkPHP("$b[1]->test()->tutu();");
  222. }
  223. private void checkPHP(String s)
  224. {
  225. try
  226. {
  227. System.out.println(s);
  228. phpParser.phpParserTester(s);
  229. }
  230. catch (ParseException e)
  231. {
  232. Assert.fail(e.getMessage());
  233. System.out.println(s);
  234. e.printStackTrace();
  235. }
  236. }
  237. private void checkHTML(String s)
  238. {
  239. try
  240. {
  241. System.out.println(s);
  242. phpParser.htmlParserTester(s);
  243. }
  244. catch (ParseException e)
  245. {
  246. System.out.println(s);
  247. Assert.fail(e.getMessage());
  248. }
  249. catch (Error e)
  250. {
  251. System.out.println(s);
  252. Assert.fail(e.getMessage());
  253. }
  254. }
  255. private void checkHTML(final File strEval, final boolean good)
  256. {
  257. ParseException ex = null;
  258. try
  259. {
  260. System.out.println("strEval = " + strEval.toString());
  261. /* PHPParser phpParser = new PHPParser();
  262. phpParser.parse(); */
  263. phpParser.htmlParserTester(strEval);
  264. }
  265. catch (FileNotFoundException e)
  266. {
  267. e.printStackTrace();
  268. }
  269. catch (ParseException e)
  270. {
  271. ex = e;
  272. }
  273. if (good)
  274. {
  275. try
  276. {
  277. assertTrue(ex == null);
  278. }
  279. catch (RuntimeException e)
  280. {
  281. ex.printStackTrace();
  282. throw e;
  283. }
  284. }
  285. else
  286. {
  287. assertNotNull(ex);
  288. }
  289. }
  290. private void testDirectory(final File file)
  291. {
  292. if (file.isDirectory())
  293. {
  294. final File[] files = file.listFiles();
  295. for (int i = 0; i < files.length; i++)
  296. {
  297. testDirectory(files[i]);
  298. }
  299. }
  300. else
  301. {
  302. if (file.getName().toUpperCase().endsWith(".PHP"))
  303. {
  304. checkHTML(file, true);
  305. }
  306. }
  307. }
  308. public void parseError(PHPParseErrorEvent e)
  309. {
  310. throw new Error(e.getMessage());
  311. }
  312. public void parseMessage(PHPParseMessageEvent e)
  313. {
  314. }
  315. // public void testPHPParserWithFiles()
  316. // {
  317. // final String folderName = "plugins/PHPParser/phpFiles";
  318. // final File dirName = new File(folderName);
  319. // assertTrue(dirName.isDirectory() || dirName.exists());
  320. // testDirectory(dirName);
  321. // }
  322. protected void setUp() throws Exception
  323. {
  324. phpParser = new PHPParser();
  325. phpParser.addParserListener(this);
  326. }
  327. }