統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Utility / SetTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (111.17 KB)

1 635eef61 spyder1211
<?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($expected, Set::merge($a, $b, array(), $c));
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
                        0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
233
                        1 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay')))
234
                );
235
                $b = array(
236
                        0 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay'))),
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
                        0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
245
                        1 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay')))
246
                );
247
                $a = array(
248
                        0 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay'))),
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
                        0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
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
                        0 => array('Person' => array('name' => 'Adam'), 'Friend' => array(array('name' => 'Bob'))),
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
                        0 => array('Person' => array('name' => 'Jeff')),
300
                        1 => array('Shirt' => array('color' => 'black'))
301
                );
302
                $b = array(
303
                        0 => array('Shirt' => array('color' => 'black')),
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
                        0 => array('class' => 605, 'test2' => 3),
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
                        0 => array(
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
                                0 => array(
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
                                0 => array(
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
                                0 => array(
954
                                        'value' => 50
955
                                )
956
                        ),
957
                        array(
958
                                'Category' => array(
959
                                        'id' => 2,
960
                                        'name' => 'Second'
961
                                ),
962
                                0 => array(
963
                                        'value' => 60
964
                                )
965
                        )
966
                );
967
                $expected = array(
968
                        array(
969
                                'Category' => array(
970
                                        'id' => 1,
971
                                        'name' => 'First'
972
                                ),
973
                                0 => array(
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
                                0 => array('id' => 1),
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
                        0 => 'b240'
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
 * @return void
1357
 */
1358
        public function testExtractMatchesNull() {
1359
                $data = array(
1360
                        'Country' => array(
1361
                                array('name' => 'Canada'),
1362
                                array('name' => 'Australia'),
1363
                                array('name' => null),
1364
                        )
1365
                );
1366
                $result = Set::extract('/Country[name=/Canada|^$/]', $data);
1367
                $expected = array(
1368
                        array(
1369
                                'Country' => array(
1370
                                        'name' => 'Canada',
1371
                                ),
1372
                        ),
1373
                        array(
1374
                                'Country' => array(
1375
                                        'name' => null,
1376
                                ),
1377
                        ),
1378
                );
1379
                $this->assertEquals($expected, $result);
1380
        }
1381
1382
/**
1383
 * testMatches method
1384
 *
1385
 * @return void
1386
 */
1387
        public function testMatches() {
1388
                $a = array(
1389
                        array('Article' => array('id' => 1, 'title' => 'Article 1')),
1390
                        array('Article' => array('id' => 2, 'title' => 'Article 2')),
1391
                        array('Article' => array('id' => 3, 'title' => 'Article 3'))
1392
                );
1393
1394
                $this->assertTrue(Set::matches(array('id=2'), $a[1]['Article']));
1395
                $this->assertFalse(Set::matches(array('id>2'), $a[1]['Article']));
1396
                $this->assertTrue(Set::matches(array('id>=2'), $a[1]['Article']));
1397
                $this->assertFalse(Set::matches(array('id>=3'), $a[1]['Article']));
1398
                $this->assertTrue(Set::matches(array('id<=2'), $a[1]['Article']));
1399
                $this->assertFalse(Set::matches(array('id<2'), $a[1]['Article']));
1400
                $this->assertTrue(Set::matches(array('id>1'), $a[1]['Article']));
1401
                $this->assertTrue(Set::matches(array('id>1', 'id<3', 'id!=0'), $a[1]['Article']));
1402
1403
                $this->assertTrue(Set::matches(array('3'), null, 3));
1404
                $this->assertTrue(Set::matches(array('5'), null, 5));
1405
1406
                $this->assertTrue(Set::matches(array('id'), $a[1]['Article']));
1407
                $this->assertTrue(Set::matches(array('id', 'title'), $a[1]['Article']));
1408
                $this->assertFalse(Set::matches(array('non-existant'), $a[1]['Article']));
1409
1410
                $this->assertTrue(Set::matches('/Article[id=2]', $a));
1411
                $this->assertFalse(Set::matches('/Article[id=4]', $a));
1412
                $this->assertTrue(Set::matches(array(), $a));
1413
1414
                $r = array(
1415
                        'Attachment' => array(
1416
                                'keep' => array()
1417
                        ),
1418
                        'Comment' => array(
1419
                                'keep' => array(
1420
                                        'Attachment' => array(
1421
                                                'fields' => array(
1422
                                                        0 => 'attachment',
1423
                                                ),
1424
                                        ),
1425
                                )
1426
                        ),
1427
                        'User' => array(
1428
                                'keep' => array()
1429
                        ),
1430
                        'Article' => array(
1431
                                'keep' => array(
1432
                                        'Comment' => array(
1433
                                                'fields' => array(
1434
                                                        0 => 'comment',
1435
                                                        1 => 'published',
1436
                                                ),
1437
                                        ),
1438
                                        'User' => array(
1439
                                                'fields' => array(
1440
                                                        0 => 'user',
1441
                                                ),
1442
                                        ),
1443
                                )
1444
                        )
1445
                );
1446
1447
                $this->assertTrue(Set::matches('/Article/keep/Comment', $r));
1448
                $this->assertEquals(array('comment', 'published'), Set::extract('/Article/keep/Comment/fields', $r));
1449
                $this->assertEquals(array('user'), Set::extract('/Article/keep/User/fields', $r));
1450
        }
1451
1452
/**
1453
 * testSetExtractReturnsEmptyArray method
1454
 *
1455
 * @return void
1456
 */
1457
        public function testSetExtractReturnsEmptyArray() {
1458
                $this->assertEquals(Set::extract(array(), '/Post/id'), array());
1459
1460
                $this->assertEquals(Set::extract('/Post/id', array()), array());
1461
1462
                $this->assertEquals(Set::extract('/Post/id', array(
1463
                        array('Post' => array('name' => 'bob')),
1464
                        array('Post' => array('name' => 'jim'))
1465
                )), array());
1466
1467
                $this->assertEquals(Set::extract(array(), 'Message.flash'), null);
1468
        }
1469
1470
/**
1471
 * testClassicExtract method
1472
 *
1473
 * @return void
1474
 */
1475
        public function testClassicExtract() {
1476
                $a = array(
1477
                        array('Article' => array('id' => 1, 'title' => 'Article 1')),
1478
                        array('Article' => array('id' => 2, 'title' => 'Article 2')),
1479
                        array('Article' => array('id' => 3, 'title' => 'Article 3'))
1480
                );
1481
1482
                $result = Set::extract($a, '{n}.Article.id');
1483
                $expected = array(1, 2, 3);
1484
                $this->assertEquals($expected, $result);
1485
1486
                $result = Set::extract($a, '{n}.Article.title');
1487
                $expected = array('Article 1', 'Article 2', 'Article 3');
1488
                $this->assertEquals($expected, $result);
1489
1490
                $result = Set::extract($a, '1.Article.title');
1491
                $expected = 'Article 2';
1492
                $this->assertEquals($expected, $result);
1493
1494
                $result = Set::extract($a, '3.Article.title');
1495
                $expected = null;
1496
                $this->assertEquals($expected, $result);
1497
1498
                $a = array(
1499
                        array(
1500
                                'Article' => array('id' => 1, 'title' => 'Article 1',
1501
                                'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
1502
                        ),
1503
                        array(
1504
                                'Article' => array('id' => 2, 'title' => 'Article 2',
1505
                                'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
1506
                        ),
1507
                        array(
1508
                                'Article' => array('id' => 3, 'title' => 'Article 3',
1509
                                'User' => array('id' => 2, 'username' => 'phpnut'))
1510
                        )
1511
                );
1512
1513
                $result = Set::extract($a, '{n}.Article.User.username');
1514
                $expected = array('mariano.iglesias', 'mariano.iglesias', 'phpnut');
1515
                $this->assertEquals($expected, $result);
1516
1517
                $a = array(
1518
                        array(
1519
                                'Article' => array(
1520
                                        'id' => 1, 'title' => 'Article 1',
1521
                                        'Comment' => array(
1522
                                                array('id' => 10, 'title' => 'Comment 10'),
1523
                                                array('id' => 11, 'title' => 'Comment 11'),
1524
                                                array('id' => 12, 'title' => 'Comment 12')
1525
                                        )
1526
                                )
1527
                        ),
1528
                        array(
1529
                                'Article' => array(
1530
                                        'id' => 2, 'title' => 'Article 2',
1531
                                        'Comment' => array(
1532
                                                array('id' => 13, 'title' => 'Comment 13'),
1533
                                                array('id' => 14, 'title' => 'Comment 14')
1534
                                        )
1535
                                )
1536
                        ),
1537
                        array('Article' => array('id' => 3, 'title' => 'Article 3'))
1538
                );
1539
1540
                $result = Set::extract($a, '{n}.Article.Comment.{n}.id');
1541
                $expected = array(array(10, 11, 12), array(13, 14), null);
1542
                $this->assertEquals($expected, $result);
1543
1544
                $result = Set::extract($a, '{n}.Article.Comment.{n}.title');
1545
                $expected = array(
1546
                        array('Comment 10', 'Comment 11', 'Comment 12'),
1547
                        array('Comment 13', 'Comment 14'),
1548
                        null
1549
                );
1550
                $this->assertEquals($expected, $result);
1551
1552
                $a = array(array('1day' => '20 sales'), array('1day' => '2 sales'));
1553
                $result = Set::extract($a, '{n}.1day');
1554
                $expected = array('20 sales', '2 sales');
1555
                $this->assertEquals($expected, $result);
1556
1557
                $a = array(
1558
                        'pages' => array('name' => 'page'),
1559
                        'fruites' => array('name' => 'fruit'),
1560
                        0 => array('name' => 'zero')
1561
                );
1562
                $result = Set::extract($a, '{s}.name');
1563
                $expected = array('page', 'fruit');
1564
                $this->assertEquals($expected, $result);
1565
1566
                $a = array(
1567
                        0 => array('pages' => array('name' => 'page')),
1568
                        1 => array('fruites' => array('name' => 'fruit')),
1569
                        'test' => array(array('name' => 'jippi')),
1570
                        'dot.test' => array(array('name' => 'jippi'))
1571
                );
1572
1573
                $result = Set::extract($a, '{n}.{s}.name');
1574
                $expected = array(0 => array('page'), 1 => array('fruit'));
1575
                $this->assertEquals($expected, $result);
1576
1577
                $result = Set::extract($a, '{s}.{n}.name');
1578
                $expected = array(array('jippi'), array('jippi'));
1579
                $this->assertEquals($expected, $result);
1580
1581
                $result = Set::extract($a, '{\w+}.{\w+}.name');
1582
                $expected = array(
1583
                        array('pages' => 'page'),
1584
                        array('fruites' => 'fruit'),
1585
                        'test' => array('jippi'),
1586
                        'dot.test' => array('jippi')
1587
                );
1588
                $this->assertEquals($expected, $result);
1589
1590
                $result = Set::extract($a, '{\d+}.{\w+}.name');
1591
                $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
1592
                $this->assertEquals($expected, $result);
1593
1594
                $result = Set::extract($a, '{n}.{\w+}.name');
1595
                $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
1596
                $this->assertEquals($expected, $result);
1597
1598
                $result = Set::extract($a, '{s}.{\d+}.name');
1599
                $expected = array(array('jippi'), array('jippi'));
1600
                $this->assertEquals($expected, $result);
1601
1602
                $result = Set::extract($a, '{s}');
1603
                $expected = array(array(array('name' => 'jippi')), array(array('name' => 'jippi')));
1604
                $this->assertEquals($expected, $result);
1605
1606
                $result = Set::extract($a, '{[a-z]}');
1607
                $expected = array(
1608
                        'test' => array(array('name' => 'jippi')),
1609
                        'dot.test' => array(array('name' => 'jippi'))
1610
                );
1611
                $this->assertEquals($expected, $result);
1612
1613
                $result = Set::extract($a, '{dot\.test}.{n}');
1614
                $expected = array('dot.test' => array(array('name' => 'jippi')));
1615
                $this->assertEquals($expected, $result);
1616
1617
                $a = new stdClass();
1618
                $a->articles = array(
1619
                        array('Article' => array('id' => 1, 'title' => 'Article 1')),
1620
                        array('Article' => array('id' => 2, 'title' => 'Article 2')),
1621
                        array('Article' => array('id' => 3, 'title' => 'Article 3'))
1622
                );
1623
1624
                $result = Set::extract($a, 'articles.{n}.Article.id');
1625
                $expected = array(1, 2, 3);
1626
                $this->assertEquals($expected, $result);
1627
1628
                $result = Set::extract($a, 'articles.{n}.Article.title');
1629
                $expected = array('Article 1', 'Article 2', 'Article 3');
1630
                $this->assertEquals($expected, $result);
1631
1632
                $a = new ArrayObject();
1633
                $a['articles'] = array(
1634
                        array('Article' => array('id' => 1, 'title' => 'Article 1')),
1635
                        array('Article' => array('id' => 2, 'title' => 'Article 2')),
1636
                        array('Article' => array('id' => 3, 'title' => 'Article 3'))
1637
                );
1638
1639
                $result = Set::extract($a, 'articles.{n}.Article.id');
1640
                $expected = array(1, 2, 3);
1641
                $this->assertEquals($expected, $result);
1642
1643
                $result = Set::extract($a, 'articles.{n}.Article.title');
1644
                $expected = array('Article 1', 'Article 2', 'Article 3');
1645
                $this->assertEquals($expected, $result);
1646
1647
                $result = Set::extract($a, 'articles.0.Article.title');
1648
                $expected = 'Article 1';
1649
                $this->assertEquals($expected, $result);
1650
        }
1651
1652
/**
1653
 * test classicExtract with keys that exceed 32bit max int.
1654
 *
1655
 * @return void
1656
 */
1657
        public function testClassicExtractMaxInt() {
1658
                $data = array(
1659
                        'Data' => array(
1660
                                '13376924712' => 'abc'
1661
                        )
1662
                );
1663
                $this->assertEquals('abc', Set::classicExtract($data, 'Data.13376924712'));
1664
        }
1665
1666
/**
1667
 * testInsert method
1668
 *
1669
 * @see Hash tests, as Set::insert() is just a proxy.
1670
 * @return void
1671
 */
1672
        public function testInsert() {
1673
                $a = array(
1674
                        'pages' => array('name' => 'page')
1675
                );
1676
1677
                $result = Set::insert($a, 'files', array('name' => 'files'));
1678
                $expected = array(
1679
                        'pages' => array('name' => 'page'),
1680
                        'files' => array('name' => 'files')
1681
                );
1682
                $this->assertEquals($expected, $result);
1683
        }
1684
1685
/**
1686
 * testRemove method
1687
 *
1688
 * @return void
1689
 */
1690
        public function testRemove() {
1691
                $a = array(
1692
                        'pages' => array('name' => 'page'),
1693
                        'files' => array('name' => 'files')
1694
                );
1695
1696
                $result = Set::remove($a, 'files');
1697
                $expected = array(
1698
                        'pages' => array('name' => 'page')
1699
                );
1700
                $this->assertEquals($expected, $result);
1701
        }
1702
1703
/**
1704
 * testCheck method
1705
 *
1706
 * @return void
1707
 */
1708
        public function testCheck() {
1709
                $set = array(
1710
                        'My Index 1' => array('First' => 'The first item')
1711
                );
1712
                $this->assertTrue(Set::check($set, 'My Index 1.First'));
1713
                $this->assertTrue(Set::check($set, 'My Index 1'));
1714
                $this->assertEquals(Set::check($set, array()), $set);
1715
1716
                $set = array(
1717
                        'My Index 1' => array('First' => array('Second' => array('Third' => array('Fourth' => 'Heavy. Nesting.'))))
1718
                );
1719
                $this->assertTrue(Set::check($set, 'My Index 1.First.Second'));
1720
                $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third'));
1721
                $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third.Fourth'));
1722
                $this->assertFalse(Set::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
1723
        }
1724
1725
/**
1726
 * testWritingWithFunkyKeys method
1727
 *
1728
 * @return void
1729
 */
1730
        public function testWritingWithFunkyKeys() {
1731
                $set = Set::insert(array(), 'Session Test', "test");
1732
                $this->assertEquals('test', Set::extract($set, 'Session Test'));
1733
1734
                $set = Set::remove($set, 'Session Test');
1735
                $this->assertFalse(Set::check($set, 'Session Test'));
1736
1737
                $expected = array('Session Test' => array('Test Case' => 'test'));
1738
                $this->assertEquals($expected, Set::insert(array(), 'Session Test.Test Case', "test"));
1739
                $this->assertTrue(Set::check($expected, 'Session Test.Test Case'));
1740
        }
1741
1742
/**
1743
 * testDiff method
1744
 *
1745
 * @return void
1746
 */
1747
        public function testDiff() {
1748
                $a = array(
1749
                        0 => array('name' => 'main'),
1750
                        1 => array('name' => 'about')
1751
                );
1752
                $b = array(
1753
                        0 => array('name' => 'main'),
1754
                        1 => array('name' => 'about'),
1755
                        2 => array('name' => 'contact')
1756
                );
1757
1758
                $result = Set::diff($a, $b);
1759
                $expected = array(
1760
                        2 => array('name' => 'contact')
1761
                );
1762
                $this->assertEquals($expected, $result);
1763
1764
                $result = Set::diff($a, array());
1765
                $expected = $a;
1766
                $this->assertEquals($expected, $result);
1767
1768
                $result = Set::diff(array(), $b);
1769
                $expected = $b;
1770
                $this->assertEquals($expected, $result);
1771
1772
                $b = array(
1773
                        0 => array('name' => 'me'),
1774
                        1 => array('name' => 'about')
1775
                );
1776
1777
                $result = Set::diff($a, $b);
1778
                $expected = array(
1779
                        0 => array('name' => 'main')
1780
                );
1781
                $this->assertEquals($expected, $result);
1782
1783
                $a = array();
1784
                $b = array('name' => 'bob', 'address' => 'home');
1785
                $result = Set::diff($a, $b);
1786
                $this->assertEquals($b, $result);
1787
1788
                $a = array('name' => 'bob', 'address' => 'home');
1789
                $b = array();
1790
                $result = Set::diff($a, $b);
1791
                $this->assertEquals($a, $result);
1792
1793
                $a = array('key' => true, 'another' => false, 'name' => 'me');
1794
                $b = array('key' => 1, 'another' => 0);
1795
                $expected = array('name' => 'me');
1796
                $result = Set::diff($a, $b);
1797
                $this->assertEquals($expected, $result);
1798
1799
                $a = array('key' => 'value', 'another' => null, 'name' => 'me');
1800
                $b = array('key' => 'differentValue', 'another' => null);
1801
                $expected = array('key' => 'value', 'name' => 'me');
1802
                $result = Set::diff($a, $b);
1803
                $this->assertEquals($expected, $result);
1804
1805
                $a = array('key' => 'value', 'another' => null, 'name' => 'me');
1806
                $b = array('key' => 'differentValue', 'another' => 'value');
1807
                $expected = array('key' => 'value', 'another' => null, 'name' => 'me');
1808
                $result = Set::diff($a, $b);
1809
                $this->assertEquals($expected, $result);
1810
1811
                $a = array('key' => 'value', 'another' => null, 'name' => 'me');
1812
                $b = array('key' => 'differentValue', 'another' => 'value');
1813
                $expected = array('key' => 'differentValue', 'another' => 'value', 'name' => 'me');
1814
                $result = Set::diff($b, $a);
1815
                $this->assertEquals($expected, $result);
1816
1817
                $a = array('key' => 'value', 'another' => null, 'name' => 'me');
1818
                $b = array(0 => 'differentValue', 1 => 'value');
1819
                $expected = $a + $b;
1820
                $result = Set::diff($a, $b);
1821
                $this->assertEquals($expected, $result);
1822
        }
1823
1824
/**
1825
 * testContains method
1826
 *
1827
 * @return void
1828
 */
1829
        public function testContains() {
1830
                $a = array(
1831
                        0 => array('name' => 'main'),
1832
                        1 => array('name' => 'about')
1833
                );
1834
                $b = array(
1835
                        0 => array('name' => 'main'),
1836
                        1 => array('name' => 'about'),
1837
                        2 => array('name' => 'contact'),
1838
                        'a' => 'b'
1839
                );
1840
1841
                $this->assertTrue(Set::contains($a, $a));
1842
                $this->assertFalse(Set::contains($a, $b));
1843
                $this->assertTrue(Set::contains($b, $a));
1844
        }
1845
1846
/**
1847
 * testCombine method
1848
 *
1849
 * @return void
1850
 */
1851
        public function testCombine() {
1852
                $result = Set::combine(array(), '{n}.User.id', '{n}.User.Data');
1853
                $this->assertTrue(empty($result));
1854
                $result = Set::combine('', '{n}.User.id', '{n}.User.Data');
1855
                $this->assertTrue(empty($result));
1856
1857
                $a = array(
1858
                        array('User' => array('id' => 2, 'group_id' => 1,
1859
                                'Data' => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'))),
1860
                        array('User' => array('id' => 14, 'group_id' => 2,
1861
                                'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
1862
                        array('User' => array('id' => 25, 'group_id' => 1,
1863
                                'Data' => array('user' => 'gwoo', 'name' => 'The Gwoo'))));
1864
                $result = Set::combine($a, '{n}.User.id');
1865
                $expected = array(2 => null, 14 => null, 25 => null);
1866
                $this->assertEquals($expected, $result);
1867
1868
                $result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
1869
                $expected = array(2 => null, 14 => null, 25 => null);
1870
                $this->assertEquals($expected, $result);
1871
1872
                $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
1873
                $expected = array(
1874
                        2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1875
                        14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
1876
                        25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
1877
                $this->assertEquals($expected, $result);
1878
1879
                $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
1880
                $expected = array(
1881
                        2 => 'Mariano Iglesias',
1882
                        14 => 'Larry E. Masters',
1883
                        25 => 'The Gwoo');
1884
                $this->assertEquals($expected, $result);
1885
1886
                $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
1887
                $expected = array(
1888
                        1 => array(
1889
                                2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1890
                                25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
1891
                        2 => array(
1892
                                14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
1893
                $this->assertEquals($expected, $result);
1894
1895
                $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
1896
                $expected = array(
1897
                        1 => array(
1898
                                2 => 'Mariano Iglesias',
1899
                                25 => 'The Gwoo'),
1900
                        2 => array(
1901
                                14 => 'Larry E. Masters'));
1902
                $this->assertEquals($expected, $result);
1903
1904
                $result = Set::combine($a, '{n}.User.id');
1905
                $expected = array(2 => null, 14 => null, 25 => null);
1906
                $this->assertEquals($expected, $result);
1907
1908
                $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
1909
                $expected = array(
1910
                        2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1911
                        14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
1912
                        25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
1913
                $this->assertEquals($expected, $result);
1914
1915
                $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
1916
                $expected = array(2 => 'Mariano Iglesias', 14 => 'Larry E. Masters', 25 => 'The Gwoo');
1917
                $this->assertEquals($expected, $result);
1918
1919
                $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
1920
                $expected = array(
1921
                        1 => array(
1922
                                2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
1923
                                25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
1924
                        2 => array(
1925
                                14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
1926
                $this->assertEquals($expected, $result);
1927
1928
                $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
1929
                $expected = array(
1930
                        1 => array(
1931
                                2 => 'Mariano Iglesias',
1932
                                25 => 'The Gwoo'),
1933
                        2 => array(
1934
                                14 => 'Larry E. Masters'));
1935
                $this->assertEquals($expected, $result);
1936
1937
                $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
1938
                $expected = array(
1939
                        1 => array(
1940
                                2 => 'mariano.iglesias: Mariano Iglesias',
1941
                                25 => 'gwoo: The Gwoo'),
1942
                        2 => array(14 => 'phpnut: Larry E. Masters'));
1943
                $this->assertEquals($expected, $result);
1944
1945
                $result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
1946
                $expected = array('mariano.iglesias: Mariano Iglesias' => 2, 'phpnut: Larry E. Masters' => 14, 'gwoo: The Gwoo' => 25);
1947
                $this->assertEquals($expected, $result);
1948
1949
                $result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
1950
                $expected = array('Mariano Iglesias: mariano.iglesias' => 2, 'Larry E. Masters: phpnut' => 14, 'The Gwoo: gwoo' => 25);
1951
                $this->assertEquals($expected, $result);
1952
1953
                $result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
1954
                $expected = array('mariano.iglesias: 2' => 'Mariano Iglesias', 'phpnut: 14' => 'Larry E. Masters', 'gwoo: 25' => 'The Gwoo');
1955
                $this->assertEquals($expected, $result);
1956
1957
                $result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
1958
                $expected = array('2: mariano.iglesias' => 'Mariano Iglesias', '14: phpnut' => 'Larry E. Masters', '25: gwoo' => 'The Gwoo');
1959
                $this->assertEquals($expected, $result);
1960
1961
                $b = new stdClass();
1962
                $b->users = array(
1963
                        array('User' => array('id' => 2, 'group_id' => 1,
1964
                                'Data' => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'))),
1965
                        array('User' => array('id' => 14, 'group_id' => 2,
1966
                                'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
1967
                        array('User' => array('id' => 25, 'group_id' => 1,
1968
                                'Data' => array('user' => 'gwoo', 'name' => 'The Gwoo'))));
1969
                $result = Set::combine($b, 'users.{n}.User.id');
1970
                $expected = array(2 => null, 14 => null, 25 => null);
1971
                $this->assertEquals($expected, $result);
1972
1973
                $result = Set::combine($b, 'users.{n}.User.id', 'users.{n}.User.non-existant');
1974
                $expected = array(2 => null, 14 => null, 25 => null);
1975
                $this->assertEquals($expected, $result);
1976
1977
                $result = Set::combine($a, 'fail', 'fail');
1978
                $this->assertSame(array(), $result);
1979
        }
1980
1981
/**
1982
 * testMapReverse method
1983
 *
1984
 * @return void
1985
 */
1986
        public function testMapReverse() {
1987
                $result = Set::reverse(null);
1988
                $this->assertEquals(null, $result);
1989
1990
                $result = Set::reverse(false);
1991
                $this->assertEquals(false, $result);
1992
1993
                $expected = array(
1994
                'Array1' => array(
1995
                                'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2'),
1996
                'Array2' => array(
1997
                                0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
1998
                                1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
1999
                                2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2000
                                3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2001
                                4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
2002
                'Array3' => array(
2003
                                0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2004
                                1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2005
                                2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2006
                                3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2007
                                4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
2008
                $map = Set::map($expected, true);
2009
                $this->assertEquals($expected['Array1']['Array1Data1'], $map->Array1->Array1Data1);
2010
                $this->assertEquals($expected['Array2'][0]['Array2Data1'], $map->Array2[0]->Array2Data1);
2011
2012
                $result = Set::reverse($map);
2013
                $this->assertEquals($expected, $result);
2014
2015
                $expected = array(
2016
                        'Post' => array('id' => 1, 'title' => 'First Post'),
2017
                        'Comment' => array(
2018
                                array('id' => 1, 'title' => 'First Comment'),
2019
                                array('id' => 2, 'title' => 'Second Comment')
2020
                        ),
2021
                        'Tag' => array(
2022
                                array('id' => 1, 'title' => 'First Tag'),
2023
                                array('id' => 2, 'title' => 'Second Tag')
2024
                        ),
2025
                );
2026
                $map = Set::map($expected);
2027
                $this->assertEquals($expected['Post']['title'], $map->title);
2028
                foreach ($map->Comment as $comment) {
2029
                        $ids[] = $comment->id;
2030
                }
2031
                $this->assertEquals(array(1, 2), $ids);
2032
2033
                $expected = array(
2034
                'Array1' => array(
2035
                                'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3', 'Array1Data4' => 'Array1Data4 value 4',
2036
                                'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
2037
                'string' => 1,
2038
                'another' => 'string',
2039
                'some' => 'thing else',
2040
                'Array2' => array(
2041
                                0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2042
                                1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2043
                                2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2044
                                3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2045
                                4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
2046
                'Array3' => array(
2047
                                0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2048
                                1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2049
                                2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2050
                                3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2051
                                4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
2052
                $map = Set::map($expected, true);
2053
                $result = Set::reverse($map);
2054
                $this->assertEquals($expected, $result);
2055
2056
                $expected = array(
2057
                'Array1' => array(
2058
                                'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3', 'Array1Data4' => 'Array1Data4 value 4',
2059
                                'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
2060
                'string' => 1,
2061
                'another' => 'string',
2062
                'some' => 'thing else',
2063
                'Array2' => array(
2064
                                0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2065
                                1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2066
                                2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2067
                                3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
2068
                                4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
2069
                'string2' => 1,
2070
                'another2' => 'string',
2071
                'some2' => 'thing else',
2072
                'Array3' => array(
2073
                                0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2074
                                1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2075
                                2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2076
                                3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
2077
                                4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')),
2078
                'string3' => 1,
2079
                'another3' => 'string',
2080
                'some3' => 'thing else');
2081
                $map = Set::map($expected, true);
2082
                $result = Set::reverse($map);
2083
                $this->assertEquals($expected, $result);
2084
2085
                $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851)));
2086
                $map = Set::map($expected);
2087
                $result = Set::reverse($map);
2088
                $this->assertEquals($expected, $result);
2089
2090
                $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851)));
2091
                $class = new stdClass;
2092
                $class->User = new stdClass;
2093
                $class->User->psword = 'whatever';
2094
                $class->User->Icon = new stdClass;
2095
                $class->User->Icon->id = 851;
2096
                $result = Set::reverse($class);
2097
                $this->assertEquals($expected, $result);
2098
2099
                $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851), 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address')));
2100
                $class = new stdClass;
2101
                $class->User = new stdClass;
2102
                $class->User->psword = 'whatever';
2103
                $class->User->Icon = new stdClass;
2104
                $class->User->Icon->id = 851;
2105
                $class->User->Profile = new stdClass;
2106
                $class->User->Profile->name = 'Some Name';
2107
                $class->User->Profile->address = 'Some Address';
2108
2109
                $result = Set::reverse($class);
2110
                $this->assertEquals($expected, $result);
2111
2112
                $expected = array('User' => array('psword' => 'whatever',
2113
                                                'Icon' => array('id' => 851),
2114
                                                'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
2115
                                                'Comment' => array(
2116
                                                                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'),
2117
                                                                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'))));
2118
2119
                $class = new stdClass;
2120
                $class->User = new stdClass;
2121
                $class->User->psword = 'whatever';
2122
                $class->User->Icon = new stdClass;
2123
                $class->User->Icon->id = 851;
2124
                $class->User->Profile = new stdClass;
2125
                $class->User->Profile->name = 'Some Name';
2126
                $class->User->Profile->address = 'Some Address';
2127
                $class->User->Comment = new stdClass;
2128
                $class->User->Comment->{'0'} = new stdClass;
2129
                $class->User->Comment->{'0'}->id = 1;
2130
                $class->User->Comment->{'0'}->article_id = 1;
2131
                $class->User->Comment->{'0'}->user_id = 1;
2132
                $class->User->Comment->{'0'}->comment = 'First Comment for First Article';
2133
                $class->User->Comment->{'0'}->published = 'Y';
2134
                $class->User->Comment->{'0'}->created = '2007-03-18 10:47:23';
2135
                $class->User->Comment->{'0'}->updated = '2007-03-18 10:49:31';
2136
                $class->User->Comment->{'1'} = new stdClass;
2137
                $class->User->Comment->{'1'}->id = 2;
2138
                $class->User->Comment->{'1'}->article_id = 1;
2139
                $class->User->Comment->{'1'}->user_id = 2;
2140
                $class->User->Comment->{'1'}->comment = 'Second Comment for First Article';
2141
                $class->User->Comment->{'1'}->published = 'Y';
2142
                $class->User->Comment->{'1'}->created = '2007-03-18 10:47:23';
2143
                $class->User->Comment->{'1'}->updated = '2007-03-18 10:49:31';
2144
2145
                $result = Set::reverse($class);
2146
                $this->assertEquals($expected, $result);
2147
2148
                $expected = array('User' => array('psword' => 'whatever',
2149
                                                'Icon' => array('id' => 851),
2150
                                                'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
2151
                                                'Comment' => array(
2152
                                                                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'),
2153
                                                                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'))));
2154
2155
                // @codingStandardsIgnoreStart
2156
                $class = new stdClass;
2157
                $class->User = new stdClass;
2158
                $class->User->psword = 'whatever';
2159
                $class->User->Icon = new stdClass;
2160
                $class->User->Icon->id = 851;
2161
                $class->User->Profile = new stdClass;
2162
                $class->User->Profile->name = 'Some Name';
2163
                $class->User->Profile->address = 'Some Address';
2164
                $class->User->Comment = array();
2165
                $comment = new stdClass;
2166
                $comment->id = 1;
2167
                $comment->article_id = 1;
2168
                $comment->user_id = 1;
2169
                $comment->comment = 'First Comment for First Article';
2170
                $comment->published = 'Y';
2171
                $comment->created = '2007-03-18 10:47:23';
2172
                $comment->updated = '2007-03-18 10:49:31';
2173
                $comment2 = new stdClass;
2174
                $comment2->id = 2;
2175
                $comment2->article_id = 1;
2176
                $comment2->user_id = 2;
2177
                $comment2->comment = 'Second Comment for First Article';
2178
                $comment2->published = 'Y';
2179
                $comment2->created = '2007-03-18 10:47:23';
2180
                $comment2->updated = '2007-03-18 10:49:31';
2181
                // @codingStandardsIgnoreEnd
2182
                $class->User->Comment = array($comment, $comment2);
2183
                $result = Set::reverse($class);
2184
                $this->assertEquals($expected, $result);
2185
2186
                $class = new stdClass;
2187
                $class->User = new stdClass;
2188
                $class->User->id = 100;
2189
                $class->someString = 'this is some string';
2190
                $class->Profile = new stdClass;
2191
                $class->Profile->name = 'Joe Mamma';
2192
2193
                $result = Set::reverse($class);
2194
                $expected = array(
2195
                        'User' => array('id' => '100'),
2196
                        'someString' => 'this is some string',
2197
                        'Profile' => array('name' => 'Joe Mamma')
2198
                );
2199
                $this->assertEquals($expected, $result);
2200
2201
                // @codingStandardsIgnoreStart
2202
                $class = new stdClass;
2203
                $class->User = new stdClass;
2204
                $class->User->id = 100;
2205
                $class->User->_name_ = 'User';
2206
                $class->Profile = new stdClass;
2207
                $class->Profile->name = 'Joe Mamma';
2208
                $class->Profile->_name_ = 'Profile';
2209
                // @codingStandardsIgnoreEnd
2210
2211
                $result = Set::reverse($class);
2212
                $expected = array('User' => array('id' => '100'), 'Profile' => array('name' => 'Joe Mamma'));
2213
                $this->assertEquals($expected, $result);
2214
        }
2215
2216
/**
2217
 * testFormatting method
2218
 *
2219
 * @return void
2220
 */
2221
        public function testFormatting() {
2222
                $data = array(
2223
                        array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
2224
                        array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => '{0}')),
2225
                        array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}')));
2226
2227
                $result = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name'));
2228
                $expected = array('Abele, Nate', 'Masters, Larry', 'Woodworth, Garrett');
2229
                $this->assertEquals($expected, $result);
2230
2231
                $result = Set::format($data, '{0}, {1}', array('{n}.Person.last_name', '{n}.Person.first_name'));
2232
                $this->assertEquals($expected, $result);
2233
2234
                $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.state'));
2235
                $expected = array('Boston, MA', 'Boondock, TN', 'Venice Beach, CA');
2236
                $this->assertEquals($expected, $result);
2237
2238
                $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.city', '{n}.Person.state'));
2239
                $expected = array('{Boston, MA}', '{Boondock, TN}', '{Venice Beach, CA}');
2240
                $this->assertEquals($expected, $result);
2241
2242
                $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.something', '{n}.Person.something'));
2243
                $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
2244
                $this->assertEquals($expected, $result);
2245
2246
                $result = Set::format($data, '{%2$d, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
2247
                $expected = array('{42, 42}', '{0, {0}}', '{0, {1}}');
2248
                $this->assertEquals($expected, $result);
2249
2250
                $result = Set::format($data, '{%1$s, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
2251
                $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
2252
                $this->assertEquals($expected, $result);
2253
2254
                $result = Set::format($data, '%2$d, %1$s', array('{n}.Person.first_name', '{n}.Person.something'));
2255
                $expected = array('42, Nate', '0, Larry', '0, Garrett');
2256
                $this->assertEquals($expected, $result);
2257
2258
                $result = Set::format($data, '%1$s, %2$d', array('{n}.Person.first_name', '{n}.Person.something'));
2259
                $expected = array('Nate, 42', 'Larry, 0', 'Garrett, 0');
2260
                $this->assertEquals($expected, $result);
2261
        }
2262
2263
/**
2264
 * testFormattingNullValues method
2265
 *
2266
 * @return void
2267
 */
2268
        public function testFormattingNullValues() {
2269
                $data = array(
2270
                        array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
2271
                        array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null)),
2272
                        array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null)));
2273
2274
                $result = Set::format($data, '%s', array('{n}.Person.something'));
2275
                $expected = array('42', '', '');
2276
                $this->assertEquals($expected, $result);
2277
2278
                $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.something'));
2279
                $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, ');
2280
                $this->assertEquals($expected, $result);
2281
        }
2282
2283
/**
2284
 * testCountDim method
2285
 *
2286
 * @return void
2287
 */
2288
        public function testCountDim() {
2289
                $data = array('one', '2', 'three');
2290
                $result = Set::countDim($data);
2291
                $this->assertEquals(1, $result);
2292
2293
                $data = array('1' => '1.1', '2', '3');
2294
                $result = Set::countDim($data);
2295
                $this->assertEquals(1, $result);
2296
2297
                $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
2298
                $result = Set::countDim($data);
2299
                $this->assertEquals(2, $result);
2300
2301
                $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
2302
                $result = Set::countDim($data);
2303
                $this->assertEquals(1, $result);
2304
2305
                $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
2306
                $result = Set::countDim($data, true);
2307
                $this->assertEquals(2, $result);
2308
2309
                $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
2310
                $result = Set::countDim($data);
2311
                $this->assertEquals(2, $result);
2312
2313
                $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
2314
                $result = Set::countDim($data, true);
2315
                $this->assertEquals(3, $result);
2316
2317
                $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')));
2318
                $result = Set::countDim($data, true);
2319
                $this->assertEquals(4, $result);
2320
2321
                $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')));
2322
                $result = Set::countDim($data, true);
2323
                $this->assertEquals(5, $result);
2324
2325
                $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')));
2326
                $result = Set::countDim($data, true);
2327
                $this->assertEquals(5, $result);
2328
2329
                $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')));
2330
                $result = Set::countDim($set, false, 0);
2331
                $this->assertEquals(2, $result);
2332
2333
                $result = Set::countDim($set, true);
2334
                $this->assertEquals(5, $result);
2335
        }
2336
2337
/**
2338
 * testMapNesting method
2339
 *
2340
 * @return void
2341
 */
2342
        public function testMapNesting() {
2343
                $expected = array(
2344
                        array(
2345
                                "IndexedPage" => array(
2346
                                        "id" => 1,
2347
                                        "url" => 'http://blah.com/',
2348
                                        'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
2349
                                        'headers' => array(
2350
                                                        'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
2351
                                                        'Server' => "Apache",
2352
                                                        'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
2353
                                                        'Cache-Control' => "private",
2354
                                                        'Pragma' => "no-cache",
2355
                                                        'Content-Type' => "text/html; charset=UTF-8",
2356
                                                        'X-Original-Transfer-Encoding' => "chunked",
2357
                                                        'Content-Length' => "50210",
2358
                                        ),
2359
                                        'meta' => array(
2360
                                                        'keywords' => array('testing', 'tests'),
2361
                                                        'description' => 'describe me',
2362
                                        ),
2363
                                        'get_vars' => '',
2364
                                        'post_vars' => array(),
2365
                                        'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
2366
                                        'redirect' => '',
2367
                                        'created' => "1195055503",
2368
                                        'updated' => "1195055503",
2369
                                )
2370
                        ),
2371
                        array(
2372
                                "IndexedPage" => array(
2373
                                        "id" => 2,
2374
                                        "url" => 'http://blah.com/',
2375
                                        'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
2376
                                        'headers' => array(
2377
                                                'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
2378
                                                'Server' => "Apache",
2379
                                                'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
2380
                                                'Cache-Control' => "private",
2381
                                                'Pragma' => "no-cache",
2382
                                                'Content-Type' => "text/html; charset=UTF-8",
2383
                                                'X-Original-Transfer-Encoding' => "chunked",
2384
                                                'Content-Length' => "50210",
2385
                                        ),
2386
                                        'meta' => array(
2387
                                                        'keywords' => array('testing', 'tests'),
2388
                                                        'description' => 'describe me',
2389
                                        ),
2390
                                        'get_vars' => '',
2391
                                        'post_vars' => array(),
2392
                                        'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
2393
                                        'redirect' => '',
2394
                                        'created' => "1195055503",
2395
                                        'updated' => "1195055503",
2396
                                ),
2397
                        )
2398
                );
2399
2400
                $mapped = Set::map($expected);
2401
                $ids = array();
2402
2403
                foreach ($mapped as $object) {
2404
                        $ids[] = $object->id;
2405
                }
2406
                $this->assertEquals(array(1, 2), $ids);
2407
                $this->assertEquals($expected[0]['IndexedPage']['headers'], get_object_vars($mapped[0]->headers));
2408
2409
                $result = Set::reverse($mapped);
2410
                $this->assertEquals($expected, $result);
2411
2412
                $data = array(
2413
                        array(
2414
                                "IndexedPage" => array(
2415
                                        "id" => 1,
2416
                                        "url" => 'http://blah.com/',
2417
                                        'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
2418
                                        'get_vars' => '',
2419
                                        'redirect' => '',
2420
                                        'created' => "1195055503",
2421
                                        'updated' => "1195055503",
2422
                                )
2423
                        ),
2424
                        array(
2425
                                "IndexedPage" => array(
2426
                                        "id" => 2,
2427
                                        "url" => 'http://blah.com/',
2428
                                        'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
2429
                                        'get_vars' => '',
2430
                                        'redirect' => '',
2431
                                        'created' => "1195055503",
2432
                                        'updated' => "1195055503",
2433
                                ),
2434
                        )
2435
                );
2436
                $mapped = Set::map($data);
2437
2438
                // @codingStandardsIgnoreStart
2439
                $expected = new stdClass();
2440
                $expected->_name_ = 'IndexedPage';
2441
                $expected->id = 2;
2442
                $expected->url = 'http://blah.com/';
2443
                $expected->hash = '68a9f053b19526d08e36c6a9ad150737933816a5';
2444
                $expected->get_vars = '';
2445
                $expected->redirect = '';
2446
                $expected->created = '1195055503';
2447
                $expected->updated = '1195055503';
2448
                // @codingStandardsIgnoreEnd
2449
                $this->assertEquals($expected, $mapped[1]);
2450
2451
                $ids = array();
2452
2453
                foreach ($mapped as $object) {
2454
                        $ids[] = $object->id;
2455
                }
2456
                $this->assertEquals(array(1, 2), $ids);
2457
2458
                $result = Set::map(null);
2459
                $expected = null;
2460
                $this->assertEquals($expected, $result);
2461
        }
2462
2463
/**
2464
 * testNestedMappedData method
2465
 *
2466
 * @return void
2467
 */
2468
        public function testNestedMappedData() {
2469
                $result = Set::map(array(
2470
                                array(
2471
                                        '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'),
2472
                                        'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
2473
                                ),
2474
                                array(
2475
                                        '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'),
2476
                                        'Author' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31', 'test' => 'working'),
2477
                                )
2478
                        ));
2479
2480
                // @codingStandardsIgnoreStart
2481
                $expected = new stdClass;
2482
                $expected->_name_ = 'Post';
2483
                $expected->id = '1';
2484
                $expected->author_id = '1';
2485
                $expected->title = 'First Post';
2486
                $expected->body = 'First Post Body';
2487
                $expected->published = 'Y';
2488
                $expected->created = "2007-03-18 10:39:23";
2489
                $expected->updated = "2007-03-18 10:41:31";
2490
2491
                $expected->Author = new stdClass;
2492
                $expected->Author->id = '1';
2493
                $expected->Author->user = 'mariano';
2494
                $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
2495
                $expected->Author->created = '2007-03-17 01:16:23';
2496
                $expected->Author->updated = '2007-03-17 01:18:31';
2497
                $expected->Author->test = 'working';
2498
                $expected->Author->_name_ = 'Author';
2499
2500
                $expected2 = new stdClass;
2501
                $expected2->_name_ = 'Post';
2502
                $expected2->id = '2';
2503
                $expected2->author_id = '3';
2504
                $expected2->title = 'Second Post';
2505
                $expected2->body = 'Second Post Body';
2506
                $expected2->published = 'Y';
2507
                $expected2->created = "2007-03-18 10:41:23";
2508
                $expected2->updated = "2007-03-18 10:43:31";
2509
2510
                $expected2->Author = new stdClass;
2511
                $expected2->Author->id = '3';
2512
                $expected2->Author->user = 'larry';
2513
                $expected2->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
2514
                $expected2->Author->created = '2007-03-17 01:20:23';
2515
                $expected2->Author->updated = '2007-03-17 01:22:31';
2516
                $expected2->Author->test = 'working';
2517
                $expected2->Author->_name_ = 'Author';
2518
                // @codingStandardsIgnoreEnd
2519
2520
                $test = array();
2521
                $test[0] = $expected;
2522
                $test[1] = $expected2;
2523
2524
                $this->assertEquals($test, $result);
2525
2526
                $result = Set::map(
2527
                        array(
2528
                                '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'),
2529
                                'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
2530
                        )
2531
                );
2532
                // @codingStandardsIgnoreStart
2533
                $expected = new stdClass;
2534
                $expected->_name_ = 'Post';
2535
                $expected->id = '1';
2536
                $expected->author_id = '1';
2537
                $expected->title = 'First Post';
2538
                $expected->body = 'First Post Body';
2539
                $expected->published = 'Y';
2540
                $expected->created = "2007-03-18 10:39:23";
2541
                $expected->updated = "2007-03-18 10:41:31";
2542
2543
                $expected->Author = new stdClass;
2544
                $expected->Author->id = '1';
2545
                $expected->Author->user = 'mariano';
2546
                $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
2547
                $expected->Author->created = "2007-03-17 01:16:23";
2548
                $expected->Author->updated = "2007-03-17 01:18:31";
2549
                $expected->Author->test = 'working';
2550
                $expected->Author->_name_ = 'Author';
2551
                // @codingStandardsIgnoreEnd
2552
                $this->assertEquals($expected, $result);
2553
2554
                //Case where extra HABTM fields come back in a result
2555
                $data = array(
2556
                        'User' => array(
2557
                                'id' => 1,
2558
                                'email' => 'user@example.com',
2559
                                'first_name' => 'John',
2560
                                'last_name' => 'Smith',
2561
                        ),
2562
                        'Piece' => array(
2563
                                array(
2564
                                        'id' => 1,
2565
                                        'title' => 'Moonlight Sonata',
2566
                                        'composer' => 'Ludwig van Beethoven',
2567
                                        'PiecesUser' => array(
2568
                                                'id' => 1,
2569
                                                'created' => '2008-01-01 00:00:00',
2570
                                                'modified' => '2008-01-01 00:00:00',
2571
                                                'piece_id' => 1,
2572
                                                'user_id' => 2,
2573
                                        )
2574
                                ),
2575
                                array(
2576
                                        'id' => 2,
2577
                                        'title' => 'Moonlight Sonata 2',
2578
                                        'composer' => 'Ludwig van Beethoven',
2579
                                        'PiecesUser' => array(
2580
                                                'id' => 2,
2581
                                                'created' => '2008-01-01 00:00:00',
2582
                                                'modified' => '2008-01-01 00:00:00',
2583
                                                'piece_id' => 2,
2584
                                                'user_id' => 2,
2585
                                        )
2586
                                )
2587
                        )
2588
                );
2589
2590
                $result = Set::map($data);
2591
2592
                // @codingStandardsIgnoreStart
2593
                $expected = new stdClass();
2594
                $expected->_name_ = 'User';
2595
                $expected->id = 1;
2596
                $expected->email = 'user@example.com';
2597
                $expected->first_name = 'John';
2598
                $expected->last_name = 'Smith';
2599
2600
                $piece = new stdClass();
2601
                $piece->id = 1;
2602
                $piece->title = 'Moonlight Sonata';
2603
                $piece->composer = 'Ludwig van Beethoven';
2604
2605
                $piece->PiecesUser = new stdClass();
2606
                $piece->PiecesUser->id = 1;
2607
                $piece->PiecesUser->created = '2008-01-01 00:00:00';
2608
                $piece->PiecesUser->modified = '2008-01-01 00:00:00';
2609
                $piece->PiecesUser->piece_id = 1;
2610
                $piece->PiecesUser->user_id = 2;
2611
                $piece->PiecesUser->_name_ = 'PiecesUser';
2612
2613
                $piece->_name_ = 'Piece';
2614
2615
                $piece2 = new stdClass();
2616
                $piece2->id = 2;
2617
                $piece2->title = 'Moonlight Sonata 2';
2618
                $piece2->composer = 'Ludwig van Beethoven';
2619
2620
                $piece2->PiecesUser = new stdClass();
2621
                $piece2->PiecesUser->id = 2;
2622
                $piece2->PiecesUser->created = '2008-01-01 00:00:00';
2623
                $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
2624
                $piece2->PiecesUser->piece_id = 2;
2625
                $piece2->PiecesUser->user_id = 2;
2626
                $piece2->PiecesUser->_name_ = 'PiecesUser';
2627
2628
                $piece2->_name_ = 'Piece';
2629
                // @codingStandardsIgnoreEnd
2630
2631
                $expected->Piece = array($piece, $piece2);
2632
2633
                $this->assertEquals($expected, $result);
2634
2635
                //Same data, but should work if _name_ has been manually defined:
2636
                $data = array(
2637
                        'User' => array(
2638
                                'id' => 1,
2639
                                'email' => 'user@example.com',
2640
                                'first_name' => 'John',
2641
                                'last_name' => 'Smith',
2642
                                '_name_' => 'FooUser',
2643
                        ),
2644
                        'Piece' => array(
2645
                                array(
2646
                                        'id' => 1,
2647
                                        'title' => 'Moonlight Sonata',
2648
                                        'composer' => 'Ludwig van Beethoven',
2649
                                        '_name_' => 'FooPiece',
2650
                                        'PiecesUser' => array(
2651
                                                'id' => 1,
2652
                                                'created' => '2008-01-01 00:00:00',
2653
                                                'modified' => '2008-01-01 00:00:00',
2654
                                                'piece_id' => 1,
2655
                                                'user_id' => 2,
2656
                                                '_name_' => 'FooPiecesUser',
2657
                                        )
2658
                                ),
2659
                                array(
2660
                                        'id' => 2,
2661
                                        'title' => 'Moonlight Sonata 2',
2662
                                        'composer' => 'Ludwig van Beethoven',
2663
                                        '_name_' => 'FooPiece',
2664
                                        'PiecesUser' => array(
2665
                                                'id' => 2,
2666
                                                'created' => '2008-01-01 00:00:00',
2667
                                                'modified' => '2008-01-01 00:00:00',
2668
                                                'piece_id' => 2,
2669
                                                'user_id' => 2,
2670
                                                '_name_' => 'FooPiecesUser',
2671
                                        )
2672
                                )
2673
                        )
2674
                );
2675
2676
                $result = Set::map($data);
2677
2678
                // @codingStandardsIgnoreStart
2679
                $expected = new stdClass();
2680
                $expected->_name_ = 'FooUser';
2681
                $expected->id = 1;
2682
                $expected->email = 'user@example.com';
2683
                $expected->first_name = 'John';
2684
                $expected->last_name = 'Smith';
2685
2686
                $piece = new stdClass();
2687
                $piece->id = 1;
2688
                $piece->title = 'Moonlight Sonata';
2689
                $piece->composer = 'Ludwig van Beethoven';
2690
                $piece->_name_ = 'FooPiece';
2691
                $piece->PiecesUser = new stdClass();
2692
                $piece->PiecesUser->id = 1;
2693
                $piece->PiecesUser->created = '2008-01-01 00:00:00';
2694
                $piece->PiecesUser->modified = '2008-01-01 00:00:00';
2695
                $piece->PiecesUser->piece_id = 1;
2696
                $piece->PiecesUser->user_id = 2;
2697
                $piece->PiecesUser->_name_ = 'FooPiecesUser';
2698
2699
                $piece2 = new stdClass();
2700
                $piece2->id = 2;
2701
                $piece2->title = 'Moonlight Sonata 2';
2702
                $piece2->composer = 'Ludwig van Beethoven';
2703
                $piece2->_name_ = 'FooPiece';
2704
                $piece2->PiecesUser = new stdClass();
2705
                $piece2->PiecesUser->id = 2;
2706
                $piece2->PiecesUser->created = '2008-01-01 00:00:00';
2707
                $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
2708
                $piece2->PiecesUser->piece_id = 2;
2709
                $piece2->PiecesUser->user_id = 2;
2710
                $piece2->PiecesUser->_name_ = 'FooPiecesUser';
2711
                // @codingStandardsIgnoreEnd
2712
2713
                $expected->Piece = array($piece, $piece2);
2714
2715
                $this->assertEquals($expected, $result);
2716
        }
2717
2718
/**
2719
 * testPushDiff method
2720
 *
2721
 * @return void
2722
 */
2723
        public function testPushDiff() {
2724
                $array1 = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2'));
2725
                $array2 = array('ModelTwo' => array('id' => 1002, 'field_one' => 'a2.m2.f1', 'field_two' => 'a2.m2.f2'));
2726
2727
                $result = Set::pushDiff($array1, $array2);
2728
2729
                $this->assertEquals($array1 + $array2, $result);
2730
2731
                $array3 = array('ModelOne' => array('id' => 1003, 'field_one' => 'a3.m1.f1', 'field_two' => 'a3.m1.f2', 'field_three' => 'a3.m1.f3'));
2732
                $result = Set::pushDiff($array1, $array3);
2733
2734
                $expected = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2', 'field_three' => 'a3.m1.f3'));
2735
                $this->assertEquals($expected, $result);
2736
2737
                $array1 = array(
2738
                                0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2')),
2739
                                1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2')));
2740
                $array2 = array(
2741
                                0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2')),
2742
                                1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2')));
2743
2744
                $result = Set::pushDiff($array1, $array2);
2745
                $this->assertEquals($array1, $result);
2746
2747
                $array3 = array(0 => array('ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2')));
2748
2749
                $result = Set::pushDiff($array1, $array3);
2750
                $expected = array(
2751
                                        0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2'),
2752
                                                'ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2')),
2753
                                        1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2')));
2754
                $this->assertEquals($expected, $result);
2755
2756
                $result = Set::pushDiff($array1, null);
2757
                $this->assertEquals($array1, $result);
2758
2759
                $result = Set::pushDiff($array1, $array2);
2760
                $this->assertEquals($array1 + $array2, $result);
2761
        }
2762
2763
/**
2764
 * testSetApply method
2765
 * @return void
2766
 */
2767
        public function testApply() {
2768
                $data = array(
2769
                        array('Movie' => array('id' => 1, 'title' => 'movie 3', 'rating' => 5)),
2770
                        array('Movie' => array('id' => 1, 'title' => 'movie 1', 'rating' => 1)),
2771
                        array('Movie' => array('id' => 1, 'title' => 'movie 2', 'rating' => 3))
2772
                );
2773
2774
                $result = Set::apply('/Movie/rating', $data, 'array_sum');
2775
                $expected = 9;
2776
                $this->assertEquals($expected, $result);
2777
2778
                $result = Set::apply('/Movie/rating', $data, 'array_product');
2779
                $expected = 15;
2780
                $this->assertEquals($expected, $result);
2781
2782
                $result = Set::apply('/Movie/title', $data, 'ucfirst', array('type' => 'map'));
2783
                $expected = array('Movie 3', 'Movie 1', 'Movie 2');
2784
                $this->assertEquals($expected, $result);
2785
2786
                $result = Set::apply('/Movie/title', $data, 'strtoupper', array('type' => 'map'));
2787
                $expected = array('MOVIE 3', 'MOVIE 1', 'MOVIE 2');
2788
                $this->assertEquals($expected, $result);
2789
2790
                $result = Set::apply('/Movie/rating', $data, array('SetTest', 'method'), array('type' => 'reduce'));
2791
                $expected = 9;
2792
                $this->assertEquals($expected, $result);
2793
2794
                $result = Set::apply('/Movie/rating', $data, 'strtoupper', array('type' => 'non existing type'));
2795
                $expected = null;
2796
                $this->assertEquals($expected, $result);
2797
        }
2798
2799
/**
2800
 * Helper method to test Set::apply()
2801
 *
2802
 * @return void
2803
 */
2804
        public static function method($val1, $val2) {
2805
                $val1 += $val2;
2806
                return $val1;
2807
        }
2808
2809
/**
2810
 * testXmlSetReverse method
2811
 *
2812
 * @return void
2813
 */
2814
        public function testXmlSetReverse() {
2815
                App::uses('Xml', 'Utility');
2816
2817
                $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2818
                <rss version="2.0">
2819
                        <channel>
2820
                        <title>Cake PHP Google Group</title>
2821
                        <link>http://groups.google.com/group/cake-php</link>
2822
                        <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>
2823
                        <language>en</language>
2824
                                <item>
2825
                                <title>constructng result array when using findall</title>
2826
                                <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
2827
                                <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>
2828
                                <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
2829
                                <author>bmil...@gmail.com(bpscrugs)</author>
2830
                                <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
2831
                                </item>
2832
                                <item>
2833
                                <title>Re: share views between actions?</title>
2834
                                <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
2835
                                <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>
2836
                                <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
2837
                                <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
2838
                                <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
2839
                         </item>
2840
                </channel>
2841
                </rss>';
2842
                $xml = Xml::build($string);
2843
                $result = Set::reverse($xml);
2844
                $expected = array('rss' => array(
2845
                        '@version' => '2.0',
2846
                        'channel' => array(
2847
                                'title' => 'Cake PHP Google Group',
2848
                                'link' => 'http://groups.google.com/group/cake-php',
2849
                                '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.',
2850
                                'language' => 'en',
2851
                                'item' => array(
2852
                                        array(
2853
                                                'title' => 'constructng result array when using findall',
2854
                                                'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
2855
                                                '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(",
2856
                                                'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
2857
                                                'author' => 'bmil...@gmail.com(bpscrugs)',
2858
                                                'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
2859
                                        ),
2860
                                        array(
2861
                                                'title' => 'Re: share views between actions?',
2862
                                                'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
2863
                                                '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',
2864
                                                'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
2865
                                                'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
2866
                                                'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
2867
                                        )
2868
                                )
2869
                        )
2870
                ));
2871
                $this->assertEquals($expected, $result);
2872
                $string = '<data><post title="Title of this post" description="cool"/></data>';
2873
2874
                $xml = Xml::build($string);
2875
                $result = Set::reverse($xml);
2876
                $expected = array('data' => array('post' => array('@title' => 'Title of this post', '@description' => 'cool')));
2877
                $this->assertEquals($expected, $result);
2878
2879
                $xml = Xml::build('<example><item><title>An example of a correctly reversed SimpleXMLElement</title><desc/></item></example>');
2880
                $result = Set::reverse($xml);
2881
                $expected = array('example' =>
2882
                        array(
2883
                                'item' => array(
2884
                                        'title' => 'An example of a correctly reversed SimpleXMLElement',
2885
                                        'desc' => '',
2886
                                )
2887
                        )
2888
                );
2889
                $this->assertEquals($expected, $result);
2890
2891
                $xml = Xml::build('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>');
2892
                $result = Set::reverse($xml);
2893
                $expected =
2894
                        array('example' => array(
2895
                                'item' => array(
2896
                                        '@attr' => '123',
2897
                                        'titles' => array(
2898
                                                'title' => array('title1', 'title2')
2899
                                        )
2900
                                )
2901
                        )
2902
                );
2903
                $this->assertEquals($expected, $result);
2904
2905
                $xml = Xml::build('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
2906
                $result = Set::reverse($xml);
2907
                $expected =
2908
                        array('example' => array(
2909
                                '@attr' => 'ex_attr',
2910
                                'item' => array(
2911
                                        '@attr' => '123',
2912
                                        'titles' => 'list',
2913
                                        '@' => 'textforitems'
2914
                                )
2915
                        )
2916
                );
2917
                $this->assertEquals($expected, $result);
2918
2919
                $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2920
                <rss version="2.0" xmlns:dc="http://www.cakephp.org/">
2921
                        <channel>
2922
                        <title>Cake PHP Google Group</title>
2923
                        <link>http://groups.google.com/group/cake-php</link>
2924
                        <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>
2925
                        <language>en</language>
2926
                                <item>
2927
                                <title>constructng result array when using findall</title>
2928
                                <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
2929
                                <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>
2930
                                        <dc:creator>cakephp</dc:creator>
2931
                                <category><![CDATA[cakephp]]></category>
2932
                                <category><![CDATA[model]]></category>
2933
                                <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
2934
                                <author>bmil...@gmail.com(bpscrugs)</author>
2935
                                <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
2936
                                </item>
2937
                                <item>
2938
                                <title>Re: share views between actions?</title>
2939
                                <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
2940
                                <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>
2941
                                        <dc:creator>cakephp</dc:creator>
2942
                                <category><![CDATA[cakephp]]></category>
2943
                                <category><![CDATA[model]]></category>
2944
                                <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
2945
                                <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
2946
                                <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
2947
                         </item>
2948
                </channel>
2949
                </rss>';
2950
2951
                $xml = Xml::build($string);
2952
                $result = Set::reverse($xml);
2953
2954
                $expected = array('rss' => array(
2955
                        '@version' => '2.0',
2956
                        'channel' => array(
2957
                                'title' => 'Cake PHP Google Group',
2958
                                'link' => 'http://groups.google.com/group/cake-php',
2959
                                '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.',
2960
                                'language' => 'en',
2961
                                'item' => array(
2962
                                        array(
2963
                                                'title' => 'constructng result array when using findall',
2964
                                                'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
2965
                                                '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(",
2966
                                                'dc:creator' => 'cakephp',
2967
                                                'category' => array('cakephp', 'model'),
2968
                                                'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
2969
                                                'author' => 'bmil...@gmail.com(bpscrugs)',
2970
                                                'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
2971
                                        ),
2972
                                        array(
2973
                                                'title' => 'Re: share views between actions?',
2974
                                                'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
2975
                                                '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',
2976
                                                'dc:creator' => 'cakephp',
2977
                                                'category' => array('cakephp', 'model'),
2978
                                                'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
2979
                                                'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
2980
                                                'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
2981
                                        )
2982
                                )
2983
                        )
2984
                ));
2985
                $this->assertEquals($expected, $result);
2986
2987
                $text = '<?xml version="1.0" encoding="UTF-8"?>
2988
                <XRDS xmlns="xri://$xrds">
2989
                <XRD xml:id="oauth" xmlns="xri://$XRD*($v*2.0)" version="2.0">
2990
                        <Type>xri://$xrds*simple</Type>
2991
                        <Expires>2008-04-13T07:34:58Z</Expires>
2992
                        <Service>
2993
                                <Type>http://oauth.net/core/1.0/endpoint/authorize</Type>
2994
                                <Type>http://oauth.net/core/1.0/parameters/auth-header</Type>
2995
                                <Type>http://oauth.net/core/1.0/parameters/uri-query</Type>
2996
                                <URI priority="10">https://ma.gnolia.com/oauth/authorize</URI>
2997
                                <URI priority="20">http://ma.gnolia.com/oauth/authorize</URI>
2998
                        </Service>
2999
                </XRD>
3000
                <XRD xmlns="xri://$XRD*($v*2.0)" version="2.0">
3001
                        <Type>xri://$xrds*simple</Type>
3002
                                <Service priority="10">
3003
                                        <Type>http://oauth.net/discovery/1.0</Type>
3004
                                        <URI>#oauth</URI>
3005
                                </Service>
3006
                </XRD>
3007
                </XRDS>';
3008
3009
                $xml = Xml::build($text);
3010
                $result = Set::reverse($xml);
3011
3012
                $expected = array('XRDS' => array(
3013
                        'XRD' => array(
3014
                                array(
3015
                                        '@xml:id' => 'oauth',
3016
                                        '@version' => '2.0',
3017
                                        'Type' => 'xri://$xrds*simple',
3018
                                        'Expires' => '2008-04-13T07:34:58Z',
3019
                                        'Service' => array(
3020
                                                'Type' => array(
3021
                                                        'http://oauth.net/core/1.0/endpoint/authorize',
3022
                                                        'http://oauth.net/core/1.0/parameters/auth-header',
3023
                                                        'http://oauth.net/core/1.0/parameters/uri-query'
3024
                                                ),
3025
                                                'URI' => array(
3026
                                                        array(
3027
                                                                '@' => 'https://ma.gnolia.com/oauth/authorize',
3028
                                                                '@priority' => '10',
3029
                                                        ),
3030
                                                        array(
3031
                                                                '@' => 'http://ma.gnolia.com/oauth/authorize',
3032
                                                                '@priority' => '20'
3033
                                                        )
3034
                                                )
3035
                                        )
3036
                                ),
3037
                                array(
3038
                                        '@version' => '2.0',
3039
                                        'Type' => 'xri://$xrds*simple',
3040
                                        'Service' => array(
3041
                                                '@priority' => '10',
3042
                                                'Type' => 'http://oauth.net/discovery/1.0',
3043
                                                'URI' => '#oauth'
3044
                                        )
3045
                                )
3046
                        )
3047
                ));
3048
                $this->assertEquals($expected, $result);
3049
        }
3050
3051
/**
3052
 * testStrictKeyCheck method
3053
 *
3054
 * @return void
3055
 */
3056
        public function testStrictKeyCheck() {
3057
                $set = array('a' => 'hi');
3058
                $this->assertFalse(Set::check($set, 'a.b'));
3059
        }
3060
3061
/**
3062
 * Tests Set::flatten
3063
 *
3064
 * @see Hash test cases, as Set::flatten() is just a proxy.
3065
 * @return void
3066
 */
3067
        public function testFlatten() {
3068
                $data = array('Larry', 'Curly', 'Moe');
3069
                $result = Set::flatten($data);
3070
                $this->assertEquals($data, $result);
3071
3072
                $data[9] = 'Shemp';
3073
                $result = Set::flatten($data);
3074
                $this->assertEquals($data, $result);
3075
3076
                $data = array(
3077
                        array(
3078
                                'Post' => array('id' => '1', 'author_id' => null, 'title' => 'First Post'),
3079
                                'Author' => array(),
3080
                        )
3081
                );
3082
                $result = Set::flatten($data);
3083
                $expected = array(
3084
                        '0.Post.id' => '1',
3085
                        '0.Post.author_id' => null,
3086
                        '0.Post.title' => 'First Post',
3087
                        '0.Author' => array()
3088
                );
3089
                $this->assertEquals($expected, $result);
3090
        }
3091
3092
/**
3093
 * Tests Set::expand
3094
 *
3095
 * @return void
3096
 */
3097
        public function testExpand() {
3098
                $data = array('My', 'Array', 'To', 'Flatten');
3099
                $flat = Set::flatten($data);
3100
                $result = Set::expand($flat);
3101
                $this->assertEquals($data, $result);
3102
        }
3103
3104
/**
3105
 * test normalization
3106
 *
3107
 * @return void
3108
 */
3109
        public function testNormalizeStrings() {
3110
                $result = Set::normalize('one,two,three');
3111
                $expected = array('one' => null, 'two' => null, 'three' => null);
3112
                $this->assertEquals($expected, $result);
3113
3114
                $result = Set::normalize('one two three', true, ' ');
3115
                $expected = array('one' => null, 'two' => null, 'three' => null);
3116
                $this->assertEquals($expected, $result);
3117
3118
                $result = Set::normalize('one  ,  two   ,  three   ', true, ',', true);
3119
                $expected = array('one' => null, 'two' => null, 'three' => null);
3120
                $this->assertEquals($expected, $result);
3121
        }
3122
3123
/**
3124
 * test normalizing arrays
3125
 *
3126
 * @return void
3127
 */
3128
        public function testNormalizeArrays() {
3129
                $result = Set::normalize(array('one', 'two', 'three'));
3130
                $expected = array('one' => null, 'two' => null, 'three' => null);
3131
                $this->assertEquals($expected, $result);
3132
3133
                $result = Set::normalize(array('one', 'two', 'three'), false);
3134
                $expected = array('one', 'two', 'three');
3135
                $this->assertEquals($expected, $result);
3136
3137
                $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false);
3138
                $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
3139
                $this->assertEquals($expected, $result);
3140
3141
                $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'));
3142
                $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
3143
                $this->assertEquals($expected, $result);
3144
3145
                $result = Set::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three'));
3146
                $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null);
3147
                $this->assertEquals($expected, $result);
3148
        }
3149
3150
/**
3151
 * test Set nest with a normal model result set. For kicks rely on Set nest detecting the key names
3152
 * automatically
3153
 *
3154
 * @return void
3155
 */
3156
        public function testNestModel() {
3157
                $input = array(
3158
                        array(
3159
                                'ModelName' => array(
3160
                                        'id' => 1,
3161
                                        'parent_id' => null
3162
                                ),
3163
                        ),
3164
                        array(
3165
                                'ModelName' => array(
3166
                                        'id' => 2,
3167
                                        'parent_id' => 1
3168
                                ),
3169
                        ),
3170
                        array(
3171
                                'ModelName' => array(
3172
                                        'id' => 3,
3173
                                        'parent_id' => 1
3174
                                ),
3175
                        ),
3176
                        array(
3177
                                'ModelName' => array(
3178
                                        'id' => 4,
3179
                                        'parent_id' => 1
3180
                                ),
3181
                        ),
3182
                        array(
3183
                                'ModelName' => array(
3184
                                        'id' => 5,
3185
                                        'parent_id' => 1
3186
                                ),
3187
                        ),
3188
                        array(
3189
                                'ModelName' => array(
3190
                                        'id' => 6,
3191
                                        'parent_id' => null
3192
                                ),
3193
                        ),
3194
                        array(
3195
                                'ModelName' => array(
3196
                                        'id' => 7,
3197
                                        'parent_id' => 6
3198
                                ),
3199
                        ),
3200
                        array(
3201
                                'ModelName' => array(
3202
                                        'id' => 8,
3203
                                        'parent_id' => 6
3204
                                ),
3205
                        ),
3206
                        array(
3207
                                'ModelName' => array(
3208
                                        'id' => 9,
3209
                                        'parent_id' => 6
3210
                                ),
3211
                        ),
3212
                        array(
3213
                                'ModelName' => array(
3214
                                        'id' => 10,
3215
                                        'parent_id' => 6
3216
                                )
3217
                        )
3218
                );
3219
                $expected = array(
3220
                        array(
3221
                                'ModelName' => array(
3222
                                        'id' => 1,
3223
                                        'parent_id' => null
3224
                                ),
3225
                                'children' => array(
3226
                                        array(
3227
                                                'ModelName' => array(
3228
                                                        'id' => 2,
3229
                                                        'parent_id' => 1
3230
                                                ),
3231
                                                'children' => array()
3232
                                        ),
3233
                                        array(
3234
                                                'ModelName' => array(
3235
                                                        'id' => 3,
3236
                                                        'parent_id' => 1
3237
                                                ),
3238
                                                'children' => array()
3239
                                        ),
3240
                                        array(
3241
                                                'ModelName' => array(
3242
                                                        'id' => 4,
3243
                                                        'parent_id' => 1
3244
                                                ),
3245
                                                'children' => array()
3246
                                        ),
3247
                                        array(
3248
                                                'ModelName' => array(
3249
                                                        'id' => 5,
3250
                                                        'parent_id' => 1
3251
                                                ),
3252
                                                'children' => array()
3253
                                        ),
3254
3255
                                )
3256
                        ),
3257
                        array(
3258
                                'ModelName' => array(
3259
                                        'id' => 6,
3260
                                        'parent_id' => null
3261
                                ),
3262
                                'children' => array(
3263
                                        array(
3264
                                                'ModelName' => array(
3265
                                                        'id' => 7,
3266
                                                        'parent_id' => 6
3267
                                                ),
3268
                                                'children' => array()
3269
                                        ),
3270
                                        array(
3271
                                                'ModelName' => array(
3272
                                                        'id' => 8,
3273
                                                        'parent_id' => 6
3274
                                                ),
3275
                                                'children' => array()
3276
                                        ),
3277
                                        array(
3278
                                                'ModelName' => array(
3279
                                                        'id' => 9,
3280
                                                        'parent_id' => 6
3281
                                                ),
3282
                                                'children' => array()
3283
                                        ),
3284
                                        array(
3285
                                                'ModelName' => array(
3286
                                                        'id' => 10,
3287
                                                        'parent_id' => 6
3288
                                                ),
3289
                                                'children' => array()
3290
                                        )
3291
                                )
3292
                        )
3293
                );
3294
                $result = Set::nest($input);
3295
                $this->assertEquals($expected, $result);
3296
        }
3297
3298
/**
3299
 * test Set nest with a normal model result set, and a nominated root id
3300
 *
3301
 * @return void
3302
 */
3303
        public function testNestModelExplicitRoot() {
3304
                $input = array(
3305
                        array(
3306
                                'ModelName' => array(
3307
                                        'id' => 1,
3308
                                        'parent_id' => null
3309
                                ),
3310
                        ),
3311
                        array(
3312
                                'ModelName' => array(
3313
                                        'id' => 2,
3314
                                        'parent_id' => 1
3315
                                ),
3316
                        ),
3317
                        array(
3318
                                'ModelName' => array(
3319
                                        'id' => 3,
3320
                                        'parent_id' => 1
3321
                                ),
3322
                        ),
3323
                        array(
3324
                                'ModelName' => array(
3325
                                        'id' => 4,
3326
                                        'parent_id' => 1
3327
                                ),
3328
                        ),
3329
                        array(
3330
                                'ModelName' => array(
3331
                                        'id' => 5,
3332
                                        'parent_id' => 1
3333
                                ),
3334
                        ),
3335
                        array(
3336
                                'ModelName' => array(
3337
                                        'id' => 6,
3338
                                        'parent_id' => null
3339
                                ),
3340
                        ),
3341
                        array(
3342
                                'ModelName' => array(
3343
                                        'id' => 7,
3344
                                        'parent_id' => 6
3345
                                ),
3346
                        ),
3347
                        array(
3348
                                'ModelName' => array(
3349
                                        'id' => 8,
3350
                                        'parent_id' => 6
3351
                                ),
3352
                        ),
3353
                        array(
3354
                                'ModelName' => array(
3355
                                        'id' => 9,
3356
                                        'parent_id' => 6
3357
                                ),
3358
                        ),
3359
                        array(
3360
                                'ModelName' => array(
3361
                                        'id' => 10,
3362
                                        'parent_id' => 6
3363
                                )
3364
                        )
3365
                );
3366
                $expected = array(
3367
                        array(
3368
                                'ModelName' => array(
3369
                                        'id' => 6,
3370
                                        'parent_id' => null
3371
                                ),
3372
                                'children' => array(
3373
                                        array(
3374
                                                'ModelName' => array(
3375
                                                        'id' => 7,
3376
                                                        'parent_id' => 6
3377
                                                ),
3378
                                                'children' => array()
3379
                                        ),
3380
                                        array(
3381
                                                'ModelName' => array(
3382
                                                        'id' => 8,
3383
                                                        'parent_id' => 6
3384
                                                ),
3385
                                                'children' => array()
3386
                                        ),
3387
                                        array(
3388
                                                'ModelName' => array(
3389
                                                        'id' => 9,
3390
                                                        'parent_id' => 6
3391
                                                ),
3392
                                                'children' => array()
3393
                                        ),
3394
                                        array(
3395
                                                'ModelName' => array(
3396
                                                        'id' => 10,
3397
                                                        'parent_id' => 6
3398
                                                ),
3399
                                                'children' => array()
3400
                                        )
3401
                                )
3402
                        )
3403
                );
3404
                $result = Set::nest($input, array('root' => 6));
3405
                $this->assertEquals($expected, $result);
3406
        }
3407
3408
/**
3409
 * test Set nest with a 1d array - this method should be able to handle any type of array input
3410
 *
3411
 * @return void
3412
 */
3413
        public function testNest1Dimensional() {
3414
                $input = array(
3415
                        array(
3416
                                'id' => 1,
3417
                                'parent_id' => null
3418
                        ),
3419
                        array(
3420
                                'id' => 2,
3421
                                'parent_id' => 1
3422
                        ),
3423
                        array(
3424
                                'id' => 3,
3425
                                'parent_id' => 1
3426
                        ),
3427
                        array(
3428
                                'id' => 4,
3429
                                'parent_id' => 1
3430
                        ),
3431
                        array(
3432
                                'id' => 5,
3433
                                'parent_id' => 1
3434
                        ),
3435
                        array(
3436
                                'id' => 6,
3437
                                'parent_id' => null
3438
                        ),
3439
                        array(
3440
                                'id' => 7,
3441
                                'parent_id' => 6
3442
                        ),
3443
                        array(
3444
                                'id' => 8,
3445
                                'parent_id' => 6
3446
                        ),
3447
                        array(
3448
                                'id' => 9,
3449
                                'parent_id' => 6
3450
                        ),
3451
                        array(
3452
                                'id' => 10,
3453
                                'parent_id' => 6
3454
                        )
3455
                );
3456
                $expected = array(
3457
                        array(
3458
                                'id' => 1,
3459
                                'parent_id' => null,
3460
                                'children' => array(
3461
                                        array(
3462
                                                'id' => 2,
3463
                                                'parent_id' => 1,
3464
                                                'children' => array()
3465
                                        ),
3466
                                        array(
3467
                                                'id' => 3,
3468
                                                'parent_id' => 1,
3469
                                                'children' => array()
3470
                                        ),
3471
                                        array(
3472
                                                'id' => 4,
3473
                                                'parent_id' => 1,
3474
                                                'children' => array()
3475
                                        ),
3476
                                        array(
3477
                                                'id' => 5,
3478
                                                'parent_id' => 1,
3479
                                                'children' => array()
3480
                                        ),
3481
3482
                                )
3483
                        ),
3484
                        array(
3485
                                'id' => 6,
3486
                                'parent_id' => null,
3487
                                'children' => array(
3488
                                        array(
3489
                                                'id' => 7,
3490
                                                'parent_id' => 6,
3491
                                                'children' => array()
3492
                                        ),
3493
                                        array(
3494
                                                'id' => 8,
3495
                                                'parent_id' => 6,
3496
                                                'children' => array()
3497
                                        ),
3498
                                        array(
3499
                                                'id' => 9,
3500
                                                'parent_id' => 6,
3501
                                                'children' => array()
3502
                                        ),
3503
                                        array(
3504
                                                'id' => 10,
3505
                                                'parent_id' => 6,
3506
                                                'children' => array()
3507
                                        )
3508
                                )
3509
                        )
3510
                );
3511
                $result = Set::nest($input, array('idPath' => '/id', 'parentPath' => '/parent_id'));
3512
                $this->assertEquals($expected, $result);
3513
        }
3514
3515
/**
3516
 * test Set nest with no specified parent data.
3517
 *
3518
 * The result should be the same as the input.
3519
 * For an easier comparison, unset all the empty children arrays from the result
3520
 *
3521
 * @return void
3522
 */
3523
        public function testMissingParent() {
3524
                $input = array(
3525
                        array(
3526
                                'id' => 1,
3527
                        ),
3528
                        array(
3529
                                'id' => 2,
3530
                        ),
3531
                        array(
3532
                                'id' => 3,
3533
                        ),
3534
                        array(
3535
                                'id' => 4,
3536
                        ),
3537
                        array(
3538
                                'id' => 5,
3539
                        ),
3540
                        array(
3541
                                'id' => 6,
3542
                        ),
3543
                        array(
3544
                                'id' => 7,
3545
                        ),
3546
                        array(
3547
                                'id' => 8,
3548
                        ),
3549
                        array(
3550
                                'id' => 9,
3551
                        ),
3552
                        array(
3553
                                'id' => 10,
3554
                        )
3555
                );
3556
3557
                $result = Set::nest($input, array('idPath' => '/id', 'parentPath' => '/parent_id'));
3558
                foreach ($result as &$row) {
3559
                        if (empty($row['children'])) {
3560
                                unset($row['children']);
3561
                        }
3562
                }
3563
                $this->assertEquals($input, $result);
3564
        }
3565
}