More tests

git-svn-id: file:///svn/phpbb/trunk@9100 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Chris Smith 2008-11-23 22:39:31 +00:00
parent 500eb05c48
commit 6e2d2f85b5
7 changed files with 147 additions and 9 deletions

View file

@ -25,12 +25,17 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase
{ {
ob_start(); ob_start();
$this->assertTrue($this->template->display($handle, false)); $this->assertTrue($this->template->display($handle, false));
$contents = str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim(ob_get_contents()))))); $contents = self::trim_template_result(ob_get_contents());
ob_end_clean(); ob_end_clean();
return $contents; return $contents;
} }
private static function trim_template_result($result)
{
return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result)))));
}
private function setup_engine() private function setup_engine()
{ {
$this->template = new template; $this->template = new template;
@ -68,6 +73,7 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase
'', // File '', // File
array(), // vars array(), // vars
array(), // block vars array(), // block vars
array(), // destroy
'', // Expected result '', // Expected result
), ),
*/ */
@ -75,84 +81,132 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase
'variable.html', 'variable.html',
array('VARIABLE' => 'value'), array('VARIABLE' => 'value'),
array(), array(),
array(),
'value', 'value',
), ),
array( array(
'if.html', 'if.html',
array(), array(),
array(), array(),
array(),
'0', '0',
), ),
array( array(
'if.html', 'if.html',
array('S_VALUE' => true), array('S_VALUE' => true),
array(), array(),
array(),
'1', '1',
), ),
array(
'if.html',
array('S_VALUE' => true, 'S_OTHER_VALUE' => true),
array(),
array(),
'1',
),
array(
'if.html',
array('S_VALUE' => false, 'S_OTHER_VALUE' => true),
array(),
array(),
'2',
),
array( array(
'loop.html', 'loop.html',
array(), array(),
array(), array(),
array(),
"noloop\nnoloop", "noloop\nnoloop",
), ),
array( array(
'loop.html', 'loop.html',
array(), array(),
array('loop' => array(array())), array('loop' => array(array())),
array(),
"loop\nloop", "loop\nloop",
), ),
array( array(
'loop.html', 'loop.html',
array(), array(),
array('loop' => array(array(), array())), array('loop' => array(array(), array())),
array(),
"loop\nloop\nloop\nloop", "loop\nloop\nloop\nloop",
), ),
array( array(
'loop_vars.html', 'loop_vars.html',
array(), array(),
array('loop' => array(array('VARIABLE' => 'x'))), array('loop' => array(array('VARIABLE' => 'x'))),
"first\n0\n0\n1\nx\nlast", array(),
"first\n0\n0\n1\nx\nset\nlast",
), ),
array( array(
'loop_vars.html', 'loop_vars.html',
array(), array(),
array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))), array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))),
"first\n0\n0\n2\nx\n1\n1\n2\ny\nlast", array(),
"first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast",
), ),
array( array(
'loop_vars.html', 'loop_vars.html',
array(), array(),
array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())),
"first\n0\n0\n2\nx\n1\n1\n2\ny\nlast\n0\n1", array(),
"first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast\n0\n\n1\nlast inner\ninner loop",
), ),
array( array(
'loop_advanced.html', 'loop_advanced.html',
array(), array(),
array('loop' => array(array(), array(), array(), array(), array(), array(), array())), array('loop' => array(array(), array(), array(), array(), array(), array(), array())),
array(),
"101234561\n101234561\n101234561\n1234561\n1\n101\n234\n10\n561\n561", "101234561\n101234561\n101234561\n1234561\n1\n101\n234\n10\n561\n561",
), ),
array( array(
'define.html', 'define.html',
array(), array(),
array(), array(),
array(),
"xyz\nabc", "xyz\nabc",
), ),
array( array(
'expressions.html', 'expressions.html',
array(), array(),
array(), array(),
trim(str_repeat("pass\n", 38)), array(),
trim(str_repeat("pass\n", 40)),
),
array(
'php.html',
array(),
array(),
array(),
'<!-- echo "test"; -->',
), ),
array( array(
'include.html', 'include.html',
array('VARIABLE' => 'value'), array('VARIABLE' => 'value'),
array(), array(),
array(),
'value', 'value',
), ),
array(
'loop_vars.html',
array(),
array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())),
array('loop'),
'',
),
array(
'loop_vars.html',
array(),
array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())),
array('loop.inner'),
"first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast",
),
); );
} }
private function run_template($file, array $vars, array $block_vars, $expected, $cache_file) private function run_template($file, array $vars, array $block_vars, array $destroy, $expected, $cache_file)
{ {
$this->template->set_filenames(array('test' => $file)); $this->template->set_filenames(array('test' => $file));
$this->template->assign_vars($vars); $this->template->assign_vars($vars);
@ -165,6 +219,11 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase
} }
} }
foreach ($destroy as $block)
{
$this->template->destroy_block_vars($block);
}
$this->assertEquals($expected, $this->display('test'), "Testing $file"); $this->assertEquals($expected, $this->display('test'), "Testing $file");
$this->assertFileExists($cache_file); $this->assertFileExists($cache_file);
} }
@ -172,18 +231,82 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase
/** /**
* @dataProvider template_data * @dataProvider template_data
*/ */
public function test_template($file, array $vars, array $block_vars, $expected) public function test_template($file, array $vars, array $block_vars, array $destroy, $expected)
{ {
$cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.' . PHP_EXT; $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.' . PHP_EXT;
$this->assertFileNotExists($cache_file); $this->assertFileNotExists($cache_file);
$this->run_template($file, $vars, $block_vars, $expected, $cache_file); $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file);
// Reset the engine state // Reset the engine state
$this->setup_engine(); $this->setup_engine();
$this->run_template($file, $vars, $block_vars, $expected, $cache_file); $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file);
}
/**
* @dataProvider template_data
*/
public function test_assign_display($file, array $vars, array $block_vars, array $destroy, $expected)
{
$this->template->set_filenames(array(
'test' => $file,
'container' => 'variable.html',
));
$this->template->assign_vars($vars);
foreach ($block_vars as $block => $loops)
{
foreach ($loops as $_vars)
{
$this->template->assign_block_vars($block, $_vars);
}
}
foreach ($destroy as $block)
{
$this->template->destroy_block_vars($block);
}
$this->assertEquals($expected, self::trim_template_result($this->template->assign_display('test')), "Testing assign_display($file)");
$this->template->assign_display('test', 'VARIABLE', false);
$this->assertEquals($expected, $this->display('container'), "Testing assign_display($file)");
}
public function test_php()
{
global $config;
$config['tpl_allow_php'] = 1;
$cache_file = $this->template->cachepath . 'php.html.' . PHP_EXT;
$this->assertFileNotExists($cache_file);
$this->run_template('php.html', array(), array(), array(), 'test', $cache_file);
unset($config['tpl_allow_php']);
}
public function test_includephp()
{
global $config;
$config['tpl_allow_php'] = 1;
$cwd = getcwd();
chdir(dirname(__FILE__) . '/templates');
//$this->run_template('includephp.html', array(), array(), array(), 'testing included php', $cache_file);
$this->template->set_filenames(array('test' => 'includephp.html'));
$this->assertEquals('testing included php', $this->display('test'), "Testing $file");
chdir($cwd);
unset($config['tpl_allow_php']);
} }
} }
?> ?>

