Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link          http://cakephp.org CakePHP(tm) Project
12
 * @package       Cake.Utility
13
 * @since         CakePHP(tm) v 2.2.0
14
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
15
 */
16
 
17
App::uses('Hash', 'Utility');
18
 
19
/**
20
 * Class HashTest
21
 *
22
 * @package       Cake.Utility
23
 */
24
class HashTest extends CakeTestCase {
25
 
26
	public static function articleData() {
27
		return array(
28
			array(
29
				'Article' => array(
30
					'id' => '1',
31
					'user_id' => '1',
32
					'title' => 'First Article',
33
					'body' => 'First Article Body'
34
				),
35
				'User' => array(
36
					'id' => '1',
37
					'user' => 'mariano',
38
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
39
				),
40
				'Comment' => array(
41
					array(
42
						'id' => '1',
43
						'article_id' => '1',
44
						'user_id' => '2',
45
						'comment' => 'First Comment for First Article',
46
					),
47
					array(
48
						'id' => '2',
49
						'article_id' => '1',
50
						'user_id' => '4',
51
						'comment' => 'Second Comment for First Article',
52
					),
53
				),
54
				'Tag' => array(
55
					array(
56
						'id' => '1',
57
						'tag' => 'tag1',
58
					),
59
					array(
60
						'id' => '2',
61
						'tag' => 'tag2',
62
					)
63
				),
64
				'Deep' => array(
65
					'Nesting' => array(
66
						'test' => array(
67
							1 => 'foo',
68
							2 => array(
69
								'and' => array('more' => 'stuff')
70
							)
71
						)
72
					)
73
				)
74
			),
75
			array(
76
				'Article' => array(
77
					'id' => '2',
78
					'user_id' => '1',
79
					'title' => 'Second Article',
80
					'body' => 'Second Article Body',
81
					'published' => 'Y',
82
				),
83
				'User' => array(
84
					'id' => '2',
85
					'user' => 'mariano',
86
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
87
				),
88
				'Comment' => array(),
89
				'Tag' => array()
90
			),
91
			array(
92
				'Article' => array(
93
					'id' => '3',
94
					'user_id' => '1',
95
					'title' => 'Third Article',
96
					'body' => 'Third Article Body',
97
				),
98
				'User' => array(
99
					'id' => '3',
100
					'user' => 'mariano',
101
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
102
				),
103
				'Comment' => array(),
104
				'Tag' => array()
105
			),
106
			array(
107
				'Article' => array(
108
					'id' => '4',
109
					'user_id' => '1',
110
					'title' => 'Fourth Article',
111
					'body' => 'Fourth Article Body',
112
				),
113
				'User' => array(
114
					'id' => '4',
115
					'user' => 'mariano',
116
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
117
				),
118
				'Comment' => array(),
119
				'Tag' => array()
120
			),
121
			array(
122
				'Article' => array(
123
					'id' => '5',
124
					'user_id' => '1',
125
					'title' => 'Fifth Article',
126
					'body' => 'Fifth Article Body',
127
				),
128
				'User' => array(
129
					'id' => '5',
130
					'user' => 'mariano',
131
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
132
					),
133
				'Comment' => array(),
134
				'Tag' => array()
135
			)
136
		);
137
	}
138
 
139
	public static function userData() {
140
		return array(
141
			array(
142
				'User' => array(
143
					'id' => 2,
144
					'group_id' => 1,
145
					'Data' => array(
146
						'user' => 'mariano.iglesias',
147
						'name' => 'Mariano Iglesias'
148
					)
149
				)
150
			),
151
			array(
152
				'User' => array(
153
					'id' => 14,
154
					'group_id' => 2,
155
					'Data' => array(
156
						'user' => 'phpnut',
157
						'name' => 'Larry E. Masters'
158
					)
159
				)
160
			),
161
			array(
162
				'User' => array(
163
					'id' => 25,
164
					'group_id' => 1,
165
					'Data' => array(
166
						'user' => 'gwoo',
167
						'name' => 'The Gwoo'
168
					)
169
				)
170
			)
171
		);
172
	}
173
 
174
/**
175
 * Test get()
176
 *
177
 * return void
178
 */
179
	public function testGet() {
180
		$data = array('abc', 'def');
181
 
182
		$result = Hash::get($data, '0');
183
		$this->assertEquals('abc', $result);
184
 
185
		$result = Hash::get($data, 0);
186
		$this->assertEquals('abc', $result);
187
 
188
		$result = Hash::get($data, '1');
189
		$this->assertEquals('def', $result);
190
 
191
		$data = self::articleData();
192
 
193
		$result = Hash::get(array(), '1.Article.title');
194
		$this->assertNull($result);
195
 
196
		$result = Hash::get($data, '');
197
		$this->assertNull($result);
198
 
199
		$result = Hash::get($data, '0.Article.title');
200
		$this->assertEquals('First Article', $result);
201
 
202
		$result = Hash::get($data, '1.Article.title');
203
		$this->assertEquals('Second Article', $result);
204
 
205
		$result = Hash::get($data, '5.Article.title');
206
		$this->assertNull($result);
207
 
208
		$result = Hash::get($data, '1.Article.title.not_there');
209
		$this->assertNull($result);
210
 
211
		$result = Hash::get($data, '1.Article');
212
		$this->assertEquals($data[1]['Article'], $result);
213
 
214
		$result = Hash::get($data, array('1', 'Article'));
215
		$this->assertEquals($data[1]['Article'], $result);
216
	}
217
 
218
/**
219
 * Test dimensions.
220
 *
221
 * @return void
222
 */
223
	public function testDimensions() {
224
		$result = Hash::dimensions(array());
225
		$this->assertEquals($result, 0);
226
 
227
		$data = array('one', '2', 'three');
228
		$result = Hash::dimensions($data);
229
		$this->assertEquals($result, 1);
230
 
231
		$data = array('1' => '1.1', '2', '3');
232
		$result = Hash::dimensions($data);
233
		$this->assertEquals($result, 1);
234
 
235
		$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
236
		$result = Hash::dimensions($data);
237
		$this->assertEquals($result, 2);
238
 
239
		$data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
240
		$result = Hash::dimensions($data);
241
		$this->assertEquals($result, 1);
242
 
243
		$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
244
		$result = Hash::dimensions($data);
245
		$this->assertEquals($result, 2);
246
	}
247
 
248
/**
249
 * Test maxDimensions
250
 *
251
 * @return void
252
 */
253
	public function testMaxDimensions() {
254
		$data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
255
		$result = Hash::maxDimensions($data);
256
		$this->assertEquals($result, 2);
257
 
258
		$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
259
		$result = Hash::maxDimensions($data);
260
		$this->assertEquals($result, 3);
261
 
262
		$data = array(
263
			'1' => array('1.1' => '1.1.1'),
264
			array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))),
265
			'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
266
		);
267
		$result = Hash::maxDimensions($data);
268
		$this->assertEquals($result, 4);
269
 
270
		$data = array(
271
			'1' => array('1.1' => '1.1.1'),
272
			array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))),
273
			'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
274
		);
275
		$result = Hash::maxDimensions($data);
276
		$this->assertEquals($result, 5);
277
 
278
		$data = array(
279
			'1' => array('1.1' => '1.1.1'),
280
			array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
281
			'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
282
		);
283
		$result = Hash::maxDimensions($data);
284
		$this->assertEquals($result, 5);
285
 
286
		$data = array(
287
			'1' => array('1.1' => '1.1.1'),
288
			array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
289
			'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
290
		);
291
		$result = Hash::maxDimensions($data);
292
		$this->assertEquals($result, 5);
293
	}
294
 
295
/**
296
 * Tests Hash::flatten
297
 *
298
 * @return void
299
 */
300
	public function testFlatten() {
301
		$data = array('Larry', 'Curly', 'Moe');
302
		$result = Hash::flatten($data);
303
		$this->assertEquals($result, $data);
304
 
305
		$data[9] = 'Shemp';
306
		$result = Hash::flatten($data);
307
		$this->assertEquals($result, $data);
308
 
309
		$data = array(
310
			array(
311
				'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
312
				'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
313
			),
314
			array(
315
				'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
316
				'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
317
			)
318
		);
319
		$result = Hash::flatten($data);
320
		$expected = array(
321
			'0.Post.id' => '1',
322
			'0.Post.author_id' => '1',
323
			'0.Post.title' => 'First Post',
324
			'0.Author.id' => '1',
325
			'0.Author.user' => 'nate',
326
			'0.Author.password' => 'foo',
327
			'1.Post.id' => '2',
328
			'1.Post.author_id' => '3',
329
			'1.Post.title' => 'Second Post',
330
			'1.Post.body' => 'Second Post Body',
331
			'1.Author.id' => '3',
332
			'1.Author.user' => 'larry',
333
			'1.Author.password' => null
334
		);
335
		$this->assertEquals($expected, $result);
336
 
337
		$data = array(
338
			array(
339
				'Post' => array('id' => '1', 'author_id' => null, 'title' => 'First Post'),
340
				'Author' => array(),
341
			)
342
		);
343
		$result = Hash::flatten($data);
344
		$expected = array(
345
			'0.Post.id' => '1',
346
			'0.Post.author_id' => null,
347
			'0.Post.title' => 'First Post',
348
			'0.Author' => array()
349
		);
350
		$this->assertEquals($expected, $result);
351
 
352
		$data = array(
353
			array('Post' => array('id' => 1)),
354
			array('Post' => array('id' => 2)),
355
		);
356
		$result = Hash::flatten($data, '/');
357
		$expected = array(
358
			'0/Post/id' => '1',
359
			'1/Post/id' => '2',
360
		);
361
		$this->assertEquals($expected, $result);
362
	}
363
 
364
/**
365
 * Test diff();
366
 *
367
 * @return void
368
 */
