PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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