View file

@ -0,0 +1,3 @@
<?php
echo "testing included php";

View file

@ -8,6 +8,7 @@
<!-- IF 32 is odd -->fail<!-- ELSE -->pass<!-- ENDIF --> <!-- IF 32 is odd -->fail<!-- ELSE -->pass<!-- ENDIF -->
<!-- IF 32 is div by 16 -->pass<!-- ELSE -->fail<!-- ENDIF -->
<!-- IF 24 == 24 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 24 == 24 -->pass<!-- ELSE -->fail<!-- ENDIF -->
@ -83,3 +84,5 @@
<!-- IF 6 % 4 == 2 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 6 % 4 == 2 -->pass<!-- ELSE -->fail<!-- ENDIF -->
<!-- IF 24 mod 12 == 0 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 24 mod 12 == 0 -->pass<!-- ELSE -->fail<!-- ENDIF -->
<!-- IF not (43 > 53) -->pass<!-- ELSE -->fail<!-- ENDIF -->

View file

@ -1,5 +1,7 @@
<!-- IF S_VALUE --> <!-- IF S_VALUE -->
1 1
<!-- ELSEIF S_OTHER_VALUE -->
2
<!-- ELSE --> <!-- ELSE -->
0 0
<!-- ENDIF --> <!-- ENDIF -->

View file

@ -0,0 +1 @@
<!-- INCLUDEPHP _dummy_include.php -->

View file

@ -9,10 +9,15 @@
{loop.VARIABLE} {loop.VARIABLE}
<!-- IF loop.VARIABLE -->set<!-- ENDIF -->
<!-- IF loop.S_LAST_ROW -->last<!-- ENDIF --> <!-- IF loop.S_LAST_ROW -->last<!-- ENDIF -->
<!-- BEGIN inner --> <!-- BEGIN inner -->
{inner.S_ROW_NUM} {inner.S_ROW_NUM}
<!-- IF inner.S_LAST_ROW and inner.S_ROW_NUM and inner.S_NUM_ROWS -->last inner<!-- ENDIF -->
<!-- END inner --> <!-- END inner -->
<!-- END loop --> <!-- END loop -->
<!-- IF .loop.inner -->inner loop<!-- ENDIF -->

View file

@ -0,0 +1 @@
<!-- PHP -->echo "test";<!-- ENDPHP -->