369
	public function testDiff() {
370
		$a = array(
371
 
372
			1 => array('name' => 'about')
373
		);
374
		$b = array(
375
 
376
			1 => array('name' => 'about'),
377
			2 => array('name' => 'contact')
378
		);
379
 
380
		$result = Hash::diff($a, array());
381
		$expected = $a;
382
		$this->assertEquals($expected, $result);
383
 
384
		$result = Hash::diff(array(), $b);
385
		$expected = $b;
386
		$this->assertEquals($expected, $result);
387
 
388
		$result = Hash::diff($a, $b);
389
		$expected = array(
390
			2 => array('name' => 'contact')
391
		);
392
		$this->assertEquals($expected, $result);
393
 
394
		$b = array(
395
 
396
			1 => array('name' => 'about')
397
		);
398
 
399
		$result = Hash::diff($a, $b);
400
		$expected = array(
401
 
402
		);
403
		$this->assertEquals($expected, $result);
404
 
405
		$a = array();
406
		$b = array('name' => 'bob', 'address' => 'home');
407
		$result = Hash::diff($a, $b);
408
		$this->assertEquals($result, $b);
409
 
410
		$a = array('name' => 'bob', 'address' => 'home');
411
		$b = array();
412
		$result = Hash::diff($a, $b);
413
		$this->assertEquals($result, $a);
414
 
415
		$a = array('key' => true, 'another' => false, 'name' => 'me');
416
		$b = array('key' => 1, 'another' => 0);
417
		$expected = array('name' => 'me');
418
		$result = Hash::diff($a, $b);
419
		$this->assertEquals($expected, $result);
420
 
421
		$a = array('key' => 'value', 'another' => null, 'name' => 'me');
422
		$b = array('key' => 'differentValue', 'another' => null);
423
		$expected = array('key' => 'value', 'name' => 'me');
424
		$result = Hash::diff($a, $b);
425
		$this->assertEquals($expected, $result);
426
 
427
		$a = array('key' => 'value', 'another' => null, 'name' => 'me');
428
		$b = array('key' => 'differentValue', 'another' => 'value');
429
		$expected = array('key' => 'value', 'another' => null, 'name' => 'me');
430
		$result = Hash::diff($a, $b);
431
		$this->assertEquals($expected, $result);
432
 
433
		$a = array('key' => 'value', 'another' => null, 'name' => 'me');
434
		$b = array('key' => 'differentValue', 'another' => 'value');
435
		$expected = array('key' => 'differentValue', 'another' => 'value', 'name' => 'me');
436
		$result = Hash::diff($b, $a);
437
		$this->assertEquals($expected, $result);
438
 
439
		$a = array('key' => 'value', 'another' => null, 'name' => 'me');
440
		$b = array(0 => 'differentValue', 1 => 'value');
441
		$expected = $a + $b;
442
		$result = Hash::diff($a, $b);
443
		$this->assertEquals($expected, $result);
444
	}
445
 
446
/**
447
 * Test merge()
448
 *
449
 * @return void
450
 */
451
	public function testMerge() {
452
		$result = Hash::merge(array('foo'), array('bar'));
453
		$this->assertEquals($result, array('foo', 'bar'));
454
 
455
		$result = Hash::merge(array('foo'), array('user' => 'bob', 'no-bar'), 'bar');
456
		$this->assertEquals($result, array('foo', 'user' => 'bob', 'no-bar', 'bar'));
457
 
458
		$a = array('foo', 'foo2');
459
		$b = array('bar', 'bar2');
460
		$expected = array('foo', 'foo2', 'bar', 'bar2');
461
		$this->assertEquals($expected, Hash::merge($a, $b));
462
 
463
		$a = array('foo' => 'bar', 'bar' => 'foo');
464
		$b = array('foo' => 'no-bar', 'bar' => 'no-foo');
465
		$expected = array('foo' => 'no-bar', 'bar' => 'no-foo');
466
		$this->assertEquals($expected, Hash::merge($a, $b));
467
 
468
		$a = array('users' => array('bob', 'jim'));
469
		$b = array('users' => array('lisa', 'tina'));
470
		$expected = array('users' => array('bob', 'jim', 'lisa', 'tina'));
471
		$this->assertEquals($expected, Hash::merge($a, $b));
472
 
473
		$a = array('users' => array('jim', 'bob'));
474
		$b = array('users' => 'none');
475
		$expected = array('users' => 'none');
476
		$this->assertEquals($expected, Hash::merge($a, $b));
477
 
478
		$a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'cakephp');
479
		$b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream');
480
		$expected = array(
481
			'users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)),
482
			'cakephp',
483
			'ice-cream'
484
		);
485
		$result = Hash::merge($a, $b);
486
		$this->assertEquals($expected, $result);
487
 
488
		$c = array(
489
			'users' => array('lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')),
490
			'chocolate'
491
		);
492
		$expected = array(
493
			'users' => array('lisa' => array('id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')),
494
			'cakephp',
495
			'ice-cream',
496
			'chocolate'
497
		);
498
		$this->assertEquals($expected, Hash::merge($a, $b, $c));
499
 
500
		$this->assertEquals($expected, Hash::merge($a, $b, array(), $c));
501
 
502
		$a = array(
503
			'Tree',
504
			'CounterCache',
505
			'Upload' => array(
506
				'folder' => 'products',
507
				'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')
508
			)
509
		);
510
		$b = array(
511
			'Cacheable' => array('enabled' => false),
512
			'Limit',
513
			'Bindable',
514
			'Validator',
515
			'Transactional'
516
		);
517
		$expected = array(
518
			'Tree',
519
			'CounterCache',
520
			'Upload' => array(
521
				'folder' => 'products',
522
				'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')
523
			),
524
			'Cacheable' => array('enabled' => false),
525
			'Limit',
526
			'Bindable',
527
			'Validator',
528
			'Transactional'
529
		);
530
		$this->assertEquals(Hash::merge($a, $b), $expected);
531
	}
532
 
533
/**
534
 * test normalizing arrays
535
 *
536
 * @return void
537
 */
538
	public function testNormalize() {
539
		$result = Hash::normalize(array('one', 'two', 'three'));
540
		$expected = array('one' => null, 'two' => null, 'three' => null);
541
		$this->assertEquals($expected, $result);
542
 
543
		$result = Hash::normalize(array('one', 'two', 'three'), false);
544
		$expected = array('one', 'two', 'three');
545
		$this->assertEquals($expected, $result);
546
 
547
		$result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false);
548
		$expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
549
		$this->assertEquals($expected, $result);
550
 
551
		$result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'));
552
		$expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
553
		$this->assertEquals($expected, $result);
554
 
555
		$result = Hash::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three'));
556
		$expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null);
557
		$this->assertEquals($expected, $result);
558
	}
559
 
560
/**
561
 * testContains method
562
 *
563
 * @return void
564
 */
565
	public function testContains() {
566
		$data = array('apple', 'bee', 'cyclops');
567
		$this->assertTrue(Hash::contains($data, array('apple')));
568
		$this->assertFalse(Hash::contains($data, array('data')));
569
 
570
		$a = array(
571
 
572
			1 => array('name' => 'about')
573
		);
574
		$b = array(
575
 
576
			1 => array('name' => 'about'),
577
			2 => array('name' => 'contact'),
578
			'a' => 'b'
579
		);
580
 
581
		$this->assertTrue(Hash::contains($a, $a));
582
		$this->assertFalse(Hash::contains($a, $b));
583
		$this->assertTrue(Hash::contains($b, $a));
584
 
585
		$a = array(
586
			array('User' => array('id' => 1)),
587
			array('User' => array('id' => 2)),
588
		);
589
		$b = array(
590
			array('User' => array('id' => 1)),
591
			array('User' => array('id' => 2)),
592
			array('User' => array('id' => 3))
593
		);
594
		$this->assertTrue(Hash::contains($b, $a));
595
		$this->assertFalse(Hash::contains($a, $b));
596
 
597
		$a = array(0 => 'test', 'string' => null);
598
		$this->assertTrue(Hash::contains($a, array('string' => null)));
599
 
600
		$a = array(0 => 'test', 'string' => null);
601
		$this->assertTrue(Hash::contains($a, array('test')));
602
	}
603
 
604
/**
605
 * testFilter method
606
 *
607
 * @return void
608
 */
609
	public function testFilter() {
610
		$result = Hash::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
611
		$expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be'));
612
		$this->assertSame($expected, $result);
613
 
614
		$result = Hash::filter(array(1, array(false)));
615
		$expected = array(1);
616
		$this->assertEquals($expected, $result);
617
 
618
		$result = Hash::filter(array(1, array(false, false)));
619
		$expected = array(1);
620
		$this->assertEquals($expected, $result);
621
 
622
		$result = Hash::filter(array(1, array('empty', false)));
623
		$expected = array(1, array('empty'));
624
		$this->assertEquals($expected, $result);
625
 
626
		$result = Hash::filter(array(1, array('2', false, array(3, null))));
627
		$expected = array(1, array('2', 2 => array(3)));
628
		$this->assertEquals($expected, $result);
629
 
630
		$this->assertSame(array(), Hash::filter(array()));
631
	}
632
 
633
/**
634
 * testNumericArrayCheck method
635
 *
636
 * @return void
637
 */
638
	public function testNumeric() {
639
		$data = array('one');
640
		$this->assertTrue(Hash::numeric(array_keys($data)));
641
 
642
		$data = array(1 => 'one');
643
		$this->assertFalse(Hash::numeric($data));
644
 
645
		$data = array('one');
646
		$this->assertFalse(Hash::numeric($data));
647
 
648
		$data = array('one' => 'two');
649
		$this->assertFalse(Hash::numeric($data));
650
 
651
		$data = array('one' => 1);
652
		$this->assertTrue(Hash::numeric($data));
653
 
654
		$data = array(0);
655
		$this->assertTrue(Hash::numeric($data));
656
 
657
		$data = array('one', 'two', 'three', 'four', 'five');
658
		$this->assertTrue(Hash::numeric(array_keys($data)));
659
 
660
		$data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
661
		$this->assertTrue(Hash::numeric(array_keys($data)));
662
 
663
		$data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
664
		$this->assertTrue(Hash::numeric(array_keys($data)));
665
 
666
		$data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
667
		$this->assertFalse(Hash::numeric(array_keys($data)));
668
 
669
		$data = array(2.4, 1, 0, -1, -2);
670
		$this->assertTrue(Hash::numeric($data));
671
	}
