Completed
Push — master ( 013c48...b39f28 )
by Kurita
02:41
created

EagerLoaderBehaviorTest::testAfterFind()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 169
Code Lines 106

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 169
rs 8.2857
cc 1
eloc 106
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 4 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once App::pluginPath('EagerLoader') . 'Test' . DS . 'bootstrap.php';
3
4
class EagerLoaderBehaviorTest extends CakeTestCase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
6
/**
7
 * autoFixtures property
8
 *
9
 * @var bool
10
 */
11
	public $autoFixtures = false;
12
13
/**
14
 * Fixtures
15
 *
16
 * @var array
17
 */
18
	public $fixtures = array(
19
		'core.user',
20
		'core.article',
21
		'core.comment',
22
		'core.attachment',
23
		'core.tag',
24
		'core.articles_tag',
25
		'core.apple',
26
		'core.sample',
27
		'core.category',
28
		'plugin.EagerLoader.external_comment',
29
		'plugin.EagerLoader.profile',
30
	);
31
32
/**
33
 * Tests find('all')
34
 *
35
 * @param string $model Name of the model
36
 * @param array $options Options for find method
37
 * @param array $fixtures Fixtures to be used
38
 * @param int $expectedQueryCount Expected query count
39
 * @param array $expectedResults Expected results
40
 * @return void
41
 *
42
 * @dataProvider dataProviderForTestFindAll
43
 */
44
	public function testFindAll($model, $options, $fixtures, $expectedQueryCount, $expectedResults) {
45
		call_user_func_array(array($this, 'loadFixtures'), $fixtures);
46
47
		$model = ClassRegistry::init($model);
48
		$model->Behaviors->load('QueryCounter');
49
50
		$results = $model->find('all', $options);
51
52
		$this->assertEquals($expectedQueryCount, $model->queryCount());
53
		$this->assertEquals($expectedResults, $results);
54
	}
55
56
/**
57
 * Data provider for testFindAll
58
 *
59
 * @return array
60
 */
