SourceForge.net Logo
prevtopnext
izh_test
    Kinds of tests
        Adapted tests and test commands.
php - Testing of php-page, generated by php.

The test allows testing html page generated by php script.

There are three things used as input parameters for php page:

  1. parameters for method GET
  2. parameters for method POST
  3. session variables
  4. content of DBMS
Described test allows passing all these parameters to test script using adapted terms.

As a result, php page returns usually:

  1. text of html-page
  2. session variables
  3. content of DBMS
Described test allows specifying all these things and comparing them with etalon.

The test uses features of standard console runner for php.

Full specification of the test looks like:
<php>
  <spec>
    <sname>test_short_name</sname>
    <fname>test full name</fname>
    <descr>test description</descr>
  </spec>
  <before> .. script items to run before php .. </before>
  <script>a.php</script>
  <out>a.html</out>
  <get>
    <par><name>get name 1</name><value>get value 1</value></par>
    <par><name>get name 2</name><value>get value 2</value></par>
    ...
  </get>
  <post>
    <par><name>post name 1</name><value>post value 1</value></par>
    <par><name>post name 2</name><value>post value 2</value></par>
    ...
  </post>
  <session>
    <par><name>session name 1</name><value>session value 1</value></par>
    <par><name>session name 2</name><value>session value 2</value></par>
    ...
  </session>
  <check_session>false</check_session>
  <after> .. script items to run before php .. </after>
</php>
Where:
script Path to file with source code of php-script. izh_test adds %test_dir% to it automatically.
getParameters passed to the php-script by method GET.
postParameters passed to the php-script by method POST.
session Variables stored in session with their values which passed to the php script in test mode.
parDescription of variable/parameter.
nameName of variable/parameter.
valueValue of variable/parameter.
out

Base file name for storing of result php page.

If the file name looks like [name].htm then

  • name of the result file will be [name].result.htm,
  • name of the etalon file will be [name].etalon.htm,
  • name of the file difference will be [name].diff.

Before using of these file names izh_test adds path to the directory with file description of the test to it so you don't have to use %test_dir% .

check_session

Flag saying if izh_test should check values of session variables after finishing of php-script.

By default false

If value of this flag is true then values of session variables are written in a file to compare it with etalon.

If the file name out (see above description of parameter out) looks like [name].htm then

  • name of the result file with values of session variables will be [name].ses.result.txt,
  • name of the etalon file with values of session variables will be [name].ses.etalon.txt,
  • name of the file difference will be [name].ses.diff.

Before using of these file names izh_test adds path to the directory with file description of the test to it so you don't have to use %test_dir% .

before Test commands executed before running of php script. (Usually preparation of DBMS state)
after Test commands executed after running of php script. (Usually return DBMS to its initial state)
Identification specification spec is standard for all the tests:
spec Identification information for the test (names, descriptions)
sname Short name for test, that is used as test name in GUI-runner and as component of path to the test in console runner . The sname should not contain line breaks. It is better if the sname is short and without white spaces.
fname Full name of test is used as test name in console runner . The fname should not contain line breaks. The fname is placed in one line/screen. So it would be better if the fname is less than 60 symbols.
descr Full description of test. It can be as long as you need and contain any number of line breaks. The description can be shown in the console and GUI runner by special command only.

To use this command you have to initialize value of substitution variable %php% which should contain right path and file name for the console client of php with all the parameters

The test inside is the test script containing 4 parts:

  1. Test commands described in part before. (preparation of test data in DBMS)
  2. Test command exec_check that executes php script and checks result html page. Substitution variable %php% should here contain path and file name to console client of php.
  3. Test command check checked file with written values of the session variables. If flag check_session wasn't set then this part is absent.
  4. Test commands described in part after. (checking of DBMS and cleaning it)

To use the test you have to do number of actions:

  1. Initialize substitution variable %php%, which should contain path and file name to console client of php with all the parameters.
  2. Include module izh_test.php in tested php script. This module contains next code:
    
    $filename_to_out_session= "";
    $root_path= "";
    $use_test_time= false;
    $test_time_count= 0;
    
    function prepare_root_path()
    {
      global $query_params, $root_path;
      if ($query_params["test_depth"]=="")
      {
        $root_path="";
      }
      else
      {
        $root_path="htdocs/";
        for ($i= 0; $i<$query_params["test_depth"]; $i++)
        {
          $root_path= "../".$root_path;
        }
      }
    }
    
    function mysql_get_time()
    {
      global $use_test_time, $test_time_count;
      if (!$use_test_time)
      {
        return "now()";
      }
      else
      {
        $test_time_count++;
        return "DATE_ADD('2000-01-01 23:59:59',INTERVAL ".$test_time_count." DAY)";
      }
    }
    
    function prepare_params()
    { 
      global $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_SESSION_VARS;
      global $argv, $argc;
      global $query_params;
      global $test_mode;
      global $filename_to_out_session, $use_test_time;  
      $query_params= array_merge($HTTP_GET_VARS,$HTTP_POST_VARS);
      if ($argc>1)
      {
        $test_mode= 1;
        for ($i= 1; $i<$argc; $i++)
        {
          $arg= $argv[$i];
          if ($arg=="use_test_time")
          {
            $use_test_time= true;
          }
          else
          {
            $pos= strpos($arg,"=");
            if (is_integer($pos))
            {
              $name=  substr($arg,0,$pos);
              $value= substr($arg,$pos+1);
              if ($name=="outs")
              {
                $filename_to_out_session= $value;
              }
              else if ($name{0}=="s")
              {
                $name= substr($name,1);
                $HTTP_SESSION_VARS[$name]= $value;
              }
              else if ($name{0}=="p")
              {
                $name= substr($name,1);
                $query_params[$name]= $value;
                $HTTP_POST_VARS[$name]= $value;
              }
              else if ($name{0}=="g")
              {
                $name= substr($name,1);
                $query_params[$name]= $value;
                $HTTP_GET_VARS[$name]= $value;
              }
            }
          }
        }
      }
      prepare_root_path();
    }
    
    function safe_trace_session()
    {
      global $filename_to_out_session;
      global $HTTP_SESSION_VARS;
      if ($filename_to_out_session!="")
      {
        $fp= fopen($filename_to_out_session, "wb");
        if ($fp!=-1)
        {
          if (sizeof($HTTP_SESSION_VARS)!=0)
          {
            ob_start();
            ob_flush();
            print_r($HTTP_SESSION_VARS);
            $session_text= str_replace("\n","\r\n",ob_get_contents());
            fwrite($fp,$session_text);
            ob_clean();
          }
          fclose($fp);      
        }
      }
    }
    
  3. Add calling of
    prepare_params();
    at the beginning of php script. This calling prepares correspondent input, output and session parameters.
  4. Add calling of
    safe_trace_session();
    at the end pf php script. This calling stores session variables in file to check them (if it required).

See completed example of testing for php-page below .


prevtopnext

SourceForge.net Logo