PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Test/Case/View/ThemeViewTest.php

http://github.com/cakephp/cakephp
PHP | 263 lines | 134 code | 31 blank | 98 comment | 1 complexity | 105e80e775e5ad6f0f8aa49abeacfba8 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /**
  3. * ThemeViewTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('View', 'View');
  20. App::uses('ThemeView', 'View');
  21. App::uses('Controller', 'Controller');
  22. /**
  23. * ThemePostsController class
  24. *
  25. * @package Cake.Test.Case.View
  26. */
  27. class ThemePostsController extends Controller {
  28. /**
  29. * name property
  30. *
  31. * @var string 'ThemePosts'
  32. */
  33. public $name = 'ThemePosts';
  34. public $theme = null;
  35. /**
  36. * index method
  37. *
  38. * @return void
  39. */
  40. public function index() {
  41. $this->set('testData', 'Some test data');
  42. $test2 = 'more data';
  43. $test3 = 'even more data';
  44. $this->set(compact('test2', 'test3'));
  45. }
  46. }
  47. /**
  48. * TestThemeView class
  49. *
  50. * @package Cake.Test.Case.View
  51. */
  52. class TestThemeView extends ThemeView {
  53. /**
  54. * renderElement method
  55. *
  56. * @param mixed $name
  57. * @param array $params
  58. * @return void
  59. */
  60. public function renderElement($name, $params = array()) {
  61. return $name;
  62. }
  63. /**
  64. * getViewFileName method
  65. *
  66. * @param mixed $name
  67. * @return void
  68. */
  69. public function getViewFileName($name = null) {
  70. return $this->_getViewFileName($name);
  71. }
  72. /**
  73. * getLayoutFileName method
  74. *
  75. * @param mixed $name
  76. * @return void
  77. */
  78. public function getLayoutFileName($name = null) {
  79. return $this->_getLayoutFileName($name);
  80. }
  81. }
  82. /**
  83. * ThemeViewTest class
  84. *
  85. * @package Cake.Test.Case.View
  86. */
  87. class ThemeViewTest extends CakeTestCase {
  88. /**
  89. * setUp method
  90. *
  91. * @return void
  92. */
  93. public function setUp() {
  94. parent::setUp();
  95. $request = new CakeRequest('posts/index');
  96. $this->Controller = new Controller($request);
  97. $this->PostsController = new ThemePostsController($request);
  98. $this->PostsController->viewPath = 'posts';
  99. $this->PostsController->index();
  100. $this->ThemeView = new ThemeView($this->PostsController);
  101. App::build(array(
  102. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  103. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  104. ));
  105. App::objects('plugins', null, false);
  106. CakePlugin::loadAll();
  107. }
  108. /**
  109. * tearDown method
  110. *
  111. * @return void
  112. */
  113. public function tearDown() {
  114. parent::tearDown();
  115. unset($this->ThemeView);
  116. unset($this->PostsController);
  117. unset($this->Controller);
  118. CakePlugin::unload();
  119. }
  120. /**
  121. * testPluginGetTemplate method
  122. *
  123. * @return void
  124. */
  125. public function testPluginThemedGetTemplate() {
  126. $this->Controller->plugin = 'TestPlugin';
  127. $this->Controller->name = 'TestPlugin';
  128. $this->Controller->viewPath = 'Tests';
  129. $this->Controller->action = 'index';
  130. $this->Controller->theme = 'TestTheme';
  131. $ThemeView = new TestThemeView($this->Controller);
  132. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS .'index.ctp';
  133. $result = $ThemeView->getViewFileName('index');
  134. $this->assertEquals($expected, $result);
  135. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Layouts' . DS .'plugin_default.ctp';
  136. $result = $ThemeView->getLayoutFileName('plugin_default');
  137. $this->assertEquals($expected, $result);
  138. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS .'default.ctp';
  139. $result = $ThemeView->getLayoutFileName('default');
  140. $this->assertEquals($expected, $result);
  141. }
  142. /**
  143. * testGetTemplate method
  144. *
  145. * @return void
  146. */
  147. public function testGetTemplate() {
  148. $this->Controller->plugin = null;
  149. $this->Controller->name = 'Pages';
  150. $this->Controller->viewPath = 'Pages';
  151. $this->Controller->action = 'display';
  152. $this->Controller->params['pass'] = array('home');
  153. $ThemeView = new TestThemeView($this->Controller);
  154. $ThemeView->theme = 'TestTheme';
  155. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS .'Pages' . DS .'home.ctp';
  156. $result = $ThemeView->getViewFileName('home');
  157. $this->assertEquals($expected, $result);
  158. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS .'index.ctp';
  159. $result = $ThemeView->getViewFileName('/Posts/index');
  160. $this->assertEquals($expected, $result);
  161. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS .'default.ctp';
  162. $result = $ThemeView->getLayoutFileName();
  163. $this->assertEquals($expected, $result);
  164. $ThemeView->layoutPath = 'rss';
  165. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp';
  166. $result = $ThemeView->getLayoutFileName();
  167. $this->assertEquals($expected, $result);
  168. $ThemeView->layoutPath = 'Emails' . DS . 'html';
  169. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp';
  170. $result = $ThemeView->getLayoutFileName();
  171. $this->assertEquals($expected, $result);
  172. }
  173. /**
  174. * testMissingView method
  175. *
  176. * @expectedException MissingViewException
  177. * @return void
  178. */
  179. public function testMissingView() {
  180. $this->Controller->plugin = null;
  181. $this->Controller->name = 'Pages';
  182. $this->Controller->viewPath = 'Pages';
  183. $this->Controller->action = 'display';
  184. $this->Controller->theme = 'my_theme';
  185. $this->Controller->params['pass'] = array('home');
  186. $View = new TestThemeView($this->Controller);
  187. ob_start();
  188. $result = $View->getViewFileName('does_not_exist');
  189. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  190. $this->assertRegExp("/PagesController::/", $expected);
  191. $this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)pages(\/|\\\)does_not_exist.ctp/", $expected);
  192. }
  193. /**
  194. * testMissingLayout method
  195. *
  196. * @expectedException MissingLayoutException
  197. * @return void
  198. */
  199. public function testMissingLayout() {
  200. $this->Controller->plugin = null;
  201. $this->Controller->name = 'Posts';
  202. $this->Controller->viewPath = 'posts';
  203. $this->Controller->layout = 'whatever';
  204. $this->Controller->theme = 'my_theme';
  205. $View = new TestThemeView($this->Controller);
  206. ob_start();
  207. $result = $View->getLayoutFileName();
  208. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  209. $this->assertRegExp("/Missing Layout/", $expected);
  210. $this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)layouts(\/|\\\)whatever.ctp/", $expected);
  211. }
  212. /**
  213. * test memory leaks that existed in _paths at one point.
  214. *
  215. * @return void
  216. */
  217. public function testMemoryLeakInPaths() {
  218. $this->Controller->plugin = null;
  219. $this->Controller->name = 'Posts';
  220. $this->Controller->viewPath = 'posts';
  221. $this->Controller->layout = 'whatever';
  222. $this->Controller->theme = 'TestTheme';
  223. $View = new ThemeView($this->Controller);
  224. $View->element('test_element');
  225. $start = memory_get_usage();
  226. for ($i = 0; $i < 10; $i++) {
  227. $View->element('test_element');
  228. }
  229. $end = memory_get_usage();
  230. $this->assertLessThanOrEqual($start + 5000, $end);
  231. }
  232. }