diff options
-rw-r--r-- | tests/all_tests.php | 2 | ||||
-rw-r--r-- | tests/template/all_tests.php | 44 | ||||
-rw-r--r-- | tests/template/template.php | 144 | ||||
-rw-r--r-- | tests/template/templates/basic.html | 18 | ||||
-rw-r--r-- | tests/template/templates/define.html | 11 | ||||
-rw-r--r-- | tests/template/templates/expressions.html | 85 | ||||
-rw-r--r-- | tests/template/templates/if.html | 5 | ||||
-rw-r--r-- | tests/template/templates/loop.html | 11 | ||||
-rw-r--r-- | tests/template/templates/loop_vars.html | 9 | ||||
-rw-r--r-- | tests/template/templates/variable.html | 1 |
10 files changed, 330 insertions, 0 deletions
diff --git a/tests/all_tests.php b/tests/all_tests.php index 8a871917eb..2b68fd7264 100644 --- a/tests/all_tests.php +++ b/tests/all_tests.php @@ -22,6 +22,7 @@ require_once 'bbcode/all_tests.php'; require_once 'utf/all_tests.php'; require_once 'request/all_tests.php'; require_once 'security/all_tests.php'; +require_once 'template/all_tests.php'; // exclude the test directory from code coverage reports PHPUnit_Util_Filter::addDirectoryToFilter('./'); @@ -41,6 +42,7 @@ class phpbb_all_tests $suite->addTest(phpbb_utf_all_tests::suite()); $suite->addTest(phpbb_request_all_tests::suite()); $suite->addTest(phpbb_security_all_tests::suite()); + $suite->addTest(phpbb_template_all_tests::suite()); return $suite; } diff --git a/tests/template/all_tests.php b/tests/template/all_tests.php new file mode 100644 index 0000000000..50f6aa6ee8 --- /dev/null +++ b/tests/template/all_tests.php @@ -0,0 +1,44 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2008 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +define('IN_PHPBB', true); + +if (!defined('PHPUnit_MAIN_METHOD')) +{ + define('PHPUnit_MAIN_METHOD', 'phpbb_template_all_tests::main'); +} + +require_once 'PHPUnit/Framework.php'; +require_once 'PHPUnit/TextUI/TestRunner.php'; + +require_once 'template/template.php'; + +class phpbb_template_all_tests +{ + public static function main() + { + PHPUnit_TextUI_TestRunner::run(self::suite()); + } + + public static function suite() + { + $suite = new PHPUnit_Framework_TestSuite('phpBB Template Engine'); + + $suite->addTestSuite('phpbb_template_template_test'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'phpbb_template_all_tests::main') +{ + phpbb_template_all_tests::main(); +} +?>
\ No newline at end of file diff --git a/tests/template/template.php b/tests/template/template.php new file mode 100644 index 0000000000..e2d9429511 --- /dev/null +++ b/tests/template/template.php @@ -0,0 +1,144 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2008 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +define('IN_PHPBB', true); +define('PHP_EXT', 'php'); +define('PHPBB_ROOT_PATH', '../phpBB/'); + +require_once 'PHPUnit/Framework.php'; + +require_once '../phpBB/includes/constants.php'; +require_once '../phpBB/includes/functions.php'; +require_once '../phpBB/includes/template.php'; + +$config = array( + 'load_tplcompile' => true +); + +class phpbb_template_template_test extends PHPUnit_Framework_TestCase +{ + private $template; + + private function display($handle) + { + ob_start(); + $this->assertTrue($this->template->display($handle, false)); + $contents = str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim(ob_get_contents()))))); + ob_end_clean(); + + return $contents; + } + + private function setup_engine() + { + $this->template = new template; + $this->template->set_custom_template(dirname(__FILE__) . '/templates/', 'tests'); + } + + protected function setUp() + { + + } + + public static function template_data() + { + return array( + /* + array( + '', // File + array(), // vars + array(), // block vars + '', // Expected result + ), + */ + array( + 'variable.html', + array('VARIABLE' => 'value'), + array(), + 'value', + ), + array( + 'if.html', + array(), + array(), + '0', + ), + array( + 'if.html', + array('S_VALUE' => true), + array(), + '1', + ), + array( + 'loop.html', + array(), + array(), + "noloop\nnoloop", + ), + array( + 'loop.html', + array(), + array('loop' => array(array())), + "loop\nloop", + ), + array( + 'loop.html', + array(), + array('loop' => array(array(), array())), + "loop\nloop\nloop", + ), + array( + 'loop_vars.html', + array(), + array('loop' => array(array('VARIABLE' => 'x'))), + "first\n0\nx\nlast", + ), + array( + 'loop_vars.html', + array(), + array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))), + "first\n0\nx\n1\ny\nlast", + ), + array( + 'define.html', + array(), + array(), + "xyz\nabc", + ), + array( + 'expressions.html', + array(), + array(), + trim(str_repeat("pass\n", 38)), + ), + ); + } + + /** + * @dataProvider template_data + */ + public function test_template($file, array $vars, array $block_vars, $expected) + { + $this->setup_engine(); + $this->template->set_filenames(array('test' => $file)); + $this->template->assign_vars($vars); + + foreach ($block_vars as $block => $loops) + { + foreach ($loops as $_vars) + { + $this->template->assign_block_vars($block, $_vars); + } + } + + $this->assertEquals($expected, $this->display('test'), "Testing $file.html"); + } +} +?>
\ No newline at end of file diff --git a/tests/template/templates/basic.html b/tests/template/templates/basic.html new file mode 100644 index 0000000000..1a3fd5a96a --- /dev/null +++ b/tests/template/templates/basic.html @@ -0,0 +1,18 @@ +<!-- IF S_FALSE --> +fail +<!-- ENDIF --> +<!-- IF S_TRUE --> +pass +<!-- ENDIF --> +<!-- IF S_FALSE --> +fail +<!-- ELSEIF S_FALSE and not S_TRUE --> +fail +<!-- ELSE --> +pass +<!-- ENDIF --> +<!-- BEGIN empty --> +fail +<!-- BEGINELSE --> +pass +<!-- END empty --> diff --git a/tests/template/templates/define.html b/tests/template/templates/define.html new file mode 100644 index 0000000000..94741e53fc --- /dev/null +++ b/tests/template/templates/define.html @@ -0,0 +1,11 @@ +<!-- DEFINE $VALUE = 'xyz' --> + +{$VALUE} + +<!-- DEFINE $VALUE = 'abc' --> + +{$VALUE} + +<!-- UNDEFINE $VALUE --> + +{$VALUE} diff --git a/tests/template/templates/expressions.html b/tests/template/templates/expressions.html new file mode 100644 index 0000000000..c70dc9418c --- /dev/null +++ b/tests/template/templates/expressions.html @@ -0,0 +1,85 @@ +<!-- IF 10 is even -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 9 is even -->fail<!-- ELSE -->pass<!-- ENDIF --> + +<!-- IF not 390 is even -->fail<!-- ELSE -->pass<!-- ENDIF --> + +<!-- IF 9 is odd -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 32 is odd -->fail<!-- ELSE -->pass<!-- ENDIF --> + + +<!-- IF 24 == 24 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 24 eq 24 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF ((((((24 == 24)))))) -->pass<!-- ELSE -->fail<!-- ENDIF --> + + +<!-- IF 24 != 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 24 <> 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 24 ne 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 24 neq 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + + +<!-- IF 10 lt 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 10 < 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + + +<!-- IF 10 le 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 10 lte 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 10 <= 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 20 le 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 20 lte 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 20 <= 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> + + +<!-- IF 9 gt 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 9 > 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> + + +<!-- IF 9 >= 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 9 gte 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 9 ge 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 9 >= 9 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 9 gte 9 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 9 ge 9 -->pass<!-- ELSE -->fail<!-- ENDIF --> + + +<!-- IF true && (10 > 4) -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF true and (10 > 4) -->pass<!-- ELSE -->fail<!-- ENDIF --> + + +<!-- IF false || true -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF false or true -->pass<!-- ELSE -->fail<!-- ENDIF --> + + +<!-- IF !false -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF !! true -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF not false -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF not not not false -->pass<!-- ELSE -->fail<!-- ENDIF --> + + +<!-- IF 6 % 4 == 2 -->pass<!-- ELSE -->fail<!-- ENDIF --> + +<!-- IF 24 mod 12 == 0 -->pass<!-- ELSE -->fail<!-- ENDIF --> diff --git a/tests/template/templates/if.html b/tests/template/templates/if.html new file mode 100644 index 0000000000..d8343c4391 --- /dev/null +++ b/tests/template/templates/if.html @@ -0,0 +1,5 @@ +<!-- IF S_VALUE --> +1 +<!-- ELSE --> +0 +<!-- ENDIF --> diff --git a/tests/template/templates/loop.html b/tests/template/templates/loop.html new file mode 100644 index 0000000000..ae921fc929 --- /dev/null +++ b/tests/template/templates/loop.html @@ -0,0 +1,11 @@ +<!-- BEGIN loop --> +loop +<!-- BEGINELSE --> +noloop +<!-- END loop --> + +<!-- IF .loop --> +loop +<!-- ELSE --> +noloop +<!-- ENDIF --> diff --git a/tests/template/templates/loop_vars.html b/tests/template/templates/loop_vars.html new file mode 100644 index 0000000000..0da0af4758 --- /dev/null +++ b/tests/template/templates/loop_vars.html @@ -0,0 +1,9 @@ +<!-- BEGIN loop --> +<!-- IF loop.S_FIRST_ROW -->first<!-- ENDIF --> + +{loop.S_ROW_COUNT} + +{loop.VARIABLE} + +<!-- IF loop.S_LAST_ROW -->last<!-- ENDIF --> +<!-- END loop --> diff --git a/tests/template/templates/variable.html b/tests/template/templates/variable.html new file mode 100644 index 0000000000..f68f91597c --- /dev/null +++ b/tests/template/templates/variable.html @@ -0,0 +1 @@ +{VARIABLE} |