home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Sigma_api_testcase.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  14.1 KB  |  369 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_api_testcase.php,v 1.4 2003/05/19 15:06:39 avb Exp $
  9.  */
  10.  
  11. class Sigma_api_TestCase extends PHPUnit_TestCase
  12. {
  13.    /**
  14.     * A template object
  15.     * @var object
  16.     */
  17.     var $tpl;
  18.  
  19.     function Sigma_api_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.     * Tests a setTemplate method 
  51.     *
  52.     */
  53.     function testSetTemplate()
  54.     {
  55.         $result = $this->tpl->setTemplate('A template', false, false);
  56.         if (PEAR::isError($result)) {
  57.             $this->assertTrue(false, 'Error setting template: '. $result->getMessage());
  58.         }
  59.         $this->assertEquals('A template', $this->tpl->get());
  60.     }
  61.  
  62.    /**
  63.     * Tests a loadTemplatefile method 
  64.     *
  65.     */
  66.     function testLoadTemplatefile()
  67.     {
  68.         $result = $this->tpl->loadTemplatefile('loadtemplatefile.html', false, false);
  69.         if (PEAR::isError($result)) {
  70.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  71.         }
  72.         $this->assertEquals('A template', trim($this->tpl->get()));
  73.     }
  74.  
  75.    /**
  76.     * Tests a setVariable method
  77.     *
  78.     */
  79.     function testSetVariable()
  80.     {
  81.         $result = $this->tpl->setTemplate('{placeholder1} {placeholder2} {placeholder3}', true, true);
  82.         if (PEAR::isError($result)) {
  83.             $this->assertTrue(false, 'Error setting template: '. $result->getMessage());
  84.         }
  85.         // "scalar" call
  86.         $this->tpl->setVariable('placeholder1', 'var1');
  87.         // array call
  88.         $this->tpl->setVariable(array(
  89.             'placeholder2' => 'var2',
  90.             'placeholder3' => 'var3'
  91.         ));
  92.         $this->assertEquals('var1 var2 var3', $this->tpl->get());
  93.     }
  94.  
  95.    /**
  96.     * Tests the <!-- INCLUDE --> functionality 
  97.     *
  98.     */
  99.     function testInclude()
  100.     {
  101.         $result = $this->tpl->loadTemplateFile('include.html', false, false);
  102.         if (PEAR::isError($result)) {
  103.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  104.         }
  105.         $this->assertEquals('Master file; Included file', trim($this->tpl->get()));
  106.     }
  107.  
  108.     function testCurrentBlock()
  109.     {
  110.         $result = $this->tpl->loadTemplateFile('blockiteration.html', true, true);
  111.         if (PEAR::isError($result)) {
  112.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  113.         }
  114.         $this->tpl->setVariable('outer', 'a');
  115.         $this->tpl->setCurrentBlock('inner_block');
  116.         for ($i = 0; $i < 5; $i++) {
  117.             $this->tpl->setVariable('inner', $i + 1);
  118.             $this->tpl->parseCurrentBlock();
  119.         } // for
  120.         $this->assertEquals('a|1|2|3|4|5#', $this->_stripWhitespace($this->tpl->get()));
  121.     }
  122.  
  123.     function testRemovePlaceholders()
  124.     {
  125.         $result = $this->tpl->setTemplate('{placeholder1},{placeholder2},{placeholder3}', true, true);
  126.         if (PEAR::isError($result)) {
  127.             $this->assertTrue(false, 'Error setting template: '. $result->getMessage());
  128.         }
  129.         // we do not set {placeholder3}
  130.         $this->tpl->setVariable(array(
  131.             'placeholder1' => 'var1',
  132.             'placeholder2' => 'var2'
  133.         ));
  134.         $this->assertEquals('var1,var2,', $this->tpl->get());
  135.  
  136.         // Default behaviour is to remove {stuff} from data as well
  137.         $result = $this->tpl->setTemplate('{placeholder1},{placeholder2},{placeholder3}', true, true);
  138.         if (PEAR::isError($result)) {
  139.             $this->assertTrue(false, 'Error setting template: '. $result->getMessage());
  140.         }
  141.         $this->tpl->setVariable(array(
  142.             'placeholder1' => 'var1',
  143.             'placeholder2' => 'var2',
  144.             'placeholder3' => 'var3{stuff}'
  145.         ));
  146.         $this->assertEquals('var1,var2,var3', $this->tpl->get());
  147.     }
  148.  
  149.     function testTouchBlock()
  150.     {
  151.         $result = $this->tpl->loadTemplateFile('blockiteration.html', false, true);
  152.         if (PEAR::isError($result)) {
  153.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  154.         }
  155.         $this->tpl->setVariable('outer', 'data');
  156.         // inner_block should be preserved in output, even if empty
  157.         $this->tpl->touchBlock('inner_block');
  158.         $this->assertEquals('data|{inner}#', $this->_stripWhitespace($this->tpl->get()));
  159.     }
  160.    
  161.     function testHideBlock()
  162.     {
  163.         if (!$this->_methodExists('hideBlock')) {
  164.             return;
  165.         }
  166.         $result = $this->tpl->loadTemplateFile('blockiteration.html', false, true);
  167.         if (PEAR::isError($result)) {
  168.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  169.         }
  170.         $this->tpl->setVariable(array(
  171.             'outer' => 'data',
  172.             'inner' => 'stuff'
  173.         ));
  174.         // inner_block is not empty, but should be removed nonetheless
  175.         $this->tpl->hideBlock('inner_block');
  176.         $this->assertEquals('data#', $this->_stripWhitespace($this->tpl->get()));
  177.     }
  178.  
  179.     function testSetGlobalVariable()
  180.     {
  181.         if (!$this->_methodExists('setGlobalVariable')) {
  182.             return;
  183.         }
  184.         $result = $this->tpl->loadTemplateFile('globals.html', false, true);
  185.         if (PEAR::isError($result)) {
  186.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  187.         }
  188.         $this->tpl->setGlobalVariable('glob', 'glob');
  189.         // {var2} is not, block_two should be removed
  190.         $this->tpl->setVariable(array(
  191.             'var1' => 'one',
  192.             'var3' => 'three'
  193.         ));
  194.         for ($i = 0; $i < 3; $i++) {
  195.             $this->tpl->setVariable('var4', $i + 1);
  196.             $this->tpl->parse('block_four');
  197.         } // for
  198.         $this->assertEquals('glob:one#glob:three|glob:1|glob:2|glob:3#', $this->_stripWhitespace($this->tpl->get()));
  199.     }
  200.  
  201.     function testOptionPreserveData()
  202.     {
  203.         if (!$this->_methodExists('setOption')) {
  204.             return;
  205.         }
  206.         $this->tpl->setTemplate('{placeholder1},{placeholder2},{placeholder3}', true, true);
  207.         $this->tpl->setOption('preserve_data', true);
  208.         $this->tpl->setVariable(array(
  209.             'placeholder1' => 'var1',
  210.             'placeholder2' => 'var2',
  211.             'placeholder3' => 'var3{stuff}'
  212.         ));
  213.         $this->assertEquals('var1,var2,var3{stuff}', $this->tpl->get());
  214.     }
  215.  
  216.     function testPlaceholderExists()
  217.     {
  218.         $this->tpl->setTemplate('{var}');
  219.         $this->assertTrue($this->tpl->placeholderExists('var'), 'Existing placeholder \'var\' reported as nonexistant');
  220.         $this->assertTrue(!$this->tpl->placeholderExists('foobar'), 'Nonexistant placeholder \'foobar\' reported as existing');
  221.         $this->assertTrue($this->tpl->placeholderExists('var', '__global__'), 'Existing in block \'__global__\' placeholder \'var\' reported as nonexistant');
  222.         $this->assertTrue(!$this->tpl->placeholderExists('foobar', '__global__'), 'Nonexistant in block \'__global__\' placeholder \'foobar\' reported as existing');
  223.     }
  224.  
  225.     function testBlockExists()
  226.     {
  227.         $this->tpl->setTemplate('{var}');
  228.         $this->assertTrue($this->tpl->blockExists('__global__'), 'Existing block \'__global__\' reported as nonexistant');
  229.         $this->assertTrue(!$this->tpl->blockExists('foobar'), 'Nonexistant block \'foobar\' reported as existing');
  230.     }
  231.  
  232.     function testAddBlock()
  233.     {
  234.         $result = $this->tpl->loadTemplatefile('blocks.html', true, true);
  235.         if (PEAR::isError($result)) {
  236.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  237.         }
  238.         $this->tpl->addBlock('var', 'added', 'added:{new_var}');
  239.         $this->assertTrue($this->tpl->blockExists('added'), 'The new block seems to be missing');
  240.         $this->assertTrue(!$this->tpl->placeholderExists('var'), 'The old variable seems to be still present in the template');
  241.         $this->tpl->setVariable('new_var', 'new_value');
  242.         $this->assertEquals('added:new_value', $this->_stripWhitespace($this->tpl->get()));
  243.     }
  244.  
  245.     function testAddBlockfile()
  246.     {
  247.         $result = $this->tpl->loadTemplatefile('blocks.html', true, true);
  248.         if (PEAR::isError($result)) {
  249.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  250.         }
  251.         $result = $this->tpl->addBlockfile('var', 'added', 'addblock.html');
  252.         if (PEAR::isError($result)) {
  253.             $this->assertTrue(false, 'Error adding block from file: '. $result->getMessage());
  254.         }
  255.         $this->assertTrue($this->tpl->blockExists('added'), 'The new block seems to be missing');
  256.         $this->assertTrue(!$this->tpl->placeholderExists('var'), 'The old variable seems to be still present in the template');
  257.         $this->tpl->setVariable('new_var', 'new_value');
  258.         $this->assertEquals('added:new_value', $this->_stripWhitespace($this->tpl->get()));
  259.     }
  260.  
  261.     function testReplaceBlock()
  262.     {
  263.         $result = $this->tpl->loadTemplatefile('blocks.html', true, true);
  264.         if (PEAR::isError($result)) {
  265.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  266.         }
  267.         $this->tpl->setVariable('old_var', 'old_value');
  268.         $this->tpl->parse('old_block');
  269.         // old_block's contents should be discarded
  270.         $this->tpl->replaceBlock('old_block', 'replaced:{replaced_var}#', false);
  271.         $this->assertTrue(!$this->tpl->blockExists('old_inner_block') && !$this->tpl->placeholderExists('old_var'),
  272.                           'The replaced block\'s contents seem to be still present');
  273.         $this->tpl->setVariable('replaced_var', 'replaced_value');
  274.         $this->tpl->parse('old_block');
  275.         // this time old_block's contents should be preserved
  276.         $this->tpl->replaceBlock('old_block', 'replaced_again:{brand_new_var}', true);
  277.         $this->tpl->setVariable('brand_new_var', 'brand_new_value');
  278.         $this->assertEquals('replaced:replaced_value#replaced_again:brand_new_value', $this->_stripWhitespace($this->tpl->get()));
  279.     }
  280.  
  281.     function testReplaceBlockfile()
  282.     {
  283.         $result = $this->tpl->loadTemplatefile('blocks.html', true, true);
  284.         if (PEAR::isError($result)) {
  285.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  286.         }
  287.         $this->tpl->setVariable('old_var', 'old_value');
  288.         $this->tpl->parse('old_block');
  289.         // old_block's contents should be discarded
  290.         $result = $this->tpl->replaceBlockfile('old_block', 'replaceblock.html', false);
  291.         if (PEAR::isError($result)) {
  292.             $this->assertTrue(false, 'Error replacing block from file: '. $result->getMessage());
  293.         }
  294.         $this->assertTrue(!$this->tpl->blockExists('old_inner_block') && !$this->tpl->placeholderExists('old_var'),
  295.                           'The replaced block\'s contents seem to be still present');
  296.         $this->tpl->setVariable(array(
  297.             'replaced_var'       => 'replaced_value',
  298.             'replaced_inner_var' => 'inner_value'
  299.         ));
  300.         $this->tpl->parse('old_block');
  301.         // this time old_block's contents should be preserved
  302.         $result = $this->tpl->replaceBlockfile('old_block', 'addblock.html', true);
  303.         if (PEAR::isError($result)) {
  304.             $this->assertTrue(false, 'Error replacing block from file: '. $result->getMessage());
  305.         }
  306.         $this->tpl->setVariable('new_var', 'again');
  307.         $this->assertEquals('replaced:replaced_value|inner_value#added:again', $this->_stripWhitespace($this->tpl->get()));
  308.     }
  309.  
  310.     function testCallback()
  311.     {
  312.         $result = $this->tpl->loadTemplatefile('callback.html', true, true);
  313.         if (PEAR::isError($result)) {
  314.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  315.         }
  316.         $this->tpl->setVariable('username', 'luser');
  317.         $this->tpl->setCallbackFunction('uppercase', 'strtoupper');
  318.         $this->tpl->setCallbackFunction('russian', array(&$this, '_doRussian'), true);
  319.         $this->tpl->setCallbackFunction('lowercase', 'strtolower');
  320.         $this->tpl->setCallBackFunction('noarg', array(&$this, '_doCallback'));
  321.         $this->assertEquals('callback#word#HELLO,LUSER!#╧≡ΦΓσ≥,luser!', $this->_stripWhitespace($this->tpl->get()));
  322.     }
  323.  
  324.     function _doCallback()
  325.     {
  326.         return 'callback';
  327.     }
  328.  
  329.     function _doRussian($arg)
  330.     {
  331.         $ary = array('Hello, {username}!' => '╧≡ΦΓσ≥, {username}!');
  332.         return isset($ary[$arg])? $ary[$arg]: $arg;
  333.     }
  334.  
  335.     function testGetBlockList()
  336.     {
  337.         // expected tree...
  338.         $tree = array(
  339.             'name'     => '__global__',
  340.             'children' => array(
  341.                 array(
  342.                     'name'     => 'outer_block',
  343.                     'children' => array(
  344.                         array('name' => 'inner_block')
  345.                     )
  346.                 )
  347.             )
  348.         );
  349.  
  350.         $result = $this->tpl->loadTemplatefile('blockiteration.html', true, true);
  351.         if (PEAR::isError($result)) {
  352.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  353.         }
  354.         $this->assertEquals($tree, $this->tpl->getBlockList('__global__', true));
  355.         $this->assertEquals(array('inner_block'), $this->tpl->getBlockList('outer_block'));
  356.     }
  357.     
  358.     function testGetPlaceholderList()
  359.     {
  360.         $result = $this->tpl->loadTemplatefile('blockiteration.html', true, true);
  361.         if (PEAR::isError($result)) {
  362.             $this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
  363.         }
  364.         $this->assertEquals(array('outer'), $this->tpl->getPlaceholderList('outer_block'));
  365.     }
  366. }
  367.  
  368. ?>
  369.