Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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