672
 
673
/**
674
 * Test simple paths.
675
 *
676
 * @return void
677
 */
678
	public function testExtractBasic() {
679
		$data = self::articleData();
680
 
681
		$result = Hash::extract($data, '');
682
		$this->assertEquals($data, $result);
683
 
684
		$result = Hash::extract($data, '0.Article.title');
685
		$this->assertEquals(array('First Article'), $result);
686
 
687
		$result = Hash::extract($data, '1.Article.title');
688
		$this->assertEquals(array('Second Article'), $result);
689
 
690
		$result = Hash::extract(array(false), '{n}.Something.another_thing');
691
		$this->assertEquals(array(), $result);
692
	}
693
 
694
/**
695
 * Test the {n} selector
696
 *
697
 * @return void
698
 */
699
	public function testExtractNumericKey() {
700
		$data = self::articleData();
701
		$result = Hash::extract($data, '{n}.Article.title');
702
		$expected = array(
703
			'First Article', 'Second Article',
704
			'Third Article', 'Fourth Article',
705
			'Fifth Article'
706
		);
707
		$this->assertEquals($expected, $result);
708
 
709
		$result = Hash::extract($data, '0.Comment.{n}.user_id');
710
		$expected = array(
711
			'2', '4'
712
		);
713
		$this->assertEquals($expected, $result);
714
	}
715
 
716
/**
717
 * Test the {n} selector with inconsistent arrays
718
 *
719
 * @return void
720
 */
721
	public function testExtractNumericMixedKeys() {
722
		$data = array(
723
			'User' => array(
724
 
725
					'id' => 4,
726
					'name' => 'Neo'
727
				),
728
				1 => array(
729
					'id' => 5,
730
					'name' => 'Morpheus'
731
				),
732
				'stringKey' => array(
733
					'name' => 'Fail'
734
				)
735
			)
736
		);
737
		$result = Hash::extract($data, 'User.{n}.name');
738
		$expected = array('Neo', 'Morpheus');
739
		$this->assertEquals($expected, $result);
740
	}
741
 
742
/**
743
 * Test the {n} selector with non-zero based arrays
744
 *
745
 * @return void
746
 */
747
	public function testExtractNumericNonZero() {
748
		$data = array(
749
			1 => array(
750
				'User' => array(
751
					'id' => 1,
752
					'name' => 'John',
753
				)
754
			),
755
			2 => array(
756
				'User' => array(
757
					'id' => 2,
758
					'name' => 'Bob',
759
				)
760
			),
761
			3 => array(
762
				'User' => array(
763
					'id' => 3,
764
					'name' => 'Tony',
765
				)
766
			)
767
		);
768
		$result = Hash::extract($data, '{n}.User.name');
769
		$expected = array('John', 'Bob', 'Tony');
770
		$this->assertEquals($expected, $result);
771
	}
772
 
773
/**
774
 * Test the {s} selector.
775
 *
776
 * @return void
777
 */
778
	public function testExtractStringKey() {
779
		$data = self::articleData();
780
		$result = Hash::extract($data, '{n}.{s}.user');
781
		$expected = array(
782
			'mariano',
783
			'mariano',
784
			'mariano',
785
			'mariano',
786
			'mariano'
787
		);
788
		$this->assertEquals($expected, $result);
789
 
790
		$result = Hash::extract($data, '{n}.{s}.Nesting.test.1');
791
		$this->assertEquals(array('foo'), $result);
792
	}
793
 
794
/**
795
 * Test the attribute presense selector.
796
 *
797
 * @return void
798
 */
799
	public function testExtractAttributePresence() {
800
		$data = self::articleData();
801
 
802
		$result = Hash::extract($data, '{n}.Article[published]');
803
		$expected = array($data[1]['Article']);
804
		$this->assertEquals($expected, $result);
805
 
806
		$result = Hash::extract($data, '{n}.Article[id][published]');
807
		$expected = array($data[1]['Article']);
808
		$this->assertEquals($expected, $result);
809
	}
810
 
811
/**
812
 * Test = and != operators.
813
 *
814
 * @return void
815
 */
816
	public function testExtractAttributeEquality() {
817
		$data = self::articleData();
818
 
819
		$result = Hash::extract($data, '{n}.Article[id=3]');
820
		$expected = array($data[2]['Article']);
821
		$this->assertEquals($expected, $result);
822
 
823
		$result = Hash::extract($data, '{n}.Article[id = 3]');
824
		$expected = array($data[2]['Article']);
825
		$this->assertEquals($expected, $result, 'Whitespace should not matter.');
826
 
827
		$result = Hash::extract($data, '{n}.Article[id!=3]');
828
		$this->assertEquals(1, $result[0]['id']);
829
		$this->assertEquals(2, $result[1]['id']);
830
		$this->assertEquals(4, $result[2]['id']);
831
		$this->assertEquals(5, $result[3]['id']);
832
	}
833
 
834
/**
835
 * Test that attribute matchers don't cause errors on scalar data.
836
 *
837
 * @return void
838
 */
839
	public function testExtractAttributeEqualityOnScalarValue() {
840
		$data = array(
841
			'Entity' => array(
842
				'id' => 1,
843
				'data1' => 'value',
844
			)
845
		);
846
		$result = Hash::extract($data, 'Entity[id=1].data1');
847
		$this->assertEquals(array('value'), $result);
848
 
849
		$data = array('Entity' => false );
850
		$result = Hash::extract($data, 'Entity[id=1].data1');
851
		$this->assertEquals(array(), $result);
852
	}
853
 
854
/**
855
 * Test comparison operators.
856
 *
857
 * @return void
858
 */
859
	public function testExtractAttributeComparison() {
860
		$data = self::articleData();
861
 
862
		$result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2]');
863
		$expected = array($data[0]['Comment'][1]);
864
		$this->assertEquals($expected, $result);
865
		$this->assertEquals(4, $expected[0]['user_id']);
866
 
867
		$result = Hash::extract($data, '{n}.Comment.{n}[user_id >= 4]');
868
		$expected = array($data[0]['Comment'][1]);
869
		$this->assertEquals($expected, $result);
870
		$this->assertEquals(4, $expected[0]['user_id']);
871
 
872
		$result = Hash::extract($data, '{n}.Comment.{n}[user_id < 3]');
873
		$expected = array($data[0]['Comment'][0]);
874
		$this->assertEquals($expected, $result);
875
		$this->assertEquals(2, $expected[0]['user_id']);
876
 
877
		$result = Hash::extract($data, '{n}.Comment.{n}[user_id <= 2]');
878
		$expected = array($data[0]['Comment'][0]);
879
		$this->assertEquals($expected, $result);
880
		$this->assertEquals(2, $expected[0]['user_id']);
881
	}
882
 
883
/**
884
 * Test multiple attributes with conditions.
885
 *
886
 * @return void
887
 */
888
	public function testExtractAttributeMultiple() {
889
		$data = self::articleData();
890
 
891
		$result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2][id=1]');
892
		$this->assertEmpty($result);
893
 
894
		$result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2][id=2]');
895
		$expected = array($data[0]['Comment'][1]);
896
		$this->assertEquals($expected, $result);
897
		$this->assertEquals(4, $expected[0]['user_id']);
898
	}
899
 
900
/**
901
 * Test attribute pattern matching.
902
 *
903
 * @return void
904
 */
905
	public function testExtractAttributePattern() {
906
		$data = self::articleData();
907
 
908
		$result = Hash::extract($data, '{n}.Article[title=/^First/]');
909
		$expected = array($data[0]['Article']);
910
		$this->assertEquals($expected, $result);
911
 
912
		$result = Hash::extract($data, '{n}.Article[title=/^Fir[a-z]+/]');
913
		$expected = array($data[0]['Article']);
914
		$this->assertEquals($expected, $result);
915
	}
916
 
917
/**
918
 * Test that extract() + matching can hit null things.
919
 */
920
	public function testExtractMatchesNull() {
921
		$data = array(
922
			'Country' => array(
923
				array('name' => 'Canada'),
924
				array('name' => 'Australia'),
925
				array('name' => null),
926
			)
927
		);
928
		$result = Hash::extract($data, 'Country.{n}[name=/Canada|^$/]');
929
		$expected = array(
930
			array(
931
				'name' => 'Canada',
932
			),
933
			array(
934
				'name' => null,
935
			),
936
		);
937
		$this->assertEquals($expected, $result);
938
	}
939
 
940
/**
941
 * Test that uneven keys are handled correctly.
942
 *
943
 * @return void
944
 */
945
	public function testExtractUnevenKeys() {
946
		$data = array(
947
			'Level1' => array(
948
				'Level2' => array('test1', 'test2'),
949
				'Level2bis' => array('test3', 'test4')
950
			)
951
		);
952
		$this->assertEquals(
953
			array('test1', 'test2'),
954
			Hash::extract($data, 'Level1.Level2')
955
		);
956
		$this->assertEquals(
957
			array('test3', 'test4'),
958
			Hash::extract($data, 'Level1.Level2bis')
959
		);
960
 
961
		$data = array(
962
			'Level1' => array(
963
				'Level2bis' => array(
964
					array('test3', 'test4'),
965
					array('test5', 'test6')
966
				)
967
			)
968
		);
969
		$expected = array(
970
			array('test3', 'test4'),
971
			array('test5', 'test6')
972
		);
973
		$this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
974
 
975
		$data['Level1']['Level2'] = array('test1', 'test2');
976
		$this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
977
	}
978
 
979
/**
980
 * testSort method
981
 *
982
 * @return void
983
 */
984
	public function testSort() {
985
		$result = Hash::sort(array(), '{n}.name', 'asc');
986
		$this->assertEquals(array(), $result);
987
 
988
		$a = array(
989
 
990
				'Person' => array('name' => 'Jeff'),
991
				'Friend' => array(array('name' => 'Nate'))
992
			),
993
			1 => array(
994
				'Person' => array('name' => 'Tracy'),
995
				'Friend' => array(array('name' => 'Lindsay'))
996
			)
997
		);
