template.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. $content = file_get_contents($path);
  51. $this->assertEquals($content, $this->tpl->content, 'template content was not set correctly');
  52. }
  53. /**
  54. * @expectedException pQueryException
  55. */
  56. function test_non_existent_file() {
  57. _tpl('non_existent_file.tpl');
  58. }
  59. function test_parse() {
  60. // Add some blocks with test variables
  61. $this->tpl->data->set('variable', '-variable value-');
  62. $object = new StdClass;
  63. $object->property = '-object property-';
  64. $this->tpl->data->set('object', $object);
  65. $this->tpl->data->set('assoc', array('index' => '-assoc index-'));
  66. $test1 = $this->tpl->data->add('test1', array('var' => 'some-variable'));
  67. $this->tpl->data->add('test1', array('var' => 'some-other-variable'));
  68. $test1->add('test2');
  69. $this->tpl->data->add('test3');
  70. // Expected content is defined in a text file
  71. $expected_content = file_get_contents($this->templates_folder.'expect_parse.html');
  72. $this->assertEquals($expected_content, $this->tpl->parse());
  73. }
  74. }
  75. ?>