pictcode / lib / Cake / Test / Case / View / XmlViewTest.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (7.294 KB)
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* XmlViewTest 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 2.1.0
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
17 |
*/
|
18 |
|
19 |
App::uses('Controller', 'Controller'); |
20 |
App::uses('CakeRequest', 'Network'); |
21 |
App::uses('CakeResponse', 'Network'); |
22 |
App::uses('XmlView', 'View'); |
23 |
|
24 |
/**
|
25 |
* XmlViewTest
|
26 |
*
|
27 |
* @package Cake.Test.Case.View
|
28 |
*/
|
29 |
class XmlViewTest extends CakeTestCase { |
30 |
|
31 |
public function setUp() { |
32 |
parent::setUp();
|
33 |
Configure::write('debug', 0); |
34 |
} |
35 |
|
36 |
/**
|
37 |
* testRenderWithoutView method
|
38 |
*
|
39 |
* @return void
|
40 |
*/
|
41 |
public function testRenderWithoutView() { |
42 |
$Request = new CakeRequest(); |
43 |
$Response = new CakeResponse(); |
44 |
$Controller = new Controller($Request, $Response); |
45 |
$data = array('users' => array('user' => array('user1', 'user2'))); |
46 |
$Controller->set(array('users' => $data, '_serialize' => 'users')); |
47 |
$View = new XmlView($Controller); |
48 |
$output = $View->render(false); |
49 |
|
50 |
$this->assertSame(Xml::build($data)->asXML(), $output); |
51 |
$this->assertSame('application/xml', $Response->type()); |
52 |
|
53 |
$data = array( |
54 |
array(
|
55 |
'User' => array( |
56 |
'username' => 'user1' |
57 |
) |
58 |
), |
59 |
array(
|
60 |
'User' => array( |
61 |
'username' => 'user2' |
62 |
) |
63 |
) |
64 |
); |
65 |
$Controller->set(array('users' => $data, '_serialize' => 'users')); |
66 |
$View = new XmlView($Controller); |
67 |
$output = $View->render(false); |
68 |
|
69 |
$expected = Xml::build(array('response' => array('users' => $data)))->asXML(); |
70 |
$this->assertSame($expected, $output); |
71 |
|
72 |
$Controller->set('_rootNode', 'custom_name'); |
73 |
$View = new XmlView($Controller); |
74 |
$output = $View->render(false); |
75 |
|
76 |
$expected = Xml::build(array('custom_name' => array('users' => $data)))->asXML(); |
77 |
$this->assertSame($expected, $output); |
78 |
} |
79 |
|
80 |
/**
|
81 |
* Test that rendering with _serialize does not load helpers
|
82 |
*
|
83 |
* @return void
|
84 |
*/
|
85 |
public function testRenderSerializeNoHelpers() { |
86 |
$Request = new CakeRequest(); |
87 |
$Response = new CakeResponse(); |
88 |
$Controller = new Controller($Request, $Response); |
89 |
$Controller->helpers = array('Html'); |
90 |
$Controller->set(array( |
91 |
'_serialize' => 'tags', |
92 |
'tags' => array('cakephp', 'framework') |
93 |
)); |
94 |
$View = new XmlView($Controller); |
95 |
$View->render();
|
96 |
$this->assertFalse(isset($View->Html), 'No helper loaded.'); |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Test that rendering with _serialize respects XML options.
|
101 |
*
|
102 |
* @return void
|
103 |
*/
|
104 |
public function testRenderSerializeWithOptions() { |
105 |
$Request = new CakeRequest(); |
106 |
$Response = new CakeResponse(); |
107 |
$Controller = new Controller($Request, $Response); |
108 |
$data = array( |
109 |
'_serialize' => array('tags'), |
110 |
'_xmlOptions' => array('format' => 'attributes', 'return' => 'domdocument'), |
111 |
'tags' => array( |
112 |
'tag' => array( |
113 |
array(
|
114 |
'id' => '1', |
115 |
'name' => 'defect' |
116 |
), |
117 |
array(
|
118 |
'id' => '2', |
119 |
'name' => 'enhancement' |
120 |
) |
121 |
) |
122 |
) |
123 |
); |
124 |
$Controller->set($data); |
125 |
$Controller->viewClass = 'Xml'; |
126 |
$View = new XmlView($Controller); |
127 |
$result = $View->render(); |
128 |
|
129 |
$expected = Xml::build(array('response' => array('tags' => $data['tags'])), $data['_xmlOptions'])->saveXML(); |
130 |
$this->assertSame($expected, $result); |
131 |
} |
132 |
|
133 |
/**
|
134 |
* Test that rendering with _serialize can work with string setting.
|
135 |
*
|
136 |
* @return void
|
137 |
*/
|
138 |
public function testRenderSerializeWithString() { |
139 |
$Request = new CakeRequest(); |
140 |
$Response = new CakeResponse(); |
141 |
$Controller = new Controller($Request, $Response); |
142 |
$data = array( |
143 |
'_serialize' => 'tags', |
144 |
'_xmlOptions' => array('format' => 'attributes'), |
145 |
'tags' => array( |
146 |
'tags' => array( |
147 |
'tag' => array( |
148 |
array(
|
149 |
'id' => '1', |
150 |
'name' => 'defect' |
151 |
), |
152 |
array(
|
153 |
'id' => '2', |
154 |
'name' => 'enhancement' |
155 |
) |
156 |
) |
157 |
) |
158 |
) |
159 |
); |
160 |
$Controller->set($data); |
161 |
$Controller->viewClass = 'Xml'; |
162 |
$View = new XmlView($Controller); |
163 |
$result = $View->render(); |
164 |
|
165 |
$expected = Xml::build($data['tags'], $data['_xmlOptions'])->asXML(); |
166 |
$this->assertSame($expected, $result); |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Test render with an array in _serialize
|
171 |
*
|
172 |
* @return void
|
173 |
*/
|
174 |
public function testRenderWithoutViewMultiple() { |
175 |
$Request = new CakeRequest(); |
176 |
$Response = new CakeResponse(); |
177 |
$Controller = new Controller($Request, $Response); |
178 |
$data = array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')); |
179 |
$Controller->set($data); |
180 |
$Controller->set('_serialize', array('no', 'user')); |
181 |
$View = new XmlView($Controller); |
182 |
$this->assertSame('application/xml', $Response->type()); |
183 |
$output = $View->render(false); |
184 |
$expected = array( |
185 |
'response' => array('no' => $data['no'], 'user' => $data['user']) |
186 |
); |
187 |
$this->assertSame(Xml::build($expected)->asXML(), $output); |
188 |
|
189 |
$Controller->set('_rootNode', 'custom_name'); |
190 |
$View = new XmlView($Controller); |
191 |
$output = $View->render(false); |
192 |
$expected = array( |
193 |
'custom_name' => array('no' => $data['no'], 'user' => $data['user']) |
194 |
); |
195 |
$this->assertSame(Xml::build($expected)->asXML(), $output); |
196 |
} |
197 |
|
198 |
/**
|
199 |
* Test render with an array in _serialize and alias
|
200 |
*
|
201 |
* @return void
|
202 |
*/
|
203 |
public function testRenderWithoutViewMultipleAndAlias() { |
204 |
$Request = new CakeRequest(); |
205 |
$Response = new CakeResponse(); |
206 |
$Controller = new Controller($Request, $Response); |
207 |
$data = array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2')); |
208 |
$Controller->set($data); |
209 |
$Controller->set('_serialize', array('new_name' => 'original_name', 'user')); |
210 |
$View = new XmlView($Controller); |
211 |
$this->assertSame('application/xml', $Response->type()); |
212 |
$output = $View->render(false); |
213 |
$expected = array( |
214 |
'response' => array('new_name' => $data['original_name'], 'user' => $data['user']) |
215 |
); |
216 |
$this->assertSame(Xml::build($expected)->asXML(), $output); |
217 |
|
218 |
$Controller->set('_rootNode', 'custom_name'); |
219 |
$View = new XmlView($Controller); |
220 |
$output = $View->render(false); |
221 |
$expected = array( |
222 |
'custom_name' => array('new_name' => $data['original_name'], 'user' => $data['user']) |
223 |
); |
224 |
$this->assertSame(Xml::build($expected)->asXML(), $output); |
225 |
} |
226 |
|
227 |
/**
|
228 |
* testRenderWithView method
|
229 |
*
|
230 |
* @return void
|
231 |
*/
|
232 |
public function testRenderWithView() { |
233 |
App::build(array('View' => array( |
234 |
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS |
235 |
))); |
236 |
$Request = new CakeRequest(); |
237 |
$Response = new CakeResponse(); |
238 |
$Controller = new Controller($Request, $Response); |
239 |
$Controller->name = $Controller->viewPath = 'Posts'; |
240 |
|
241 |
$data = array( |
242 |
array(
|
243 |
'User' => array( |
244 |
'username' => 'user1' |
245 |
) |
246 |
), |
247 |
array(
|
248 |
'User' => array( |
249 |
'username' => 'user2' |
250 |
) |
251 |
) |
252 |
); |
253 |
$Controller->set('users', $data); |
254 |
$View = new XmlView($Controller); |
255 |
$output = $View->render('index'); |
256 |
|
257 |
$expected = array( |
258 |
'users' => array('user' => array('user1', 'user2')) |
259 |
); |
260 |
$expected = Xml::build($expected)->asXML(); |
261 |
$this->assertSame($expected, $output); |
262 |
$this->assertSame('application/xml', $Response->type()); |
263 |
$this->assertInstanceOf('HelperCollection', $View->Helpers); |
264 |
} |
265 |
|
266 |
} |