998
		$b = array(
999
 
1000
				'Person' => array('name' => 'Tracy'),
1001
				'Friend' => array(array('name' => 'Lindsay'))
1002
			),
1003
			1 => array(
1004
				'Person' => array('name' => 'Jeff'),
1005
				'Friend' => array(array('name' => 'Nate'))
1006
			)
1007
		);
1008
		$a = Hash::sort($a, '{n}.Friend.{n}.name', 'asc');
1009
		$this->assertEquals($a, $b);
1010
 
1011
		$b = array(
1012
 
1013
				'Person' => array('name' => 'Jeff'),
1014
				'Friend' => array(array('name' => 'Nate'))
1015
			),
1016
			1 => array(
1017
				'Person' => array('name' => 'Tracy'),
1018
				'Friend' => array(array('name' => 'Lindsay'))
1019
			)
1020
		);
1021
		$a = array(
1022
 
1023
				'Person' => array('name' => 'Tracy'),
1024
				'Friend' => array(array('name' => 'Lindsay'))
1025
			),
1026
			1 => array(
1027
				'Person' => array('name' => 'Jeff'),
1028
				'Friend' => array(array('name' => 'Nate'))
1029
			)
1030
		);
1031
		$a = Hash::sort($a, '{n}.Friend.{n}.name', 'desc');
1032
		$this->assertEquals($a, $b);
1033
 
1034
		$a = array(
1035
 
1036
				'Person' => array('name' => 'Jeff'),
1037
				'Friend' => array(array('name' => 'Nate'))
1038
			),
1039
			1 => array(
1040
				'Person' => array('name' => 'Tracy'),
1041
				'Friend' => array(array('name' => 'Lindsay'))
1042
			),
1043
			2 => array(
1044
				'Person' => array('name' => 'Adam'),
1045
				'Friend' => array(array('name' => 'Bob'))
1046
			)
1047
		);
1048
		$b = array(
1049
 
1050
				'Person' => array('name' => 'Adam'),
1051
				'Friend' => array(array('name' => 'Bob'))
1052
			),
1053
			1 => array(
1054
				'Person' => array('name' => 'Jeff'),
1055
				'Friend' => array(array('name' => 'Nate'))
1056
			),
1057
			2 => array(
1058
				'Person' => array('name' => 'Tracy'),
1059
				'Friend' => array(array('name' => 'Lindsay'))
1060
			)
1061
		);
1062
		$a = Hash::sort($a, '{n}.Person.name', 'asc');
1063
		$this->assertEquals($a, $b);
1064
 
1065
		$a = array(
1066
 
1067
			1 => array('Shirt' => array('color' => 'black'))
1068
		);
1069
		$b = array(
1070
 
1071
			1 => array('Person' => array('name' => 'Jeff')),
1072
		);
1073
		$a = Hash::sort($a, '{n}.Person.name', 'ASC', 'STRING');
1074
		$this->assertSame($a, $b);
1075
 
1076
		$names = array(
1077
			array('employees' => array(
1078
				array('name' => array('first' => 'John', 'last' => 'Doe')))
1079
			),
1080
			array('employees' => array(
1081
				array('name' => array('first' => 'Jane', 'last' => 'Doe')))
1082
			),
1083
			array('employees' => array(array('name' => array()))),
1084
			array('employees' => array(array('name' => array())))
1085
		);
1086
		$result = Hash::sort($names, '{n}.employees.0.name', 'asc');
1087
		$expected = array(
1088
			array('employees' => array(
1089
				array('name' => array('first' => 'John', 'last' => 'Doe')))
1090
			),
1091
			array('employees' => array(
1092
				array('name' => array('first' => 'Jane', 'last' => 'Doe')))
1093
			),
1094
			array('employees' => array(array('name' => array()))),
1095
			array('employees' => array(array('name' => array())))
1096
		);
1097
		$this->assertSame($expected, $result);
1098
 
1099
		$a = array(
1100
			'SU' => array(
1101
				'total_fulfillable' => 2
1102
			),
1103
			'AA' => array(
1104
				'total_fulfillable' => 1
1105
			),
1106
			'LX' => array(
1107
				'total_fulfillable' => 0
1108
			),
1109
			'BL' => array(
1110
				'total_fulfillable' => 3
1111
			),
1112
		);
1113
		$expected = array(
1114
			'LX' => array(
1115
				'total_fulfillable' => 0
1116
			),
1117
			'AA' => array(
1118
				'total_fulfillable' => 1
1119
			),
1120
			'SU' => array(
1121
				'total_fulfillable' => 2
1122
			),
1123
			'BL' => array(
1124
				'total_fulfillable' => 3
1125
			),
1126
		);
1127
		$result = Hash::sort($a, '{s}.total_fulfillable', 'asc');
1128
		$this->assertSame($expected, $result);
1129
	}
1130
 
1131
/**
1132
 * Test sort() with numeric option.
1133
 *
1134
 * @return void
1135
 */
1136
	public function testSortNumeric() {
1137
		$items = array(
1138
			array('Item' => array('price' => '155,000')),
1139
			array('Item' => array('price' => '139,000')),
1140
			array('Item' => array('price' => '275,622')),
1141
			array('Item' => array('price' => '230,888')),
1142
			array('Item' => array('price' => '66,000')),
1143
		);
1144
		$result = Hash::sort($items, '{n}.Item.price', 'asc', 'numeric');
1145
		$expected = array(
1146
			array('Item' => array('price' => '66,000')),
1147
			array('Item' => array('price' => '139,000')),
1148
			array('Item' => array('price' => '155,000')),
1149
			array('Item' => array('price' => '230,888')),
1150
			array('Item' => array('price' => '275,622')),
1151
		);
1152
		$this->assertEquals($expected, $result);
1153
 
1154
		$result = Hash::sort($items, '{n}.Item.price', 'desc', 'numeric');
1155
		$expected = array(
1156
			array('Item' => array('price' => '275,622')),
1157
			array('Item' => array('price' => '230,888')),
1158
			array('Item' => array('price' => '155,000')),
1159
			array('Item' => array('price' => '139,000')),
1160
			array('Item' => array('price' => '66,000')),
1161
		);
1162
		$this->assertEquals($expected, $result);
1163
	}
1164
 
1165
/**
1166
 * Test natural sorting.
1167
 *
1168
 * @return void
1169
 */
1170
	public function testSortNatural() {
1171
		if (version_compare(PHP_VERSION, '5.4.0', '<')) {
1172
			$this->markTestSkipped('SORT_NATURAL is available since PHP 5.4.');
1173
		}
1174
		$items = array(
1175
			array('Item' => array('image' => 'img1.jpg')),
1176
			array('Item' => array('image' => 'img99.jpg')),
1177
			array('Item' => array('image' => 'img12.jpg')),
1178
			array('Item' => array('image' => 'img10.jpg')),
1179
			array('Item' => array('image' => 'img2.jpg')),
1180
		);
1181
		$result = Hash::sort($items, '{n}.Item.image', 'desc', 'natural');
1182
		$expected = array(
1183
			array('Item' => array('image' => 'img99.jpg')),
1184
			array('Item' => array('image' => 'img12.jpg')),
1185
			array('Item' => array('image' => 'img10.jpg')),
1186
			array('Item' => array('image' => 'img2.jpg')),
1187
			array('Item' => array('image' => 'img1.jpg')),
1188
		);
1189
		$this->assertEquals($expected, $result);
1190
 
1191
		$result = Hash::sort($items, '{n}.Item.image', 'asc', 'natural');
1192
		$expected = array(
1193
			array('Item' => array('image' => 'img1.jpg')),
1194
			array('Item' => array('image' => 'img2.jpg')),
1195
			array('Item' => array('image' => 'img10.jpg')),
1196
			array('Item' => array('image' => 'img12.jpg')),
1197
			array('Item' => array('image' => 'img99.jpg')),
1198
		);
1199
		$this->assertEquals($expected, $result);
1200
	}
1201
 
1202
/**
1203
 * Test that sort() with 'natural' type will fallback to 'regular' as SORT_NATURAL is introduced in PHP 5.4
1204
 *
1205
 * @return void
1206
 */
1207
	public function testSortNaturalFallbackToRegular() {
1208
		if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
1209
			$this->markTestSkipped('Skipping SORT_NATURAL fallback test on PHP >= 5.4');
1210
		}
1211
 
1212
		$a = array(
1213
 
1214
			1 => array('Shirt' => array('color' => 'black'))
1215
		);
1216
		$b = array(
1217
 
1218
			1 => array('Person' => array('name' => 'Jeff')),
1219
		);
1220
		$sorted = Hash::sort($a, '{n}.Person.name', 'asc', 'natural');
1221
		$this->assertEquals($sorted, $b);
1222
	}
1223
 
1224
/**
1225
 * test sorting with out of order keys.
1226
 *
1227
 * @return void
1228
 */
1229
	public function testSortWithOutOfOrderKeys() {
1230
		$data = array(
1231
			9 => array('class' => 510, 'test2' => 2),
1232
			1 => array('class' => 500, 'test2' => 1),
1233
			2 => array('class' => 600, 'test2' => 2),
1234
			5 => array('class' => 625, 'test2' => 4),
1235
 
1236
		);
1237
		$expected = array(
1238
			array('class' => 500, 'test2' => 1),
1239
			array('class' => 510, 'test2' => 2),
1240
			array('class' => 600, 'test2' => 2),
1241
			array('class' => 605, 'test2' => 3),
1242
			array('class' => 625, 'test2' => 4),
1243
		);
1244
		$result = Hash::sort($data, '{n}.class', 'asc');
1245
		$this->assertEquals($expected, $result);
1246
 
1247
		$result = Hash::sort($data, '{n}.test2', 'asc');
1248
		$this->assertEquals($expected, $result);
1249
	}
1250
 
1251
/**
1252
 * test sorting with string keys.
1253
 *
1254
 * @return void
1255
 */
