1 package junit.sourceforge.phpeclipse;
 
   2 /**********************************************************************
 
   3 Copyright (c) 2002 Klaus Hartlage - www.eclipseproject.de
 
   4 All rights reserved. This program and the accompanying materials
 
   5 are made available under the terms of the Common Public License v1.0
 
   6 which accompanies this distribution, and is available at
 
   7 http://www.eclipse.org/legal/cpl-v10.html
 
   8 **********************************************************************/
 
  10 import org.eclipse.core.runtime.CoreException;
 
  12 import junit.framework.TestCase;
 
  14 import net.sourceforge.phpeclipse.phpeditor.PHPParser;
 
  17  *  Tests the php parser
 
  19 public class PHPParserTestCase extends TestCase {
 
  23   public PHPParserTestCase(String name) {
 
  28    *  Test the PHP Parser with different PHP snippets
 
  30   public void testPHPParser() {
 
  31     checkHTML("\n\n\n\n  <?php print \"Hello world\" ?>");
 
  32     checkHTML("<?php phpinfo(); ?>");
 
  33     checkHTML("<?php phpinfo()?>");
 
  34     checkHTML("<?php phpinfo(); ?> foo <?php phpinfo(); ?>");
 
  35     checkHTML(" <?php //this is a line comment ?>");
 
  37     checkPHP("echo \"Test\", \"me\";");
 
  38     checkPHP("print (\"Test me\");");
 
  39     checkPHP("$s = <<<HEREDOC \n dskjfhskj\n \n\nHEREDOC;"); 
 
  40     checkPHP("$a == 0 ? print \"true\" : print \"false\";");
 
  41     checkPHP("if(!$result = mysql_query($sql)) return(array());");
 
  42     checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }");
 
  43     checkPHP("call_user_method_array($function_name[1], ${$objectname}, $arguments);");
 
  44     checkPHP("@$connect_function($dbhost, $user, $pw);");
 
  45     checkPHP("$conn = @$connect_function($dbhost, $user, $pw);");
 
  46     checkPHP("global ${$objectname}; ");
 
  47     checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; }  ");
 
  48     checkPHP("unset($this->blockvariables[$block][$varname]);");
 
  49     checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);");
 
  50     checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));");
 
  51     checkPHP("define('MAIL_MIME_CRLF', $crlf, true);");
 
  52     checkPHP("static $last_run = 0;");
 
  53     checkPHP("unset($headers['Subject']);");
 
  54     checkPHP("switch($func) {\n case \"f0\":\n case \"f1\":\n f1();\n break; \n case \"tt\": \n default: \n f0(); \n break;\n }");
 
  55     checkPHP("function validateAndParseResponse($code, &$arguments) { }");
 
  56     checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");");
 
  57     checkPHP("$this->container = new $container_class($container_options);");
 
  58     checkPHP("class Cmd extends PEAR { var $arrSetting     = array(); }");
 
  59     checkPHP("class Cmd extends PEAR { var $arrSetting     = array(), $i=10; }");
 
  60     checkPHP("if (isset($test)) { } elseif (isset($lang)) { }");
 
  61     checkPHP("require_once(\"mainfile.php\");  ");
 
  62     checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
 
  63     checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) {\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "}\n");
 
  64     checkPHP("while (eregi(\"footer.php\",$PHP_SELF)) :\n" + "Header(\"Location: index.php\");\n" + "die();\n" + "endwhile;\n");
 
  65     checkPHP("$tipath = \"images/topics/\";");
 
  66     checkPHP("$reasons = array(\"1\", \"2\",\"test\");");
 
  67     checkPHP("if ($home == 1) { message_box(); blocks(Center);}");
 
  68     checkPHP("$bresult = sql_query(\"select * from \".$prefix.\"_banner WHERE type='0' AND active='1'\", $dbi);");
 
  69     checkPHP("switch($func) {\n case \"f1\":\n f1();\n break; \n default: \n f0(); \n break;\n }");
 
  70     checkPHP("list ($catid) = sql_fetch_row($result, $dbi);");
 
  71     checkPHP("if (!$name) { \n }");
 
  72     checkPHP("mt_srand((double)microtime()*1000000);");
 
  73     checkPHP("\"\\\"\";");
 
  74     checkPHP("$v->read();");
 
  75     checkPHP("$alttext = ereg_replace(\"\\\"\", \"\", $alttext);");
 
  76     checkPHP("$message .= \"\"._THISISAUTOMATED.\"\\n\\n\";");
 
  77     checkPHP("if (!empty($pass) AND $pass==$passwd) { }");
 
  78     checkPHP("$AllowableHTML = array(\"b\"=>1,\n \"i\"=>1);");
 
  79     checkPHP("if ($term{0}!=$firstChar) {}");
 
  81       "echo \"<center><b>\"._NOADMINYET.\"</b></center><br><br>\"\n"
 
  82         + ".\"<form action=\\\"admin.php\\\" method=\\\"post\\\">\"\n"
 
  83         + ".\"<tr><td><b>\"._NICKNAME.\":</b></td><td><input type=\\\"text\\\" name=\\\"name\\\" size=\\\"30\\\" maxlength=\\\"25\\\"></td></tr>\"\n"
 
  85     checkPHP("/* \n overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/) \n */");
 
  86     checkPHP("if ($arrAtchCookie[1]==0 && $IdAtchPostId!=null){  } ");
 
  87     checkPHP("$arrAtchCookie[1] -= filesize(realpath($AtchTempDir).\"/\".$xattachlist)/ 1024; ");
 
  89       "if (!isset($message)){ \n"
 
  90         + "$message = $myrow[post_text];\n"
 
  91         + "$message = eregi_replace(\"\\[addsig]\", \"\\n-----------------\\n\" .    $myrow[user_sig], $message); \n"
 
  92         + "$message = str_replace(\"<BR>\", \"\\n\", $message); \n"
 
  93         + "$message = str_replace(\"<br>\", \"\\n\", $message); \n } ");
 
  94     checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));");
 
  95     checkPHP("$ol = new Overlib();");
 
  96     checkPHP("$risultato = mysql_query($sql) or\n    die(mysql_error());");
 
  99   private void checkPHP(String strEval) {
 
 101       parser.phpParse(strEval, 1);
 
 102     } catch (CoreException e) {
 
 106   private void checkHTML(String strEval) {
 
 107     parser.htmlParse(strEval);
 
 111    *  The JUnit setup method
 
 113   protected void setUp() {
 
 114     parser = new PHPParser(null);