5 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
8 * Licensed under The MIT License
9 * For full copyright and license information, please see the LICENSE.txt
10 * Redistributions of files must retain the above copyright notice
12 * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13 * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14 * @package Cake.Test.Case.View
15 * @since CakePHP(tm) v 1.2.0.4206
16 * @license http://www.opensource.org/licenses/mit-license.php MIT License
19 App::uses('View', 'View');
20 App::uses('ThemeView', 'View');
21 App::uses('Controller', 'Controller');
24 * ThemePosts2Controller class
26 * @package Cake.Test.Case.View
28 class ThemePosts2Controller extends Controller {
35 public $name = 'ThemePosts';
44 public function index() {
46 'testData' => 'Some test data',
47 'test2' => 'more data',
48 'test3' => 'even more data',
55 * TestTheme2View class
57 * @package Cake.Test.Case.View
59 class TestTheme2View extends ThemeView {
62 * renderElement method
65 * @param array $params
68 public function renderElement($name, $params = array()) {
73 * getViewFileName method
78 public function getViewFileName($name = null) {
79 return $this->_getViewFileName($name);
83 * getLayoutFileName method
88 public function getLayoutFileName($name = null) {
89 return $this->_getLayoutFileName($name);
97 * @package Cake.Test.Case.View
99 class ThemeViewTest extends CakeTestCase {
106 public function setUp() {
108 $request = new CakeRequest('posts/index');
109 $this->Controller = new Controller($request);
110 $this->PostsController = new ThemePosts2Controller($request);
111 $this->PostsController->viewPath = 'posts';
112 $this->PostsController->index();
113 $this->ThemeView = new ThemeView($this->PostsController);
115 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
116 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
118 App::objects('plugins', null, false);
119 CakePlugin::load(array('TestPlugin'));
127 public function tearDown() {
129 unset($this->ThemeView);
130 unset($this->PostsController);
131 unset($this->Controller);
132 CakePlugin::unload();
136 * testPluginGetTemplate method
140 public function testPluginThemedGetTemplate() {
141 $this->Controller->plugin = 'TestPlugin';
142 $this->Controller->name = 'TestPlugin';
143 $this->Controller->viewPath = 'Tests';
144 $this->Controller->action = 'index';
145 $this->Controller->theme = 'TestTheme';
147 $ThemeView = new TestTheme2View($this->Controller);
148 $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS . 'index.ctp';
149 $result = $ThemeView->getViewFileName('index');
150 $this->assertEquals($expected, $result);
152 $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Layouts' . DS . 'plugin_default.ctp';
153 $result = $ThemeView->getLayoutFileName('plugin_default');
154 $this->assertEquals($expected, $result);
156 $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp';
157 $result = $ThemeView->getLayoutFileName('default');
158 $this->assertEquals($expected, $result);
162 * testGetTemplate method
166 public function testGetTemplate() {
167 $this->Controller->plugin = null;
168 $this->Controller->name = 'Pages';
169 $this->Controller->viewPath = 'Pages';
170 $this->Controller->action = 'display';
171 $this->Controller->params['pass'] = array('home');
173 $ThemeView = new TestTheme2View($this->Controller);
174 $ThemeView->theme = 'TestTheme';
175 $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp';
176 $result = $ThemeView->getViewFileName('home');
177 $this->assertEquals($expected, $result);
179 $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS . 'index.ctp';
180 $result = $ThemeView->getViewFileName('/Posts/index');
181 $this->assertEquals($expected, $result);
183 $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp';
184 $result = $ThemeView->getLayoutFileName();
185 $this->assertEquals($expected, $result);
187 $ThemeView->layoutPath = 'rss';
188 $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp';
189 $result = $ThemeView->getLayoutFileName();
190 $this->assertEquals($expected, $result);
192 $ThemeView->layoutPath = 'Emails' . DS . 'html';
193 $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp';
194 $result = $ThemeView->getLayoutFileName();
195 $this->assertEquals($expected, $result);
199 * testMissingView method
201 * @expectedException MissingViewException
204 public function testMissingView() {
205 $this->Controller->plugin = null;
206 $this->Controller->name = 'Pages';
207 $this->Controller->viewPath = 'Pages';
208 $this->Controller->action = 'display';
209 $this->Controller->theme = 'my_theme';
211 $this->Controller->params['pass'] = array('home');
213 $View = new TestTheme2View($this->Controller);
215 $View->getViewFileName('does_not_exist');
216 $expected = ob_get_clean();
217 $this->assertRegExp("/PagesController::/", $expected);
218 $this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)pages(\/|\\\)does_not_exist.ctp/", $expected);
222 * testMissingLayout method
224 * @expectedException MissingLayoutException
227 public function testMissingLayout() {
228 $this->Controller->plugin = null;
229 $this->Controller->name = 'Posts';
230 $this->Controller->viewPath = 'posts';
231 $this->Controller->layout = 'whatever';
232 $this->Controller->theme = 'my_theme';
234 $View = new TestTheme2View($this->Controller);
236 $View->getLayoutFileName();
237 $expected = ob_get_clean();
238 $this->assertRegExp("/Missing Layout/", $expected);
239 $this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)layouts(\/|\\\)whatever.ctp/", $expected);
243 * test memory leaks that existed in _paths at one point.
247 public function testMemoryLeakInPaths() {
248 $this->Controller->plugin = null;
249 $this->Controller->name = 'Posts';
250 $this->Controller->viewPath = 'posts';
251 $this->Controller->layout = 'whatever';
252 $this->Controller->theme = 'TestTheme';
254 $View = new ThemeView($this->Controller);
255 $View->element('test_element');
257 $start = memory_get_usage();
258 for ($i = 0; $i < 10; $i++) {
259 $View->element('test_element');
261 $end = memory_get_usage();
262 $this->assertLessThanOrEqual($start + 5000, $end);