1256
	public function testSortString() {
1257
		$toSort = array(
1258
			'four' => array('number' => 4, 'some' => 'foursome'),
1259
			'six' => array('number' => 6, 'some' => 'sixsome'),
1260
			'five' => array('number' => 5, 'some' => 'fivesome'),
1261
			'two' => array('number' => 2, 'some' => 'twosome'),
1262
			'three' => array('number' => 3, 'some' => 'threesome')
1263
		);
1264
		$sorted = Hash::sort($toSort, '{s}.number', 'asc');
1265
		$expected = array(
1266
			'two' => array('number' => 2, 'some' => 'twosome'),
1267
			'three' => array('number' => 3, 'some' => 'threesome'),
1268
			'four' => array('number' => 4, 'some' => 'foursome'),
1269
			'five' => array('number' => 5, 'some' => 'fivesome'),
1270
			'six' => array('number' => 6, 'some' => 'sixsome')
1271
		);
1272
		$this->assertEquals($expected, $sorted);
1273
 
1274
		$menus = array(
1275
			'blogs' => array('title' => 'Blogs', 'weight' => 3),
1276
			'comments' => array('title' => 'Comments', 'weight' => 2),
1277
			'users' => array('title' => 'Users', 'weight' => 1),
1278
		);
1279
		$expected = array(
1280
			'users' => array('title' => 'Users', 'weight' => 1),
1281
			'comments' => array('title' => 'Comments', 'weight' => 2),
1282
			'blogs' => array('title' => 'Blogs', 'weight' => 3),
1283
		);
1284
		$result = Hash::sort($menus, '{s}.weight', 'ASC');
1285
		$this->assertEquals($expected, $result);
1286
	}
1287
 
1288
/**
1289
 * Test insert()
1290
 *
1291
 * @return void
1292
 */
1293
	public function testInsertSimple() {
1294
		$a = array(
1295
			'pages' => array('name' => 'page')
1296
		);
1297
		$result = Hash::insert($a, 'files', array('name' => 'files'));
1298
		$expected = array(
1299
			'pages' => array('name' => 'page'),
1300
			'files' => array('name' => 'files')
1301
		);
1302
		$this->assertEquals($expected, $result);
1303
 
1304
		$a = array(
1305
			'pages' => array('name' => 'page')
1306
		);
1307
		$result = Hash::insert($a, 'pages.name', array());
1308
		$expected = array(
1309
			'pages' => array('name' => array()),
1310
		);
1311
		$this->assertEquals($expected, $result);
1312
	}
1313
 
1314
/**
1315
 * Test inserting with multiple values.
1316
 *
1317
 * @return void
1318
 */
1319
	public function testInsertMulti() {
1320
		$data = self::articleData();
1321
 
1322
		$result = Hash::insert($data, '{n}.Article.insert', 'value');
1323
		$this->assertEquals('value', $result[0]['Article']['insert']);
1324
		$this->assertEquals('value', $result[1]['Article']['insert']);
1325
 
1326
		$result = Hash::insert($data, '{n}.Comment.{n}.insert', 'value');
1327
		$this->assertEquals('value', $result[0]['Comment'][0]['insert']);
1328
		$this->assertEquals('value', $result[0]['Comment'][1]['insert']);
1329
	}
1330
 
1331
/**
1332
 * Test that insert() can insert data over a string value.
1333
 *
1334
 * @return void
1335
 */
1336
	public function testInsertOverwriteStringValue() {
1337
		$data = array(
1338
			'Some' => array(
1339
				'string' => 'value'
1340
			)
1341
		);
1342
		$result = Hash::insert($data, 'Some.string.value', array('values'));
1343
		$expected = array(
1344
			'Some' => array(
1345
				'string' => array(
1346
					'value' => array('values')
1347
				)
1348
			)
1349
		);
1350
		$this->assertEquals($expected, $result);
1351
	}
1352
 
1353
/**
1354
 * Test remove() method.
1355
 *
1356
 * @return void
1357
 */
1358
	public function testRemove() {
1359
		$a = array(
1360
			'pages' => array('name' => 'page'),
1361
			'files' => array('name' => 'files')
1362
		);
1363
 
1364
		$result = Hash::remove($a, 'files');
1365
		$expected = array(
1366
			'pages' => array('name' => 'page')
1367
		);
1368
		$this->assertEquals($expected, $result);
1369
 
1370
		$a = array(
1371
			'pages' => array(
1372
 
1373
				1 => array(
1374
					'name' => 'about',
1375
					'vars' => array('title' => 'page title')
1376
				)
1377
			)
1378
		);
1379
 
1380
		$result = Hash::remove($a, 'pages.1.vars');
1381
		$expected = array(
1382
			'pages' => array(
1383
 
1384
				1 => array('name' => 'about')
1385
			)
1386
		);
1387
		$this->assertEquals($expected, $result);
1388
 
1389
		$result = Hash::remove($a, 'pages.2.vars');
1390
		$expected = $a;
1391
		$this->assertEquals($expected, $result);
1392
	}
1393
 
1394
/**
1395
 * Test removing multiple values.
1396
 *
1397
 * @return void
1398
 */
1399
	public function testRemoveMulti() {
1400
		$data = self::articleData();
1401
 
1402
		$result = Hash::remove($data, '{n}.Article.title');
1403
		$this->assertFalse(isset($result[0]['Article']['title']));
1404
		$this->assertFalse(isset($result[1]['Article']['title']));
1405
 
1406
		$result = Hash::remove($data, '{n}.Article.{s}');
1407
		$this->assertFalse(isset($result[0]['Article']['id']));
1408
		$this->assertFalse(isset($result[0]['Article']['user_id']));
1409
		$this->assertFalse(isset($result[0]['Article']['title']));
1410
		$this->assertFalse(isset($result[0]['Article']['body']));
1411
	}
1412
 
1413
/**
1414
 * testCheck method
1415
 *
1416
 * @return void
1417
 */
