test_template.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. __p::load_plugin('template');
  3. class pQueryTemplateTest extends UnitTestCase {
  4. const TEMPLATES_FOLDER = 'templates/';
  5. var $templates_folder;
  6. var $file;
  7. var $tpl;
  8. function __construct() {
  9. parent::__construct('pQuery template plugin');
  10. }
  11. function setUp() {
  12. // Set root to tests/templates
  13. $this->templates_folder = PQUERY_ROOT.'test/'.self::TEMPLATES_FOLDER;
  14. __tpl::set_root($this->templates_folder, false);
  15. // Load the test template
  16. $this->file = 'test.tpl';
  17. $this->tpl = _tpl($this->file);
  18. }
  19. function test_add_root_relative() {
  20. $folder = PQUERY_ROOT.'test/';
  21. $folder_relative = 'test/';
  22. __tpl::add_root($folder_relative);
  23. $this->assertTrue(in_array($folder, __tpl::$include_path), 'folder was not added to include path');
  24. }
  25. function test_add_root_absolute() {
  26. $folder = PQUERY_ROOT.'test/';
  27. __tpl::add_root($folder, false);
  28. $this->assertTrue(in_array($folder, __tpl::$include_path), 'folder was not added to include path');
  29. }
  30. function test_add_root_failure() {
  31. $this->expectException('pQueryException');
  32. __tpl::add_root('non_existing_folder');
  33. }
  34. function test_set_root_relative() {
  35. $folder = PQUERY_ROOT.'test/';
  36. $folder_relative = 'test/';
  37. __tpl::set_root($folder_relative);
  38. $this->assertEqual(array($folder), __tpl::$include_path, 'folder was not set as only include path');
  39. }
  40. function test_set_root_absolute() {
  41. $folder = PQUERY_ROOT.'test/';
  42. __tpl::set_root($folder, false);
  43. $this->assertEqual(array($folder), __tpl::$include_path, 'folder was not set as only include path');
  44. }
  45. function test_constructor() {
  46. $this->assertIsA($this->tpl, 'pQueryTemplate', 'constructor does not return pQueryTemplate object');
  47. }
  48. function test_open_template_file() {
  49. $path = $this->templates_folder.$this->file;
  50. $content = file_get_contents($path);
  51. $this->assertEqual($this->tpl->content, $content, 'template content is not set correctly');
  52. }
  53. function test_non_existent_file() {
  54. $this->expectException('pQueryException');
  55. _tpl('non_existent_file.tpl');
  56. }
  57. function test_parse() {
  58. // Add some blocks with test variables
  59. $test1 = $this->tpl->data->add('test1', array('var' => 'some-variable'));
  60. $this->tpl->data->add('test1', array('var' => 'some-other-variable'));
  61. $test1->add('test2');
  62. $this->tpl->data->add('test3');
  63. // Expected content is defined in a text file
  64. $expected_content = file_get_contents($this->templates_folder.'expect_parse.txt');
  65. $this->assertEqual($this->tpl->parse(), $expected_content, 'parsed templated does not match expected content');
  66. }
  67. }
  68. ?>