PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/simpletests/test_smarty3.php

http://github.com/MrAnchovy/Kohana_Smarty3
PHP | 160 lines | 131 code | 19 blank | 10 comment | 0 complexity | 7571e611cf06c58bc1ed72a1ef7c97e9 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * @package Smarty
  4. * @category Tests
  5. * @author Mr Anchovy
  6. * @copyright (c) 2011 Mr Anchovy
  7. * @license http://opensource.org/licenses/ISC
  8. */
  9. class Test_Smarty3 extends UnitTestCase {
  10. function test_php_view_functions_still_work() {
  11. // IMPORTANT do this first, before the Smarty class autoloader is in scope
  12. try {
  13. $random = md5(microtime());
  14. View::bind_global('global_bound_variable', $random);
  15. View::set_global('global_variable', $random);
  16. $view = View::factory('smarty_test/test_php');
  17. $view->variable = $random;
  18. $view->bind('bound_variable', $random);
  19. $ok = TRUE;
  20. } catch (Exception $e) {
  21. $ok = FALSE;
  22. throw $e;
  23. }
  24. $this->assertTrue($ok, 'There should be no exceptions');
  25. }
  26. function test_can_load_smarty_template() {
  27. // these templates are in smarty3/views
  28. $view = View::factory('smarty_test/test.tpl');
  29. $this->assertIsA($view, 'Smarty_View', 'Should be a Smarty_View object|%s');
  30. }
  31. function test_can_load_php_template() {
  32. $view = View::factory('smarty_test/test_php');
  33. $this->assertIsA($view, 'View', 'Should be a View object|%s');
  34. }
  35. function test_can_load_smarty_template_with_legacy_syntax() {
  36. $view = View::factory('smarty:smarty_test/test');
  37. $this->assertIsA($view, 'Smarty_View', 'Should be a Smarty_View object|%s');
  38. }
  39. function test_can_load_smarty_with_no_template() {
  40. $view = View::factory('smarty:');
  41. $this->assertIsA($view, 'Smarty_View', 'Should be a Smarty_View object|%s');
  42. }
  43. function test_can_load_smarty_template_from_absolute_path() {
  44. $view = View::factory(dirname(__FILE__).'/templates/test_absolute.tpl');
  45. $this->assertIsA($view, 'Smarty_View', 'Should be a Smarty_View object|%s');
  46. }
  47. function test_implicit_variable_assignment() {
  48. $view = View::factory('smarty_test/test.tpl');
  49. // use random values throughout to avoid caching and/or object persistence issues
  50. $random = md5(microtime());
  51. $view->variable = $random;
  52. $output = $view->render();
  53. $this->assertPattern("/Variable \[$random\]/", $output, "Output should contain Variable [$random]|%s");
  54. }
  55. function test_variable_assignment_with_smarty_assign() {
  56. $view = View::factory('smarty_test/test.tpl');
  57. $random = md5(microtime());
  58. $view->smarty()->assign('variable', $random);
  59. $output = $view->render();
  60. $this->assertPattern("/Variable \[$random\]/", $output, "Output should contain Variable [$random]|%s");
  61. }
  62. function test_smarty_assign_creates_view_property() {
  63. $view = View::factory('smarty_test/test.tpl');
  64. $random = md5(microtime());
  65. $view->smarty()->assign('variable', $random);
  66. $this->assertIdentical($view->variable, $random, '$view->variable'." should equal [$random]|%s");
  67. }
  68. function test_smarty_local_variable_is_local() {
  69. $view = View::factory('smarty_test/test.tpl');
  70. $random = md5(microtime());
  71. $view->variable = $random;
  72. $view2 = View::factory('smarty_test/test2.tpl');
  73. $output = $view2->render();
  74. $this->assertNoPattern("/$random/", $output, "Output should not contain $random|%s");
  75. }
  76. function test_global_view_variable_is_global_in_smarty() {
  77. $random = md5(microtime());
  78. View::set_global('global_variable', $random);
  79. $view = View::factory('smarty_test/test.tpl');
  80. $output = $view->render();
  81. $this->assertPattern("/Global variable \[$random\]/", $output, "Output should contain Global variable [$random]|%s");
  82. }
  83. function test_global_view_variable_is_global_in_php() {
  84. $random = md5(microtime());
  85. View::set_global('global_variable', $random);
  86. $view = View::factory('smarty_test/test_php');
  87. $output = $view->render();
  88. $this->assertPattern("/PHP Global variable \[$random\]/", $output, "Output should contain PHP Global variable [$random]|%s");
  89. }
  90. function test_smarty_bound_variable_is_bound() {
  91. $view = View::factory('smarty_test/test.tpl');
  92. $random = md5(microtime());
  93. $view->bind('bound_variable', $random);
  94. $random = md5($random);
  95. $output = $view->render();
  96. $this->assertPattern("/Bound variable \[$random\]/", $output, "Output should contain Bound variable [$random]|%s");
  97. }
  98. function test_smarty_bound_variable_is_local() {
  99. $view = View::factory('smarty_test/test.tpl');
  100. $random = md5(microtime());
  101. $view->bind('variable', $random);
  102. $random = md5($random);
  103. $view2 = View::factory('smarty_test/test2.tpl');
  104. $output = $view2->render();
  105. $this->assertNoPattern("/$random/", $output, "Output should not contain $random|%s");
  106. }
  107. function test_global_bound_view_variable_is_global_and_bound_in_smarty() {
  108. $random = md5(microtime());
  109. View::bind_global('global_variable', $random);
  110. $view = View::factory('smarty_test/test.tpl');
  111. $random = md5($random);
  112. $output = $view->render();
  113. $this->assertPattern("/Global variable \[$random\]/", $output, "Output should contain Global variable [$random]|%s");
  114. }
  115. function test_global_bound_view_variable_is_global_and_bound_in_php() {
  116. $random = md5(microtime());
  117. View::bind_global('global_variable', $random);
  118. $view = View::factory('smarty_test/test_php');
  119. $random = md5($random);
  120. $output = $view->render();
  121. $this->assertPattern("/PHP Global variable \[$random\]/", $output, "Output should contain PHP Global variable [$random]|%s");
  122. }
  123. function test_smarty_include() {
  124. $view = View::factory('smarty_test/include.tpl');
  125. $view->variable = $random = md5(microtime());
  126. $output = $view->render();
  127. $this->assertPattern("/Parent template provided \[$random\]/", $output, "Output should contain 'Parent template provided [$random]'|%s");
  128. }
  129. function test_smarty_include_different_module() {
  130. $view = View::factory('smarty_test/test_view_path.tpl');
  131. $view->variable = $random = md5(microtime());
  132. $output = $view->render();
  133. $this->assertPattern("/Template in simpletest\/views \[$random\]/", $output, "Output should contain 'Template in simpletest/views [$random']|%s");
  134. }
  135. function test_template_inheritance() {
  136. $view = View::factory('smarty_test/mypage.tpl');
  137. $output = $view->render();
  138. $this->assertPattern("/My HTML Page Body goes here/", $output, "Output should contain 'My HTML Page Body goes here|%s");
  139. }
  140. }