1418
	public function testCheck() {
1419
		$set = array(
1420
			'My Index 1' => array('First' => 'The first item')
1421
		);
1422
		$this->assertTrue(Hash::check($set, 'My Index 1.First'));
1423
		$this->assertTrue(Hash::check($set, 'My Index 1'));
1424
 
1425
		$set = array(
1426
			'My Index 1' => array(
1427
				'First' => array(
1428
					'Second' => array(
1429
						'Third' => array(
1430
							'Fourth' => 'Heavy. Nesting.'
1431
						)
1432
					)
1433
				)
1434
			)
1435
		);
1436
		$this->assertTrue(Hash::check($set, 'My Index 1.First.Second'));
1437
		$this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third'));
1438
		$this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third.Fourth'));
1439
		$this->assertFalse(Hash::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
1440
	}
1441
 
1442
/**
1443
 * testCombine method
1444
 *
1445
 * @return void
1446
 */
1447
	public function testCombine() {
1448
		$result = Hash::combine(array(), '{n}.User.id', '{n}.User.Data');
1449
		$this->assertTrue(empty($result));
1450
 
1451
		$a = self::userData();
1452
 
1453
		$result = Hash::combine($a, '{n}.User.id');
1454
		$expected = array(2 => null, 14 => null, 25 => null);
1455
		$this->assertEquals($expected, $result);
1456
 
1457
		$result = Hash::combine($a, '{n}.User.id', '{n}.User.non-existant');
1458
		$expected = array(2 => null, 14 => null, 25 => null);
1459
		$this->assertEquals($expected, $result);
1460
 
1461
		$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data');
1462
		$expected = array(
1463
			2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1464
			14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
1465
			25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
1466
		$this->assertEquals($expected, $result);
1467
 
1468
		$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name');
1469
		$expected = array(
1470
			2 => 'Mariano Iglesias',
1471
			14 => 'Larry E. Masters',
1472
			25 => 'The Gwoo');
1473
		$this->assertEquals($expected, $result);
1474
	}
1475
 
1476
/**
1477
 * test combine() with a group path.
1478
 *
1479
 * @return void
1480
 */
1481
	public function testCombineWithGroupPath() {
1482
		$a = self::userData();
1483
 
1484
		$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
1485
		$expected = array(
1486
			1 => array(
1487
				2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1488
				25 => array('user' => 'gwoo', 'name' => 'The Gwoo')
1489
			),
1490
			2 => array(
1491
				14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')
1492
			)
1493
		);
1494
		$this->assertEquals($expected, $result);
1495
 
1496
		$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
1497
		$expected = array(
1498
			1 => array(
1499
				2 => 'Mariano Iglesias',
1500
				25 => 'The Gwoo'
1501
			),
1502
			2 => array(
1503
				14 => 'Larry E. Masters'
1504
			)
1505
		);
1506
		$this->assertEquals($expected, $result);
1507
 
1508
		$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
1509
		$expected = array(
1510
			1 => array(
1511
				2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1512
				25 => array('user' => 'gwoo', 'name' => 'The Gwoo')
1513
			),
1514
			2 => array(
1515
				14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')
1516
			)
1517
		);
1518
		$this->assertEquals($expected, $result);
1519
 
1520
		$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
1521
		$expected = array(
1522
			1 => array(
1523
				2 => 'Mariano Iglesias',
1524
				25 => 'The Gwoo'
1525
			),
1526
			2 => array(
1527
				14 => 'Larry E. Masters'
1528
			)
1529
		);
1530
		$this->assertEquals($expected, $result);
1531
	}
1532
 
1533
/**
1534
 * Test combine with formatting rules.
1535
 *
1536
 * @return void
1537
 */
1538
	public function testCombineWithFormatting() {
1539
		$a = self::userData();
1540
 
1541
		$result = Hash::combine(
1542
			$a,
1543
			'{n}.User.id',
1544
			array('%1$s: %2$s', '{n}.User.Data.user', '{n}.User.Data.name'),
1545
			'{n}.User.group_id'
1546
		);
1547
		$expected = array(
1548
			1 => array(
1549
				2 => 'mariano.iglesias: Mariano Iglesias',
1550
				25 => 'gwoo: The Gwoo'
1551
			),
1552
			2 => array(
1553
				14 => 'phpnut: Larry E. Masters'
1554
			)
1555
		);
1556
		$this->assertEquals($expected, $result);
1557
 
1558
		$result = Hash::combine(
1559
			$a,
1560
			array(
1561
				'%s: %s',
1562
				'{n}.User.Data.user',
1563
				'{n}.User.Data.name'
1564
			),
1565
			'{n}.User.id'
1566
		);
1567
		$expected = array(
1568
			'mariano.iglesias: Mariano Iglesias' => 2,
1569
			'phpnut: Larry E. Masters' => 14,
1570
			'gwoo: The Gwoo' => 25
1571
		);
1572
		$this->assertEquals($expected, $result);
1573
 
1574
		$result = Hash::combine(
1575
			$a,
1576
			array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'),
1577
			'{n}.User.Data.name'
1578
		);
1579
		$expected = array(
1580
			'mariano.iglesias: 2' => 'Mariano Iglesias',
1581
			'phpnut: 14' => 'Larry E. Masters',
1582
			'gwoo: 25' => 'The Gwoo'
1583
		);
1584
		$this->assertEquals($expected, $result);
1585
 
1586
		$result = Hash::combine(
1587
			$a,
1588
			array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'),
1589
			'{n}.User.Data.name'
1590
		);
1591
		$expected = array(
1592
			'2: mariano.iglesias' => 'Mariano Iglesias',
1593
			'14: phpnut' => 'Larry E. Masters',
1594
			'25: gwoo' => 'The Gwoo'
1595
		);
1596
		$this->assertEquals($expected, $result);
1597
	}
1598
 
1599
/**
1600
 * testFormat method
1601
 *
1602
 * @return void
1603
 */
1604
	public function testFormat() {
1605
		$data = self::userData();
1606
 
1607
		$result = Hash::format(
1608
			$data,
1609
			array('{n}.User.Data.user', '{n}.User.id'),
1610
			'%s, %s'
1611
		);
1612
		$expected = array(
1613
			'mariano.iglesias, 2',
1614
			'phpnut, 14',
1615
			'gwoo, 25'
1616
		);
1617
		$this->assertEquals($expected, $result);
1618
 
1619
		$result = Hash::format(
1620
			$data,
1621
			array('{n}.User.Data.user', '{n}.User.id'),
1622
			'%2$s, %1$s'
1623
		);
1624
		$expected = array(
1625
			'2, mariano.iglesias',
1626
			'14, phpnut',
1627
			'25, gwoo'
1628
		);
1629
		$this->assertEquals($expected, $result);
1630
	}
1631
 
1632
/**
1633
 * testFormattingNullValues method
1634
 *
1635
 * @return void
1636
 */
1637
	public function testFormatNullValues() {
1638
		$data = array(
1639
			array('Person' => array(
1640
				'first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42'
1641
			)),
1642
			array('Person' => array(
1643
				'first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null
1644
			)),
1645
			array('Person' => array(
1646
				'first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null
1647
			))
1648
		);
1649
 
1650
		$result = Hash::format($data, array('{n}.Person.something'), '%s');
1651
		$expected = array('42', '', '');
1652
		$this->assertEquals($expected, $result);
1653
 
1654
		$result = Hash::format($data, array('{n}.Person.city', '{n}.Person.something'), '%s, %s');
1655
		$expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, ');
1656
		$this->assertEquals($expected, $result);
1657
	}
1658
 
1659
/**
1660
 * Test map()
1661
 *
1662
 * @return void
1663
 */
1664
	public function testMap() {
1665
		$data = self::articleData();
1666
 
1667
		$result = Hash::map($data, '{n}.Article.id', array($this, 'mapCallback'));
1668
		$expected = array(2, 4, 6, 8, 10);
1669
		$this->assertEquals($expected, $result);
1670
	}
1671
 
1672
	public function testApply() {
1673
		$data = self::articleData();
1674
 
1675
		$result = Hash::apply($data, '{n}.Article.id', 'array_sum');
1676
		$this->assertEquals(15, $result);
1677
	}
1678
 
1679
/**
1680
 * Test reduce()
1681
 *
1682
 * @return void
1683
 */
1684
	public function testReduce() {
1685
		$data = self::articleData();
1686
 
1687
		$result = Hash::reduce($data, '{n}.Article.id', array($this, 'reduceCallback'));
1688
		$this->assertEquals(15, $result);
1689
	}
1690
 
1691
/**
1692
 * testing method for map callbacks.
1693
 *
1694
 * @param mixed $value
1695
 * @return mixed.
1696
 */
1697
	public function mapCallback($value) {
1698
		return $value * 2;
1699
	}
1700
 
1701
/**
1702
 * testing method for reduce callbacks.
1703
 *
1704
 * @param mixed $one
1705
 * @param mixed $two
1706
 * @return mixed.
1707
 */
1708
	public function reduceCallback($one, $two) {
1709
		return $one + $two;
1710
	}
1711
 
1712
/**
1713
 * test Hash nest with a normal model result set. For kicks rely on Hash nest detecting the key names
1714
 * automatically
1715
 *
1716
 * @return void
1717
 */
1718
	public function testNestModel() {
1719
		$input = array(
1720
			array(
1721
				'ModelName' => array(
1722
					'id' => 1,
1723
					'parent_id' => null
1724
				),
1725
			),
1726
			array(
1727
				'ModelName' => array(
1728
					'id' => 2,
1729
					'parent_id' => 1
1730
				),
1731
			),
1732
			array(
1733
				'ModelName' => array(
1734
					'id' => 3,
1735
					'parent_id' => 1
1736
				),
1737
			),
1738
			array(
1739
				'ModelName' => array(
1740
					'id' => 4,
1741
					'parent_id' => 1
1742
				),
1743
			),
1744
			array(
1745
				'ModelName' => array(
1746
					'id' => 5,
1747
					'parent_id' => 1
1748
				),
1749
			),
1750
			array(
1751
				'ModelName' => array(
1752
					'id' => 6,
1753
					'parent_id' => null
1754
				),
1755
			),
1756
			array(
1757
				'ModelName' => array(
1758
					'id' => 7,
1759
					'parent_id' => 6
1760
				),
1761
			),
1762
			array(
1763
				'ModelName' => array(
1764
					'id' => 8,
1765
					'parent_id' => 6
1766
				),
1767
			),
1768
			array(
1769
				'ModelName' => array(
1770
					'id' => 9,
1771
					'parent_id' => 6
1772
				),
1773
			),
1774
			array(
1775
				'ModelName' => array(
1776
					'id' => 10,
1777
					'parent_id' => 6
1778
				)
1779
			)
1780
		);
1781
		$expected = array(
1782
			array(
1783
				'ModelName' => array(
1784
					'id' => 1,
1785
					'parent_id' => null
1786
				),
1787
				'children' => array(
1788
					array(
1789
						'ModelName' => array(
1790
							'id' => 2,
1791
							'parent_id' => 1
1792
						),
1793
						'children' => array()
1794
					),
1795
					array(
1796
						'ModelName' => array(
1797
							'id' => 3,
1798
							'parent_id' => 1
1799
						),
1800
						'children' => array()
1801
					),
1802
					array(
1803
						'ModelName' => array(
1804
							'id' => 4,
1805
							'parent_id' => 1
1806
						),
1807
						'children' => array()
1808
					),
1809
					array(
1810
						'ModelName' => array(
1811
							'id' => 5,
1812
							'parent_id' => 1
1813
						),
1814
						'children' => array()
1815
					),
1816
 
1817
				)
1818
			),
1819
			array(
1820
				'ModelName' => array(
1821
					'id' => 6,
1822
					'parent_id' => null
1823
				),
1824
				'children' => array(
1825
					array(
1826
						'ModelName' => array(
1827
							'id' => 7,
1828
							'parent_id' => 6
1829
						),
1830
						'children' => array()
1831
					),
1832
					array(
1833
						'ModelName' => array(
1834
							'id' => 8,
1835
							'parent_id' => 6
1836
						),
1837
						'children' => array()
1838
					),
1839
					array(
1840
						'ModelName' => array(
1841
							'id' => 9,
1842
							'parent_id' => 6
1843
						),
1844
						'children' => array()
1845
					),
1846
					array(
1847
						'ModelName' => array(
1848
							'id' => 10,
1849
							'parent_id' => 6
1850
						),
1851
						'children' => array()
1852
					)
1853
				)
1854
			)
1855
		);
1856
		$result = Hash::nest($input);
1857
		$this->assertEquals($expected, $result);
1858
	}
1859
 
1860
/**
1861
 * test Hash nest with a normal model result set, and a nominated root id
1862
 *
1863
 * @return void
1864
 */
1865
	public function testNestModelExplicitRoot() {
1866
		$input = array(
1867
			array(
1868
				'ModelName' => array(
1869
					'id' => 1,
1870
					'parent_id' => null
1871
				),
1872
			),
1873
			array(
1874
				'ModelName' => array(
1875
					'id' => 2,
1876
					'parent_id' => 1
1877
				),
1878
			),
1879
			array(
1880
				'ModelName' => array(
1881
					'id' => 3,
1882
					'parent_id' => 1
1883
				),
1884
			),
1885
			array(
1886
				'ModelName' => array(
1887
					'id' => 4,
1888
					'parent_id' => 1
1889
				),
1890
			),
1891
			array(
1892
				'ModelName' => array(
1893
					'id' => 5,
1894
					'parent_id' => 1
1895
				),
1896
			),
1897
			array(
1898
				'ModelName' => array(
1899
					'id' => 6,
1900
					'parent_id' => null
1901
				),
1902
			),
1903
			array(
1904
				'ModelName' => array(
1905
					'id' => 7,
1906
					'parent_id' => 6
1907
				),
1908
			),
1909
			array(
1910
				'ModelName' => array(
1911
					'id' => 8,
1912
					'parent_id' => 6
1913
				),
1914
			),
1915
			array(
1916
				'ModelName' => array(
1917
					'id' => 9,
1918
					'parent_id' => 6
1919
				),
1920
			),
1921
			array(
1922
				'ModelName' => array(
1923
					'id' => 10,
1924
					'parent_id' => 6
1925
				)
1926
			)
1927
		);
1928
		$expected = array(
1929
			array(
1930
				'ModelName' => array(
1931
					'id' => 6,
1932
					'parent_id' => null
1933
				),
1934
				'children' => array(
1935
					array(
1936
						'ModelName' => array(
1937
							'id' => 7,
1938
							'parent_id' => 6
1939
						),
1940
						'children' => array()
1941
					),
1942
					array(
1943
						'ModelName' => array(
1944
							'id' => 8,
1945
							'parent_id' => 6
1946
						),
1947
						'children' => array()
1948
					),
1949
					array(
1950
						'ModelName' => array(
1951
							'id' => 9,
1952
							'parent_id' => 6
1953
						),
1954
						'children' => array()
1955
					),
1956
					array(
1957
						'ModelName' => array(
1958
							'id' => 10,
1959
							'parent_id' => 6
1960
						),
1961
						'children' => array()
1962
					)
1963
				)
1964
			)
1965
		);
1966
		$result = Hash::nest($input, array('root' => 6));
1967
		$this->assertEquals($expected, $result);
1968
	}
1969
 
1970
/**
1971
 * test Hash nest with a 1d array - this method should be able to handle any type of array input
1972
 *
1973
 * @return void
1974
 */
1975
	public function testNest1Dimensional() {
1976
		$input = array(
1977
			array(
1978
				'id' => 1,
1979
				'parent_id' => null
1980
			),
1981
			array(
1982
				'id' => 2,
1983
				'parent_id' => 1
1984
			),
1985
			array(
1986
				'id' => 3,
1987
				'parent_id' => 1
1988
			),
1989
			array(
1990
				'id' => 4,
1991
				'parent_id' => 1
1992
			),
1993
			array(
1994
				'id' => 5,
1995
				'parent_id' => 1
1996
			),
1997
			array(
1998
				'id' => 6,
1999
				'parent_id' => null
2000
			),
2001
			array(
2002
				'id' => 7,
2003
				'parent_id' => 6
2004
			),
2005
			array(
2006
				'id' => 8,
2007
				'parent_id' => 6
2008
			),
2009
			array(
2010
				'id' => 9,
2011
				'parent_id' => 6
2012
			),
2013
			array(
2014
				'id' => 10,
2015
				'parent_id' => 6
2016
			)
2017
		);
2018
		$expected = array(
2019
			array(
2020
				'id' => 1,
2021
				'parent_id' => null,
2022
				'children' => array(
2023
					array(
2024
						'id' => 2,
2025
						'parent_id' => 1,
2026
						'children' => array()
2027
					),
2028
					array(
2029
						'id' => 3,
2030
						'parent_id' => 1,
2031
						'children' => array()
2032
					),
2033
					array(
2034
						'id' => 4,
2035
						'parent_id' => 1,
2036
						'children' => array()
2037
					),
2038
					array(
2039
						'id' => 5,
2040
						'parent_id' => 1,
2041
						'children' => array()
2042
					),
2043
 
2044
				)
2045
			),
2046
			array(
2047
				'id' => 6,
2048
				'parent_id' => null,
2049
				'children' => array(
2050
					array(
2051
						'id' => 7,
2052
						'parent_id' => 6,
2053
						'children' => array()
2054
					),
2055
					array(
2056
						'id' => 8,
2057
						'parent_id' => 6,
2058
						'children' => array()
2059
					),
2060
					array(
2061
						'id' => 9,
2062
						'parent_id' => 6,
2063
						'children' => array()
2064
					),
2065
					array(
2066
						'id' => 10,
2067
						'parent_id' => 6,
2068
						'children' => array()
2069
					)
2070
				)
2071
			)
2072
		);
2073
		$result = Hash::nest($input, array('idPath' => '{n}.id', 'parentPath' => '{n}.parent_id'));
2074
		$this->assertEquals($expected, $result);
2075
	}
2076
 
2077
/**
2078
 * test Hash nest with no specified parent data.
2079
 *
2080
 * The result should be the same as the input.
2081
 * For an easier comparison, unset all the empty children arrays from the result
2082
 *
2083
 * @return void
2084
 */
2085
	public function testMissingParent() {
2086
		$input = array(
2087
			array(
2088
				'id' => 1,
2089
			),
2090
			array(
2091
				'id' => 2,
2092
			),
2093
			array(
2094
				'id' => 3,
2095
			),
2096
			array(
2097
				'id' => 4,
2098
			),
2099
			array(
2100
				'id' => 5,
2101
			),
2102
			array(
2103
				'id' => 6,
2104
			),
2105
			array(
2106
				'id' => 7,
2107
			),
2108
			array(
2109
				'id' => 8,
2110
			),
2111
			array(
2112
				'id' => 9,
2113
			),
2114
			array(
2115
				'id' => 10,
2116
			)
2117
		);
2118
 
2119
		$result = Hash::nest($input, array('idPath' => '{n}.id', 'parentPath' => '{n}.parent_id'));
2120
		foreach ($result as &$row) {
2121
			if (empty($row['children'])) {
2122
				unset($row['children']);
2123
			}
2124
		}
2125
		$this->assertEquals($input, $result);
2126
	}
2127
 
2128
/**
2129
 * testMergeDiff method
2130
 *
2131
 * @return void
2132
 */
2133
	public function testMergeDiff() {
2134
		$first = array(
2135
			'ModelOne' => array(
2136
				'id' => 1001,
2137
				'field_one' => 'a1.m1.f1',
2138
				'field_two' => 'a1.m1.f2'
2139
			)
2140
		);
2141
		$second = array(
2142
			'ModelTwo' => array(
2143
				'id' => 1002,
2144
				'field_one' => 'a2.m2.f1',
2145
				'field_two' => 'a2.m2.f2'
2146
			)
2147
		);
2148
		$result = Hash::mergeDiff($first, $second);
2149
		$this->assertEquals($result, $first + $second);
2150
 
2151
		$result = Hash::mergeDiff($first, array());
2152
		$this->assertEquals($result, $first);
2153
 
2154
		$result = Hash::mergeDiff(array(), $first);
2155
		$this->assertEquals($result, $first);
2156
 
2157
		$third = array(
2158
			'ModelOne' => array(
2159
				'id' => 1003,
2160
				'field_one' => 'a3.m1.f1',
2161
				'field_two' => 'a3.m1.f2',
2162
				'field_three' => 'a3.m1.f3'
2163
			)
2164
		);
2165
		$result = Hash::mergeDiff($first, $third);
2166
		$expected = array(
2167
			'ModelOne' => array(
2168
				'id' => 1001,
2169
				'field_one' => 'a1.m1.f1',
2170
				'field_two' => 'a1.m1.f2',
2171
				'field_three' => 'a3.m1.f3'
2172
			)
2173
		);
2174
		$this->assertEquals($expected, $result);
2175
 
2176
		$first = array(
2177
 
2178
			1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2'))
2179
		);
2180
		$second = array(
2181
 
2182
			1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2'))
2183
		);
2184
 
2185
		$result = Hash::mergeDiff($first, $second);
2186
		$this->assertEquals($result, $first);
2187
 
2188
		$third = array(
2189
 
2190
				'ModelThree' => array(
2191
					'id' => 1003,
2192
					'field_one' => 's3.0.m3.f1',
2193
					'field_two' => 's3.0.m3.f2'
2194
				)
2195
			)
2196
		);
2197
 
2198
		$result = Hash::mergeDiff($first, $third);
2199
		$expected = array(
2200
 
2201
				'ModelOne' => array(
2202
					'id' => 1001,
2203
					'field_one' => 's1.0.m1.f1',
2204
					'field_two' => 's1.0.m1.f2'
2205
				),
2206
				'ModelThree' => array(
2207
					'id' => 1003,
2208
					'field_one' => 's3.0.m3.f1',
2209
					'field_two' => 's3.0.m3.f2'
2210
				)
2211
			),
2212
			1 => array(
2213
				'ModelTwo' => array(
2214
					'id' => 1002,
2215
					'field_one' => 's1.1.m2.f2',
2216
					'field_two' => 's1.1.m2.f2'
2217
				)
2218
			)
2219
		);
2220
		$this->assertEquals($expected, $result);
2221
 
2222
		$result = Hash::mergeDiff($first, null);
2223
		$this->assertEquals($result, $first);
2224
 
2225
		$result = Hash::mergeDiff($first, $second);
2226
		$this->assertEquals($result, $first + $second);
2227
	}
