home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Sigma_usage_testcase.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  3.6 KB  |  132 lines

  1. <?php
  2.  
  3. /**
  4.  * Unit tests for HTML_Template_Sigma package.
  5.  * 
  6.  * @author Alexey Borzov <avb@php.net>
  7.  * 
  8.  * $Id: Sigma_usage_testcase.php,v 1.2 2003/04/22 19:01:04 avb Exp $
  9.  */
  10.  
  11. class Sigma_Usage_TestCase extends PHPUnit_TestCase
  12. {
  13.    /**
  14.     * A template object
  15.     * @var object
  16.     */
  17.     var $tpl;
  18.  
  19.     function Sigma_Usage_TestCase($name)
  20.     {
  21.         $this->PHPUnit_TestCase($name);
  22.     }
  23.  
  24.     function setUp()
  25.     {
  26.         $className = 'HTML_Template_' . $GLOBALS['IT_class'];
  27.         $this->tpl =& new $className('./templates');
  28.     }
  29.  
  30.     function tearDown()
  31.     {
  32.         unset($this->tpl);
  33.     }
  34.  
  35.     function _stripWhitespace($str)
  36.     {
  37.         return preg_replace('/\\s+/', '', $str);
  38.     }
  39.  
  40.     function _methodExists($name) 
  41.     {
  42.         if (in_array(strtolower($name), get_class_methods($this->tpl))) {
  43.             return true;
  44.         }
  45.         $this->assertTrue(false, 'method '. $name . ' not implemented in ' . get_class($this->tpl));
  46.         return false;
  47.     }
  48.  
  49.  
  50.    /**
  51.     * Tests iterations over two blocks
  52.     *
  53.     */
  54.     function testBlockIteration()
  55.     {
  56.         $data = array(
  57.             'a',
  58.             array('b', array('1', '2', '3', '4')),
  59.             'c',
  60.             array('d', array('5', '6', '7'))
  61.         );
  62.         
  63.         $result = $this->tpl->loadTemplateFile('blockiteration.html', true, true);
  64.         if (PEAR::isError($result)) {
  65.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  66.         }
  67.         foreach ($data as $value) {
  68.             if (is_array($value)) {
  69.                 $this->tpl->setVariable('outer', $value[0]);
  70.                 foreach ($value[1] as $v) {
  71.                     $this->tpl->setVariable('inner', $v);
  72.                     $this->tpl->parse('inner_block');
  73.                 }
  74.             } else {
  75.                 $this->tpl->setVariable('outer', $value);
  76.             }
  77.             $this->tpl->parse('outer_block');
  78.         }
  79.         $this->assertEquals('a#b|1|2|3|4#c#d|5|6|7#', $this->_stripWhitespace($this->tpl->get()));
  80.     }
  81.  
  82.    /**
  83.     * 
  84.     *
  85.     */
  86.     function testTouchBlockIteration()
  87.     {
  88.         $data = array('a','b','c','d','e');
  89.         $result = $this->tpl->loadTemplateFile('blockiteration.html', true, true);
  90.         if (PEAR::isError($result)) {
  91.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  92.         }
  93.         for ($i = 0; $i < count($data); $i++) {
  94.             $this->tpl->setVariable('outer', $data[$i]);
  95.             // the inner_block is empty and should be removed
  96.             if (0 == $i % 2) {
  97.                 $this->tpl->touchBlock('inner_block');
  98.             }
  99.             $this->tpl->parse('outer_block');
  100.         }
  101.         $this->assertEquals('a|#b#c|#d#e|#', $this->_stripWhitespace($this->tpl->get()));
  102.     }
  103.  
  104.    /**
  105.     *
  106.     */
  107.     function testHideBlockIteration()
  108.     {
  109.         if (!$this->_methodExists('hideBlock')) {
  110.             return;
  111.         }
  112.         $data = array('a','b','c','d','e');
  113.         $result = $this->tpl->loadTemplateFile('blockiteration.html', true, true);
  114.         if (PEAR::isError($result)) {
  115.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  116.         }
  117.         for ($i = 0; $i < count($data); $i++) {
  118.             $this->tpl->setVariable(array(
  119.                 'inner' => $i + 1,
  120.                 'outer' => $data[$i]
  121.             ));
  122.             // the inner_block is not empty, but should be removed
  123.             if (0 == $i % 2) {
  124.                 $this->tpl->hideBlock('inner_block');
  125.             }
  126.             $this->tpl->parse('outer_block');
  127.         }
  128.         $this->assertEquals('a#b|2#c#d|4#e#', $this->_stripWhitespace($this->tpl->get()));
  129.     }
  130. }
  131. ?>
  132.