61
	public function dataProviderForTestFindAll() {
62
		return array(
63
			array(
64
				// {{{ #0
65
				'Attachment',
66
				array(
67
					'fields' => 'Attachment.id',
68
					'contain' => array(
69
						'Comment' => array('fields' => 'Comment.id'),
70
						'Comment.Article' => array('fields' => 'Article.id'),
71
						'Comment.Article.User' => array('fields' => 'User.id'),
72
					),
73
				),
74
				array('Attachment', 'Comment', 'Article', 'User'),
75
				1,
76
				array(
77
					array(
78
						'Attachment' => array(
79
							'id' => '1',
80
							'comment_id' => '5',
81
						),
82
						'Comment' => array(
83
							'id' => '5',
84
							'article_id' => '2',
85
							'Article' => array(
86
								'id' => '2',
87
								'user_id' => '3',
88
								'User' => array(
89
									'id' => '3',
90
								),
91
							),
92
						),
93
					),
94
				),
95
				// }}}
96
			),
97
			array(
98
				// {{{ #1
99
				'Article',
100
				array(
101
					'fields' => array('Article.id'),
102
					'contain' => array(
103
						'User' => array('fields' => 'User.id'),
104
						'Comment' => array(
105
							'fields' => 'Comment.id',
106
							'order' => 'Comment.id',
107
							'User' => array('fields' => 'User.id'),
108
						),
109
					),
110
				),
111
				array('Article', 'Comment', 'User'),
112
				2,
113
				array(
114
					array(
115
						'Article' => array(
116
							'id' => '1',
117
							'user_id' => '1',
118
						),
119
						'User' => array(
120
							'id' => '1',
121
						),
122
						'Comment' => array(
123
							array(
124
								'id' => '1',
125
								'article_id' => '1',
126
								'user_id' => '2',
127
								'User' => array(
128
									'id' => '2',
129
								),
130
							),
131
							array(
132
								'id' => '2',
133
								'article_id' => '1',
134
								'user_id' => '4',
135
								'User' => array(
136
									'id' => '4',
137
								),
138
							),
139
							array(
140
								'id' => '3',
141
								'article_id' => '1',
142
								'user_id' => '1',
143
								'User' => array(
144
									'id' => '1',
145
								),
146
							),
147
							array(
148
								'id' => '4',
149
								'article_id' => '1',
150
								'user_id' => '1',
151
								'User' => array(
152
									'id' => '1',
153
								),
154
							),
155
						),
156
					),
157
					array(
158
						'Article' => array(
159
							'id' => '2',
160
							'user_id' => '3',
161
						),
162
						'User' => array(
163
							'id' => '3',
164
						),
165
						'Comment' => array(
166
							array(
167
								'id' => '5',
168
								'article_id' => '2',
169
								'user_id' => '1',
170
								'User' => array(
171
									'id' => '1',
172
								),
173
							),
174
							array(
175
								'id' => '6',
176
								'article_id' => '2',
177
								'user_id' => '2',
178
								'User' => array(
179
									'id' => '2',
180
								),
181
							),
182
						),
183
					),
184
					array(
185
						'Article' => array(
186
							'id' => '3',
187
							'user_id' => '1',
188
						),
189
						'User' => array(
190
							'id' => '1',
191
						),
192
						'Comment' => array(),
193
					),
194
				),
195
				// }}}
196
			),
197
			array(
198
				// {{{ #2
199
				'User',
200
				array(
201
					'fields' => array('User.user'),
202
					'contain' => array(
203
						'Article' => array(
204
							'fields' => array('Article.title'),
205
							'limit' => 1,
206
						),
207
					),
208
				),
209
				array('User', 'Article'),
210
				5,
211
				array(
212
					array(
213
						'User' => array(
214
							'id' => '1',
215
							'user' => 'mariano',
216
						),
217
						'Article' => array(
218
							array(
219
								'user_id' => '1',
220
								'title' => 'First Article',
221
							),
222
						),
223
					),
224
					array(
225
						'User' => array(
226
							'id' => '2',
227
							'user' => 'nate',
228
						),
229
						'Article' => array(
230
						),
231
					),
232
					array(
233
						'User' => array(
234
							'id' => '3',
235
							'user' => 'larry',
236
						),
237
						'Article' => array(
238
							array(
239
								'user_id' => '3',
240
								'title' => 'Second Article',
241
							),
242
						),
243
					),
244
					array(
245
						'User' => array(
246
							'id' => '4',
247
							'user' => 'garrett',
248
						),
249
						'Article' => array(
250
						),
251
					),
252
				),
253
				// }}}
254
			),
255
			array(
256
				// {{{ #3
257
				'Article',
258
				array(
259
					'fields' => 'Article.id',
260
					'contain' => array(
261
						'Tag' => array(
262
							'fields' => array('Tag.tag'),
263
						),
264
					),
265
					'conditions' => array(
266
						'Article.id' => 1,
267
					),
268
				),
269
				array('Article', 'Tag', 'ArticlesTag'),
270
				2,
271
				array(
272
					array(
273
						'Article' => array(
274
							'id' => '1',
275
						),
276
						'Tag' => array(
277
							array(
278
								'id' => '1',
279
								'tag' => 'tag1',
280
								'ArticlesTag' => array(
281
									'article_id' => '1',
282
									'tag_id' => '1',
283
								),
284
							),
285
							array(
286
								'id' => '2',
287
								'tag' => 'tag2',
288
								'ArticlesTag' => array(
289
									'article_id' => '1',
290
									'tag_id' => '2',
291
								),
292
							),
293
						),
294
					),
295
				),
296
				// }}}
297
			),
298
			array(
299
				// {{{ #4
300
				'User',
301
				array(
302
					'fields' => 'User.id',
303
					'contain' => array(
304
						'Article' => array(
305
							'fields' => array('Article.id'),
306
							'conditions' => array('Article.user_id' => 3),
307
						),
308
					),
309
					'conditions' => array(
310
						'User.id' => array('1', '3'),
311
					),
312
				),
313
				array('User', 'Article'),
314
				2,
315
				array(
316
					array(
317
						'User' => array(
318
							'id' => '1',
319
						),
320
						'Article' => array(),
321
					),
322
					array(
323
						'User' => array(
324
							'id' => '3',
325
						),
326
						'Article' => array(
327
							array(
328
								'id' => '2',
329
								'user_id' => '3',
330
							),
331
						),
332
					),
333
				),
334
				// }}}
335
			),
336
			array(
337
				// {{{ #5
338
				'Article',
339
				array(
340
					'fields' => 'Article.id',
341
					'contain' => array(
342
						'FirstComment' => array('fields' => 'id'),
343
						'SecondComment' => array('fields' => 'id'),
344
					),
345
				),
346
				array('Article', 'Comment'),
347
				7,
348
				array(
349
					array(
350
						'Article' => array(
351
							'id' => '1',
352
						),
353
						'FirstComment' => array(
354
							'id' => '1',
355
							'article_id' => '1',
356
						),
357
						'SecondComment' => array(
358
							'id' => '2',
359
							'article_id' => '1',
360
						),
361
					),
362
					array(
363
						'Article' => array(
364
							'id' => '2',
365
						),
366
						'FirstComment' => array(
367
							'id' => '5',
368
							'article_id' => '2',
369
						),
370
						'SecondComment' => array(
371
							'id' => '6',
372
							'article_id' => '2',
373
						),
374
					),
375
					array(
376
						'Article' => array(
377
							'id' => '3',
378
						),
379
						'FirstComment' => array(),
380
						'SecondComment' => array(),
381
					),
382
				),
383
				// }}}
384
			),
385
		);
386
	}