2228
 
2229
/**
2230
 * Tests Hash::expand
2231
 *
2232
 * @return void
2233
 */
2234
	public function testExpand() {
2235
		$data = array('My', 'Array', 'To', 'Flatten');
2236
		$flat = Hash::flatten($data);
2237
		$result = Hash::expand($flat);
2238
		$this->assertEquals($data, $result);
2239
 
2240
		$data = array(
2241
			'0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
2242
			'0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
2243
			'1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
2244
			'1.Author.user' => 'larry', '1.Author.password' => null
2245
		);
2246
		$result = Hash::expand($data);
2247
		$expected = array(
2248
			array(
2249
				'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
2250
				'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
2251
			),
2252
			array(
2253
				'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
2254
				'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
2255
			)
2256
		);
2257
		$this->assertEquals($expected, $result);
2258
 
2259
		$data = array(
2260
			'0/Post/id' => 1,
2261
			'0/Post/name' => 'test post'
2262
		);
2263
		$result = Hash::expand($data, '/');
2264
		$expected = array(
2265
			array(
2266
				'Post' => array(
2267
					'id' => 1,
2268
					'name' => 'test post'
2269
				)
2270
			)
2271
		);
2272
		$this->assertEquals($expected, $result);
2273
 
2274
		$data = array('a.b.100.a' => null, 'a.b.200.a' => null);
2275
		$expected = array(
2276
			'a' => array(
2277
				'b' => array(
2278
					100 => array('a' => null),
2279
					200 => array('a' => null)
2280
				)
2281
			)
2282
		);
2283
		$result = Hash::expand($data);
2284
		$this->assertEquals($expected, $result);
2285
	}
2286
 
2287
/**
2288
 * Test that flattening a large complex set doesn't loop forever.
2289
 *
2290
 * @return void
2291
 */
