PHP Classes

File: test/test.php

Recommend this page to a friend!
  Classes of Manuel Lemos   PHP Forms Class with HTML Generator and JavaScript Validation   test/test.php   Download  
File: test/test.php
Role: Unit test script
Content type: text/plain
Description: Unit test suite script
Class: PHP Forms Class with HTML Generator and JavaScript Validation
HTML forms generation and validation.
Author: By
Last change: Added support to avoid giving focus when the focus input is not set.
Date: 5 years ago
Size: 7,479 bytes
 

Contents

Class file image Download
<?php

/*
 * test.php
 *
 * @(#) $Id: test.php,v 1.9 2019/01/18 06:08:01 mlemos Exp $
 *
 */

   
$__tests=array(
       
'singleclienterror'=>array(
           
'script'=>'../test_form.php',
           
'generatedfile'=>'generated/test_form.php.html',
           
'expectedfile'=>'expect/test_form.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>0,
               
'ErrorMessagePrefix'=>''
           
),
           
'clear'=>array(
               
'doit'
           
)
        ),
       
'allclienterrors'=>array(
           
'script'=>'../test_form.php',
           
'generatedfile'=>'generated/all_client_errors_test_form.php.html',
           
'expectedfile'=>'expect/all_client_errors_test_form.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>1,
               
'ErrorMessagePrefix'=>''
           
),
           
'clear'=>array(
               
'doit'
           
)
        ),
       
'singleservererror'=>array(
           
'script'=>'../test_form.php',
           
'generatedfile'=>'generated/server_test_form.php.html',
           
'expectedfile'=>'expect/server_test_form.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>0,
               
'ErrorMessagePrefix'=>''
           
),
           
'post'=>array(
               
'doit'=>'1'
           
)
        ),
       
'allservererrors'=>array(
           
'script'=>'../test_form.php',
           
'generatedfile'=>'generated/all_server_errors_test_form.php.html',
           
'expectedfile'=>'expect/all_server_errors_test_form.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>1,
               
'ErrorMessagePrefix'=>''
           
),
           
'post'=>array(
               
'doit'=>'1'
           
)
        ),
       
'singleclienterrordate'=>array(
           
'script'=>'../test_date_input.php',
           
'generatedfile'=>'generated/test_date_input.php.html',
           
'expectedfile'=>'expect/test_date_input.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>0,
               
'today'=>'2000-01-01',
               
'start_date'=>'2000-01-02',
               
'end_date'=>'2000-01-08'
           
),
           
'clear'=>array(
               
'doit'
           
)
        ),
       
'setdate'=>array(
           
'script'=>'../test_date_input.php',
           
'generatedfile'=>'generated/set_date_test_date_input.php.html',
           
'expectedfile'=>'expect/set_date_test_date_input.php.html',
           
'options'=>array(
               
'set_date'=>'2000-01-01',
               
'today'=>'2000-01-01',
               
'start_date'=>'2000-01-01',
               
'end_date'=>'2000-01-01'
           
),
        ),
       
'allclienterrorsdate'=>array(
           
'script'=>'../test_date_input.php',
           
'generatedfile'=>'generated/all_client_errors_test_date_input.php.html',
           
'expectedfile'=>'expect/all_client_errors_test_date_input.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>1,
               
'today'=>'2000-01-01',
               
'start_date'=>'2000-01-02',
               
'end_date'=>'2000-01-08'
           
),
           
'clear'=>array(
               
'doit'
           
)
        ),
       
'singleclienterrorage'=>array(
           
'script'=>'../test_age_date_input.php',
           
'generatedfile'=>'generated/test_age_date_input.php.html',
           
'expectedfile'=>'expect/test_age_date_input.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>0,
               
'today'=>'2008-10-01',
               
'start_date'=>'1995-01-01',
               
'end_date'=>'2019-01-01'
           
),
           
'clear'=>array(
               
'doit'
           
)
        ),
       
'allclienterrorsage'=>array(
           
'script'=>'../test_date_input.php',
           
'generatedfile'=>'generated/all_client_errors_test_age_date_input.php.html',
           
'expectedfile'=>'expect/all_client_errors_test_age_date_input.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>1,
               
'today'=>'2000-01-01',
               
'start_date'=>'2000-01-02',
               
'end_date'=>'2000-01-08'
           
),
           
'clear'=>array(
               
'doit'
           
)
        ),
       
'setagedate'=>array(
           
'script'=>'../test_age_date_input.php',
           
'generatedfile'=>'generated/set_age_date_test_date_input.php.html',
           
'expectedfile'=>'expect/set_age_date_test_date_input.php.html',
           
'options'=>array(
               
'set_date'=>'2000-01-01',
               
'today'=>'2000-01-01',
               
'start_date'=>'2000-01-01',
               
'end_date'=>'2000-01-01'
           
),
        ),
       
