PHP Classes

File: HtmlFormTests.php

Recommend this page to a friend!
  Classes of Bruyndonckx   HTML Generator Class   HtmlFormTests.php   Download  
File: HtmlFormTests.php
Role: Unit test script
Content type: text/plain
Description: Test HTML Forms
Class: HTML Generator Class
Generate HTML documents programmatically
Author: By
Last change:
Date: 16 years ago
Size: 7,941 bytes
 

Contents

Class file image Download
<?php
require_once ('lib/simpletest/unit_tester.php');
require_once (
'html/HtmlForm.inc') ;

define ('DEFAULT_FORM', "<form style=\"display:inline;\" method=\"post\" enctype=\"application/x-www-form-urlencoded\" >") ;

class
HtmlFormTests extends UnitTestCase {


    public function
testTextArea () {

       
$t = new HtmlTextArea () ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$r = str_replace ("\n", "", $r) ;
       
$this->assertEqual ($r,DEFAULT_FORM."<textarea></textarea></form>") ;

       
$t = new HtmlTextArea (2, 40, "name", "class") ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_array (str_split ($r)) ;
       
$this->assertEqual ($r,DEFAULT_FORM."<textarea name=\"name\" rows=\"2\" cols=\"40\" class=\"class\" ></textarea></form>") ;

       
$t = new HtmlTextArea () ;
       
$t->setData ("default text") ;
       
$t->setRows (3) ;
       
$t->setColumns (50) ;
       
$t->setName ("my name") ;
       
$t->setTabIndex (1) ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$r = str_replace ("\n", "", $r) ;
       
$this->assertEqual ($r,DEFAULT_FORM."<textarea name=\"my name\" rows=\"3\" cols=\"50\" tabindex=\"1\" >default text</textarea></form>") ;
        echo
"<hr>$r<hr>";
    }

    public function
testSelect() {

       
$t = new HtmlSelect () ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$r = str_replace ("\n", "", $r) ;
       
$this->assertEqual ($r,DEFAULT_FORM."<select></select></form>") ;

       
$t = new HtmlSelect ("my name") ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r,"<form style=\"display:inline;\" method=\"post\" enctype=\"application/x-www-form-urlencoded\" ><select name=\"my name\" ></select></form>") ;

       
$t = new HtmlSelect ("my name") ;
       
$t->addElement (new HtmlOption ("menu een")) ;
       
$t->addElement (new HtmlOption ("menu twee")) ;
       
$t->addElement (new HtmlOption ("menu drie", "drie")) ;
       
$t->addElement (new HtmlOption ("menu vier", "vier")) ;
       
$t->setMultiple() ;
       
$t->setSize (5) ;
       
$this->assertTrue ($t->setSelectedByValue ("vier")) ;

       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r,DEFAULT_FORM."<select name=\"my name\" size=\"5\" multiple ><option >menu een</option><option >menu twee</option><option value=\"drie\" >menu drie</option><option value=\"vier\" selected >menu vier</option></select></form>") ;
        echo
"<hr>$r<hr>";

       
$t = new HtmlSelect () ;
       
$a = array ("US" => "United States", "BE" => "Belgium", "NL" => "Netherlands") ;
       
$t->addElement ($a) ;
        
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, DEFAULT_FORM."<select><option value=\"US\" >United States</option><option value=\"BE\" >Belgium</option><option value=\"NL\" >Netherlands</option></select></form>") ;
         echo
"<hr>$r<hr>";
    }

    public function
testLabel () {

       
$t = new HtmlLabel () ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r,DEFAULT_FORM."<label></label></form>") ;


       
$t = new HtmlLabel ("mijn label text", "id") ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r,DEFAULT_FORM."<label for=\"id\">mijn label text</label></form>") ;
        echo
"<hr>$r<hr>";
    }

    public function
testFieldSet () {

       
$t = new HtmlFieldSet () ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r,DEFAULT_FORM."<fieldset></fieldset></form>") ;

       
$t = new HtmlFieldSet (new HtmlLabel ("Mijn label"), "legende") ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r,DEFAULT_FORM."<fieldset><legend>legende</legend><label>Mijn label</label></fieldset></form>") ;
        echo
"<hr>$r<hr>";
    }

    public function
testInputFields () {

       
$t = new HtmlFormInput (FORM_TEXT, "text value", "text name") ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
        //print_array (str_split ($r)) ;
       
$this->assertEqual ($r,DEFAULT_FORM."<input name=\"text name\" value=\"text value\" type=\"text\" id=\"text name\" /></form>") ;

       
$t->setName ("another name") ;
       
$t->setValue ("another value") ;
       
$t->setType (FORM_PASSWORD) ;
       
$t->setChecked() ;
       
$t->setDisabled() ;
       
$t->setTabIndex(2) ;
       
$t->setSize(10) ;
       
$t->setMaxLength(100) ;
       
$form = new HtmlForm ($t->build()) ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
        //print_array (str_split ($r)) ;

       
$this->assertEqual ($r,DEFAULT_FORM."<input name=\"another name\" value=\"another value\" type=\"password\" checked disabled tabindex=\"2\" size=\"10\" maxlength=\"100\" id=\"text name\" /></form>") ;

        echo
"<hr>$r<hr>";

       
$table = new HtmlTable () ;
       
$i = 0 ;
       
$table->setData ($i++, 0, new HtmlTextField ()) ;
       
$table->setData ($i++, 0, new HtmlPasswordField ()) ;
       
$table->setData ($i++, 0, new HtmlCheckBox ()) ;
       
$table->setData ($i++, 0, new HtmlRadioButton ()) ;
       
$table->setData ($i++, 0, new HtmlSubmitButton ()) ;
       
$table->setData ($i++, 0, new HtmlResetButton ()) ;
       
$table->setData ($i++, 0, new HtmlFileSubmit ()) ;
       
$table->setData ($i++, 0, new HtmlHiddenField ()) ;

       
$form = new HtmlForm ($table->build()) ;
       
$r = $form->build() ;
        echo
"<hr>$r<hr>";
    }

    public function
testForm () {

       
$form = new HtmlForm () ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<form style=\"display:inline;\" method=\"post\" enctype=\"application/x-www-form-urlencoded\" ></form>") ;

       
$form->setData ("test data") ;
       
$form->setAction ("bad action") ;
       
$form->setEncType ("no enc type") ;

       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<form style=\"display:inline;\" action=\"bad action\" method=\"post\" enctype=\"no enc type\" >test data</form>") ;

       
$form = new HtmlForm ("data", "action", "get", "enctype") ;
       
$r = $form->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<form style=\"display:inline;\" action=\"action\" method=\"get\" enctype=\"enctype\" >data</form>") ;
    }
}

?>