PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/View/test/Horde/View/BaseTest.php

https://github.com/wrobel/horde
PHP | 246 lines | 146 code | 44 blank | 56 comment | 0 complexity | 312af2525ba4c5150310ec10bf413747 MD5 | raw file
Possible License(s): BSD-2-Clause, AGPL-1.0, LGPL-2.1, LGPL-3.0, BSD-3-Clause, LGPL-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Copyright 2007-2008 Maintainable Software, LLC
  4. * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
  5. *
  6. * @author Mike Naberezny <mike@maintainable.com>
  7. * @author Derek DeVries <derek@maintainable.com>
  8. * @author Chuck Hagenbuch <chuck@horde.org>
  9. * @license http://www.horde.org/licenses/bsd
  10. * @category Horde
  11. * @package View
  12. * @subpackage UnitTests
  13. */
  14. /**
  15. * @group view
  16. * @author Mike Naberezny <mike@maintainable.com>
  17. * @author Derek DeVries <derek@maintainable.com>
  18. * @author Chuck Hagenbuch <chuck@horde.org>
  19. * @license http://www.horde.org/licenses/bsd
  20. * @category Horde
  21. * @package View
  22. * @subpackage UnitTests
  23. */
  24. class Horde_View_BaseTest extends Horde_Test_Case
  25. {
  26. protected $_view = null;
  27. public function setUp()
  28. {
  29. $this->_view = new Horde_View();
  30. $this->_view->addTemplatePath(dirname(__FILE__) . '/fixtures/');
  31. }
  32. /*##########################################################################
  33. # Assignment
  34. ##########################################################################*/
  35. // test setting/getting dynamic properties
  36. public function testSet()
  37. {
  38. $this->_view->publicVar = 'test';
  39. $this->assertEquals('test', $this->_view->publicVar);
  40. }
  41. public function testAssign()
  42. {
  43. $this->_view->assign(array('publicVar' => 'test'));
  44. $this->assertEquals('test', $this->_view->publicVar);
  45. }
  46. public function testAssignDoesntOverridePrivateVariables()
  47. {
  48. try {
  49. $this->_view->assign(array('_templatePath' => 'test'));
  50. } catch (Exception $e) {
  51. return;
  52. }
  53. $this->fail('Overwriting a private/protected variable should fail');
  54. }
  55. public function testAssignAllowsUnderscoreVariables()
  56. {
  57. $this->_view->assign(array('_private' => 'test'));
  58. $this->assertEquals('test', $this->_view->_private);
  59. }
  60. // test accessing variable
  61. public function testAccessVar()
  62. {
  63. $this->_view->testVar = 'test';
  64. $this->assertTrue(!empty($this->_view->testVar));
  65. $this->_view->testVar2 = '';
  66. $this->assertTrue(empty($this->_view->testVar2));
  67. $this->assertTrue(isset($this->_view->testVar2));
  68. $this->assertTrue(!isset($this->_view->testVar3));
  69. }
  70. // test adding a template path
  71. public function testAddTemplatePath()
  72. {
  73. $this->_view->addTemplatePath('app/views/shared/');
  74. $expected = array('app/views/shared/',
  75. dirname(__FILE__) . '/fixtures/',
  76. './');
  77. $this->assertEquals($expected, $this->_view->getTemplatePaths());
  78. }
  79. // test adding a template path
  80. public function testAddTemplatePathAddSlash()
  81. {
  82. $this->_view->addTemplatePath('app/views/shared');
  83. $expected = array('app/views/shared/',
  84. dirname(__FILE__) . '/fixtures/',
  85. './');
  86. $this->assertEquals($expected, $this->_view->getTemplatePaths());
  87. }
  88. /*##########################################################################
  89. # Rendering
  90. ##########################################################################*/
  91. // test rendering
  92. public function testRender()
  93. {
  94. $this->_view->myVar = 'test';
  95. $expected = "<div>test</div>";
  96. $this->assertEquals($expected, $this->_view->render('testRender.html.php'));
  97. }
  98. // test rendering
  99. public function testRenderNoExtension()
  100. {
  101. $this->_view->myVar = 'test';
  102. $expected = "<div>test</div>";
  103. $this->assertEquals($expected, $this->_view->render('testRender'));
  104. }
  105. // test that the
  106. public function testRenderPathOrder()
  107. {
  108. $this->_view->myVar = 'test';
  109. // we should be rendering the testRender.html in fixtures/
  110. $expected = "<div>test</div>";
  111. $this->assertEquals($expected, $this->_view->render('testRender'));
  112. // after we specify the 'subdir' path, it should read from subdir path first
  113. $this->_view->addTemplatePath(dirname(__FILE__) . '/fixtures/subdir/');
  114. $expected = "<div>subdir test</div>";
  115. $this->assertEquals($expected, $this->_view->render('testRender'));
  116. }
  117. /*##########################################################################
  118. # Partials
  119. ##########################################################################*/
  120. // test rendering partial
  121. public function testRenderPartial()
  122. {
  123. $this->_view->myVar1 = 'main';
  124. $this->_view->myVar2 = 'partial';
  125. $expected = '<div>main<p>partial</p></div>';
  126. $this->assertEquals($expected, $this->_view->render('testPartial'));
  127. }
  128. // test rendering partial with object passed in
  129. public function testRenderPartialObject()
  130. {
  131. $this->_view->myObject = (object)array('string_value' => 'hello world');
  132. $expected = '<div><p>hello world</p></div>';
  133. $this->assertEquals($expected, $this->_view->render('testPartialObject'));
  134. }
  135. // test rendering partial with locals passed in
  136. public function testRenderPartialLocals()
  137. {
  138. $expected = '<div><p>hello world</p></div>';
  139. $this->assertEquals($expected, $this->_view->render('testPartialLocals'));
  140. }
  141. // test rendering partial with collection passed in
  142. public function testRenderPartialCollection()
  143. {
  144. $this->_view->myObjects = array((object)array('string_value' => 'hello'),
  145. (object)array('string_value' => 'world'));
  146. $expected = '<div><p>hello</p><p>world</p></div>';
  147. $this->assertEquals($expected, $this->_view->render('testPartialCollection'));
  148. }
  149. // test rendering partial with empty set as collection
  150. public function testRenderPartialCollectionEmpty()
  151. {
  152. $this->_view->myObjects = null;
  153. $expected = '<div></div>';
  154. $this->assertEquals($expected, $this->_view->render('testPartialCollection'));
  155. }
  156. // test rendering partial with empty array as collection
  157. public function testRenderPartialCollectionEmptyArray()
  158. {
  159. $this->_view->myObjects = array();
  160. $expected = '<div></div>';
  161. $this->assertEquals($expected, $this->_view->render('testPartialCollection'));
  162. }
  163. // partial collection is a model collection
  164. public function testRenderPartialModelCollection()
  165. {
  166. $this->_view->myObjects = array((object)array('string_value' => 'name a'), (object)array('string_value' => 'name b'));
  167. $expected = '<div><p>name a</p><p>name b</p></div>';
  168. $this->assertEquals($expected, $this->_view->render('testPartialCollection'));
  169. }
  170. /*##########################################################################
  171. # Escape output
  172. ##########################################################################*/
  173. public function testEscapeTemplate()
  174. {
  175. $this->_view->myVar = '"escaping"';
  176. $this->_view->addHelper(new Horde_View_Helper_Text($this->_view));
  177. $expected = "<div>test &quot;escaping&quot; quotes</div>";
  178. $this->assertEquals($expected, $this->_view->render('testEscape'));
  179. }
  180. // test adding a helper
  181. public function testAddHorde_View_Helper_Text()
  182. {
  183. $str = 'The quick brown fox jumps over the lazy dog tomorrow morning.';
  184. // helper doesn't exist
  185. try {
  186. $this->_view->truncateMiddle($str, 40);
  187. } catch (Exception $e) {}
  188. $this->assertTrue($e instanceof Horde_View_Exception);
  189. // add text helper
  190. $this->_view->addHelper(new Horde_View_Helper_Text($this->_view));
  191. $expected = 'The quick brown fox... tomorrow morning.';
  192. $this->assertEquals($expected, $this->_view->truncateMiddle($str, 40));
  193. }
  194. // test adding a helper where methods conflict
  195. public function testAddHorde_View_Helper_TextMethodOverwrite()
  196. {
  197. // add text helper
  198. $this->_view->addHelper(new Horde_View_Helper_Text($this->_view));
  199. // successful when trying to add it again
  200. $this->_view->addHelper(new Horde_View_Helper_Text($this->_view));
  201. }
  202. }