2292
	public function testFlattenInfiniteLoop() {
2293
		$data = array(
2294
			'Order.ASI' => '0',
2295
			'Order.Accounting' => '0',
2296
			'Order.Admin' => '0',
2297
			'Order.Art' => '0',
2298
			'Order.ArtChecker' => '0',
2299
			'Order.Canned' => '0',
2300
			'Order.Customer_Tags' => '',
2301
			'Order.Embroidery' => '0',
2302
			'Order.Item.0.Product.style_number' => 'a11222',
2303
			'Order.Item.0.Product.slug' => 'a11222',
2304
			'Order.Item.0.Product._id' => '4ff8b8d3d7bbe8ad30000000',
2305
			'Order.Item.0.Product.Color.slug' => 'kelly_green',
2306
			'Order.Item.0.Product.ColorSizes.0.Color.color' => 'Sport Grey',
2307
			'Order.Item.0.Product.ColorSizes.0.Color.slug' => 'sport_grey',
2308
			'Order.Item.0.Product.ColorSizes.1.Color.color' => 'Kelly Green',
2309
			'Order.Item.0.Product.ColorSizes.1.Color.slug' => 'kelly_green',
2310
			'Order.Item.0.Product.ColorSizes.2.Color.color' => 'Orange',
2311
			'Order.Item.0.Product.ColorSizes.2.Color.slug' => 'orange',
2312
			'Order.Item.0.Product.ColorSizes.3.Color.color' => 'Yellow Haze',
2313
			'Order.Item.0.Product.ColorSizes.3.Color.slug' => 'yellow_haze',
2314
			'Order.Item.0.Product.brand' => 'OUTER BANKS',
2315
			'Order.Item.0.Product.style' => 'T-shirt',
2316
			'Order.Item.0.Product.description' => 'uhiuhuih oin ooi ioo ioio',
2317
			'Order.Item.0.Product.sizes.0.Size.qty' => '',
2318
			'Order.Item.0.Product.sizes.0.Size.size' => '0-3mo',
2319
			'Order.Item.0.Product.sizes.0.Size.id' => '38',
2320
			'Order.Item.0.Product.sizes.1.Size.qty' => '',
2321
			'Order.Item.0.Product.sizes.1.Size.size' => '3-6mo',
2322
			'Order.Item.0.Product.sizes.1.Size.id' => '39',
2323
			'Order.Item.0.Product.sizes.2.Size.qty' => '78',
2324
			'Order.Item.0.Product.sizes.2.Size.size' => '6-9mo',
2325
			'Order.Item.0.Product.sizes.2.Size.id' => '40',
2326
			'Order.Item.0.Product.sizes.3.Size.qty' => '',
2327
			'Order.Item.0.Product.sizes.3.Size.size' => '6-12mo',
2328
			'Order.Item.0.Product.sizes.3.Size.id' => '41',
2329
			'Order.Item.0.Product.sizes.4.Size.qty' => '',
2330
			'Order.Item.0.Product.sizes.4.Size.size' => '12-18mo',
2331
			'Order.Item.0.Product.sizes.4.Size.id' => '42',
2332
			'Order.Item.0.Art.imprint_locations.0.id' => 2,
2333
			'Order.Item.0.Art.imprint_locations.0.name' => 'Left Chest',
2334
			'Order.Item.0.Art.imprint_locations.0.imprint_type.id' => 7,
2335
			'Order.Item.0.Art.imprint_locations.0.imprint_type.type' => 'Embroidery',
2336
			'Order.Item.0.Art.imprint_locations.0.art' => '',
2337
			'Order.Item.0.Art.imprint_locations.0.num_colors' => 3,
2338
			'Order.Item.0.Art.imprint_locations.0.description' => 'Wooo! This is Embroidery!!',
2339
			'Order.Item.0.Art.imprint_locations.0.lines.0' => 'Platen',
2340
			'Order.Item.0.Art.imprint_locations.0.lines.1' => 'Logo',
2341
			'Order.Item.0.Art.imprint_locations.0.height' => 4,
2342
			'Order.Item.0.Art.imprint_locations.0.width' => 5,
2343
			'Order.Item.0.Art.imprint_locations.0.stitch_density' => 'Light',
2344
			'Order.Item.0.Art.imprint_locations.0.metallic_thread' => true,
2345
			'Order.Item.0.Art.imprint_locations.1.id' => 4,
2346
			'Order.Item.0.Art.imprint_locations.1.name' => 'Full Back',
2347
			'Order.Item.0.Art.imprint_locations.1.imprint_type.id' => 6,
2348
			'Order.Item.0.Art.imprint_locations.1.imprint_type.type' => 'Screenprinting',
2349
			'Order.Item.0.Art.imprint_locations.1.art' => '',
2350
			'Order.Item.0.Art.imprint_locations.1.num_colors' => 3,
2351
			'Order.Item.0.Art.imprint_locations.1.description' => 'Wooo! This is Screenprinting!!',
2352
			'Order.Item.0.Art.imprint_locations.1.lines.0' => 'Platen',
2353
			'Order.Item.0.Art.imprint_locations.1.lines.1' => 'Logo',
2354
			'Order.Item.0.Art.imprint_locations.2.id' => 26,
2355
			'Order.Item.0.Art.imprint_locations.2.name' => 'HS - JSY Name Below',
2356
			'Order.Item.0.Art.imprint_locations.2.imprint_type.id' => 9,
2357
			'Order.Item.0.Art.imprint_locations.2.imprint_type.type' => 'Names',
2358
			'Order.Item.0.Art.imprint_locations.2.description' => 'Wooo! This is Names!!',
2359
			'Order.Item.0.Art.imprint_locations.2.sizes.S.0.active' => 1,
2360
			'Order.Item.0.Art.imprint_locations.2.sizes.S.0.name' => 'Benjamin Talavera',
2361
			'Order.Item.0.Art.imprint_locations.2.sizes.S.0.color' => 'Red',
2362
			'Order.Item.0.Art.imprint_locations.2.sizes.S.0.height' => '3',
2363
			'Order.Item.0.Art.imprint_locations.2.sizes.S.0.layout' => 'Arched',
2364
			'Order.Item.0.Art.imprint_locations.2.sizes.S.0.style' => 'Classic',
2365
			'Order.Item.0.Art.imprint_locations.2.sizes.S.1.active' => 0,
2366
			'Order.Item.0.Art.imprint_locations.2.sizes.S.1.name' => 'Rishi Narayan',
2367
			'Order.Item.0.Art.imprint_locations.2.sizes.S.1.color' => 'Cardinal',
2368
			'Order.Item.0.Art.imprint_locations.2.sizes.S.1.height' => '4',
2369
			'Order.Item.0.Art.imprint_locations.2.sizes.S.1.layout' => 'Straight',
2370
			'Order.Item.0.Art.imprint_locations.2.sizes.S.1.style' => 'Team US',
2371
			'Order.Item.0.Art.imprint_locations.2.sizes.M.0.active' => 1,
2372
			'Order.Item.0.Art.imprint_locations.2.sizes.M.0.name' => 'Brandon Plasters',
2373
			'Order.Item.0.Art.imprint_locations.2.sizes.M.0.color' => 'Red',
2374
			'Order.Item.0.Art.imprint_locations.2.sizes.M.0.height' => '3',
2375
			'Order.Item.0.Art.imprint_locations.2.sizes.M.0.layout' => 'Arched',
2376
			'Order.Item.0.Art.imprint_locations.2.sizes.M.0.style' => 'Classic',
2377
			'Order.Item.0.Art.imprint_locations.2.sizes.M.1.active' => 0,
2378
			'Order.Item.0.Art.imprint_locations.2.sizes.M.1.name' => 'Andrew Reed',
2379
			'Order.Item.0.Art.imprint_locations.2.sizes.M.1.color' => 'Cardinal',
2380
			'Order.Item.0.Art.imprint_locations.2.sizes.M.1.height' => '4',
2381
			'Order.Item.0.Art.imprint_locations.2.sizes.M.1.layout' => 'Straight',
2382
			'Order.Item.0.Art.imprint_locations.2.sizes.M.1.style' => 'Team US',
2383
			'Order.Job.0._id' => 'job-1',
2384
			'Order.Job.0.type' => 'screenprinting',
2385
			'Order.Job.0.postPress' => 'job-2',
2386
			'Order.Job.1._id' => 'job-2',
2387
			'Order.Job.1.type' => 'embroidery',
2388
			'Order.Postpress' => '0',
2389
			'Order.PriceAdjustment.0._id' => 'price-adjustment-1',
2390
			'Order.PriceAdjustment.0.adjustment' => '-20',
2391
			'Order.PriceAdjustment.0.adjustment_type' => 'percent',
2392
			'Order.PriceAdjustment.0.type' => 'grand_total',
2393
			'Order.PriceAdjustment.1.adjustment' => '20',
2394
			'Order.PriceAdjustment.1.adjustment_type' => 'flat',
2395
			'Order.PriceAdjustment.1.min-items' => '10',
2396
			'Order.PriceAdjustment.1.type' => 'min-items',
2397
			'Order.PriceAdjustment.1._id' => 'another-test-adjustment',
2398
			'Order.Purchasing' => '0',
2399
			'Order.QualityControl' => '0',
2400
			'Order.Receiving' => '0',
2401
			'Order.ScreenPrinting' => '0',
2402
			'Order.Stage.art_approval' => 0,
2403
			'Order.Stage.draft' => 1,
2404
			'Order.Stage.quote' => 1,
2405
			'Order.Stage.order' => 1,
2406
			'Order.StoreLiason' => '0',
2407
			'Order.Tag_UI_Email' => '',
2408
			'Order.Tags' => '',
2409
			'Order._id' => 'test-2',
2410
			'Order.add_print_location' => '',
2411
			'Order.created' => '2011-Dec-29 05:40:18',
2412
			'Order.force_admin' => '0',
2413
			'Order.modified' => '2012-Jul-25 01:24:49',
2414
			'Order.name' => 'towering power',
2415
			'Order.order_id' => '135961',
2416
			'Order.slug' => 'test-2',
2417
			'Order.title' => 'test job 2',
2418
			'Order.type' => 'ttt'
2419
		);
2420
		$expanded = Hash::expand($data);
2421
		$flattened = Hash::flatten($expanded);
2422
		$this->assertEquals($data, $flattened);
2423
	}
2424
 
2425
}