test_template.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. $this->templates_folder = PQUERY_ROOT.'test/'.self::TEMPLATES_FOLDER;
  13. __tpl::set_root($this->templates_folder, false);
  14. $this->file = 'test.tpl';
  15. $this->tpl = _tpl($this->file);
  16. }
  17. function test_add_root_relative() {
  18. $folder = PQUERY_ROOT.'test/';
  19. $folder_relative = 'test/';
  20. __tpl::add_root($folder_relative);
  21. $this->assertTrue(in_array($folder, __tpl::$include_path), 'folder was not added to include path');
  22. }
  23. function test_add_root_absolute() {
  24. $folder = PQUERY_ROOT.'test/';
  25. __tpl::add_root($folder, false);
  26. $this->assertTrue(in_array($folder, __tpl::$include_path), 'folder was not added to include path');
  27. }
  28. function test_add_root_failure() {
  29. $this->expectException('pQueryException');
  30. __tpl::add_root('non_existing_folder');
  31. }
  32. function test_set_root_relative() {
  33. $folder = PQUERY_ROOT.'test/';
  34. $folder_relative = 'test/';
  35. __tpl::set_root($folder_relative);
  36. $this->assertEqual(array($folder), __tpl::$include_path, 'folder was not set as only include path');
  37. }
  38. function test_set_root_absolute() {
  39. $folder = PQUERY_ROOT.'test/';
  40. __tpl::set_root($folder, false);
  41. $this->assertEqual(array($folder), __tpl::$include_path, 'folder was not set as only include path');
  42. }
  43. function test_constructor() {
  44. $this->assertIsA($this->tpl, 'pQueryTemplate', 'constructor does not return pQueryTemplate object');
  45. }
  46. function test_open_template_file() {
  47. $path = $this->templates_folder.$this->file;
  48. $content = file_get_contents($path);
  49. $this->assertEqual($this->tpl->content, $content, 'template content is not set correctly');
  50. }
  51. function test_non_existent_file() {
  52. $this->expectException('pQueryException');
  53. _tpl('non_existent_file.tpl');
  54. }
  55. function test_parse() {
  56. $expected_content = file_get_contents($this->templates_folder.'expect_parse.txt');
  57. $test1 = $this->tpl->data->add('test1', array('var' => 'some-variable'));
  58. $this->tpl->data->add('test1', array('var' => 'some-other-variable'));
  59. $test1->add('test2');
  60. $this->tpl->data->add('test3');
  61. $this->assertEqual($this->tpl->parse(), $expected_content, 'parsed templated does not match expected content');
  62. }
  63. }
  64. ?>