387
388
/**
389
 * Tests that find('list') also works
390
 *
391
 * @return void
392
 */
393
	public function testFindList() {
394
		$this->loadFixtures('Article', 'User');
395
396
		$Article = ClassRegistry::init('Article');
397
		$result = $Article->find('list', array(
398
			'fields' => array('Article.id', 'User.id'),
399
			'contain' => array('User')
400
		));
401
402
		$expected = array(
403
			1 => 1,
404
			2 => 3,
405
			3 => 1,
406
		);
407
408
		$this->assertEquals($expected, $result);
409
	}
410
411
/**
412
 * Tests that afterFind is called correctly
413
 *
414
 * @return void
415
 */
416
	public function testAfterFind() {
417
		$this->loadFixtures('Comment', 'Tag', 'ArticlesTag', 'Article', 'User', 'Profile', 'Attachment');
418
419
		$Comment = $this->getMockForModel('Comment', array('afterFind'));
420
		$Comment->expects($this->once())
421
			->method('afterFind')
422
			->with(
423
				// {{{
424
				array(
425
					array(
426
						'Comment' => array(
427
							'id' => '5',
428
							'article_id' => '2',
429
						),
430
						'Article' => array(
431
							'id' => '2',
432
							'user_id' => '3',
433
							'User' => array(
434
								'id' => '3',
435
								'Profile' => array(
436
									'id' => '1',
437
									'user_id' => '3',
438
								),
439
							),
440
							'Tag' => array(
441
								array(
442
									'id' => '1',
443
									'ArticlesTag' => array(
444
										'article_id' => '2',
445
										'tag_id' => '1',
446
									),
447
								),
448
								array(
449
									'id' => '3',
450
									'ArticlesTag' => array(
451
										'article_id' => '2',
452
										'tag_id' => '3',
453
									),
454
								),
455
							),
456
						),
457
						'Attachment' => array(
458
							'id' => '1',
459
							'comment_id' => '5',
460
						),
461
					),
462
				),
463
				true
464
				//}}}
465
			);
466
467
		$Tag = $this->getMockForModel('Tag', array('afterFind'));
468
		$Tag->expects($this->at(0))
469
			->method('afterFind')
470
			->with(
471
				// {{{
472
				array(
473
					array(
474
						'Tag' => array(
475
							'id' => '1',
476
						),
477
					),
478
				),
479
				false
480
				// }}}
481
			)
482
			->will($this->returnArgument(0));
483
484
		$Tag->expects($this->at(1))
485
			->method('afterFind')
486
			->with(
487
				// {{{
488
				array(
489
					array(
490
						'Tag' => array(
491
							'id' => '3',
492
						),
493
					),
494
				),
495
				false
496
				// }}}
497
			)
498
			->will($this->returnArgument(0));
499
500
		$Article = $this->getMockForModel('Article', array('afterFind'));
501
		$Article->expects($this->once())
502
			->method('afterFind')
503
			->with(
504
				// {{{
505
				array(
506
					array(
507
						'Article' => array(
508
							'id' => '2',
509
							'user_id' => '3',
510
						),
511
					)
512
				),
513
				false
514
				// }}}
515
			)
516
			->will($this->returnArgument(0));
517
518
		$User = $this->getMockForModel('User', array('afterFind'));
519
		$User->expects($this->once())
520
			->method('afterFind')
521
			->with(
522
				// {{{
523
				array(
524
					array(
525
						'User' => array(
526
							'id' => '3',
527
						),
528
					),
529
				),
530
				false
531
				// }}}
532
			)
533
			->will($this->returnArgument(0));
534
535
		$Profile = $this->getMockForModel('Profile', array('afterFind'));
536
		$Profile->expects($this->once())
537
			->method('afterFind')
538
			->with(
539
				// {{{
540
				array(
541
					array(
542
						'Profile' => array(
543
							'id' => '1',
544
							'user_id' => '3',
545
						),
546
					),
547
				),
548
				false
549
				// }}}
550
			)
551
			->will($this->returnArgument(0));
552
553
		$Attachment = $this->getMockForModel('Attachment', array('afterFind'));
554
		$Attachment->expects($this->once())
555
			->method('afterFind')
556
			->with(
557
				// {{{
558
				array(
559
					array(
560
						'Attachment' => array(
561
							'id' => '1',
562
							'comment_id' => '5',
563
						),
564
					),
565
				),
566
				false
567
				// }}}
568
			)
569
			->will($this->returnArgument(0));
570
571
		$result = $Comment->find('first', array(
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
572
			'fields' => 'Comment.id',
573
			'contain' => array(
574
				'Article' => array('fields' => 'Article.id'),
575
				'Article.Tag' => array('fields' => 'Tag.id'),
576
				'Article.User' => array('fields' => 'User.id'),
577
				'Article.User.Profile' => array('fields' => 'Profile.id'),
578
				'Attachment' => array('fields' => 'Attachment.id'),
579
			),
580
			'conditions' => array(
581
				'Comment.id' => 5,
582
			),
583
		));
584
	}
