PHP Classes
elePHPant
Icontem

File: HtmlTests.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Bruyndonckx  >  HTML Generator Class  >  HtmlTests.php  >  Download  
File: HtmlTests.php
Role: Unit test script
Content type: text/plain
Description: General HTML Tests
Class: HTML Generator Class
Generate HTML documents programmatically
Author: By
Last change:
Date: 7 years ago
Size: 9,000 bytes
 

Contents

Class file image Download
<?php
require_once ('lib/simpletest/unit_tester.php');
require_once (
'html/HtmlLink.inc') ;
require_once (
'html/HtmlList.inc') ;
require_once (
'html/HtmlImage.inc') ;
require_once (
'html/HtmlJavaScript.inc') ;
require_once (
'html/HtmlBlock.inc') ;
require_once (
'html/HtmlTabControl.inc') ;
require_once (
'html/HtmlAligner.inc') ;


class
HtmlTests extends UnitTestCase {

    const
TEST_PATH = "junit/data/" ;

    public function
testLink () {

       
$t = new HtmlLink () ;
       
$r = $t->build() ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<a></a>") ;

       
$t->setHREF ("www.IronMaiden.com") ;
       
$t->setName ("iron maiden") ;
       
$r = $t->build() ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<a href=\"http://www.IronMaiden.com\" name=\"iron maiden\" ></a>") ;

       
$t = new HtmlLink ("http://www.marillion.com") ;
       
$r = $t->build() ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<a href=\"http://www.marillion.com\" ></a>") ;

       
$t = new HtmlLink ("http://www.marillion.com", "Marillion website") ;
       
$t->setOnClick ("on_click") ;
       
$r = $t->build() ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<a href=\"http://www.marillion.com\" onclick=\"on_click\" >Marillion website</a>") ;
        echo
"<hr>$r<hr>";

       
$t = new HtmlLink ('#', 'geen link') ;
       
$t->openInNewWindow();
       
$r = $t->build() ;
       
$this->assertNull ($r) ;
    }

    public function
testOrderedLists () {

       
$t = new HtmlOrderedList () ;
       
$r = $t->build () ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "") ;

       
$t->addData("eerste item") ;
       
$t->addData("tweede item") ;
       
$t->addData(new HtmlLink ("www.ironmaiden.com", "Iron Maiden")) ;

       
$r = $t->build() ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual($r, "<ol>\n<li>eerste item</li>\n<li>tweede item</li>\n<li><a href=\"http://www.ironmaiden.com\" >Iron Maiden</a></li></ol>\n") ;
        echo
"<hr>$r<hr>";
    }

    public function
testUnorderedLists () {
       
$t = new HtmlUnorderedList () ;
       
$r = $t->build () ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "") ;

       
$t->setData (2, "tweede item") ;
       
$t->setData (1, "eerste item") ;
       
$t->setData (0, new HtmlImage ("/junit/data/good_avatar.jpg", "test image")) ;

       
$r = $t->build () ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual($r, "<ul><li><img src=\"/junit/data/good_avatar.jpg\" alt=\"test image\" /></li><li>eerste item</li><li>tweede item</li></ul>") ;
        echo
"<hr>$r<hr>";
    }

    public function
testImage () {

       
$t = new HtmlImage () ;
       
$r = $t->build () ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<img/>") ;

       
$t->setSRC ("image.jpg") ;
       
$t->setAlt ("een voor-beeld") ;
       
$t->setWidth (100) ;
       
$t->setHeight (200) ;
       
$t->setAlign (HTML_RIGHT) ;
       
$t->setBorder (0) ;
       
$r = $t->build () ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities ($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<img src=\"image.jpg\" alt=\"een voor-beeld\" width=\"100\" height=\"200\" align=\"right\" border=\"0\" />") ;

       
// check first if our test-file exists...
       
if (file_exists(HtmlTests::TEST_PATH . "jan.jpg")) {
           
$t = new HtmlImage () ;
           
$t->setImage (HtmlTests::TEST_PATH . "jan.jpg") ;

           
$r = $t->build () ;
           
//print_r (htmlentities ($r)) ; echo "<hr>" ;
           
$this->assertEqual ($r, "<img src=\"junit/data/jan.jpg\" width=\"120\" height=\"79\" />") ;
            echo
"<hr>$r<hr>";
        }
    }

    public function
testJavaScript () {

       
$t = new HtmlJavaScript ("java", "non-java") ;
       
$r = $t->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<script language=\"JavaScript\" type=\"text/javascript\"><!--document.writeln ('java');--></script><noscript>non-java</noscript>") ;

       
$t = new HtmlJavaScript (new HtmlImage(), new HtmlLink ("test", "test")) ;
       
