pictcode / lib / Cake / Test / Case / View / ThemeViewTest.php @ 0b1b8047
履歴 | 表示 | アノテート | ダウンロード (7.289 KB)
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* ThemeViewTest file
|
4 |
*
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
7 |
*
|
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
|
11 |
*
|
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
|
17 |
*/
|
18 |
|
19 |
App::uses('View', 'View'); |
20 |
App::uses('ThemeView', 'View'); |
21 |
App::uses('Controller', 'Controller'); |
22 |
|
23 |
/**
|
24 |
* ThemePosts2Controller class
|
25 |
*
|
26 |
* @package Cake.Test.Case.View
|
27 |
*/
|
28 |
class ThemePosts2Controller extends Controller { |
29 |
|
30 |
/**
|
31 |
* name property
|
32 |
*
|
33 |
* @var string
|
34 |
*/
|
35 |
public $name = 'ThemePosts'; |
36 |
|
37 |
public $theme = null; |
38 |
|
39 |
/**
|
40 |
* index method
|
41 |
*
|
42 |
* @return void
|
43 |
*/
|
44 |
public function index() { |
45 |
$this->set(array( |
46 |
'testData' => 'Some test data', |
47 |
'test2' => 'more data', |
48 |
'test3' => 'even more data', |
49 |
)); |
50 |
} |
51 |
|
52 |
} |
53 |
|
54 |
/**
|
55 |
* TestTheme2View class
|
56 |
*
|
57 |
* @package Cake.Test.Case.View
|
58 |
*/
|
59 |
class TestTheme2View extends ThemeView { |
60 |
|
61 |
/**
|
62 |
* renderElement method
|
63 |
*
|
64 |
* @param string $name
|
65 |
* @param array $params
|
66 |
* @return void
|
67 |
*/
|
68 |
public function renderElement($name, $params = array()) { |
69 |
return $name; |
70 |
} |
71 |
|
72 |
/**
|
73 |
* getViewFileName method
|
74 |
*
|
75 |
* @param string $name
|
76 |
* @return void
|
77 |
*/
|
78 |
public function getViewFileName($name = null) { |
79 |
return $this->_getViewFileName($name); |
80 |
} |
81 |
|
82 |
/**
|
83 |
* getLayoutFileName method
|
84 |
*
|
85 |
* @param string $name
|
86 |
* @return void
|
87 |
*/
|
88 |
public function getLayoutFileName($name = null) { |
89 |
return $this->_getLayoutFileName($name); |
90 |
} |
91 |
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* ThemeViewTest class
|
96 |
*
|
97 |
* @package Cake.Test.Case.View
|
98 |
*/
|
99 |
class ThemeViewTest extends CakeTestCase { |
100 |
|
101 |
/**
|
102 |
* setUp method
|
103 |
*
|
104 |
* @return void
|
105 |
*/
|
106 |
public function setUp() { |
107 |
parent::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); |
114 |
App::build(array( |
115 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS), |
116 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS) |
117 |
)); |
118 |
App::objects('plugins', null, false); |
119 |
CakePlugin::load(array('TestPlugin')); |
120 |
} |
121 |
|
122 |
/**
|
123 |
* tearDown method
|
124 |
*
|
125 |
* @return void
|
126 |
*/
|
127 |
public function tearDown() { |
128 |
parent::tearDown();
|
129 |
unset($this->ThemeView); |
130 |
unset($this->PostsController); |
131 |
unset($this->Controller); |
132 |
CakePlugin::unload();
|
133 |
} |
134 |
|
135 |
/**
|
136 |
* testPluginGetTemplate method
|
137 |
*
|
138 |
* @return void
|
139 |
*/
|
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'; |
146 |
|
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); |
151 |
|
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); |
155 |
|
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); |
159 |
} |
160 |
|
161 |
/**
|
162 |
* testGetTemplate method
|
163 |
*
|
164 |
* @return void
|
165 |
*/
|
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'); |
172 |
|
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); |
178 |
|
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); |
182 |
|
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); |
186 |
|
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); |
191 |
|
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); |
196 |
} |
197 |
|
198 |
/**
|
199 |
* testMissingView method
|
200 |
*
|
201 |
* @expectedException MissingViewException
|
202 |
* @return void
|
203 |
*/
|
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'; |
210 |
|
211 |
$this->Controller->params['pass'] = array('home'); |
212 |
|
213 |
$View = new TestTheme2View($this->Controller); |
214 |
ob_start(); |
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); |
219 |
} |
220 |
|
221 |
/**
|
222 |
* testMissingLayout method
|
223 |
*
|
224 |
* @expectedException MissingLayoutException
|
225 |
* @return void
|
226 |
*/
|
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'; |
233 |
|
234 |
$View = new TestTheme2View($this->Controller); |
235 |
ob_start(); |
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); |
240 |
} |
241 |
|
242 |
/**
|
243 |
* test memory leaks that existed in _paths at one point.
|
244 |
*
|
245 |
* @return void
|
246 |
*/
|
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'; |
253 |
|
254 |
$View = new ThemeView($this->Controller); |
255 |
$View->element('test_element'); |
256 |
|
257 |
$start = memory_get_usage();
|
258 |
for ($i = 0; $i < 10; $i++) { |
259 |
$View->element('test_element'); |
260 |
} |
261 |
$end = memory_get_usage();
|
262 |
$this->assertLessThanOrEqual($start + 5000, $end); |
263 |
} |
264 |
} |