585
586
/**
587
 * Tests that afterFind works for in case of getting empty results.
588
 *
589
 * @return void
590
 */
591
	public function testAfterFindNoResults() {
592
		$this->loadFixtures('User', 'Article');
593
594
		$User = ClassRegistry::init('User');
595
596
		$user = $User->find('all', array(
597
			'contain' => 'Article',
598
			'conditions' => '1 != 1',
599
		));
600
601
		$this->assertSame(array(), $user);
602
	}
603
604
/**
605
 * Tests no contain
606
 *
607
 * @return void
608
 */
609
	public function testNoContain() {
610
		$this->loadFixtures('User', 'Article');
611
		$User = ClassRegistry::init('User');
612
		$result = $User->find('first', array('contain' => false));
613
		$this->assertFalse(isset($result['Article']));
614
	}
615
616
/**
617
 * Tests external datasource
618
 *
619
 * @return void
620
 */
621
	public function testExternalDatasource() {
622
		$this->loadFixtures('Article', 'ExternalComment');
623
624
		$Article = ClassRegistry::init('Article');
625
626
		$result = $Article->find('first', array(
627
			'fields' => 'id',
628
			'contain' => 'ExternalComment',
629
			'conditions' => array('id' => 3),
630
		));
631
632
		$expected = array(
633
			'Article' => array(
634
				'id' => 3,
635
			),
636
			'ExternalComment' => array(
637
				array(
638
					'id' => 1,
639
					'article_id' => 3,
640
					'comment' => 'External Comment',
641
				),
642
			)
643
		);
644
645
		$this->assertFalse($Article->useDbConfig === $Article->ExternalComment->useDbConfig);
646
		$this->assertEquals($expected, $result);
647
	}
648
649
/**
650
 * Tests that EagerLoaderBehavior can be coexistent with ContainableBehavior
651
 *
652
 * @return void
653
 */