$r = $t->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<script language=\"JavaScript\" type=\"text/javascript\"><!--document.writeln ('<img\/>');--></script><noscript><a href=\"http://test\" >test</a></noscript>") ;

       
$t = new HtmlJavaScript ("java", NULL) ;
       
$t->setRaw() ;
       
$r = $t->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<script language=\"JavaScript\" type=\"text/javascript\"><!--java--></script><noscript></noscript>") ;

       
$t = new HtmlJavaScript ("java", NULL) ;
       
$r = $t->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
print_r (htmlentities($r)) ; echo "<hr>" ;
    }

    public function
testHtmlBlock () {
       
$t = new HtmlBlock () ;
       
$r = $t->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<div></div>") ;

       
$t = new HtmlBlock ("mijn tekst", "my_class", false) ;
       
$r = $t->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
        //print_array (str_split ($r)) ;
       
$this->assertEqual ($r, "<span class=\"my_class\" >mijn tekst</span>") ;

       
$t = new HtmlBlock () ;
       
$t->addData ("een") ;
       
$t->addData (new HtmlImage()) ;
       
$r = $t->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<div>een<img/></div>") ;

       
$t = new HtmlBlock (NULL, '#mystyle') ;
       
$t->addData ("een") ;
       
$r = $t->build() ;
       
$r = str_replace ("\n", "", $r) ;
       
//print_r (htmlentities($r)) ; echo "<hr>" ;
       
$this->assertEqual ($r, "<div style=\"mystyle\" >een</div>") ;
    }

   
// public function testHtmlMarker () {
    // $t = new HtmlMarker ('test') ;
    // $r = $t->build() ;
    // //print_r (htmlentities ($r)) ;
    // $this->assertEqual ($r, '<!-- test -->') ;
    //
    // $t = new HtmlMarker () ;
    // $t->addData ('test=1') ;
    // $t->addData ('karma=10') ;
    // $r = $t->build() ;
    // //print_r (htmlentities ($r)) ;
    // $this->assertEqual ($r, '<!-- test=1,karma=10 -->') ;
    // }

   
public function testSetHREF () {

       
$e = new HtmlElement () ;
       
$t = $e->setHREF ("www.ironmaiden.com") ;
       
$this->assertEqual ($t, "http://www.ironmaiden.com") ;

       
$t = $e->setHREF ("HttP://www.ironmaiden.com") ;
       
$this->assertEqual ($t, "HttP://www.ironmaiden.com") ;

       
$t = $e->setHREF ("#") ;
       
$this->assertEqual ($t, "#") ;

       
$t = $e->setHREF ("javascript:blabla") ;
       
$this->assertEqual ($t, "javascript:blabla") ;

       
$t = $e->setHREF ("") ;
       
$this->assertEqual ($t, "") ;
    }

    public function
testQuote () {
       
$s = 'ja"n' ;
       
$t = HtmlElement::quote ($s) ;
       
$this->assertEqual ($t, 'ja&quot;n') ;
    }


    public function
testHtmlTabControl () {

       
$d = new HtmlDocument ('test') ;

       
$t = new HtmlTabControl ($d, 'skins/default/') ;
       
$t->addTab ('een.html' , 'een') ;
       
$t->addTab ('twee.html', 'twee') ;
       
$t->addTab ('drie.html', 'drie') ;
       
$t->setActive(1) ;
       
$d->setBody ($t) ;

       
$s = $d->build() ;

       
$this->assertPattern ('/navlist.css/', $s) ;
       
$this->assertPattern ('/een.html/' , $s) ;
       
$this->assertPattern ('/twee.html/', $s) ;
       
$this->assertPattern ('/drie.html/', $s) ;

       
$this->assertPattern ('/>een</' , $s) ;
       
$this->assertPattern ('/>twee</', $s) ;
       
$this->assertPattern ('/>drie</', $s) ;

       
$this->assertPattern ('/active/', $s) ;
       
$this->assertPattern ('/current/', $s) ;

        echo
$s ;
    }

    public function
testHorizontalAligner () {

       
$t1 = new HtmlCheckBox('Een test', 'test') ;
       
$t2 = new HtmlLabel('Een label', 'test') ;
       
$t = horizontalAlign ($t1, $t2) ;
       
//echo(htmlentities($t)."<br>");
       
$t = ereg_replace("[ \n\r]", '', $t) ;
       
$expect = str_replace(' ', '', '<table><tr><td></td><td><input name="test" value="Een test" type="checkbox" id="test" /></td><td><label for="test">Een label</label> </td></tr></table>') ;

       
$this->assertEqual ($t, $expect) ;
    }

}

?>