template.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. include_once 'config.php';
  3. __p::load_plugin('template');
  4. class pQueryTemplateTest extends PHPUnit_Framework_TestCase {
  5. const TEMPLATES_FOLDER = 'templates/';
  6. var $templates_folder;
  7. var $file;
  8. var $tpl;
  9. function setUp() {
  10. // Set root to tests/templates
  11. $this->templates_folder = PQUERY_ROOT.'test/'.self::TEMPLATES_FOLDER;
  12. __tpl::set_root($this->templates_folder, false);
  13. // Load the test template
  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. /**
  29. * @expectedException pQueryException
  30. */
  31. function test_add_root_failure() {
  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->assertEquals(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->assertEquals(array($folder), __tpl::$include_path, 'folder was not set as only include path');
  44. }
  45. function test_constructor() {
  46. $this->assertInstanceOf('pQueryTemplate', $this->tpl, 'constructor does not return pQueryTemplate object');
  47. }
  48. function test_open_template_file() {
  49. $path = $this->templates_folder.$this->file;
  50. $this->assertStringEqualsFile($path, $this->tpl->content, 'template content was not set correctly');
  51. }
  52. /**
  53. * @expectedException pQueryException
  54. */
  55. function test_non_existent_file() {
  56. _tpl('non_existent_file.tpl');
  57. }
  58. function test_parse() {
  59. // Add some blocks with test variables
  60. $this->tpl->data->set('variable', '-variable value-');
  61. $object = new StdClass;
  62. $object->property = '-object property-';
  63. $this->tpl->data->set('object', $object);
  64. $this->tpl->data->set('assoc', array('index' => '-assoc index-'));
  65. $test1 = $this->tpl->data->add('test1', array('var' => 'some-variable'));
  66. $this->tpl->data->add('test1', array('var' => 'some-other-variable'));
  67. $test1->add('test2');
  68. $this->tpl->data->add('test3');
  69. // Expected content is defined in a text file
  70. $expected_content = file_get_contents($this->templates_folder.'expect_parse.html');
  71. $this->assertEquals($expected_content, $this->tpl->parse());
  72. }
  73. }
  74. ?>