654
	public function testCoexistentWithContainableBehavior() {
655
		$this->loadFixtures('Comment', 'Article', 'User');
656
657
		$expected = array(
658
			// {{{
659
			array(
660
				'Comment' => array(
661
					'id' => 1, 'article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article',
662
					'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'
663
				),
664
				'Article' => array(
665
					'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body',
666
					'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
667
					'User' => array(
668
						'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
669
						'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
670
					),
671
				),
672
			),
673
			array(
674
				'Comment' => array(
675
					'id' => 2, 'article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article',
676
					'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'
677
				),
678
				'Article' => array(
679
					'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body',
680
					'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
681
					'User' => array(
682
						'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
683
						'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
684
					),
685
				),
686
			),
687
			array(
688
				'Comment' => array(
689
					'id' => 3, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article',
690
					'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'
691
				),
692
				'Article' => array(
693
					'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body',
694
					'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
695
					'User' => array(
696
						'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
697
						'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
698
					),
699
				),
700
			),
701
			array(
702
				'Comment' => array(
703
					'id' => 4, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article',
704
					'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'
705
				),
706
				'Article' => array(
707
					'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body',
708
					'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
709
					'User' => array(
710
						'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
711
						'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
712
					),
713
				),
714
			),
715
			array(
716
				'Comment' => array(
717
					'id' => 5, 'article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article',
718
					'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'
719
				),
720
				'Article' => array(
721
					'id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body',
722
					'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31',
723
					'User' => array(
724
						'id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
725
						'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'
726
					),
727
				),
728
			),
729
			array(
730
				'Comment' => array(
731
					'id' => 6, 'article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article',
732
					'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31'
733
				),
734
				'Article' => array(
735
					'id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body',
736
					'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31',
737
					'User' => array(
738
						'id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
739
						'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'
740
					),
741
				),
742
			),
743
			// }}}
744
		);
745
746
		$options = array('contain' => 'Article.User');
747
748
		$Comment = ClassRegistry::init('Comment');
749
		$Comment->Behaviors->load('QueryCounter');
750
751
		$results = $Comment->find('all', $options);
752
		$this->assertEquals(1, $Comment->queryCount());
753
		$this->assertEquals($expected, $results);
754
755
		$Comment->Behaviors->load('Containable');
756
		$results = $Comment->find('all', $options);
757
		$this->assertEquals(1, $Comment->queryCount());
758
		$this->assertEquals($expected, $results);
759
760
		$Comment->Behaviors->disable('EagerLoader');
761
		$results = $Comment->find('all', $options);
762
		$this->assertEquals(7, $Comment->queryCount()); // ContainableBehavior cannot load deep associations eagerly
763
		$this->assertEquals($expected, $results);
764
	}
765
766
/**
767
 * Tests caling find method in afterFind
768
 *
769
 * @return void
770
 */
771
	public function testCallingFindInAfterFind() {
772
		$this->loadFixtures('Apple', 'Sample');
773
774
		$Apple = ClassRegistry::init('Apple');
775
776
		$options = array(
777
			'fields' => array(
778
				'Apple.id',
779
			),
780
			'contain' => 'SampleA',
781
			'conditions' => array('Apple.id' => 3),
782
		);
783
784
		$expected = array(
785
			// {{{
786
			'Apple' => array(
787
				'id' => '3',
788
			),
789
			'SampleA' => array(
790
				'id' => '1',
791
				'apple_id' => '3',
792
				'name' => 'sample1',
793
				'Apple' => array(
794
					'id' => '1',
795
					'apple_id' => '2',
796
					'ParentApple' => array(
797
						'id' => '2',
798
						'name' => 'Bright Red Apple'
799
					)
800
				)
801
			)
802
			// }}}
803
		);
804
		$result = $Apple->find('first', $options);
805
		$this->assertEquals($expected, $result);
806
807
		$Apple->Behaviors->load('Containable');
808
		$Apple->Behaviors->disable('EagerLoader');
809
810
		$expected = array(
811
			// {{{
812
			'Apple' => array(
813
				'id' => '3',
814
			),
815
			'SampleA' => array(
816
				'id' => '1',
817
				'apple_id' => '3',
818
				'name' => 'sample1',
819
				'Apple' => array(
820
					'id' => '1',
821
					'apple_id' => '2',
822
					'ParentApple' => array(
823
						'id' => '2',
824
						'name' => 'Bright Red Apple'
825
					)
826
				)
827
			),
828
			'Sample' => array( // ContainableBehavior will contain Sample wrongly
829
				array(
830
					'id' => '1',
831
					'apple_id' => '3',
832
					'name' => 'sample1',
833
					'Apple' => array(
834
						'id' => '1',
835
						'apple_id' => '2',
836
						'ParentApple' => array(
837
							'id' => '2',
838
							'name' => 'Bright Red Apple'
839
						)
840
					)
841
				)
842
			),
843
			// }}}
844
		);
845
		$result = $Apple->find('first', $options);
846
		$this->assertEquals($expected, $result);
847
	}
848
849
/**
850
 * Tests non-existent belongsTo association
851
 *
852
 * @return void
853
 */
