comparison cake/tests/cases/libs/view/helpers/xml.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * XmlHelperTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs.view.helpers
17 * @since CakePHP(tm) v 1.2.0.4206
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
21 define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
22 }
23 App::import('Helper', 'Xml');
24
25 /**
26 * TestXml class
27 *
28 * @package cake
29 * @subpackage cake.tests.cases.libs.view.helpers
30 */
31 class TestXml extends Object {
32
33 /**
34 * content property
35 *
36 * @var string ''
37 * @access public
38 */
39 var $content = '';
40
41 /**
42 * construct method
43 *
44 * @param mixed $content
45 * @access private
46 * @return void
47 */
48 function __construct($content) {
49 $this->content = $content;
50 }
51
52 /**
53 * toString method
54 *
55 * @access public
56 * @return void
57 */
58 function toString() {
59 return $this->content;
60 }
61 }
62
63 /**
64 * XmlHelperTest class
65 *
66 * @package cake
67 * @subpackage cake.tests.cases.libs.view.helpers
68 */
69 class XmlHelperTest extends CakeTestCase {
70
71 /**
72 * setUp method
73 *
74 * @access public
75 * @return void
76 */
77 function setUp() {
78 $this->Xml =& new XmlHelper();
79 $this->Xml->beforeRender();
80 $manager =& XmlManager::getInstance();
81 $manager->namespaces = array();
82 }
83
84 /**
85 * tearDown method
86 *
87 * @access public
88 * @return void
89 */
90 function tearDown() {
91 unset($this->Xml);
92 }
93
94 /**
95 * testAddNamespace method
96 *
97 * @access public
98 * @return void
99 */
100 function testAddNamespace() {
101 $this->Xml->addNs('custom', 'http://example.com/dtd.xml');
102 $manager =& XmlManager::getInstance();
103
104 $expected = array('custom' => 'http://example.com/dtd.xml');
105 $this->assertEqual($manager->namespaces, $expected);
106 }
107
108 /**
109 * testRemoveNamespace method
110 *
111 * @access public
112 * @return void
113 */
114 function testRemoveNamespace() {
115 $this->Xml->addNs('custom', 'http://example.com/dtd.xml');
116 $this->Xml->addNs('custom2', 'http://example.com/dtd2.xml');
117 $manager =& XmlManager::getInstance();
118
119 $expected = array('custom' => 'http://example.com/dtd.xml', 'custom2' => 'http://example.com/dtd2.xml');
120 $this->assertEqual($manager->namespaces, $expected);
121
122 $this->Xml->removeNs('custom');
123 $expected = array('custom2' => 'http://example.com/dtd2.xml');
124 $this->assertEqual($manager->namespaces, $expected);
125 }
126
127 /**
128 * testRenderZeroElement method
129 *
130 * @access public
131 * @return void
132 */
133 function testRenderZeroElement() {
134 $result = $this->Xml->elem('count', null, 0);
135 $expected = '<count>0</count>';
136 $this->assertEqual($result, $expected);
137
138 $result = $this->Xml->elem('count', null, array('cdata' => true, 'value' => null));
139 $expected = '<count />';
140 $this->assertEqual($result, $expected);
141 }
142
143 /**
144 * testRenderElementWithNamespace method
145 *
146 * @access public
147 * @return void
148 */
149 function testRenderElementWithNamespace() {
150 $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), 'content');
151 $expected = '<myNameSpace:count>content</myNameSpace:count>';
152 $this->assertEqual($result, $expected);
153
154 $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), 'content', false);
155 $expected = '<myNameSpace:count>content';
156 $this->assertEqual($result, $expected);
157
158 $expected .= '</myNameSpace:count>';
159 $result .= $this->Xml->closeElem();
160 $this->assertEqual($result, $expected);
161 }
162
163 /**
164 * testRenderElementWithComplexContent method
165 *
166 * @access public
167 * @return void
168 */
169 function testRenderElementWithComplexContent() {
170 $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), array('contrived' => 'content'));
171 $expected = '<myNameSpace:count><content /></myNameSpace:count>';
172 $this->assertEqual($result, $expected);
173
174 $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), array('cdata' => true, 'value' => 'content'));
175 $expected = '<myNameSpace:count><![CDATA[content]]></myNameSpace:count>';
176 $this->assertEqual($result, $expected);
177 }
178
179 /**
180 * testSerialize method
181 *
182 * @access public
183 * @return void
184 */
185 function testSerialize() {
186 $data = array(
187 'test1' => 'test with no quotes',
188 'test2' => 'test with "double quotes"'
189 );
190 $result = $this->Xml->serialize($data);
191 $expected = '<std_class test1="test with no quotes" test2="test with &quot;double quotes&quot;" />';
192 $this->assertIdentical($result, $expected);
193
194 $data = array(
195 'test1' => 'test with no quotes',
196 'test2' => 'test without double quotes'
197 );
198 $result = $this->Xml->serialize($data);
199 $expected = '<std_class test1="test with no quotes" test2="test without double quotes" />';
200 $this->assertIdentical($result, $expected);
201
202 $data = array(
203 'ServiceDay' => array('ServiceTime' => array('ServiceTimePrice' => array('dollar' => 1, 'cents' => '2')))
204 );
205 $result = $this->Xml->serialize($data);
206 $expected = '<service_day><service_time><service_time_price dollar="1" cents="2" /></service_time></service_day>';
207 $this->assertIdentical($result, $expected);
208
209 $data = array(
210 'ServiceDay' => array('ServiceTime' => array('ServiceTimePrice' => array('dollar' => 1, 'cents' => '2')))
211 );
212 $result = $this->Xml->serialize($data, array('format' => 'tags'));
213 $expected = '<service_day><service_time><service_time_price><dollar>1</dollar><cents>2</cents></service_time_price></service_time></service_day>';
214 $this->assertIdentical($result, $expected);
215
216 $data = array(
217 'Pages' => array('id' => 2, 'url' => 'http://www.url.com/rb/153/?id=bbbb&t=access')
218 );
219 $result = $this->Xml->serialize($data);
220 $expected = '<pages id="2" url="http://www.url.com/rb/153/?id=bbbb&amp;t=access" />';
221 $this->assertIdentical($result, $expected);
222
223 $test = array(
224 'Test' => array('test' => true)
225 );
226 $expected = '<test test="1" />';
227 $result = $this->Xml->serialize($test);
228 $this->assertidentical($expected, $result);
229 }
230
231 /**
232 * testSerializeOnMultiDimensionalArray method
233 *
234 * @access public
235 * @return void
236 */
237 function testSerializeOnMultiDimensionalArray() {
238 $data = array(
239 'Statuses' => array(
240 array('Status' => array('id' => 1)),
241 array('Status' => array('id' => 2))
242 )
243 );
244 $result = $this->Xml->serialize($data, array('format' => 'tags'));
245 $expected = '<statuses><status><id>1</id></status><status><id>2</id></status></statuses>';
246 $this->assertIdentical($result, $expected);
247
248 }
249
250 /**
251 * testHeader method
252 *
253 * @access public
254 * @return void
255 */
256 function testHeader() {
257 $expectedDefaultEncoding = Configure::read('App.encoding');
258 if (empty($expectedDefaultEncoding)) {
259 $expectedDefaultEncoding = 'UTF-8';
260 }
261 $attrib = array();
262 $result = $this->Xml->header($attrib);
263 $expected = '<?xml version="1.0" encoding="'.$expectedDefaultEncoding.'" ?>';
264 $this->assertIdentical($result, $expected);
265
266 $attrib = array(
267 'encoding' => 'UTF-8',
268 'version' => '1.1'
269 );
270 $result = $this->Xml->header($attrib);
271 $expected = '<?xml version="1.1" encoding="UTF-8" ?>';
272 $this->assertIdentical($result, $expected);
273
274 $attrib = array(
275 'encoding' => 'UTF-8',
276 'version' => '1.2',
277 'additional' => 'attribute'
278 );
279 $result = $this->Xml->header($attrib);
280 $expected = '<?xml version="1.2" encoding="UTF-8" additional="attribute" ?>';
281 $this->assertIdentical($result, $expected);
282
283 $attrib = 'encoding="UTF-8" someOther="value"';
284 $result = $this->Xml->header($attrib);
285 $expected = '<?xml encoding="UTF-8" someOther="value" ?>';
286 $this->assertIdentical($result, $expected);
287 }
288
289 /**
290 * test that calling elem() and then header() doesn't break
291 *
292 * @return void
293 */
294 function testElemThenHeader() {
295 $this->Xml->elem('test', array(), 'foo', false);
296 $this->assertPattern('/<\?xml/', $this->Xml->header());
297 }
298 }