'singleclienterrorcustomvalidation'=>array(
           
'script'=>'../test_custom_validation.php',
           
'generatedfile'=>'generated/test_custom_validation.php.html',
           
'expectedfile'=>'expect/test_custom_validation.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>0,
            ),
           
'clear'=>array(
               
'doit'
           
)
        ),
       
'allclienterrorscustomvalidation'=>array(
           
'script'=>'../test_custom_validation.php',
           
'generatedfile'=>'generated/all_client_errors_test_custom_validation.php.html',
           
'expectedfile'=>'expect/all_client_errors_test_custom_validation.php.html',
           
'options'=>array(
               
'ShowAllErrors'=>1,
            ),
           
'clear'=>array(
               
'doit'
           
)
        ),
       
'javascriptstringescaping'=>array(
           
'script'=>'../test_javascript_string_escaping.php',
           
'generatedfile'=>'generated/test_javascript_string_escaping.php.txt',
           
'expectedfile'=>'expect/test_javascript_string_escaping.php.txt',
        ),
    );

   
define('__TEST',1);
    if(IsSet(
$_SERVER['argv'])
    &&
GetType($_SERVER['argv']) == 'array'
   
&& Count($_SERVER['argv']) > 1)
    {
       
$__few = array();
        for(
$__a = 1; $__a < count($_SERVER['argv']); ++$__a)
        {
           
$__name = $_SERVER['argv'][$__a];
            if(!IsSet(
$__tests[$__name]))
            {
                if(!IsSet(
$__test_sets[$__name]))
                    die(
$__name." is not a valid test name.\n");
                foreach(
$__test_sets[$__name] as $__set_name)
                {
                    if(!IsSet(
$__tests[$__set_name]))
                        die(
$__set_name." is not a valid test name from the set ".$__name.".\n");
                   
$__few[$__set_name] = $__tests[$__set_name];
                }
            }
            else
               
$__few[$__name] = $__tests[$__name];
        }
       
$__tests = $__few;
    }
    for(
$__different=$__test=$__checked=0, Reset($__tests); $__test<count($__tests); Next($__tests), $__test++)
    {
       
$__name=Key($__tests);
       
$__script=$__tests[$__name]['script'];
        if(!
file_exists($__script))
        {
            echo
"\n".'Test script '.$__script.' does not exist.'."\n".str_repeat('_',80)."\n";
            continue;
        }
        echo
'Test "'.$__name.'": ... ';
       
flush();
        if(IsSet(
$__tests[$__name]['options']))
           
$__test_options=$__tests[$__name]['options'];
        else
           
$__test_options=array();
        if(IsSet(
$__tests[$__name]['clear']))
        {
            for(
$__p=0; $__p<count($__tests[$__name]['clear']); $__p++)
            {
               
$__k=$__tests[$__name]['clear'][$__p];
                Unset(
$_POST[$__k]);
                if(IsSet(
$HTTP_POST_VARS))
                    Unset(
$HTTP_POST_VARS[$__k]);
                if(IsSet(
$GLOBALS))
                    Unset(
$GLOBALS[$__k]);
                Unset($
$__k);
            }
        }
        if(IsSet(
$__tests[$__name]['post']))
        {
           
$_POST=$HTTP_POST_VARS=$__tests[$__name]['post'];
           
$_GET=$HTTP_GET_VARS=array();
           
$_SERVER['REQUEST_METHOD']='POST';
        }
        else
        {
           
$_POST=$HTTP_POST_VARS=$_GET=$HTTP_GET_VARS=array();
           
$_SERVER['REQUEST_METHOD']='GET';
        }
       
ob_start();
        require(
$__script);
       
$output=ob_get_contents();
       
ob_end_clean();
       
$generated=$__tests[$__name]['generatedfile'];
        if(!(
$file = fopen($generated, 'wb')))
            die(
'Could not create the generated output file '.$generated."\n");
        if(!
fputs($file, $output)
        || !
fclose($file))
            die(
'Could not save the generated output to the file '.$generated."\n");
       
$expected=$__tests[$__name]['expectedfile'];
        if(!
file_exists($expected))
        {
            echo
"\n".'Expected output file '.$expected.' does not exist.'."\n".str_repeat('_',80)."\n";
            continue;
        }
       
$diff=array();
       
exec('diff '.$expected.' '.$generated, $diff);
        if(
count($diff))
        {
            echo
"FAILED\n".'Output of script '.$__script.' is different from the expected file '.$expected." .\n".str_repeat('_',80)."\n";
            for(
$line=0; $line<count($diff); $line++)
                echo
$diff[$line]."\n";
            echo
str_repeat('_',80)."\n";
           
flush();
           
$__different++;
        }
        else
            echo
"OK\n";
       
$__checked++;
    }
    echo
$__checked.' test '.($__checked==1 ? 'was' : 'were').' performed, '.($__checked!=$__test ? (($__test-$__checked==1) ? ' 1 test was skipped, ' : ($__test-$__checked).' tests were skipped, ') : '').($__different ? $__different.' failed' : 'none has failed').'.'."\n";

?>