854
	public function testNonExistentBelongsTo() {
855
		$this->loadFixtures('Category');
856
857
		$Category = ClassRegistry::init('Category');
858
		$result = $Category->find('first', array(
859
			'contain' => array(
860
				'ParentCategory'
861
			),
862
		));
863
864
		$expected = array(
865
			'Category' => array(
866
				'id' => '1',
867
				'parent_id' => '0',
868
				'name' => 'Category 1',
869
				'created' => '2007-03-18 15:30:23',
870
				'updated' => '2007-03-18 15:32:31',
871
				'is_root' => 1,
872
			),
873
			'ParentCategory' => array(),
874
		);
875
876
		$this->assertEquals($expected, $result);
877
	}
878
879
/**
880
 * Tests duplicated aliases
881
 *
882
 * @return void
883
 */
884
	public function testDuplicatedAliases() {
885
		$this->loadFixtures('Comment', 'Article', 'User', 'Profile');
886
887
		$Comment = ClassRegistry::init('Comment');
888
		$Comment->Behaviors->load('QueryCounter');
889
890
		$result = $Comment->find('first', array(
891
			'contain' => array(
892
				'Article.User.Profile',
893
				'User',
894
			),
895
			'conditions' => array('Comment.id' => 5),
896
		));
897
898
		$expected = array(
899
			// {{{
900
			'Comment' => array(
901
				'id' => '5',
902
				'article_id' => '2',
903
				'user_id' => '1',
904
				'comment' => 'First Comment for Second Article',
905
				'published' => 'Y',
906
				'created' => '2007-03-18 10:53:23',
907
				'updated' => '2007-03-18 10:55:31'
908
			),
909
			'Article' => array(
910
				'id' => '2',
911
				'user_id' => '3',
912
				'title' => 'Second Article',
913
				'body' => 'Second Article Body',
914
				'published' => 'Y',
915
				'created' => '2007-03-18 10:41:23',
916
				'updated' => '2007-03-18 10:43:31',
917
				'User' => array(
918
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
919
					'id' => '3',
920
					'user' => 'larry',
921
					'created' => '2007-03-17 01:20:23',
922
					'updated' => '2007-03-17 01:22:31',
923
					'Profile' => array(
924
						'id' => '1',
925
						'user_id' => '3',
926
						'nickname' => 'phpnut',
927
						'company' => 'Cake Software Foundation, Inc.'
928
					)
929
				)
930
			),
931
			'User' => array(
932
				'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
933
				'id' => '1',
934
				'user' => 'mariano',
935
				'created' => '2007-03-17 01:16:23',
936
				'updated' => '2007-03-17 01:18:31'
937
			)
938
			// }}}
939
		);
940
941
		$this->assertEquals(2, $Comment->queryCount());
942
		$this->assertEquals($expected, $result);
943
	}
944
945
/**
946
 * Tests virtual fields
947
 *
948
 * @return void
949
 */
950
	public function testVirtualFields() {
951
		$this->loadFixtures('Category');
952
953
		$Category = ClassRegistry::init('Category');
954
		$results = $Category->find('all', array(
955
			'fields' => array(
956
				'Category.id',
957
				'Category.parent_id',
958
				'Category.is_root',
959
			),
960
			'contain' => array(
961
				'ParentCategory' => array(
962
					'fields' => array('id', 'parent_id', 'is_root'),
963
					'conditions' => array(),
964
				),
965
			),
966
			'conditions' => array(
967
				'Category.is_root' => 0,
968
				'ParentCategory.is_root' => 1,
969
			),
970
			'order' => array(
971
				'ParentCategory.is_root',
972
			),
973
		));
974
975
		$expected = array(
976
			// {{{
977
			array(
978
				'Category' => array(
979
					'id' => 2,
980
					'parent_id' => 1,
981
					'is_root' => 0,
982
				),
983
				'ParentCategory' => array(
984
					'id' => 1,
985
					'parent_id' => 0,
986
					'is_root' => 1,
987
				),
988
			),
989
			array(
990
				'Category' => array(
991
					'id' => 3,
992
					'parent_id' => 1,
993
					'is_root' => 0,
994
				),
995
				'ParentCategory' => array(
996
					'id' => 1,
997
					'parent_id' => 0,
998
					'is_root' => 1,
999
				),
1000
			),
1001
			array(
1002
				'Category' => array(
1003
					'id' => 6,
1004
					'parent_id' => 5,
1005
					'is_root' => 0,
1006
				),
1007
				'ParentCategory' => array(
1008
					'id' => 5,
1009
					'parent_id' => 0,
1010
					'is_root' => 1,
1011
				),
1012
			),
1013
			// }}}
1014
		);
1015
1016
		$this->assertEquals($expected, $results);
1017
	}
1018
}
1019