comparison cake/tests/cases/libs/set.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 * SetTest 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
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 App::import('Core', 'Set');
21
22 /**
23 * SetTest class
24 *
25 * @package cake
26 * @subpackage cake.tests.cases.libs
27 */
28 class SetTest extends CakeTestCase {
29
30 /**
31 * testNumericKeyExtraction method
32 *
33 * @access public
34 * @return void
35 */
36 function testNumericKeyExtraction() {
37 $data = array('plugin' => null, 'controller' => '', 'action' => '', 1, 'whatever');
38 $this->assertIdentical(Set::extract($data, '{n}'), array(1, 'whatever'));
39 $this->assertIdentical(Set::diff($data, Set::extract($data, '{n}')), array('plugin' => null, 'controller' => '', 'action' => ''));
40 }
41
42 /**
43 * testEnum method
44 *
45 * @access public
46 * @return void
47 */
48 function testEnum() {
49 $result = Set::enum(1, 'one, two');
50 $this->assertIdentical($result, 'two');
51 $result = Set::enum(2, 'one, two');
52 $this->assertNull($result);
53
54 $set = array('one', 'two');
55 $result = Set::enum(0, $set);
56 $this->assertIdentical($result, 'one');
57 $result = Set::enum(1, $set);
58 $this->assertIdentical($result, 'two');
59
60 $result = Set::enum(1, array('one', 'two'));
61 $this->assertIdentical($result, 'two');
62 $result = Set::enum(2, array('one', 'two'));
63 $this->assertNull($result);
64
65 $result = Set::enum('first', array('first' => 'one', 'second' => 'two'));
66 $this->assertIdentical($result, 'one');
67 $result = Set::enum('third', array('first' => 'one', 'second' => 'two'));
68 $this->assertNull($result);
69
70 $result = Set::enum('no', array('no' => 0, 'yes' => 1));
71 $this->assertIdentical($result, 0);
72 $result = Set::enum('not sure', array('no' => 0, 'yes' => 1));
73 $this->assertNull($result);
74
75 $result = Set::enum(0);
76 $this->assertIdentical($result, 'no');
77 $result = Set::enum(1);
78 $this->assertIdentical($result, 'yes');
79 $result = Set::enum(2);
80 $this->assertNull($result);
81 }
82
83 /**
84 * testFilter method
85 *
86 * @access public
87 * @return void
88 */
89 function testFilter() {
90 $result = Set::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
91 $expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be', false));
92 $this->assertIdentical($result, $expected);
93 }
94
95 /**
96 * testNumericArrayCheck method
97 *
98 * @access public
99 * @return void
100 */
101 function testNumericArrayCheck() {
102 $data = array('one');
103 $this->assertTrue(Set::numeric(array_keys($data)));
104
105 $data = array(1 => 'one');
106 $this->assertFalse(Set::numeric($data));
107
108 $data = array('one');
109 $this->assertFalse(Set::numeric($data));
110
111 $data = array('one' => 'two');
112 $this->assertFalse(Set::numeric($data));
113
114 $data = array('one' => 1);
115 $this->assertTrue(Set::numeric($data));
116
117 $data = array(0);
118 $this->assertTrue(Set::numeric($data));
119
120 $data = array('one', 'two', 'three', 'four', 'five');
121 $this->assertTrue(Set::numeric(array_keys($data)));
122
123 $data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
124 $this->assertTrue(Set::numeric(array_keys($data)));
125
126 $data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
127 $this->assertTrue(Set::numeric(array_keys($data)));
128
129 $data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
130 $this->assertFalse(Set::numeric(array_keys($data)));
131 }
132
133 /**
134 * testKeyCheck method
135 *
136 * @access public
137 * @return void
138 */
139 function testKeyCheck() {
140 $data = array('Multi' => array('dimensonal' => array('array')));
141 $this->assertTrue(Set::check($data, 'Multi.dimensonal'));
142 $this->assertFalse(Set::check($data, 'Multi.dimensonal.array'));
143
144 $data = array(
145 array(
146 'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
147 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
148 'Comment' => array(
149 array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
150 array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
151 ),
152 'Tag' => array(
153 array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
154 array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
155 )
156 ),
157 array(
158 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
159 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
160 'Comment' => array(),
161 'Tag' => array()
162 )
163 );
164 $this->assertTrue(Set::check($data, '0.Article.user_id'));
165 $this->assertTrue(Set::check($data, '0.Comment.0.id'));
166 $this->assertFalse(Set::check($data, '0.Comment.0.id.0'));
167 $this->assertTrue(Set::check($data, '0.Article.user_id'));
168 $this->assertFalse(Set::check($data, '0.Article.user_id.a'));
169 }
170
171 /**
172 * testMerge method
173 *
174 * @access public
175 * @return void
176 */
177 function testMerge() {
178 $r = Set::merge(array('foo'));
179 $this->assertIdentical($r, array('foo'));
180
181 $r = Set::merge('foo');
182 $this->assertIdentical($r, array('foo'));
183
184 $r = Set::merge('foo', 'bar');
185 $this->assertIdentical($r, array('foo', 'bar'));
186
187 if (substr(PHP_VERSION, 0, 1) >= 5) {
188 $r = eval('class StaticSetCaller{static function merge($a, $b){return Set::merge($a, $b);}} return StaticSetCaller::merge("foo", "bar");');
189 $this->assertIdentical($r, array('foo', 'bar'));
190 }
191
192 $r = Set::merge('foo', array('user' => 'bob', 'no-bar'), 'bar');
193 $this->assertIdentical($r, array('foo', 'user' => 'bob', 'no-bar', 'bar'));
194
195 $a = array('foo', 'foo2');
196 $b = array('bar', 'bar2');
197 $this->assertIdentical(Set::merge($a, $b), array('foo', 'foo2', 'bar', 'bar2'));
198
199 $a = array('foo' => 'bar', 'bar' => 'foo');
200 $b = array('foo' => 'no-bar', 'bar' => 'no-foo');
201 $this->assertIdentical(Set::merge($a, $b), array('foo' => 'no-bar', 'bar' => 'no-foo'));
202
203 $a = array('users' => array('bob', 'jim'));
204 $b = array('users' => array('lisa', 'tina'));
205 $this->assertIdentical(Set::merge($a, $b), array('users' => array('bob', 'jim', 'lisa', 'tina')));
206
207 $a = array('users' => array('jim', 'bob'));
208 $b = array('users' => 'none');
209 $this->assertIdentical(Set::merge($a, $b), array('users' => 'none'));
210
211 $a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'cakephp');
212 $b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream');
213 $this->assertIdentical(Set::merge($a, $b), array('users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)), 'cakephp', 'ice-cream'));
214
215 $c = array('users' => array('lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'chocolate');
216 $expected = array('users' => array('lisa' => array('id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'cakephp', 'ice-cream', 'chocolate');
217 $this->assertIdentical(Set::merge($a, $b, $c), $expected);
218
219 $this->assertIdentical(Set::merge($a, $b, array(), $c), $expected);
220
221 $r = Set::merge($a, $b, $c);
222 $this->assertIdentical($r, $expected);
223
224 $a = array('Tree', 'CounterCache',
225 'Upload' => array('folder' => 'products',
226 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')));
227 $b = array('Cacheable' => array('enabled' => false),
228 'Limit',
229 'Bindable',
230 'Validator',
231 'Transactional');
232
233 $expected = array('Tree', 'CounterCache',
234 'Upload' => array('folder' => 'products',
235 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),
236 'Cacheable' => array('enabled' => false),
237 'Limit',
238 'Bindable',
239 'Validator',
240 'Transactional');
241
242 $this->assertIdentical(Set::merge($a, $b), $expected);
243
244 $expected = array('Tree' => null, 'CounterCache' => null,
245 'Upload' => array('folder' => 'products',
246 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),
247 'Cacheable' => array('enabled' => false),
248 'Limit' => null,
249 'Bindable' => null,
250 'Validator' => null,
251 'Transactional' => null);
252
253 $this->assertIdentical(Set::normalize(Set::merge($a, $b)), $expected);
254 }
255
256 /**
257 * testSort method
258 *
259 * @access public
260 * @return void
261 */
262 function testSort() {
263 $a = array(
264 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
265 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
266 );
267 $b = array(
268 0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
269 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
270
271 );
272 $a = Set::sort($a, '{n}.Friend.{n}.name', 'asc');
273 $this->assertIdentical($a, $b);
274
275 $b = array(
276 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
277 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
278 );
279 $a = array(
280 0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
281 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
282
283 );
284 $a = Set::sort($a, '{n}.Friend.{n}.name', 'desc');
285 $this->assertIdentical($a, $b);
286
287 $a = array(
288 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
289 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
290 2 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob')))
291 );
292 $b = array(
293 0 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob'))),
294 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
295 2 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
296 );
297 $a = Set::sort($a, '{n}.Person.name', 'asc');
298 $this->assertIdentical($a, $b);
299
300 $a = array(
301 array(7,6,4),
302 array(3,4,5),
303 array(3,2,1),
304 );
305
306 $b = array(
307 array(3,2,1),
308 array(3,4,5),
309 array(7,6,4),
310 );
311
312 $a = Set::sort($a, '{n}.{n}', 'asc');
313 $this->assertIdentical($a, $b);
314
315 $a = array(
316 array(7,6,4),
317 array(3,4,5),
318 array(3,2,array(1,1,1)),
319 );
320
321 $b = array(
322 array(3,2,array(1,1,1)),
323 array(3,4,5),
324 array(7,6,4),
325 );
326
327 $a = Set::sort($a, '{n}', 'asc');
328 $this->assertIdentical($a, $b);
329
330 $a = array(
331 0 => array('Person' => array('name' => 'Jeff')),
332 1 => array('Shirt' => array('color' => 'black'))
333 );
334 $b = array(
335 0 => array('Shirt' => array('color' => 'black')),
336 1 => array('Person' => array('name' => 'Jeff')),
337 );
338 $a = Set::sort($a, '{n}.Person.name', 'ASC');
339 $this->assertIdentical($a, $b);
340
341 $names = array(
342 array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
343 array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
344 array('employees' => array(array('name' => array()))),
345 array('employees' => array(array('name' => array())))
346 );
347 $result = Set::sort($names, '{n}.employees.0.name', 'asc', 1);
348 $expected = array(
349 array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
350 array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
351 array('employees' => array(array('name' => array()))),
352 array('employees' => array(array('name' => array())))
353 );
354 $this->assertEqual($result, $expected);
355 }
356
357 /**
358 * test sorting with out of order keys.
359 *
360 * @return void
361 */
362 function testSortWithOutOfOrderKeys() {
363 $data = array(
364 9 => array('class' => 510, 'test2' => 2),
365 1 => array('class' => 500, 'test2' => 1),
366 2 => array('class' => 600, 'test2' => 2),
367 5 => array('class' => 625, 'test2' => 4),
368 0 => array('class' => 605, 'test2' => 3),
369 );
370 $expected = array(
371 array('class' => 500, 'test2' => 1),
372 array('class' => 510, 'test2' => 2),
373 array('class' => 600, 'test2' => 2),
374 array('class' => 605, 'test2' => 3),
375 array('class' => 625, 'test2' => 4),
376 );
377 $result = Set::sort($data, '{n}.class', 'asc');
378 $this->assertEqual($expected, $result);
379
380 $result = Set::sort($data, '{n}.test2', 'asc');
381 $this->assertEqual($expected, $result);
382 }
383
384 /**
385 * testExtract method
386 *
387 * @access public
388 * @return void
389 */
390 function testExtract() {
391 $a = array(
392 array(
393 'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
394 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
395 'Comment' => array(
396 array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
397 array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
398 ),
399 'Tag' => array(
400 array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
401 array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
402 ),
403 'Deep' => array(
404 'Nesting' => array(
405 'test' => array(
406 1 => 'foo',
407 2 => array(
408 'and' => array('more' => 'stuff')
409 )
410 )
411 )
412 )
413 ),
414 array(
415 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
416 'User' => array('id' => '2', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
417 'Comment' => array(),
418 'Tag' => array()
419 ),
420 array(
421 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
422 'User' => array('id' => '3', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
423 'Comment' => array(),
424 'Tag' => array()
425 ),
426 array(
427 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
428 'User' => array('id' => '4', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
429 'Comment' => array(),
430 'Tag' => array()
431 ),
432 array(
433 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
434 'User' => array('id' => '5', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
435 'Comment' => array(),
436 'Tag' => array()
437 )
438 );
439 $b = array('Deep' => $a[0]['Deep']);
440 $c = array(
441 array('a' => array('I' => array('a' => 1))),
442 array(
443 'a' => array(
444 2
445 )
446 ),
447 array('a' => array('II' => array('a' => 3, 'III' => array('a' => array('foo' => 4))))),
448 );
449
450 $expected = array(array('a' => $c[2]['a']));
451 $r = Set::extract('/a/II[a=3]/..', $c);
452 $this->assertEqual($r, $expected);
453
454 $expected = array(1, 2, 3, 4, 5);
455 $this->assertEqual(Set::extract('/User/id', $a), $expected);
456
457 $expected = array(1, 2, 3, 4, 5);
458 $this->assertEqual(Set::extract('/User/id', $a), $expected);
459
460 $expected = array(
461 array('id' => 1), array('id' => 2), array('id' => 3), array('id' => 4), array('id' => 5)
462 );
463
464 $r = Set::extract('/User/id', $a, array('flatten' => false));
465 $this->assertEqual($r, $expected);
466
467 $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
468 $this->assertEqual(Set::extract('/Deep/Nesting/test', $a), $expected);
469 $this->assertEqual(Set::extract('/Deep/Nesting/test', $b), $expected);
470
471 $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
472 $r = Set::extract('/Deep/Nesting/test/1/..', $a);
473 $this->assertEqual($r, $expected);
474
475 $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
476 $r = Set::extract('/Deep/Nesting/test/2/and/../..', $a);
477 $this->assertEqual($r, $expected);
478
479 $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
480 $r = Set::extract('/Deep/Nesting/test/2/../../../Nesting/test/2/..', $a);
481 $this->assertEqual($r, $expected);
482
483 $expected = array(2);
484 $r = Set::extract('/User[2]/id', $a);
485 $this->assertEqual($r, $expected);
486
487 $expected = array(4, 5);
488 $r = Set::extract('/User[id>3]/id', $a);
489 $this->assertEqual($r, $expected);
490
491 $expected = array(2, 3);
492 $r = Set::extract('/User[id>1][id<=3]/id', $a);
493 $this->assertEqual($r, $expected);
494
495 $expected = array(array('I'), array('II'));
496 $r = Set::extract('/a/@*', $c);
497 $this->assertEqual($r, $expected);
498
499 $single = array(
500 'User' => array(
501 'id' => 4,
502 'name' => 'Neo',
503 )
504 );
505 $tricky = array(
506 0 => array(
507 'User' => array(
508 'id' => 1,
509 'name' => 'John',
510 )
511 ),
512 1 => array(
513 'User' => array(
514 'id' => 2,
515 'name' => 'Bob',
516 )
517 ),
518 2 => array(
519 'User' => array(
520 'id' => 3,
521 'name' => 'Tony',
522 )
523 ),
524 'User' => array(
525 'id' => 4,
526 'name' => 'Neo',
527 )
528 );
529
530 $expected = array(1, 2, 3, 4);
531 $r = Set::extract('/User/id', $tricky);
532 $this->assertEqual($r, $expected);
533
534 $expected = array(4);
535 $r = Set::extract('/User/id', $single);
536 $this->assertEqual($r, $expected);
537
538 $expected = array(1, 3);
539 $r = Set::extract('/User[name=/n/]/id', $tricky);
540 $this->assertEqual($r, $expected);
541
542 $expected = array(4);
543 $r = Set::extract('/User[name=/N/]/id', $tricky);
544 $this->assertEqual($r, $expected);
545
546 $expected = array(1, 3, 4);
547 $r = Set::extract('/User[name=/N/i]/id', $tricky);
548 $this->assertEqual($r, $expected);
549
550 $expected = array(array('id', 'name'), array('id', 'name'), array('id', 'name'), array('id', 'name'));
551 $r = Set::extract('/User/@*', $tricky);
552 $this->assertEqual($r, $expected);
553
554 $common = array(
555 array(
556 'Article' => array(
557 'id' => 1,
558 'name' => 'Article 1',
559 ),
560 'Comment' => array(
561 array(
562 'id' => 1,
563 'user_id' => 5,
564 'article_id' => 1,
565 'text' => 'Comment 1',
566 ),
567 array(
568 'id' => 2,
569 'user_id' => 23,
570 'article_id' => 1,
571 'text' => 'Comment 2',
572 ),
573 array(
574 'id' => 3,
575 'user_id' => 17,
576 'article_id' => 1,
577 'text' => 'Comment 3',
578 ),
579 ),
580 ),
581 array(
582 'Article' => array(
583 'id' => 2,
584 'name' => 'Article 2',
585 ),
586 'Comment' => array(
587 array(
588 'id' => 4,
589 'user_id' => 2,
590 'article_id' => 2,
591 'text' => 'Comment 4',
592 'addition' => '',
593 ),
594 array(
595 'id' => 5,
596 'user_id' => 23,
597 'article_id' => 2,
598 'text' => 'Comment 5',
599 'addition' => 'foo',
600 ),
601 ),
602 ),
603 array(
604 'Article' => array(
605 'id' => 3,
606 'name' => 'Article 3',
607 ),
608 'Comment' => array(),
609 )
610 );
611
612 $r = Set::extract('/Comment/id', $common);
613 $expected = array(1, 2, 3, 4, 5);
614 $this->assertEqual($r, $expected);
615
616 $expected = array(1, 2, 4, 5);
617 $r = Set::extract('/Comment[id!=3]/id', $common);
618 $this->assertEqual($r, $expected);
619
620 $r = Set::extract('/', $common);
621 $this->assertEqual($r, $common);
622
623 $expected = array(1, 2, 4, 5);
624 $r = Set::extract($common, '/Comment[id!=3]/id');
625 $this->assertEqual($r, $expected);
626
627 $expected = array($common[0]['Comment'][2]);
628 $r = Set::extract($common, '/Comment/2');
629 $this->assertEqual($r, $expected);
630
631 $expected = array($common[0]['Comment'][0]);
632 $r = Set::extract($common, '/Comment[1]/.[id=1]');
633 $this->assertEqual($r, $expected);
634
635 $expected = array($common[1]['Comment'][1]);
636 $r = Set::extract($common, '/1/Comment/.[2]');
637 $this->assertEqual($r, $expected);
638
639 $expected = array();
640 $r = Set::extract('/User/id', array());
641 $this->assertEqual($r, $expected);
642
643 $expected = array(5);
644 $r = Set::extract('/Comment/id[:last]', $common);
645 $this->assertEqual($r, $expected);
646
647 $expected = array(1);
648 $r = Set::extract('/Comment/id[:first]', $common);
649 $this->assertEqual($r, $expected);
650
651 $expected = array(3);
652 $r = Set::extract('/Article[:last]/id', $common);
653 $this->assertEqual($r, $expected);
654
655 $expected = array(array('Comment' => $common[1]['Comment'][0]));
656 $r = Set::extract('/Comment[addition=]', $common);
657 $this->assertEqual($r, $expected);
658
659 $habtm = array(
660 array(
661 'Post' => array(
662 'id' => 1,
663 'title' => 'great post',
664 ),
665 'Comment' => array(
666 array(
667 'id' => 1,
668 'text' => 'foo',
669 'User' => array(
670 'id' => 1,
671 'name' => 'bob'
672 ),
673 ),
674 array(
675 'id' => 2,
676 'text' => 'bar',
677 'User' => array(
678 'id' => 2,
679 'name' => 'tod'
680 ),
681 ),
682 ),
683 ),
684 array(
685 'Post' => array(
686 'id' => 2,
687 'title' => 'fun post',
688 ),
689 'Comment' => array(
690 array(
691 'id' => 3,
692 'text' => '123',
693 'User' => array(
694 'id' => 3,
695 'name' => 'dan'
696 ),
697 ),
698 array(
699 'id' => 4,
700 'text' => '987',
701 'User' => array(
702 'id' => 4,
703 'name' => 'jim'
704 ),
705 ),
706 ),
707 ),
708 );
709
710 $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
711 $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
712 $this->assertEqual($r[1]['Comment']['User']['name'], 'dan');
713 $this->assertEqual(count($r), 2);
714
715 $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
716 $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
717
718 $this->assertEqual($r[1]['Comment']['User']['name'], 'tod');
719 $this->assertEqual(count($r), 2);
720
721 $tree = array(
722 array(
723 'Category' => array('name' => 'Category 1'),
724 'children' => array(array('Category' => array('name' => 'Category 1.1')))
725 ),
726 array(
727 'Category' => array('name' => 'Category 2'),
728 'children' => array(
729 array('Category' => array('name' => 'Category 2.1')),
730 array('Category' => array('name' => 'Category 2.2'))
731 )
732 ),
733 array(
734 'Category' => array('name' => 'Category 3'),
735 'children' => array(array('Category' => array('name' => 'Category 3.1')))
736 )
737 );
738
739 $expected = array(array('Category' => $tree[1]['Category']));
740 $r = Set::extract('/Category[name=Category 2]', $tree);
741 $this->assertEqual($r, $expected);
742
743 $expected = array(
744 array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children'])
745 );
746 $r = Set::extract('/Category[name=Category 2]/..', $tree);
747 $this->assertEqual($r, $expected);
748
749 $expected = array(
750 array('children' => $tree[1]['children'][0]),
751 array('children' => $tree[1]['children'][1])
752 );
753 $r = Set::extract('/Category[name=Category 2]/../children', $tree);
754 $this->assertEqual($r, $expected);
755
756 $habtm = array(
757 array(
758 'Post' => array(
759 'id' => 1,
760 'title' => 'great post',
761 ),
762 'Comment' => array(
763 array(
764 'id' => 1,
765 'text' => 'foo',
766 'User' => array(
767 'id' => 1,
768 'name' => 'bob'
769 ),
770 ),
771 array(
772 'id' => 2,
773 'text' => 'bar',
774 'User' => array(
775 'id' => 2,
776 'name' => 'tod'
777 ),
778 ),
779 ),
780 ),
781 array(
782 'Post' => array(
783 'id' => 2,
784 'title' => 'fun post',
785 ),
786 'Comment' => array(
787 array(
788 'id' => 3,
789 'text' => '123',
790 'User' => array(
791 'id' => 3,
792 'name' => 'dan'
793 ),
794 ),
795 array(
796 'id' => 4,
797 'text' => '987',
798 'User' => array(
799 'id' => 4,
800 'name' => 'jim'
801 ),
802 ),
803 ),
804 ),
805 );
806
807 $r = Set::extract('/Comment/User[name=/\w+/]/..', $habtm);
808 $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
809 $this->assertEqual($r[1]['Comment']['User']['name'], 'tod');
810 $this->assertEqual($r[2]['Comment']['User']['name'], 'dan');
811 $this->assertEqual($r[3]['Comment']['User']['name'], 'dan');
812 $this->assertEqual(count($r), 4);
813
814 $r = Set::extract('/Comment/User[name=/[a-z]+/]/..', $habtm);
815 $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
816 $this->assertEqual($r[1]['Comment']['User']['name'], 'tod');
817 $this->assertEqual($r[2]['Comment']['User']['name'], 'dan');
818 $this->assertEqual($r[3]['Comment']['User']['name'], 'dan');
819 $this->assertEqual(count($r), 4);
820
821 $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
822 $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
823 $this->assertEqual($r[1]['Comment']['User']['name'], 'dan');
824 $this->assertEqual(count($r), 2);
825
826 $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
827 $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
828 $this->assertEqual($r[1]['Comment']['User']['name'], 'tod');
829 $this->assertEqual(count($r), 2);
830
831 $mixedKeys = array(
832 'User' => array(
833 0 => array(
834 'id' => 4,
835 'name' => 'Neo'
836 ),
837 1 => array(
838 'id' => 5,
839 'name' => 'Morpheus'
840 ),
841 'stringKey' => array()
842 )
843 );
844 $expected = array('Neo', 'Morpheus');
845 $r = Set::extract('/User/name', $mixedKeys);
846 $this->assertEqual($r, $expected);
847
848 $f = array(
849 array(
850 'file' => array(
851 'name' => 'zipfile.zip',
852 'type' => 'application/zip',
853 'tmp_name' => '/tmp/php178.tmp',
854 'error' => 0,
855 'size' => '564647'
856 )
857 ),
858 array(
859 'file' => array(
860 'name' => 'zipfile2.zip',
861 'type' => 'application/x-zip-compressed',
862 'tmp_name' => '/tmp/php179.tmp',
863 'error' => 0,
864 'size' => '354784'
865 )
866 ),
867 array(
868 'file' => array(
869 'name' => 'picture.jpg',
870 'type' => 'image/jpeg',
871 'tmp_name' => '/tmp/php180.tmp',
872 'error' => 0,
873 'size' => '21324'
874 )
875 )
876 );
877 $expected = array(array('name' => 'zipfile2.zip','type' => 'application/x-zip-compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784'));
878 $r = Set::extract('/file/.[type=application/x-zip-compressed]', $f);
879 $this->assertEqual($r, $expected);
880
881 $expected = array(array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'));
882 $r = Set::extract('/file/.[type=application/zip]', $f);
883 $this->assertEqual($r, $expected);
884
885 $f = array(
886 array(
887 'file' => array(
888 'name' => 'zipfile.zip',
889 'type' => 'application/zip',
890 'tmp_name' => '/tmp/php178.tmp',
891 'error' => 0,
892 'size' => '564647'
893 )
894 ),
895 array(
896 'file' => array(
897 'name' => 'zipfile2.zip',
898 'type' => 'application/x zip compressed',
899 'tmp_name' => '/tmp/php179.tmp',
900 'error' => 0,
901 'size' => '354784'
902 )
903 ),
904 array(
905 'file' => array(
906 'name' => 'picture.jpg',
907 'type' => 'image/jpeg',
908 'tmp_name' => '/tmp/php180.tmp',
909 'error' => 0,
910 'size' => '21324'
911 )
912 )
913 );
914 $expected = array(array('name' => 'zipfile2.zip','type' => 'application/x zip compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784'));
915 $r = Set::extract('/file/.[type=application/x zip compressed]', $f);
916 $this->assertEqual($r, $expected);
917
918 $expected = array(
919 array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'),
920 array('name' => 'zipfile2.zip','type' => 'application/x zip compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784')
921 );
922 $r = Set::extract('/file/.[tmp_name=/tmp\/php17/]', $f);
923 $this->assertEqual($r, $expected);
924
925 $hasMany = array(
926 'Node' => array(
927 'id' => 1,
928 'name' => 'First',
929 'state' => 50
930 ),
931 'ParentNode' => array(
932 0 => array(
933 'id' => 2,
934 'name' => 'Second',
935 'state' => 60,
936 )
937 )
938 );
939 $result = Set::extract('/ParentNode/name', $hasMany);
940 $expected = array('Second');
941 $this->assertEqual($result, $expected);
942
943 $data = array(
944 array(
945 'Category' => array(
946 'id' => 1,
947 'name' => 'First'
948 ),
949 0 => array(
950 'value' => 50
951 )
952 ),
953 array(
954 'Category' => array(
955 'id' => 2,
956 'name' => 'Second'
957 ),
958 0 => array(
959 'value' => 60
960 )
961 )
962 );
963 $expected = array(
964 array(
965 'Category' => array(
966 'id' => 1,
967 'name' => 'First'
968 ),
969 0 => array(
970 'value' => 50
971 )
972 )
973 );
974 $result = Set::extract('/Category[id=1]/..', $data);
975 $this->assertEqual($result, $expected);
976 }
977
978 /**
979 * test parent selectors with extract
980 *
981 * @return void
982 */
983 function testExtractParentSelector() {
984 $tree = array(
985 array(
986 'Category' => array(
987 'name' => 'Category 1'
988 ),
989 'children' => array(
990 array(
991 'Category' => array(
992 'name' => 'Category 1.1'
993 )
994 )
995 )
996 ),
997 array(
998 'Category' => array(
999 'name' => 'Category 2'
1000 ),
1001 'children' => array(
1002 array(
1003 'Category' => array(
1004 'name' => 'Category 2.1'
1005 )
1006 ),
1007 array(
1008 'Category' => array(
1009 'name' => 'Category 2.2'
1010 )
1011 ),
1012 )
1013 ),
1014 array(
1015 'Category' => array(
1016 'name' => 'Category 3'
1017 ),
1018 'children' => array(
1019 array(
1020 'Category' => array(
1021 'name' => 'Category 3.1'
1022 )
1023 )
1024 )
1025 )
1026 );
1027 $expected = array(array('Category' => $tree[1]['Category']));
1028 $r = Set::extract('/Category[name=Category 2]', $tree);
1029 $this->assertEqual($r, $expected);
1030
1031 $expected = array(array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children']));
1032 $r = Set::extract('/Category[name=Category 2]/..', $tree);
1033 $this->assertEqual($r, $expected);
1034
1035 $expected = array(array('children' => $tree[1]['children'][0]), array('children' => $tree[1]['children'][1]));
1036 $r = Set::extract('/Category[name=Category 2]/../children', $tree);
1037 $this->assertEqual($r, $expected);
1038
1039 $single = array(
1040 array(
1041 'CallType' => array(
1042 'name' => 'Internal Voice'
1043 ),
1044 'x' => array(
1045 'hour' => 7
1046 )
1047 )
1048 );
1049
1050 $expected = array(7);
1051 $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $single);
1052 $this->assertEqual($r, $expected);
1053
1054 $multiple = array(
1055 array(
1056 'CallType' => array(
1057 'name' => 'Internal Voice'
1058 ),
1059 'x' => array(
1060 'hour' => 7
1061 )
1062 ),
1063 array(
1064 'CallType' => array(
1065 'name' => 'Internal Voice'
1066 ),
1067 'x' => array(
1068 'hour' => 2
1069 )
1070 ),
1071 array(
1072 'CallType' => array(
1073 'name' => 'Internal Voice'
1074 ),
1075 'x' => array(
1076 'hour' => 1
1077 )
1078 )
1079 );
1080
1081 $expected = array(7,2,1);
1082 $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $multiple);
1083 $this->assertEqual($r, $expected);
1084
1085 $a = array(
1086 'Model' => array(
1087 '0' => array(
1088 'id' => 18,
1089 'SubModelsModel' => array(
1090 'id' => 1,
1091 'submodel_id' => 66,
1092 'model_id' => 18,
1093 'type' => 1
1094 ),
1095 ),
1096 '1' => array(
1097 'id' => 0,
1098 'SubModelsModel' => array(
1099 'id' => 2,
1100 'submodel_id' => 66,
1101 'model_id' => 0,
1102 'type' => 1
1103 ),
1104 ),
1105 '2' => array(
1106 'id' => 17,
1107 'SubModelsModel' => array(
1108 'id' => 3,
1109 'submodel_id' => 66,
1110 'model_id' => 17,
1111 'type' => 2
1112 ),
1113 ),
1114 '3' => array(
1115 'id' => 0,
1116 'SubModelsModel' => array(
1117 'id' => 4,
1118 'submodel_id' => 66,
1119 'model_id' => 0,
1120 'type' => 2
1121 )
1122 )
1123 )
1124 );
1125
1126 $expected = array(
1127 array(
1128 'Model' => array(
1129 'id' => 17,
1130 'SubModelsModel' => array(
1131 'id' => 3,
1132 'submodel_id' => 66,
1133 'model_id' => 17,
1134 'type' => 2
1135 ),
1136 )
1137 ),
1138 array(
1139 'Model' => array(
1140 'id' => 0,
1141 'SubModelsModel' => array(
1142 'id' => 4,
1143 'submodel_id' => 66,
1144 'model_id' => 0,
1145 'type' => 2
1146 )
1147 )
1148 )
1149 );
1150 $r = Set::extract('/Model/SubModelsModel[type=2]/..', $a);
1151 $this->assertEqual($r, $expected);
1152 }
1153
1154 /**
1155 * test that extract() still works when arrays don't contain a 0 index.
1156 *
1157 * @return void
1158 */
1159 function testExtractWithNonZeroArrays() {
1160 $nonZero = array(
1161 1 => array(
1162 'User' => array(
1163 'id' => 1,
1164 'name' => 'John',
1165 )
1166 ),
1167 2 => array(
1168 'User' => array(
1169 'id' => 2,
1170 'name' => 'Bob',
1171 )
1172 ),
1173 3 => array(
1174 'User' => array(
1175 'id' => 3,
1176 'name' => 'Tony',
1177 )
1178 )
1179 );
1180 $expected = array(1, 2, 3);
1181 $r = Set::extract('/User/id', $nonZero);
1182 $this->assertEqual($r, $expected);
1183
1184 $expected = array(
1185 array('User' => array('id' => 1, 'name' => 'John')),
1186 array('User' => array('id' => 2, 'name' => 'Bob')),
1187 array('User' => array('id' => 3, 'name' => 'Tony')),
1188 );
1189 $result = Set::extract('/User', $nonZero);
1190 $this->assertEqual($result, $expected);
1191
1192 $nonSequential = array(
1193 'User' => array(
1194 0 => array('id' => 1),
1195 2 => array('id' => 2),
1196 6 => array('id' => 3),
1197 9 => array('id' => 4),
1198 3 => array('id' => 5),
1199 ),
1200 );
1201
1202 $nonZero = array(
1203 'User' => array(
1204 2 => array('id' => 1),
1205 4 => array('id' => 2),
1206 6 => array('id' => 3),
1207 9 => array('id' => 4),
1208 3 => array('id' => 5),
1209 ),
1210 );
1211
1212 $expected = array(1, 2, 3, 4, 5);
1213 $this->assertEqual(Set::extract('/User/id', $nonSequential), $expected);
1214
1215 $result = Set::extract('/User/id', $nonZero);
1216 $this->assertEqual($result, $expected, 'Failed non zero array key extract');
1217
1218 $expected = array(1, 2, 3, 4, 5);
1219 $this->assertEqual(Set::extract('/User/id', $nonSequential), $expected);
1220
1221 $result = Set::extract('/User/id', $nonZero);
1222 $this->assertEqual($result, $expected, 'Failed non zero array key extract');
1223
1224 $startingAtOne = array(
1225 'Article' => array(
1226 1 => array(
1227 'id' => 1,
1228 'approved' => 1,
1229 ),
1230 )
1231 );
1232
1233 $expected = array(0 => array('Article' => array('id' => 1, 'approved' => 1)));
1234 $result = Set::extract('/Article[approved=1]', $startingAtOne);
1235 $this->assertEqual($result, $expected);
1236
1237 $items = array(
1238 240 => array(
1239 'A' => array(
1240 'field1' => 'a240',
1241 'field2' => 'a240',
1242 ),
1243 'B' => array(
1244 'field1' => 'b240',
1245 'field2' => 'b240'
1246 ),
1247 )
1248 );
1249
1250 $expected = array(
1251 0 => 'b240'
1252 );
1253
1254 $result = Set::extract('/B/field1', $items);
1255 $this->assertIdentical($result, $expected);
1256 $this->assertIdentical($result, Set::extract('{n}.B.field1', $items));
1257 }
1258 /**
1259 * testExtractWithArrays method
1260 *
1261 * @access public
1262 * @return void
1263 */
1264 function testExtractWithArrays() {
1265 $data = array(
1266 'Level1' => array(
1267 'Level2' => array('test1', 'test2'),
1268 'Level2bis' => array('test3', 'test4')
1269 )
1270 );
1271 $this->assertEqual(Set::extract('/Level1/Level2', $data), array(array('Level2' => array('test1', 'test2'))));
1272 $this->assertEqual(Set::extract('/Level1/Level2bis', $data), array(array('Level2bis' => array('test3', 'test4'))));
1273 }
1274
1275 /**
1276 * test extract() with elements that have non-array children.
1277 *
1278 * @return void
1279 */
1280 function testExtractWithNonArrayElements() {
1281 $data = array(
1282 'node' => array(
1283 array('foo'),
1284 'bar'
1285 )
1286 );
1287 $result = Set::extract('/node', $data);
1288 $expected = array(
1289 array('node' => array('foo')),
1290 'bar'
1291 );
1292 $this->assertEqual($result, $expected);
1293
1294 $data = array(
1295 'node' => array(
1296 'foo' => array('bar'),
1297 'bar' => array('foo')
1298 )
1299 );
1300 $result = Set::extract('/node', $data);
1301 $expected = array(
1302 array('foo' => array('bar')),
1303 array('bar' => array('foo')),
1304 );
1305 $this->assertEqual($result, $expected);
1306
1307 $data = array(
1308 'node' => array(
1309 'foo' => array(
1310 'bar'
1311 ),
1312 'bar' => 'foo'
1313 )
1314 );
1315 $result = Set::extract('/node', $data);
1316 $expected = array(
1317 array('foo' => array('bar')),
1318 'foo'
1319 );
1320 $this->assertEqual($result, $expected);
1321 }
1322
1323 /**
1324 * testMatches method
1325 *
1326 * @access public
1327 * @return void
1328 */
1329 function testMatches() {
1330 $a = array(
1331 array('Article' => array('id' => 1, 'title' => 'Article 1')),
1332 array('Article' => array('id' => 2, 'title' => 'Article 2')),
1333 array('Article' => array('id' => 3, 'title' => 'Article 3'))
1334 );
1335
1336 $this->assertTrue(Set::matches(array('id=2'), $a[1]['Article']));
1337 $this->assertFalse(Set::matches(array('id>2'), $a[1]['Article']));
1338 $this->assertTrue(Set::matches(array('id>=2'), $a[1]['Article']));
1339 $this->assertFalse(Set::matches(array('id>=3'), $a[1]['Article']));
1340 $this->assertTrue(Set::matches(array('id<=2'), $a[1]['Article']));
1341 $this->assertFalse(Set::matches(array('id<2'), $a[1]['Article']));
1342 $this->assertTrue(Set::matches(array('id>1'), $a[1]['Article']));
1343 $this->assertTrue(Set::matches(array('id>1', 'id<3', 'id!=0'), $a[1]['Article']));
1344
1345 $this->assertTrue(Set::matches(array('3'), null, 3));
1346 $this->assertTrue(Set::matches(array('5'), null, 5));
1347
1348 $this->assertTrue(Set::matches(array('id'), $a[1]['Article']));
1349 $this->assertTrue(Set::matches(array('id', 'title'), $a[1]['Article']));
1350 $this->assertFalse(Set::matches(array('non-existant'), $a[1]['Article']));
1351
1352 $this->assertTrue(Set::matches('/Article[id=2]', $a));
1353 $this->assertFalse(Set::matches('/Article[id=4]', $a));
1354 $this->assertTrue(Set::matches(array(), $a));
1355
1356 $r = array(
1357 'Attachment' => array(
1358 'keep' => array()
1359 ),
1360 'Comment' => array(
1361 'keep' => array(
1362 'Attachment' => array(
1363 'fields' => array(
1364 0 => 'attachment',
1365 ),
1366 ),
1367 )
1368 ),
1369 'User' => array(
1370 'keep' => array()
1371 ),
1372 'Article' => array(
1373 'keep' => array(
1374 'Comment' => array(
1375 'fields' => array(
1376 0 => 'comment',
1377 1 => 'published',
1378 ),
1379 ),
1380 'User' => array(
1381 'fields' => array(
1382 0 => 'user',
1383 ),
1384 ),
1385 )
1386 )
1387 );
1388
1389 $this->assertTrue(Set::matches('/Article/keep/Comment', $r));
1390 $this->assertEqual(Set::extract('/Article/keep/Comment/fields', $r), array('comment', 'published'));
1391 $this->assertEqual(Set::extract('/Article/keep/User/fields', $r), array('user'));
1392
1393
1394 }
1395
1396 /**
1397 * testSetExtractReturnsEmptyArray method
1398 *
1399 * @access public
1400 * @return void
1401 */
1402 function testSetExtractReturnsEmptyArray() {
1403
1404 $this->assertIdentical(Set::extract(array(), '/Post/id'), array());
1405
1406 $this->assertIdentical(Set::extract('/Post/id', array()), array());
1407
1408 $this->assertIdentical(Set::extract('/Post/id', array(
1409 array('Post' => array('name' => 'bob')),
1410 array('Post' => array('name' => 'jim'))
1411 )), array());
1412
1413 $this->assertIdentical(Set::extract(array(), 'Message.flash'), null);
1414
1415 }
1416
1417 /**
1418 * testClassicExtract method
1419 *
1420 * @access public
1421 * @return void
1422 */
1423 function testClassicExtract() {
1424 $a = array(
1425 array('Article' => array('id' => 1, 'title' => 'Article 1')),
1426 array('Article' => array('id' => 2, 'title' => 'Article 2')),
1427 array('Article' => array('id' => 3, 'title' => 'Article 3'))
1428 );
1429
1430 $result = Set::extract($a, '{n}.Article.id');
1431 $expected = array( 1, 2, 3 );
1432 $this->assertIdentical($result, $expected);
1433
1434 $result = Set::extract($a, '{n}.Article.title');
1435 $expected = array( 'Article 1', 'Article 2', 'Article 3' );
1436 $this->assertIdentical($result, $expected);
1437
1438 $result = Set::extract($a, '1.Article.title');
1439 $expected = 'Article 2';
1440 $this->assertIdentical($result, $expected);
1441
1442 $result = Set::extract($a, '3.Article.title');
1443 $expected = null;
1444 $this->assertIdentical($result, $expected);
1445
1446 $a = array(
1447 array(
1448 'Article' => array('id' => 1, 'title' => 'Article 1',
1449 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
1450 ),
1451 array(
1452 'Article' => array('id' => 2, 'title' => 'Article 2',
1453 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
1454 ),
1455 array(
1456 'Article' => array('id' => 3, 'title' => 'Article 3',
1457 'User' => array('id' => 2, 'username' => 'phpnut'))
1458 )
1459 );
1460
1461 $result = Set::extract($a, '{n}.Article.User.username');
1462 $expected = array( 'mariano.iglesias', 'mariano.iglesias', 'phpnut' );
1463 $this->assertIdentical($result, $expected);
1464
1465 $a = array(
1466 array('Article' => array('id' => 1, 'title' => 'Article 1',
1467 'Comment' => array(
1468 array('id' => 10, 'title' => 'Comment 10'),
1469 array('id' => 11, 'title' => 'Comment 11'),
1470 array('id' => 12, 'title' => 'Comment 12')))),
1471 array('Article' => array('id' => 2, 'title' => 'Article 2',
1472 'Comment' => array(
1473 array('id' => 13, 'title' => 'Comment 13'),
1474 array('id' => 14, 'title' => 'Comment 14')))),
1475 array('Article' => array('id' => 3, 'title' => 'Article 3')));
1476
1477 $result = Set::extract($a, '{n}.Article.Comment.{n}.id');
1478 $expected = array (array(10, 11, 12), array(13, 14), null);
1479 $this->assertIdentical($result, $expected);
1480
1481 $result = Set::extract($a, '{n}.Article.Comment.{n}.title');
1482 $expected = array(
1483 array('Comment 10', 'Comment 11', 'Comment 12'),
1484 array('Comment 13', 'Comment 14'),
1485 null
1486 );
1487 $this->assertIdentical($result, $expected);
1488
1489 $a = array(array('1day' => '20 sales'), array('1day' => '2 sales'));
1490 $result = Set::extract($a, '{n}.1day');
1491 $expected = array('20 sales', '2 sales');
1492 $this->assertIdentical($result, $expected);
1493
1494 $a = array(
1495 'pages' => array('name' => 'page'),
1496 'fruites' => array('name' => 'fruit'),
1497 0 => array('name' => 'zero')
1498 );
1499 $result = Set::extract($a, '{s}.name');
1500 $expected = array('page','fruit');
1501 $this->assertIdentical($result, $expected);
1502
1503 $a = array(
1504 0 => array('pages' => array('name' => 'page')),
1505 1 => array('fruites'=> array('name' => 'fruit')),
1506 'test' => array(array('name' => 'jippi')),
1507 'dot.test' => array(array('name' => 'jippi'))
1508 );
1509
1510 $result = Set::extract($a, '{n}.{s}.name');
1511 $expected = array(0 => array('page'), 1 => array('fruit'));
1512 $this->assertIdentical($result, $expected);
1513
1514 $result = Set::extract($a, '{s}.{n}.name');
1515 $expected = array(array('jippi'), array('jippi'));
1516 $this->assertIdentical($result, $expected);
1517
1518 $result = Set::extract($a,'{\w+}.{\w+}.name');
1519 $expected = array(
1520 array('pages' => 'page'),
1521 array('fruites' => 'fruit'),
1522 'test' => array('jippi'),
1523 'dot.test' => array('jippi')
1524 );
1525 $this->assertIdentical($result, $expected);
1526
1527 $result = Set::extract($a,'{\d+}.{\w+}.name');
1528 $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
1529 $this->assertIdentical($result, $expected);
1530
1531 $result = Set::extract($a,'{n}.{\w+}.name');
1532 $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
1533 $this->assertIdentical($result, $expected);
1534
1535 $result = Set::extract($a,'{s}.{\d+}.name');
1536 $expected = array(array('jippi'), array('jippi'));
1537 $this->assertIdentical($result, $expected);
1538
1539 $result = Set::extract($a,'{s}');
1540 $expected = array(array(array('name' => 'jippi')), array(array('name' => 'jippi')));
1541 $this->assertIdentical($result, $expected);
1542
1543 $result = Set::extract($a,'{[a-z]}');
1544 $expected = array(
1545 'test' => array(array('name' => 'jippi')),
1546 'dot.test' => array(array('name' => 'jippi'))
1547 );
1548 $this->assertIdentical($result, $expected);
1549
1550 $result = Set::extract($a, '{dot\.test}.{n}');
1551 $expected = array('dot.test' => array(array('name' => 'jippi')));
1552 $this->assertIdentical($result, $expected);
1553
1554 $a = new stdClass();
1555 $a->articles = array(
1556 array('Article' => array('id' => 1, 'title' => 'Article 1')),
1557 array('Article' => array('id' => 2, 'title' => 'Article 2')),
1558 array('Article' => array('id' => 3, 'title' => 'Article 3')));
1559
1560 $result = Set::extract($a, 'articles.{n}.Article.id');
1561 $expected = array( 1, 2, 3 );
1562 $this->assertIdentical($result, $expected);
1563
1564 $result = Set::extract($a, 'articles.{n}.Article.title');
1565 $expected = array( 'Article 1', 'Article 2', 'Article 3' );
1566 $this->assertIdentical($result, $expected);
1567 }
1568
1569 /**
1570 * testInsert method
1571 *
1572 * @access public
1573 * @return void
1574 */
1575 function testInsert() {
1576 $a = array(
1577 'pages' => array('name' => 'page')
1578 );
1579
1580 $result = Set::insert($a, 'files', array('name' => 'files'));
1581 $expected = array(
1582 'pages' => array('name' => 'page'),
1583 'files' => array('name' => 'files')
1584 );
1585 $this->assertIdentical($result, $expected);
1586
1587 $a = array(
1588 'pages' => array('name' => 'page')
1589 );
1590 $result = Set::insert($a, 'pages.name', array());
1591 $expected = array(
1592 'pages' => array('name' => array()),
1593 );
1594 $this->assertIdentical($result, $expected);
1595
1596 $a = array(
1597 'pages' => array(
1598 0 => array('name' => 'main'),
1599 1 => array('name' => 'about')
1600 )
1601 );
1602
1603 $result = Set::insert($a, 'pages.1.vars', array('title' => 'page title'));
1604 $expected = array(
1605 'pages' => array(
1606 0 => array('name' => 'main'),
1607 1 => array('name' => 'about', 'vars' => array('title' => 'page title'))
1608 )
1609 );
1610 $this->assertIdentical($result, $expected);
1611 }
1612
1613 /**
1614 * testRemove method
1615 *
1616 * @access public
1617 * @return void
1618 */
1619 function testRemove() {
1620 $a = array(
1621 'pages' => array('name' => 'page'),
1622 'files' => array('name' => 'files')
1623 );
1624
1625 $result = Set::remove($a, 'files', array('name' => 'files'));
1626 $expected = array(
1627 'pages' => array('name' => 'page')
1628 );
1629 $this->assertIdentical($result, $expected);
1630
1631 $a = array(
1632 'pages' => array(
1633 0 => array('name' => 'main'),
1634 1 => array('name' => 'about', 'vars' => array('title' => 'page title'))
1635 )
1636 );
1637
1638 $result = Set::remove($a, 'pages.1.vars', array('title' => 'page title'));
1639 $expected = array(
1640 'pages' => array(
1641 0 => array('name' => 'main'),
1642 1 => array('name' => 'about')
1643 )
1644 );
1645 $this->assertIdentical($result, $expected);
1646
1647 $result = Set::remove($a, 'pages.2.vars', array('title' => 'page title'));
1648 $expected = $a;
1649 $this->assertIdentical($result, $expected);
1650 }
1651
1652 /**
1653 * testCheck method
1654 *
1655 * @access public
1656 * @return void
1657 */
1658 function testCheck() {
1659 $set = array(
1660 'My Index 1' => array('First' => 'The first item')
1661 );
1662 $this->assertTrue(Set::check($set, 'My Index 1.First'));
1663 $this->assertTrue(Set::check($set, 'My Index 1'));
1664 $this->assertTrue(Set::check($set, array()));
1665
1666 $set = array(
1667 'My Index 1' => array('First' => array('Second' => array('Third' => array('Fourth' => 'Heavy. Nesting.'))))
1668 );
1669 $this->assertTrue(Set::check($set, 'My Index 1.First.Second'));
1670 $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third'));
1671 $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third.Fourth'));
1672 $this->assertFalse(Set::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
1673 }
1674
1675 /**
1676 * testWritingWithFunkyKeys method
1677 *
1678 * @access public
1679 * @return void
1680 */
1681 function testWritingWithFunkyKeys() {
1682 $set = Set::insert(array(), 'Session Test', "test");
1683 $this->assertEqual(Set::extract($set, 'Session Test'), 'test');
1684
1685 $set = Set::remove($set, 'Session Test');
1686 $this->assertFalse(Set::check($set, 'Session Test'));
1687
1688 $this->assertTrue($set = Set::insert(array(), 'Session Test.Test Case', "test"));
1689 $this->assertTrue(Set::check($set, 'Session Test.Test Case'));
1690 }
1691
1692 /**
1693 * testDiff method
1694 *
1695 * @access public
1696 * @return void
1697 */
1698 function testDiff() {
1699 $a = array(
1700 0 => array('name' => 'main'),
1701 1 => array('name' => 'about')
1702 );
1703 $b = array(
1704 0 => array('name' => 'main'),
1705 1 => array('name' => 'about'),
1706 2 => array('name' => 'contact')
1707 );
1708
1709 $result = Set::diff($a, $b);
1710 $expected = array(
1711 2 => array('name' => 'contact')
1712 );
1713 $this->assertIdentical($result, $expected);
1714
1715 $result = Set::diff($a, array());
1716 $expected = $a;
1717 $this->assertIdentical($result, $expected);
1718
1719 $result = Set::diff(array(), $b);
1720 $expected = $b;
1721 $this->assertIdentical($result, $expected);
1722
1723 $b = array(
1724 0 => array('name' => 'me'),
1725 1 => array('name' => 'about')
1726 );
1727
1728 $result = Set::diff($a, $b);
1729 $expected = array(
1730 0 => array('name' => 'main')
1731 );
1732 $this->assertIdentical($result, $expected);
1733
1734 $a = array();
1735 $b = array('name' => 'bob', 'address' => 'home');
1736 $result = Set::diff($a, $b);
1737 $this->assertIdentical($result, $b);
1738
1739
1740 $a = array('name' => 'bob', 'address' => 'home');
1741 $b = array();
1742 $result = Set::diff($a, $b);
1743 $this->assertIdentical($result, $a);
1744
1745 $a = array('key' => true, 'another' => false, 'name' => 'me');
1746 $b = array('key' => 1, 'another' => 0);
1747 $expected = array('name' => 'me');
1748 $result = Set::diff($a, $b);
1749 $this->assertIdentical($result, $expected);
1750
1751 $a = array('key' => 'value', 'another' => null, 'name' => 'me');
1752 $b = array('key' => 'differentValue', 'another' => null);
1753 $expected = array('key' => 'value', 'name' => 'me');
1754 $result = Set::diff($a, $b);
1755 $this->assertIdentical($result, $expected);
1756
1757 $a = array('key' => 'value', 'another' => null, 'name' => 'me');
1758 $b = array('key' => 'differentValue', 'another' => 'value');
1759 $expected = array('key' => 'value', 'another' => null, 'name' => 'me');
1760 $result = Set::diff($a, $b);
1761 $this->assertIdentical($result, $expected);
1762
1763 $a = array('key' => 'value', 'another' => null, 'name' => 'me');
1764 $b = array('key' => 'differentValue', 'another' => 'value');
1765 $expected = array('key' => 'differentValue', 'another' => 'value', 'name' => 'me');
1766 $result = Set::diff($b, $a);
1767 $this->assertIdentical($result, $expected);
1768
1769 $a = array('key' => 'value', 'another' => null, 'name' => 'me');
1770 $b = array(0 => 'differentValue', 1 => 'value');
1771 $expected = $a + $b;
1772 $result = Set::diff($a, $b);
1773 $this->assertIdentical($result, $expected);
1774 }
1775
1776 /**
1777 * testContains method
1778 *
1779 * @access public
1780 * @return void
1781 */
1782 function testContains() {
1783 $a = array(
1784 0 => array('name' => 'main'),
1785 1 => array('name' => 'about')
1786 );
1787 $b = array(
1788 0 => array('name' => 'main'),
1789 1 => array('name' => 'about'),
1790 2 => array('name' => 'contact'),
1791 'a' => 'b'
1792 );
1793
1794 $this->assertTrue(Set::contains($a, $a));
1795 $this->assertFalse(Set::contains($a, $b));
1796 $this->assertTrue(Set::contains($b, $a));
1797 }
1798
1799 /**
1800 * testCombine method
1801 *
1802 * @access public
1803 * @return void
1804 */
1805 function testCombine() {
1806 $result = Set::combine(array(), '{n}.User.id', '{n}.User.Data');
1807 $this->assertFalse($result);
1808 $result = Set::combine('', '{n}.User.id', '{n}.User.Data');
1809 $this->assertFalse($result);
1810
1811 $a = array(
1812 array('User' => array('id' => 2, 'group_id' => 1,
1813 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
1814 array('User' => array('id' => 14, 'group_id' => 2,
1815 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
1816 array('User' => array('id' => 25, 'group_id' => 1,
1817 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
1818 $result = Set::combine($a, '{n}.User.id');
1819 $expected = array(2 => null, 14 => null, 25 => null);
1820 $this->assertIdentical($result, $expected);
1821
1822 $result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
1823 $expected = array(2 => null, 14 => null, 25 => null);
1824 $this->assertIdentical($result, $expected);
1825
1826 $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
1827 $expected = array(
1828 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1829 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
1830 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
1831 $this->assertIdentical($result, $expected);
1832
1833 $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
1834 $expected = array(
1835 2 => 'Mariano Iglesias',
1836 14 => 'Larry E. Masters',
1837 25 => 'The Gwoo');
1838 $this->assertIdentical($result, $expected);
1839
1840 $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
1841 $expected = array(
1842 1 => array(
1843 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1844 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
1845 2 => array(
1846 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
1847 $this->assertIdentical($result, $expected);
1848
1849 $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
1850 $expected = array(
1851 1 => array(
1852 2 => 'Mariano Iglesias',
1853 25 => 'The Gwoo'),
1854 2 => array(
1855 14 => 'Larry E. Masters'));
1856 $this->assertIdentical($result, $expected);
1857
1858 $result = Set::combine($a, '{n}.User.id');
1859 $expected = array(2 => null, 14 => null, 25 => null);
1860 $this->assertIdentical($result, $expected);
1861
1862 $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
1863 $expected = array(
1864 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1865 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
1866 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
1867 $this->assertIdentical($result, $expected);
1868
1869 $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
1870 $expected = array(2 => 'Mariano Iglesias', 14 => 'Larry E. Masters', 25 => 'The Gwoo');
1871 $this->assertIdentical($result, $expected);
1872
1873 $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
1874 $expected = array(
1875 1 => array(
1876 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1877 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
1878 2 => array(
1879 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
1880 $this->assertIdentical($result, $expected);
1881
1882 $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
1883 $expected = array(
1884 1 => array(
1885 2 => 'Mariano Iglesias',
1886 25 => 'The Gwoo'),
1887 2 => array(
1888 14 => 'Larry E. Masters'));
1889 $this->assertIdentical($result, $expected);
1890
1891 $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
1892 $expected = array (
1893 1 => array (
1894 2 => 'mariano.iglesias: Mariano Iglesias',
1895 25 => 'gwoo: The Gwoo'),
1896 2 => array (14 => 'phpnut: Larry E. Masters'));
1897 $this->assertIdentical($result, $expected);
1898
1899 $result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
1900 $expected = array('mariano.iglesias: Mariano Iglesias' => 2, 'phpnut: Larry E. Masters' => 14, 'gwoo: The Gwoo' => 25);
1901 $this->assertIdentical($result, $expected);
1902
1903 $result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
1904 $expected = array('Mariano Iglesias: mariano.iglesias' => 2, 'Larry E. Masters: phpnut' => 14, 'The Gwoo: gwoo' => 25);
1905 $this->assertIdentical($result, $expected);
1906
1907 $result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
1908 $expected = array('mariano.iglesias: 2' => 'Mariano Iglesias', 'phpnut: 14' => 'Larry E. Masters', 'gwoo: 25' => 'The Gwoo');
1909 $this->assertIdentical($result, $expected);
1910
1911 $result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
1912 $expected = array('2: mariano.iglesias' => 'Mariano Iglesias', '14: phpnut' => 'Larry E. Masters', '25: gwoo' => 'The Gwoo');
1913 $this->assertIdentical($result, $expected);
1914
1915 $b = new stdClass();
1916 $b->users = array(
1917 array('User' => array('id' => 2, 'group_id' => 1,
1918 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
1919 array('User' => array('id' => 14, 'group_id' => 2,
1920 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
1921 array('User' => array('id' => 25, 'group_id' => 1,
1922 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
1923 $result = Set::combine($b, 'users.{n}.User.id');
1924 $expected = array(2 => null, 14 => null, 25 => null);
1925 $this->assertIdentical($result, $expected);
1926
1927 $result = Set::combine($b, 'users.{n}.User.id', 'users.{n}.User.non-existant');
1928 $expected = array(2 => null, 14 => null, 25 => null);
1929 $this->assertIdentical($result, $expected);
1930
1931 $result = Set::combine($a, 'fail', 'fail');
1932 $this->assertEqual($result, array());
1933 }
1934
1935 /**
1936 * testMapReverse method
1937 *
1938 * @access public
1939 * @return void
1940 */
1941 function testMapReverse() {
1942 $result = Set::reverse(null);
1943 $this->assertEqual($result, null);
1944
1945 $result = Set::reverse(false);
1946 $this->assertEqual($result, false);
1947
1948 $expected = array(
1949 'Array1' => array(
1950 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2'),
1951 'Array2' => array(
1952 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
1953 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
1954 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
1955 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
1956 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
1957 'Array3' => array(
1958 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
1959 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
1960 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
1961 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
1962 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
1963 $map = Set::map($expected, true);
1964 $this->assertEqual($map->Array1->Array1Data1, $expected['Array1']['Array1Data1']);
1965 $this->assertEqual($map->Array2[0]->Array2Data1, $expected['Array2'][0]['Array2Data1']);
1966
1967 $result = Set::reverse($map);
1968 $this->assertIdentical($result, $expected);
1969
1970 $expected = array(
1971 'Post' => array('id'=> 1, 'title' => 'First Post'),
1972 'Comment' => array(
1973 array('id'=> 1, 'title' => 'First Comment'),
1974 array('id'=> 2, 'title' => 'Second Comment')
1975 ),
1976 'Tag' => array(
1977 array('id'=> 1, 'title' => 'First Tag'),
1978 array('id'=> 2, 'title' => 'Second Tag')
1979 ),
1980 );
1981 $map = Set::map($expected);
1982 $this->assertIdentical($map->title, $expected['Post']['title']);
1983 foreach ($map->Comment as $comment) {
1984 $ids[] = $comment->id;
1985 }
1986 $this->assertIdentical($ids, array(1, 2));
1987
1988 $expected = array(
1989 'Array1' => array(
1990 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4',
1991 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
1992 'string' => 1,
1993 'another' => 'string',
1994 'some' => 'thing else',
1995 'Array2' => array(
1996 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
1997 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
1998 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
1999 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2000 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
2001 'Array3' => array(
2002 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2003 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2004 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2005 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2006 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
2007 $map = Set::map($expected, true);
2008 $result = Set::reverse($map);
2009 $this->assertIdentical($result, $expected);
2010
2011 $expected = array(
2012 'Array1' => array(
2013 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4',
2014 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
2015 'string' => 1,
2016 'another' => 'string',
2017 'some' => 'thing else',
2018 'Array2' => array(
2019 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2020 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2021 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2022 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2023 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
2024 'string2' => 1,
2025 'another2' => 'string',
2026 'some2' => 'thing else',
2027 'Array3' => array(
2028 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2029 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2030 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2031 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2032 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')),
2033 'string3' => 1,
2034 'another3' => 'string',
2035 'some3' => 'thing else');
2036 $map = Set::map($expected, true);
2037 $result = Set::reverse($map);
2038 $this->assertIdentical($result, $expected);
2039
2040 $expected = array('User' => array('psword'=> 'whatever', 'Icon' => array('id'=> 851)));
2041 $map = Set::map($expected);
2042 $result = Set::reverse($map);
2043 $this->assertIdentical($result, $expected);
2044
2045 $expected = array('User' => array('psword'=> 'whatever', 'Icon' => array('id'=> 851)));
2046 $class = new stdClass;
2047 $class->User = new stdClass;
2048 $class->User->psword = 'whatever';
2049 $class->User->Icon = new stdClass;
2050 $class->User->Icon->id = 851;
2051 $result = Set::reverse($class);
2052 $this->assertIdentical($result, $expected);
2053
2054 $expected = array('User' => array('psword'=> 'whatever', 'Icon' => array('id'=> 851), 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address')));
2055 $class = new stdClass;
2056 $class->User = new stdClass;
2057 $class->User->psword = 'whatever';
2058 $class->User->Icon = new stdClass;
2059 $class->User->Icon->id = 851;
2060 $class->User->Profile = new stdClass;
2061 $class->User->Profile->name = 'Some Name';
2062 $class->User->Profile->address = 'Some Address';
2063
2064 $result = Set::reverse($class);
2065 $this->assertIdentical($result, $expected);
2066
2067 $expected = array('User' => array('psword'=> 'whatever',
2068 'Icon' => array('id'=> 851),
2069 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
2070 'Comment' => array(
2071 array('id' => 1, 'article_id' => 1, 'user_id' => 1, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
2072 array('id' => 2, 'article_id' => 1, 'user_id' => 2, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
2073
2074 $class = new stdClass;
2075 $class->User = new stdClass;
2076 $class->User->psword = 'whatever';
2077 $class->User->Icon = new stdClass;
2078 $class->User->Icon->id = 851;
2079 $class->User->Profile = new stdClass;
2080 $class->User->Profile->name = 'Some Name';
2081 $class->User->Profile->address = 'Some Address';
2082 $class->User->Comment = new stdClass;
2083 $class->User->Comment->{'0'} = new stdClass;
2084 $class->User->Comment->{'0'}->id = 1;
2085 $class->User->Comment->{'0'}->article_id = 1;
2086 $class->User->Comment->{'0'}->user_id = 1;
2087 $class->User->Comment->{'0'}->comment = 'First Comment for First Article';
2088 $class->User->Comment->{'0'}->published = 'Y';
2089 $class->User->Comment->{'0'}->created = '2007-03-18 10:47:23';
2090 $class->User->Comment->{'0'}->updated = '2007-03-18 10:49:31';
2091 $class->User->Comment->{'1'} = new stdClass;
2092 $class->User->Comment->{'1'}->id = 2;
2093 $class->User->Comment->{'1'}->article_id = 1;
2094 $class->User->Comment->{'1'}->user_id = 2;
2095 $class->User->Comment->{'1'}->comment = 'Second Comment for First Article';
2096 $class->User->Comment->{'1'}->published = 'Y';
2097 $class->User->Comment->{'1'}->created = '2007-03-18 10:47:23';
2098 $class->User->Comment->{'1'}->updated = '2007-03-18 10:49:31';
2099
2100 $result = Set::reverse($class);
2101 $this->assertIdentical($result, $expected);
2102
2103 $expected = array('User' => array('psword'=> 'whatever',
2104 'Icon' => array('id'=> 851),
2105 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
2106 'Comment' => array(
2107 array('id' => 1, 'article_id' => 1, 'user_id' => 1, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
2108 array('id' => 2, 'article_id' => 1, 'user_id' => 2, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
2109
2110 $class = new stdClass;
2111 $class->User = new stdClass;
2112 $class->User->psword = 'whatever';
2113 $class->User->Icon = new stdClass;
2114 $class->User->Icon->id = 851;
2115 $class->User->Profile = new stdClass;
2116 $class->User->Profile->name = 'Some Name';
2117 $class->User->Profile->address = 'Some Address';
2118 $class->User->Comment = array();
2119 $comment = new stdClass;
2120 $comment->id = 1;
2121 $comment->article_id = 1;
2122 $comment->user_id = 1;
2123 $comment->comment = 'First Comment for First Article';
2124 $comment->published = 'Y';
2125 $comment->created = '2007-03-18 10:47:23';
2126 $comment->updated = '2007-03-18 10:49:31';
2127 $comment2 = new stdClass;
2128 $comment2->id = 2;
2129 $comment2->article_id = 1;
2130 $comment2->user_id = 2;
2131 $comment2->comment = 'Second Comment for First Article';
2132 $comment2->published = 'Y';
2133 $comment2->created = '2007-03-18 10:47:23';
2134 $comment2->updated = '2007-03-18 10:49:31';
2135 $class->User->Comment = array($comment, $comment2);
2136 $result = Set::reverse($class);
2137 $this->assertIdentical($result, $expected);
2138
2139 $model = new Model(array('id' => false, 'name' => 'Model', 'table' => false));
2140 $expected = array(
2141 'Behaviors' => array('modelName' => 'Model', '_attached' => array(), '_disabled' => array(), '__methods' => array(), '__mappedMethods' => array()),
2142 'useDbConfig' => 'default', 'useTable' => false, 'displayField' => null, 'id' => false, 'data' => array(), 'table' => 'models', 'primaryKey' => 'id', '_schema' => null, 'validate' => array(),
2143 'validationErrors' => array(), 'tablePrefix' => null, 'name' => 'Model', 'alias' => 'Model', 'tableToModel' => array(), 'logTransactions' => false, 'cacheQueries' => false,
2144 'belongsTo' => array(), 'hasOne' => array(), 'hasMany' => array(), 'hasAndBelongsToMany' => array(), 'actsAs' => null, 'whitelist' => array(), 'cacheSources' => true,
2145 'findQueryType' => null, 'recursive' => 1, 'order' => null, 'virtualFields' => array(),
2146 '__associationKeys' => array(
2147 'belongsTo' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'counterCache'),
2148 'hasOne' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'dependent'),
2149 'hasMany' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'dependent', 'exclusive', 'finderQuery', 'counterQuery'),
2150 'hasAndBelongsToMany' => array('className', 'joinTable', 'with', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery', 'deleteQuery', 'insertQuery')),
2151 '__associations' => array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'), '__backAssociation' => array(), '__insertID' => null, '__numRows' => null, '__affectedRows' => null,
2152 '_findMethods' => array('all' => true, 'first' => true, 'count' => true, 'neighbors' => true, 'list' => true, 'threaded' => true));
2153 $result = Set::reverse($model);
2154
2155 ksort($result);
2156 ksort($expected);
2157
2158 $this->assertIdentical($result, $expected);
2159
2160 $class = new stdClass;
2161 $class->User = new stdClass;
2162 $class->User->id = 100;
2163 $class->someString = 'this is some string';
2164 $class->Profile = new stdClass;
2165 $class->Profile->name = 'Joe Mamma';
2166
2167 $result = Set::reverse($class);
2168 $expected = array('User' => array('id' => '100'), 'someString'=> 'this is some string', 'Profile' => array('name' => 'Joe Mamma'));
2169 $this->assertEqual($result, $expected);
2170
2171 $class = new stdClass;
2172 $class->User = new stdClass;
2173 $class->User->id = 100;
2174 $class->User->_name_ = 'User';
2175 $class->Profile = new stdClass;
2176 $class->Profile->name = 'Joe Mamma';
2177 $class->Profile->_name_ = 'Profile';
2178
2179 $result = Set::reverse($class);
2180 $expected = array('User' => array('id' => '100'), 'Profile' => array('name' => 'Joe Mamma'));
2181 $this->assertEqual($result, $expected);
2182 }
2183
2184 /**
2185 * testFormatting method
2186 *
2187 * @access public
2188 * @return void
2189 */
2190 function testFormatting() {
2191 $data = array(
2192 array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
2193 array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => '{0}')),
2194 array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}')));
2195
2196 $result = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name'));
2197 $expected = array('Abele, Nate', 'Masters, Larry', 'Woodworth, Garrett');
2198 $this->assertEqual($result, $expected);
2199
2200 $result = Set::format($data, '{0}, {1}', array('{n}.Person.last_name', '{n}.Person.first_name'));
2201 $this->assertEqual($result, $expected);
2202
2203 $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.state'));
2204 $expected = array('Boston, MA', 'Boondock, TN', 'Venice Beach, CA');
2205 $this->assertEqual($result, $expected);
2206
2207 $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.city', '{n}.Person.state'));
2208 $expected = array('{Boston, MA}', '{Boondock, TN}', '{Venice Beach, CA}');
2209 $this->assertEqual($result, $expected);
2210
2211 $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.something', '{n}.Person.something'));
2212 $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
2213 $this->assertEqual($result, $expected);
2214
2215 $result = Set::format($data, '{%2$d, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
2216 $expected = array('{42, 42}', '{0, {0}}', '{0, {1}}');
2217 $this->assertEqual($result, $expected);
2218
2219 $result = Set::format($data, '{%1$s, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
2220 $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
2221 $this->assertEqual($result, $expected);
2222
2223 $result = Set::format($data, '%2$d, %1$s', array('{n}.Person.first_name', '{n}.Person.something'));
2224 $expected = array('42, Nate', '0, Larry', '0, Garrett');
2225 $this->assertEqual($result, $expected);
2226
2227 $result = Set::format($data, '%1$s, %2$d', array('{n}.Person.first_name', '{n}.Person.something'));
2228 $expected = array('Nate, 42', 'Larry, 0', 'Garrett, 0');
2229 $this->assertEqual($result, $expected);
2230 }
2231
2232 /**
2233 * testCountDim method
2234 *
2235 * @access public
2236 * @return void
2237 */
2238 function testCountDim() {
2239 $data = array('one', '2', 'three');
2240 $result = Set::countDim($data);
2241 $this->assertEqual($result, 1);
2242
2243 $data = array('1' => '1.1', '2', '3');
2244 $result = Set::countDim($data);
2245 $this->assertEqual($result, 1);
2246
2247 $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
2248 $result = Set::countDim($data);
2249 $this->assertEqual($result, 2);
2250
2251 $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
2252 $result = Set::countDim($data);
2253 $this->assertEqual($result, 1);
2254
2255 $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
2256 $result = Set::countDim($data, true);
2257 $this->assertEqual($result, 2);
2258
2259 $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
2260 $result = Set::countDim($data);
2261 $this->assertEqual($result, 2);
2262
2263 $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
2264 $result = Set::countDim($data, true);
2265 $this->assertEqual($result, 3);
2266
2267 $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
2268 $result = Set::countDim($data, true);
2269 $this->assertEqual($result, 4);
2270
2271 $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
2272 $result = Set::countDim($data, true);
2273 $this->assertEqual($result, 5);
2274
2275 $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
2276 $result = Set::countDim($data, true);
2277 $this->assertEqual($result, 5);
2278
2279 $set = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
2280 $result = Set::countDim($set, false, 0);
2281 $this->assertEqual($result, 2);
2282
2283 $result = Set::countDim($set, true);
2284 $this->assertEqual($result, 5);
2285 }
2286
2287 /**
2288 * testMapNesting method
2289 *
2290 * @access public
2291 * @return void
2292 */
2293 function testMapNesting() {
2294 $expected = array(
2295 array(
2296 "IndexedPage" => array(
2297 "id" => 1,
2298 "url" => 'http://blah.com/',
2299 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
2300 'headers' => array(
2301 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
2302 'Server' => "Apache",
2303 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
2304 'Cache-Control' => "private",
2305 'Pragma' => "no-cache",
2306 'Content-Type' => "text/html; charset=UTF-8",
2307 'X-Original-Transfer-Encoding' => "chunked",
2308 'Content-Length' => "50210",
2309 ),
2310 'meta' => array(
2311 'keywords' => array('testing','tests'),
2312 'description'=>'describe me',
2313 ),
2314 'get_vars' => '',
2315 'post_vars' => array(),
2316 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
2317 'redirect' => '',
2318 'created' => "1195055503",
2319 'updated' => "1195055503",
2320 )
2321 ),
2322 array(
2323 "IndexedPage" => array(
2324 "id" => 2,
2325 "url" => 'http://blah.com/',
2326 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
2327 'headers' => array(
2328 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
2329 'Server' => "Apache",
2330 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
2331 'Cache-Control' => "private",
2332 'Pragma' => "no-cache",
2333 'Content-Type' => "text/html; charset=UTF-8",
2334 'X-Original-Transfer-Encoding' => "chunked",
2335 'Content-Length' => "50210",
2336 ),
2337 'meta' => array(
2338 'keywords' => array('testing','tests'),
2339 'description'=>'describe me',
2340 ),
2341 'get_vars' => '',
2342 'post_vars' => array(),
2343 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
2344 'redirect' => '',
2345 'created' => "1195055503",
2346 'updated' => "1195055503",
2347 ),
2348 )
2349 );
2350
2351 $mapped = Set::map($expected);
2352 $ids = array();
2353
2354 foreach($mapped as $object) {
2355 $ids[] = $object->id;
2356 }
2357 $this->assertEqual($ids, array(1, 2));
2358 $this->assertEqual(get_object_vars($mapped[0]->headers), $expected[0]['IndexedPage']['headers']);
2359
2360 $result = Set::reverse($mapped);
2361 $this->assertIdentical($result, $expected);
2362
2363 $data = array(
2364 array(
2365 "IndexedPage" => array(
2366 "id" => 1,
2367 "url" => 'http://blah.com/',
2368 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
2369 'get_vars' => '',
2370 'redirect' => '',
2371 'created' => "1195055503",
2372 'updated' => "1195055503",
2373 )
2374 ),
2375 array(
2376 "IndexedPage" => array(
2377 "id" => 2,
2378 "url" => 'http://blah.com/',
2379 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
2380 'get_vars' => '',
2381 'redirect' => '',
2382 'created' => "1195055503",
2383 'updated' => "1195055503",
2384 ),
2385 )
2386 );
2387 $mapped = Set::map($data);
2388
2389 $expected = new stdClass();
2390 $expected->_name_ = 'IndexedPage';
2391 $expected->id = 2;
2392 $expected->url = 'http://blah.com/';
2393 $expected->hash = '68a9f053b19526d08e36c6a9ad150737933816a5';
2394 $expected->get_vars = '';
2395 $expected->redirect = '';
2396 $expected->created = "1195055503";
2397 $expected->updated = "1195055503";
2398 $this->assertIdentical($mapped[1], $expected);
2399
2400 $ids = array();
2401
2402 foreach($mapped as $object) {
2403 $ids[] = $object->id;
2404 }
2405 $this->assertEqual($ids, array(1, 2));
2406
2407 $result = Set::map(null);
2408 $expected = null;
2409 $this->assertEqual($result, $expected);
2410 }
2411
2412 /**
2413 * testNestedMappedData method
2414 *
2415 * @access public
2416 * @return void
2417 */
2418 function testNestedMappedData() {
2419 $result = Set::map(array(
2420 array(
2421 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
2422 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
2423 )
2424 , array(
2425 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
2426 'Author' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31', 'test' => 'working'),
2427 )
2428 ));
2429
2430 $expected = new stdClass;
2431 $expected->_name_ = 'Post';
2432 $expected->id = '1';
2433 $expected->author_id = '1';
2434 $expected->title = 'First Post';
2435 $expected->body = 'First Post Body';
2436 $expected->published = 'Y';
2437 $expected->created = "2007-03-18 10:39:23";
2438 $expected->updated = "2007-03-18 10:41:31";
2439
2440 $expected->Author = new stdClass;
2441 $expected->Author->id = '1';
2442 $expected->Author->user = 'mariano';
2443 $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
2444 $expected->Author->created = "2007-03-17 01:16:23";
2445 $expected->Author->updated = "2007-03-17 01:18:31";
2446 $expected->Author->test = "working";
2447 $expected->Author->_name_ = 'Author';
2448
2449 $expected2 = new stdClass;
2450 $expected2->_name_ = 'Post';
2451 $expected2->id = '2';
2452 $expected2->author_id = '3';
2453 $expected2->title = 'Second Post';
2454 $expected2->body = 'Second Post Body';
2455 $expected2->published = 'Y';
2456 $expected2->created = "2007-03-18 10:41:23";
2457 $expected2->updated = "2007-03-18 10:43:31";
2458
2459 $expected2->Author = new stdClass;
2460 $expected2->Author->id = '3';
2461 $expected2->Author->user = 'larry';
2462 $expected2->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
2463 $expected2->Author->created = "2007-03-17 01:20:23";
2464 $expected2->Author->updated = "2007-03-17 01:22:31";
2465 $expected2->Author->test = "working";
2466 $expected2->Author->_name_ = 'Author';
2467
2468 $test = array();
2469 $test[0] = $expected;
2470 $test[1] = $expected2;
2471
2472 $this->assertIdentical($test, $result);
2473
2474 $result = Set::map(
2475 array(
2476 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
2477 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
2478 )
2479 );
2480 $expected = new stdClass;
2481 $expected->_name_ = 'Post';
2482 $expected->id = '1';
2483 $expected->author_id = '1';
2484 $expected->title = 'First Post';
2485 $expected->body = 'First Post Body';
2486 $expected->published = 'Y';
2487 $expected->created = "2007-03-18 10:39:23";
2488 $expected->updated = "2007-03-18 10:41:31";
2489
2490 $expected->Author = new stdClass;
2491 $expected->Author->id = '1';
2492 $expected->Author->user = 'mariano';
2493 $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
2494 $expected->Author->created = "2007-03-17 01:16:23";
2495 $expected->Author->updated = "2007-03-17 01:18:31";
2496 $expected->Author->test = "working";
2497 $expected->Author->_name_ = 'Author';
2498 $this->assertIdentical($expected, $result);
2499
2500 //Case where extra HABTM fields come back in a result
2501 $data = array(
2502 'User' => array(
2503 'id' => 1,
2504 'email' => 'user@example.com',
2505 'first_name' => 'John',
2506 'last_name' => 'Smith',
2507 ),
2508 'Piece' => array(
2509 array(
2510 'id' => 1,
2511 'title' => 'Moonlight Sonata',
2512 'composer' => 'Ludwig van Beethoven',
2513 'PiecesUser' => array(
2514 'id' => 1,
2515 'created' => '2008-01-01 00:00:00',
2516 'modified' => '2008-01-01 00:00:00',
2517 'piece_id' => 1,
2518 'user_id' => 2,
2519 )
2520 ),
2521 array(
2522 'id' => 2,
2523 'title' => 'Moonlight Sonata 2',
2524 'composer' => 'Ludwig van Beethoven',
2525 'PiecesUser' => array(
2526 'id' => 2,
2527 'created' => '2008-01-01 00:00:00',
2528 'modified' => '2008-01-01 00:00:00',
2529 'piece_id' => 2,
2530 'user_id' => 2,
2531 )
2532 )
2533 )
2534 );
2535
2536 $result = Set::map($data);
2537
2538 $expected = new stdClass();
2539 $expected->_name_ = 'User';
2540 $expected->id = 1;
2541 $expected->email = 'user@example.com';
2542 $expected->first_name = 'John';
2543 $expected->last_name = 'Smith';
2544
2545 $piece = new stdClass();
2546 $piece->id = 1;
2547 $piece->title = 'Moonlight Sonata';
2548 $piece->composer = 'Ludwig van Beethoven';
2549
2550 $piece->PiecesUser = new stdClass();
2551 $piece->PiecesUser->id = 1;
2552 $piece->PiecesUser->created = '2008-01-01 00:00:00';
2553 $piece->PiecesUser->modified = '2008-01-01 00:00:00';
2554 $piece->PiecesUser->piece_id = 1;
2555 $piece->PiecesUser->user_id = 2;
2556 $piece->PiecesUser->_name_ = 'PiecesUser';
2557
2558 $piece->_name_ = 'Piece';
2559
2560
2561 $piece2 = new stdClass();
2562 $piece2->id = 2;
2563 $piece2->title = 'Moonlight Sonata 2';
2564 $piece2->composer = 'Ludwig van Beethoven';
2565
2566 $piece2->PiecesUser = new stdClass();
2567 $piece2->PiecesUser->id = 2;
2568 $piece2->PiecesUser->created = '2008-01-01 00:00:00';
2569 $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
2570 $piece2->PiecesUser->piece_id = 2;
2571 $piece2->PiecesUser->user_id = 2;
2572 $piece2->PiecesUser->_name_ = 'PiecesUser';
2573
2574 $piece2->_name_ = 'Piece';
2575
2576 $expected->Piece = array($piece, $piece2);
2577
2578 $this->assertIdentical($expected, $result);
2579
2580 //Same data, but should work if _name_ has been manually defined:
2581 $data = array(
2582 'User' => array(
2583 'id' => 1,
2584 'email' => 'user@example.com',
2585 'first_name' => 'John',
2586 'last_name' => 'Smith',
2587 '_name_' => 'FooUser',
2588 ),
2589 'Piece' => array(
2590 array(
2591 'id' => 1,
2592 'title' => 'Moonlight Sonata',
2593 'composer' => 'Ludwig van Beethoven',
2594 '_name_' => 'FooPiece',
2595 'PiecesUser' => array(
2596 'id' => 1,
2597 'created' => '2008-01-01 00:00:00',
2598 'modified' => '2008-01-01 00:00:00',
2599 'piece_id' => 1,
2600 'user_id' => 2,
2601 '_name_' => 'FooPiecesUser',
2602 )
2603 ),
2604 array(
2605 'id' => 2,
2606 'title' => 'Moonlight Sonata 2',
2607 'composer' => 'Ludwig van Beethoven',
2608 '_name_' => 'FooPiece',
2609 'PiecesUser' => array(
2610 'id' => 2,
2611 'created' => '2008-01-01 00:00:00',
2612 'modified' => '2008-01-01 00:00:00',
2613 'piece_id' => 2,
2614 'user_id' => 2,
2615 '_name_' => 'FooPiecesUser',
2616 )
2617 )
2618 )
2619 );
2620
2621 $result = Set::map($data);
2622
2623 $expected = new stdClass();
2624 $expected->_name_ = 'FooUser';
2625 $expected->id = 1;
2626 $expected->email = 'user@example.com';
2627 $expected->first_name = 'John';
2628 $expected->last_name = 'Smith';
2629
2630 $piece = new stdClass();
2631 $piece->id = 1;
2632 $piece->title = 'Moonlight Sonata';
2633 $piece->composer = 'Ludwig van Beethoven';
2634 $piece->_name_ = 'FooPiece';
2635 $piece->PiecesUser = new stdClass();
2636 $piece->PiecesUser->id = 1;
2637 $piece->PiecesUser->created = '2008-01-01 00:00:00';
2638 $piece->PiecesUser->modified = '2008-01-01 00:00:00';
2639 $piece->PiecesUser->piece_id = 1;
2640 $piece->PiecesUser->user_id = 2;
2641 $piece->PiecesUser->_name_ = 'FooPiecesUser';
2642
2643 $piece2 = new stdClass();
2644 $piece2->id = 2;
2645 $piece2->title = 'Moonlight Sonata 2';
2646 $piece2->composer = 'Ludwig van Beethoven';
2647 $piece2->_name_ = 'FooPiece';
2648 $piece2->PiecesUser = new stdClass();
2649 $piece2->PiecesUser->id = 2;
2650 $piece2->PiecesUser->created = '2008-01-01 00:00:00';
2651 $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
2652 $piece2->PiecesUser->piece_id = 2;
2653 $piece2->PiecesUser->user_id = 2;
2654 $piece2->PiecesUser->_name_ = 'FooPiecesUser';
2655
2656 $expected->Piece = array($piece, $piece2);
2657
2658 $this->assertIdentical($expected, $result);
2659 }
2660
2661 /**
2662 * testPushDiff method
2663 *
2664 * @access public
2665 * @return void
2666 */
2667 function testPushDiff() {
2668 $array1 = array('ModelOne' => array('id'=>1001, 'field_one'=>'a1.m1.f1', 'field_two'=>'a1.m1.f2'));
2669 $array2 = array('ModelTwo' => array('id'=>1002, 'field_one'=>'a2.m2.f1', 'field_two'=>'a2.m2.f2'));
2670
2671 $result = Set::pushDiff($array1, $array2);
2672
2673 $this->assertIdentical($result, $array1 + $array2);
2674
2675 $array3 = array('ModelOne' => array('id'=>1003, 'field_one'=>'a3.m1.f1', 'field_two'=>'a3.m1.f2', 'field_three'=>'a3.m1.f3'));
2676 $result = Set::pushDiff($array1, $array3);
2677
2678 $expected = array('ModelOne' => array('id'=>1001, 'field_one'=>'a1.m1.f1', 'field_two'=>'a1.m1.f2', 'field_three'=>'a3.m1.f3'));
2679 $this->assertIdentical($result, $expected);
2680
2681
2682 $array1 = array(
2683 0 => array('ModelOne' => array('id'=>1001, 'field_one'=>'s1.0.m1.f1', 'field_two'=>'s1.0.m1.f2')),
2684 1 => array('ModelTwo' => array('id'=>1002, 'field_one'=>'s1.1.m2.f2', 'field_two'=>'s1.1.m2.f2')));
2685 $array2 = array(
2686 0 => array('ModelOne' => array('id'=>1001, 'field_one'=>'s2.0.m1.f1', 'field_two'=>'s2.0.m1.f2')),
2687 1 => array('ModelTwo' => array('id'=>1002, 'field_one'=>'s2.1.m2.f2', 'field_two'=>'s2.1.m2.f2')));
2688
2689 $result = Set::pushDiff($array1, $array2);
2690 $this->assertIdentical($result, $array1);
2691
2692 $array3 = array(0 => array('ModelThree' => array('id'=>1003, 'field_one'=>'s3.0.m3.f1', 'field_two'=>'s3.0.m3.f2')));
2693
2694 $result = Set::pushDiff($array1, $array3);
2695 $expected = array(
2696 0 => array('ModelOne' => array('id'=>1001, 'field_one'=>'s1.0.m1.f1', 'field_two'=>'s1.0.m1.f2'),
2697 'ModelThree' => array('id'=>1003, 'field_one'=>'s3.0.m3.f1', 'field_two'=>'s3.0.m3.f2')),
2698 1 => array('ModelTwo' => array('id'=>1002, 'field_one'=>'s1.1.m2.f2', 'field_two'=>'s1.1.m2.f2')));
2699 $this->assertIdentical($result, $expected);
2700
2701 $result = Set::pushDiff($array1, null);
2702 $this->assertIdentical($result, $array1);
2703
2704 $result = Set::pushDiff($array1, $array2);
2705 $this->assertIdentical($result, $array1+$array2);
2706 }
2707
2708 /**
2709 * testSetApply method
2710 * @access public
2711 * @return void
2712 *
2713 */
2714 function testApply() {
2715 $data = array(
2716 array('Movie' => array('id' => 1, 'title' => 'movie 3', 'rating' => 5)),
2717 array('Movie' => array('id' => 1, 'title' => 'movie 1', 'rating' => 1)),
2718 array('Movie' => array('id' => 1, 'title' => 'movie 2', 'rating' => 3))
2719 );
2720
2721 $result = Set::apply('/Movie/rating', $data, 'array_sum');
2722 $expected = 9;
2723 $this->assertEqual($result, $expected);
2724
2725 if (PHP5) {
2726 $result = Set::apply('/Movie/rating', $data, 'array_product');
2727 $expected = 15;
2728 $this->assertEqual($result, $expected);
2729 }
2730
2731 $result = Set::apply('/Movie/title', $data, 'ucfirst', array('type' => 'map'));
2732 $expected = array('Movie 3', 'Movie 1', 'Movie 2');
2733 $this->assertEqual($result, $expected);
2734
2735 $result = Set::apply('/Movie/title', $data, 'strtoupper', array('type' => 'map'));
2736 $expected = array('MOVIE 3', 'MOVIE 1', 'MOVIE 2');
2737 $this->assertEqual($result, $expected);
2738
2739 $result = Set::apply('/Movie/rating', $data, array('SetTest', '_method'), array('type' => 'reduce'));
2740 $expected = 9;
2741 $this->assertEqual($result, $expected);
2742
2743 $result = Set::apply('/Movie/rating', $data, 'strtoupper', array('type' => 'non existing type'));
2744 $expected = null;
2745 $this->assertEqual($result, $expected);
2746
2747 }
2748
2749 /**
2750 * Helper method to test Set::apply()
2751 *
2752 * @access protected
2753 * @return void
2754 */
2755 function _method($val1, $val2) {
2756 $val1 += $val2;
2757 return $val1;
2758 }
2759
2760 /**
2761 * testXmlSetReverse method
2762 *
2763 * @access public
2764 * @return void
2765 */
2766 function testXmlSetReverse() {
2767 App::import('Core', 'Xml');
2768
2769 $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2770 <rss version="2.0">
2771 <channel>
2772 <title>Cake PHP Google Group</title>
2773 <link>http://groups.google.com/group/cake-php</link>
2774 <description>Search this group before posting anything. There are over 20,000 posts and it&amp;#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description>
2775 <language>en</language>
2776 <item>
2777 <title>constructng result array when using findall</title>
2778 <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
2779 <description>i&#39;m using cakephp to construct a logical data model array that will be &lt;br&gt; passed to a flex app. I have the following model association: &lt;br&gt; ServiceDay-&amp;gt;(hasMany)ServiceTi me-&amp;gt;(hasMany)ServiceTimePrice. So what &lt;br&gt; the current output from my findall is something like this example: &lt;br&gt; &lt;p&gt;Array( &lt;br&gt; [0] =&amp;gt; Array(</description>
2780 <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
2781 <author>bmil...@gmail.com(bpscrugs)</author>
2782 <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
2783 </item>
2784 <item>
2785 <title>Re: share views between actions?</title>
2786 <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
2787 <description>Then perhaps you might do us all a favour and refrain from replying to &lt;br&gt; things you do not understand. That goes especially for asinine comments. &lt;br&gt; Indeed. &lt;br&gt; To sum up: &lt;br&gt; No comment. &lt;br&gt; In my day, a simple &amp;quot;RTFM&amp;quot; would suffice. I&#39;ll keep in mind to ignore any &lt;br&gt; further responses from you. &lt;br&gt; You (and I) were referring to the *online documentation*, not other</description>
2788 <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
2789 <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
2790 <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
2791 </item>
2792 </channel>
2793 </rss>';
2794 $xml = new Xml($string);
2795 $result = Set::reverse($xml);
2796 $expected = array('Rss' => array(
2797 'version' => '2.0',
2798 'Channel' => array(
2799 'title' => 'Cake PHP Google Group',
2800 'link' => 'http://groups.google.com/group/cake-php',
2801 'description' => 'Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
2802 'language' => 'en',
2803 'Item' => array(
2804 array(
2805 'title' => 'constructng result array when using findall',
2806 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
2807 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br><p>Array( <br> [0] =&gt; Array(",
2808 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
2809 'author' => 'bmil...@gmail.com(bpscrugs)',
2810 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
2811 ),
2812 array(
2813 'title' => 'Re: share views between actions?',
2814 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
2815 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
2816 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
2817 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
2818 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
2819 )
2820 )
2821 )
2822 ));
2823 $this->assertEqual($result, $expected);
2824 $string ='<data><post title="Title of this post" description="cool"/></data>';
2825
2826 $xml = new Xml($string);
2827 $result = Set::reverse($xml);
2828 $expected = array('Data' => array('Post' => array('title' => 'Title of this post', 'description' => 'cool')));
2829 $this->assertEqual($result, $expected);
2830
2831 $xml = new Xml('<example><item><title>An example of a correctly reversed XMLNode</title><desc/></item></example>');
2832 $result = Set::reverse($xml);
2833 $expected = array('Example' =>
2834 array(
2835 'Item' => array(
2836 'title' => 'An example of a correctly reversed XMLNode',
2837 'desc' => array(),
2838 )
2839 )
2840 );
2841 $this->assertIdentical($result, $expected);
2842
2843 $xml = new Xml('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>');
2844 $result = Set::reverse($xml);
2845 $expected =
2846 array('Example' => array(
2847 'Item' => array(
2848 'attr' => '123',
2849 'Titles' => array(
2850 'Title' => array('title1', 'title2')
2851 )
2852 )
2853 )
2854 );
2855 $this->assertIdentical($result, $expected);
2856
2857 $xml = new Xml('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
2858 $result = Set::reverse($xml);
2859 $expected =
2860 array('Example' => array(
2861 'attr' => 'ex_attr',
2862 'Item' => array(
2863 'attr' => '123',
2864 'titles' => 'list',
2865 'value' => 'textforitems'
2866 )
2867 )
2868 );
2869 $this->assertIdentical($result, $expected);
2870
2871 $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2872 <rss version="2.0">
2873 <channel>
2874 <title>Cake PHP Google Group</title>
2875 <link>http://groups.google.com/group/cake-php</link>
2876 <description>Search this group before posting anything. There are over 20,000 posts and it&amp;#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description>
2877 <language>en</language>
2878 <item>
2879 <title>constructng result array when using findall</title>
2880 <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
2881 <description>i&#39;m using cakephp to construct a logical data model array that will be &lt;br&gt; passed to a flex app. I have the following model association: &lt;br&gt; ServiceDay-&amp;gt;(hasMany)ServiceTi me-&amp;gt;(hasMany)ServiceTimePrice. So what &lt;br&gt; the current output from my findall is something like this example: &lt;br&gt; &lt;p&gt;Array( &lt;br&gt; [0] =&amp;gt; Array(</description>
2882 <dc:creator>cakephp</dc:creator>
2883 <category><![CDATA[cakephp]]></category>
2884 <category><![CDATA[model]]></category>
2885 <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
2886 <author>bmil...@gmail.com(bpscrugs)</author>
2887 <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
2888 </item>
2889 <item>
2890 <title>Re: share views between actions?</title>
2891 <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
2892 <description>Then perhaps you might do us all a favour and refrain from replying to &lt;br&gt; things you do not understand. That goes especially for asinine comments. &lt;br&gt; Indeed. &lt;br&gt; To sum up: &lt;br&gt; No comment. &lt;br&gt; In my day, a simple &amp;quot;RTFM&amp;quot; would suffice. I&#39;ll keep in mind to ignore any &lt;br&gt; further responses from you. &lt;br&gt; You (and I) were referring to the *online documentation*, not other</description>
2893 <dc:creator>cakephp</dc:creator>
2894 <category><![CDATA[cakephp]]></category>
2895 <category><![CDATA[model]]></category>
2896 <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
2897 <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
2898 <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
2899 </item>
2900 </channel>
2901 </rss>';
2902
2903 $xml = new Xml($string);
2904 $result = Set::reverse($xml);
2905
2906 $expected = array('Rss' => array(
2907 'version' => '2.0',
2908 'Channel' => array(
2909 'title' => 'Cake PHP Google Group',
2910 'link' => 'http://groups.google.com/group/cake-php',
2911 'description' => 'Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
2912 'language' => 'en',
2913 'Item' => array(
2914 array(
2915 'title' => 'constructng result array when using findall',
2916 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
2917 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br><p>Array( <br> [0] =&gt; Array(",
2918 'creator' => 'cakephp',
2919 'Category' => array('cakephp', 'model'),
2920 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
2921 'author' => 'bmil...@gmail.com(bpscrugs)',
2922 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
2923 ),
2924 array(
2925 'title' => 'Re: share views between actions?',
2926 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
2927 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
2928 'creator' => 'cakephp',
2929 'Category' => array('cakephp', 'model'),
2930 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
2931 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
2932 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
2933 )
2934 )
2935 )
2936 ));
2937 $this->assertEqual($result, $expected);
2938
2939 $text = '<?xml version="1.0" encoding="UTF-8"?>
2940 <XRDS xmlns="xri://$xrds">
2941 <XRD xml:id="oauth" xmlns="xri://$XRD*($v*2.0)" version="2.0">
2942 <Type>xri://$xrds*simple</Type>
2943 <Expires>2008-04-13T07:34:58Z</Expires>
2944 <Service>
2945 <Type>http://oauth.net/core/1.0/endpoint/authorize</Type>
2946 <Type>http://oauth.net/core/1.0/parameters/auth-header</Type>
2947 <Type>http://oauth.net/core/1.0/parameters/uri-query</Type>
2948 <URI priority="10">https://ma.gnolia.com/oauth/authorize</URI>
2949 <URI priority="20">http://ma.gnolia.com/oauth/authorize</URI>
2950 </Service>
2951 </XRD>
2952 <XRD xmlns="xri://$XRD*($v*2.0)" version="2.0">
2953 <Type>xri://$xrds*simple</Type>
2954 <Service priority="10">
2955 <Type>http://oauth.net/discovery/1.0</Type>
2956 <URI>#oauth</URI>
2957 </Service>
2958 </XRD>
2959 </XRDS>';
2960
2961 $xml = new Xml($text);
2962 $result = Set::reverse($xml);
2963
2964 $expected = array('XRDS' => array(
2965 'xmlns' => 'xri://$xrds',
2966 'XRD' => array(
2967 array(
2968 'xml:id' => 'oauth',
2969 'xmlns' => 'xri://$XRD*($v*2.0)',
2970 'version' => '2.0',
2971 'Type' => 'xri://$xrds*simple',
2972 'Expires' => '2008-04-13T07:34:58Z',
2973 'Service' => array(
2974 'Type' => array(
2975 'http://oauth.net/core/1.0/endpoint/authorize',
2976 'http://oauth.net/core/1.0/parameters/auth-header',
2977 'http://oauth.net/core/1.0/parameters/uri-query'
2978 ),
2979 'URI' => array(
2980 array(
2981 'value' => 'https://ma.gnolia.com/oauth/authorize',
2982 'priority' => '10',
2983 ),
2984 array(
2985 'value' => 'http://ma.gnolia.com/oauth/authorize',
2986 'priority' => '20'
2987 )
2988 )
2989 )
2990 ),
2991 array(
2992 'xmlns' => 'xri://$XRD*($v*2.0)',
2993 'version' => '2.0',
2994 'Type' => 'xri://$xrds*simple',
2995 'Service' => array(
2996 'priority' => '10',
2997 'Type' => 'http://oauth.net/discovery/1.0',
2998 'URI' => '#oauth'
2999 )
3000 )
3001 )
3002 ));
3003 $this->assertEqual($result, $expected);
3004 }
3005
3006 /**
3007 * testStrictKeyCheck method
3008 *
3009 * @access public
3010 * @return void
3011 */
3012 function testStrictKeyCheck() {
3013 $set = array('a' => 'hi');
3014 $this->assertFalse(Set::check($set, 'a.b'));
3015 }
3016
3017 /**
3018 * Tests Set::flatten
3019 *
3020 * @access public
3021 * @return void
3022 */
3023 function testFlatten() {
3024 $data = array('Larry', 'Curly', 'Moe');
3025 $result = Set::flatten($data);
3026 $this->assertEqual($result, $data);
3027
3028 $data[9] = 'Shemp';
3029 $result = Set::flatten($data);
3030 $this->assertEqual($result, $data);
3031
3032 $data = array(
3033 array(
3034 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
3035 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
3036 ),
3037 array(
3038 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
3039 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
3040 )
3041 );
3042
3043 $result = Set::flatten($data);
3044 $expected = array(
3045 '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
3046 '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
3047 '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
3048 '1.Author.user' => 'larry', '1.Author.password' => null
3049 );
3050 $this->assertEqual($result, $expected);
3051 }
3052
3053 /**
3054 * test normalization
3055 *
3056 * @return void
3057 */
3058 function testNormalizeStrings() {
3059 $result = Set::normalize('one,two,three');
3060 $expected = array('one' => null, 'two' => null, 'three' => null);
3061 $this->assertEqual($expected, $result);
3062
3063 $result = Set::normalize('one two three', true, ' ');
3064 $expected = array('one' => null, 'two' => null, 'three' => null);
3065 $this->assertEqual($expected, $result);
3066
3067 $result = Set::normalize('one , two , three ', true, ',', true);
3068 $expected = array('one' => null, 'two' => null, 'three' => null);
3069 $this->assertEqual($expected, $result);
3070 }
3071
3072 /**
3073 * test normalizing arrays
3074 *
3075 * @return void
3076 */
3077 function testNormalizeArrays() {
3078 $result = Set::normalize(array('one', 'two', 'three'));
3079 $expected = array('one' => null, 'two' => null, 'three' => null);
3080 $this->assertEqual($expected, $result);
3081
3082 $result = Set::normalize(array('one', 'two', 'three'), false);
3083 $expected = array('one', 'two', 'three');
3084 $this->assertEqual($expected, $result);
3085
3086 $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false);
3087 $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
3088 $this->assertEqual($expected, $result);
3089
3090 $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'));
3091 $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
3092 $this->assertEqual($expected, $result);
3093
3094 $result = Set::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three'));
3095 $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null);
3096 $this->assertEqual($expected, $result);
3097 }
3098 }