EagerLoaderBehaviorTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 1148
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 0
dl 0
loc 1148
rs 9.2173
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testFindAll() 0 11 1
B dataProviderForTestFindAll() 0 499 1
A testFindList() 0 17 1
B testAfterFind() 0 169 1
A testAfterFindNoResults() 0 12 1
A testNoContain() 0 6 1
B testExternalDatasource() 0 27 1
B testCoexistentWithContainableBehavior() 0 111 1
B testCallingFindInAfterFind() 0 36 1
B testNonExistentBelongsTo() 0 24 1
A testDuplicatedAliases() 0 60 1
A testVirtualFields() 0 68 1
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
		'plugin.EagerLoader.articles_category',
31
	);
32
33
/**
34
 * Tests find('all')
35
 *
36
 * @param string $model Name of the model
37
 * @param array $options Options for find method
38
 * @param array $fixtures Fixtures to be used
39
 * @param int $expectedQueryCount Expected query count
40
 * @param array $expectedResults Expected results
41
 * @return void
42
 *
43
 * @dataProvider dataProviderForTestFindAll
44
 */
45
	public function testFindAll($model, $options, $fixtures, $expectedQueryCount, $expectedResults) {
46
		call_user_func_array(array($this, 'loadFixtures'), $fixtures);
47
48
		$model = ClassRegistry::init($model);
49
		$model->Behaviors->load('QueryCounter');
50
51
		$results = $model->find('all', $options);
52
53
		$this->assertEquals($expectedQueryCount, $model->queryCount());
54
		$this->assertEquals($expectedResults, $results);
55
	}
56
57
/**
58
 * Data provider for testFindAll
59
 *
60
 * @return array
61
 */
62
	public function dataProviderForTestFindAll() {
63
		return array(
64
			array(
65
				// {{{ #0
66
				'Attachment',
67
				array(
68
					'fields' => 'Attachment.id',
69
					'contain' => array(
70
						'Comment' => array('fields' => 'Comment.id'),
71
						'Comment.Article' => array('fields' => 'Article.id'),
72
						'Comment.Article.User' => array('fields' => 'User.id'),
73
					),
74
				),
75
				array('Attachment', 'Comment', 'Article', 'User'),
76
				1,
77
				array(
78
					array(
79
						'Attachment' => array(
80
							'id' => '1',
81
							'comment_id' => '5',
82
						),
83
						'Comment' => array(
84
							'id' => '5',
85
							'article_id' => '2',
86
							'Article' => array(
87
								'id' => '2',
88
								'user_id' => '3',
89
								'User' => array(
90
									'id' => '3',
91
								),
92
							),
93
						),
94
					),
95
				),
96
				// }}}
97
			),
98
			array(
99
				// {{{ #1
100
				'Article',
101
				array(
102
					'fields' => array('Article.id'),
103
					'contain' => array(
104
						'User' => array('fields' => 'User.id'),
105
						'Comment' => array(
106
							'fields' => 'Comment.id',
107
							'order' => 'Comment.id',
108
							'User' => array('fields' => 'User.id'),
109
						),
110
					),
111
				),
112
				array('Article', 'Comment', 'User'),
113
				2,
114
				array(
115
					array(
116
						'Article' => array(
117
							'id' => '1',
118
							'user_id' => '1',
119
						),
120
						'User' => array(
121
							'id' => '1',
122
						),
123
						'Comment' => array(
124
							array(
125
								'id' => '1',
126
								'article_id' => '1',
127
								'user_id' => '2',
128
								'User' => array(
129
									'id' => '2',
130
								),
131
							),
132
							array(
133
								'id' => '2',
134
								'article_id' => '1',
135
								'user_id' => '4',
136
								'User' => array(
137
									'id' => '4',
138
								),
139
							),
140
							array(
141
								'id' => '3',
142
								'article_id' => '1',
143
								'user_id' => '1',
144
								'User' => array(
145
									'id' => '1',
146
								),
147
							),
148
							array(
149
								'id' => '4',
150
								'article_id' => '1',
151
								'user_id' => '1',
152
								'User' => array(
153
									'id' => '1',
154
								),
155
							),
156
						),
157
					),
158
					array(
159
						'Article' => array(
160
							'id' => '2',
161
							'user_id' => '3',
162
						),
163
						'User' => array(
164
							'id' => '3',
165
						),
166
						'Comment' => array(
167
							array(
168
								'id' => '5',
169
								'article_id' => '2',
170
								'user_id' => '1',
171
								'User' => array(
172
									'id' => '1',
173
								),
174
							),
175
							array(
176
								'id' => '6',
177
								'article_id' => '2',
178
								'user_id' => '2',
179
								'User' => array(
180
									'id' => '2',
181
								),
182
							),
183
						),
184
					),
185
					array(
186
						'Article' => array(
187
							'id' => '3',
188
							'user_id' => '1',
189
						),
190
						'User' => array(
191
							'id' => '1',
192
						),
193
						'Comment' => array(),
194
					),
195
				),
196
				// }}}
197
			),
198
			array(
199
				// {{{ #2
200
				'User',
201
				array(
202
					'fields' => array('User.user'),
203
					'contain' => array(
204
						'Article' => array(
205
							'fields' => array('Article.title'),
206
							'limit' => 1,
207
						),
208
					),
209
				),
210
				array('User', 'Article'),
211
				5,
212
				array(
213
					array(
214
						'User' => array(
215
							'id' => '1',
216
							'user' => 'mariano',
217
						),
218
						'Article' => array(
219
							array(
220
								'user_id' => '1',
221
								'title' => 'First Article',
222
							),
223
						),
224
					),
225
					array(
226
						'User' => array(
227
							'id' => '2',
228
							'user' => 'nate',
229
						),
230
						'Article' => array(
231
						),
232
					),
233
					array(
234
						'User' => array(
235
							'id' => '3',
236
							'user' => 'larry',
237
						),
238
						'Article' => array(
239
							array(
240
								'user_id' => '3',
241
								'title' => 'Second Article',
242
							),
243
						),
244
					),
245
					array(
246
						'User' => array(
247
							'id' => '4',
248
							'user' => 'garrett',
249
						),
250
						'Article' => array(
251
						),
252
					),
253
				),
254
				// }}}
255
			),
256
			array(
257
				// {{{ #3
258
				'Article',
259
				array(
260
					'fields' => 'Article.id',
261
					'contain' => array(
262
						'Tag' => array(
263
							'fields' => array('Tag.tag'),
264
						),
265
					),
266
					'conditions' => array(
267
						'Article.id' => 1,
268
					),
269
				),
270
				array('Article', 'Tag', 'ArticlesTag'),
271
				2,
272
				array(
273
					array(
274
						'Article' => array(
275
							'id' => '1',
276
						),
277
						'Tag' => array(
278
							array(
279
								'id' => '1',
280
								'tag' => 'tag1',
281
								'ArticlesTag' => array(
282
									'article_id' => '1',
283
									'tag_id' => '1',
284
								),
285
							),
286
							array(
287
								'id' => '2',
288
								'tag' => 'tag2',
289
								'ArticlesTag' => array(
290
									'article_id' => '1',
291
									'tag_id' => '2',
292
								),
293
							),
294
						),
295
					),
296
				),
297
				// }}}
298
			),
299
			array(
300
				// {{{ #4
301
				'User',
302
				array(
303
					'fields' => 'User.id',
304
					'contain' => array(
305
						'Article' => array(
306
							'fields' => array('Article.id'),
307
							'conditions' => array('Article.user_id' => 3),
308
						),
309
					),
310
					'conditions' => array(
311
						'User.id' => array('1', '3'),
312
					),
313
				),
314
				array('User', 'Article'),
315
				2,
316
				array(
317
					array(
318
						'User' => array(
319
							'id' => '1',
320
						),
321
						'Article' => array(),
322
					),
323
					array(
324
						'User' => array(
325
							'id' => '3',
326
						),
327
						'Article' => array(
328
							array(
329
								'id' => '2',
330
								'user_id' => '3',
331
							),
332
						),
333
					),
334
				),
335
				// }}}
336
			),
337
			array(
338
				// {{{ #5
339
				'Article',
340
				array(
341
					'fields' => 'Article.id',
342
					'contain' => array(
343
						'FirstComment' => array('fields' => 'id'),
344
						'SecondComment' => array('fields' => 'id'),
345
					),
346
				),
347
				array('Article', 'Comment'),
348
				7,
349
				array(
350
					array(
351
						'Article' => array(
352
							'id' => '1',
353
						),
354
						'FirstComment' => array(
355
							'id' => '1',
356
							'article_id' => '1',
357
						),
358
						'SecondComment' => array(
359
							'id' => '2',
360
							'article_id' => '1',
361
						),
362
					),
363
					array(
364
						'Article' => array(
365
							'id' => '2',
366
						),
367
						'FirstComment' => array(
368
							'id' => '5',
369
							'article_id' => '2',
370
						),
371
						'SecondComment' => array(
372
							'id' => '6',
373
							'article_id' => '2',
374
						),
375
					),
376
					array(
377
						'Article' => array(
378
							'id' => '3',
379
						),
380
						'FirstComment' => array(),
381
						'SecondComment' => array(),
382
					),
383
				),
384
				// }}}
385
			),
386
			array(
387
				// {{{ #6
388
				'Article',
389
				array(
390
					'fields' => 'Article.id',
391
					'contain' => array(
392
						'FirstComment' => array('fields' => 'id'),
393
						'SecondComment' => array('fields' => 'id'),
394
					),
395
				),
396
				array('Article', 'Comment'),
397
				7,
398
				array(
399
					array(
400
						'Article' => array(
401
							'id' => '1',
402
						),
403
						'FirstComment' => array(
404
							'id' => '1',
405
							'article_id' => '1',
406
						),
407
						'SecondComment' => array(
408
							'id' => '2',
409
							'article_id' => '1',
410
						),
411
					),
412
					array(
413
						'Article' => array(
414
							'id' => '2',
415
						),
416
						'FirstComment' => array(
417
							'id' => '5',
418
							'article_id' => '2',
419
						),
420
						'SecondComment' => array(
421
							'id' => '6',
422
							'article_id' => '2',
423
						),
424
					),
425
					array(
426
						'Article' => array(
427
							'id' => '3',
428
						),
429
						'FirstComment' => array(),
430
						'SecondComment' => array(),
431
					),
432
				),
433
				// }}}
434
			),
435
			array(
436
				// {{{ #7 Deep associations
437
				'User',
438
				array(
439
					'fields' => 'User.id',
440
					'contain' => array(
441
						'Article' => array(
442
							'fields' => array('Article.id'),
443
							'User' => array(
444
								'fields' => array('User.id'),
445
								'Profile' => array(
446
									'fields' => array('Profile.id'),
447
								),
448
								'Article' => array(
449
									'fields' => array('Article.id'),
450
									'User' => array(
451
										'fields' => array('User.id'),
452
									),
453
									'Tag' => array(
454
										'fields' => array('Tag.id'),
455
									),
456
								),
457
							),
458
						),
459
					),
460
					'conditions' => array(
461
						'User.id' => '3',
462
					),
463
				),
464
				array('User', 'Article', 'Profile', 'ArticlesTag', 'Tag'),
465
				4,
466
				array(
467
					array(
468
						'User' => array(
469
							'id' => 3,
470
						),
471
						'Article' => array(
472
							array(
473
								'id' => '2',
474
								'user_id' => '3',
475
								'User' => array(
476
									'id' => '3',
477
									'Profile' => array(
478
										'id' => '1',
479
										'user_id' => '3',
480
									),
481
									'Article' => array(
482
										array(
483
											'id' => '2',
484
											'user_id' => '3',
485
											'User' => array(
486
												'id' => '3',
487
											),
488
											'Tag' => array(
489
												array(
490
													'id' => '1',
491
													'ArticlesTag' => array(
492
														'article_id' => '2',
493
														'tag_id' => '1',
494
													),
495
												),
496
												array(
497
													'id' => '3',
498
													'ArticlesTag' => array(
499
														'article_id' => '2',
500
														'tag_id' => '3',
501
													),
502
												),
503
											),
504
										),
505
									),
506
								),
507
							),
508
						),
509
					),
510
				),
511
				// }}}
512
			),
513
			array(
514
				// {{{ #8 Parent not exists
515
				'Category',
516
				array(
517
					'fields' => 'Category.id',
518
					'contain' => array(
519
						'ParentCategory' => array(
520
							'fields' => array('ParentCategory.id'),
521
							'Article' => array(
522
								'fields' => array('Article.id'),
523
							),
524
						),
525
					),
526
					'conditions' => array('Category.id' => array(1, 2)),
527
				),
528
				array('Category', 'Article', 'ArticlesCategory'),
529
				2,
530
				array(
531
					array(
532
						'Category' => array(
533
							'id' => '1',
534
							'parent_id' => '0',
535
						),
536
						'ParentCategory' => array(),
537
					),
538
					array(
539
						'Category' => array(
540
							'id' => '2',
541
							'parent_id' => '1',
542
						),
543
						'ParentCategory' => array(
544
							'id' => '1',
545
							'Article' => array(
546
								array(
547
									'id' => '1',
548
									'ArticlesCategory' => array(
549
										'article_id' => '1',
550
										'category_id' => '1',
551
									),
552
								),
553
							),
554
						),
555
					),
556
				),
557
				// }}}
558
			),
559
		);
560
	}
561
562
/**
563
 * Tests that find('list') also works
564
 *
565
 * @return void
566
 */
567
	public function testFindList() {
568
		$this->loadFixtures('Article', 'User');
569
570
		$Article = ClassRegistry::init('Article');
571
		$result = $Article->find('list', array(
572
			'fields' => array('Article.id', 'User.id'),
573
			'contain' => array('User')
574
		));
575
576
		$expected = array(
577
			1 => 1,
578
			2 => 3,
579
			3 => 1,
580
		);
581
582
		$this->assertEquals($expected, $result);
583
	}
584
585
/**
586
 * Tests that afterFind is called correctly
587
 *
588
 * @return void
589
 */
590
	public function testAfterFind() {
591
		$this->loadFixtures('Comment', 'Tag', 'ArticlesTag', 'Article', 'User', 'Profile', 'Attachment');
592
593
		$Comment = $this->getMockForModel('Comment', array('afterFind'));
594
		$Comment->expects($this->once())
595
			->method('afterFind')
596
			->with(
597
				// {{{
598
				array(
599
					array(
600
						'Comment' => array(
601
							'id' => '5',
602
							'article_id' => '2',
603
						),
604
						'Article' => array(
605
							'id' => '2',
606
							'user_id' => '3',
607
							'User' => array(
608
								'id' => '3',
609
								'Profile' => array(
610
									'id' => '1',
611
									'user_id' => '3',
612
								),
613
							),
614
							'Tag' => array(
615
								array(
616
									'id' => '1',
617
									'ArticlesTag' => array(
618
										'article_id' => '2',
619
										'tag_id' => '1',
620
									),
621
								),
622
								array(
623
									'id' => '3',
624
									'ArticlesTag' => array(
625
										'article_id' => '2',
626
										'tag_id' => '3',
627
									),
628
								),
629
							),
630
						),
631
						'Attachment' => array(
632
							'id' => '1',
633
							'comment_id' => '5',
634
						),
635
					),
636
				),
637
				true
638
				//}}}
639
			);
640
641
		$Tag = $this->getMockForModel('Tag', array('afterFind'));
642
		$Tag->expects($this->at(0))
643
			->method('afterFind')
644
			->with(
645
				// {{{
646
				array(
647
					array(
648
						'Tag' => array(
649
							'id' => '1',
650
						),
651
					),
652
				),
653
				false
654
				// }}}
655
			)
656
			->will($this->returnArgument(0));
657
658
		$Tag->expects($this->at(1))
659
			->method('afterFind')
660
			->with(
661
				// {{{
662
				array(
663
					array(
664
						'Tag' => array(
665
							'id' => '3',
666
						),
667
					),
668
				),
669
				false
670
				// }}}
671
			)
672
			->will($this->returnArgument(0));
673
674
		$Article = $this->getMockForModel('Article', array('afterFind'));
675
		$Article->expects($this->once())
676
			->method('afterFind')
677
			->with(
678
				// {{{
679
				array(
680
					array(
681
						'Article' => array(
682
							'id' => '2',
683
							'user_id' => '3',
684
						),
685
					)
686
				),
687
				false
688
				// }}}
689
			)
690
			->will($this->returnArgument(0));
691
692
		$User = $this->getMockForModel('User', array('afterFind'));
693
		$User->expects($this->once())
694
			->method('afterFind')
695
			->with(
696
				// {{{
697
				array(
698
					array(
699
						'User' => array(
700
							'id' => '3',
701
						),
702
					),
703
				),
704
				false
705
				// }}}
706
			)
707
			->will($this->returnArgument(0));
708
709
		$Profile = $this->getMockForModel('Profile', array('afterFind'));
710
		$Profile->expects($this->once())
711
			->method('afterFind')
712
			->with(
713
				// {{{
714
				array(
715
					array(
716
						'Profile' => array(
717
							'id' => '1',
718
							'user_id' => '3',
719
						),
720
					),
721
				),
722
				false
723
				// }}}
724
			)
725
			->will($this->returnArgument(0));
726
727
		$Attachment = $this->getMockForModel('Attachment', array('afterFind'));
728
		$Attachment->expects($this->once())
729
			->method('afterFind')
730
			->with(
731
				// {{{
732
				array(
733
					array(
734
						'Attachment' => array(
735
							'id' => '1',
736
							'comment_id' => '5',
737
						),
738
					),
739
				),
740
				false
741
				// }}}
742
			)
743
			->will($this->returnArgument(0));
744
745
		$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...
746
			'fields' => 'Comment.id',
747
			'contain' => array(
748
				'Article' => array('fields' => 'Article.id'),
749
				'Article.Tag' => array('fields' => 'Tag.id'),
750
				'Article.User' => array('fields' => 'User.id'),
751
				'Article.User.Profile' => array('fields' => 'Profile.id'),
752
				'Attachment' => array('fields' => 'Attachment.id'),
753
			),
754
			'conditions' => array(
755
				'Comment.id' => 5,
756
			),
757
		));
758
	}
759
760
/**
761
 * Tests that afterFind works for in case of getting empty results.
762
 *
763
 * @return void
764
 */
765
	public function testAfterFindNoResults() {
766
		$this->loadFixtures('User', 'Article');
767
768
		$User = ClassRegistry::init('User');
769
770
		$user = $User->find('all', array(
771
			'contain' => 'Article',
772
			'conditions' => '1 != 1',
773
		));
774
775
		$this->assertSame(array(), $user);
776
	}
777
778
/**
779
 * Tests no contain
780
 *
781
 * @return void
782
 */
783
	public function testNoContain() {
784
		$this->loadFixtures('User', 'Article');
785
		$User = ClassRegistry::init('User');
786
		$result = $User->find('first', array('contain' => false));
787
		$this->assertFalse(isset($result['Article']));
788
	}
789
790
/**
791
 * Tests external datasource
792
 *
793
 * @return void
794
 */
795
	public function testExternalDatasource() {
796
		$this->loadFixtures('Article', 'ExternalComment');
797
798
		$Article = ClassRegistry::init('Article');
799
800
		$result = $Article->find('first', array(
801
			'fields' => 'id',
802
			'contain' => 'ExternalComment',
803
			'conditions' => array('id' => 3),
804
		));
805
806
		$expected = array(
807
			'Article' => array(
808
				'id' => 3,
809
			),
810
			'ExternalComment' => array(
811
				array(
812
					'id' => 1,
813
					'article_id' => 3,
814
					'comment' => 'External Comment',
815
				),
816
			)
817
		);
818
819
		$this->assertFalse($Article->useDbConfig === $Article->ExternalComment->useDbConfig);
820
		$this->assertEquals($expected, $result);
821
	}
822
823
/**
824
 * Tests that EagerLoaderBehavior can be coexistent with ContainableBehavior
825
 *
826
 * @return void
827
 */
828
	public function testCoexistentWithContainableBehavior() {
829
		$this->loadFixtures('Comment', 'Article', 'User');
830
831
		$expected = array(
832
			// {{{
833
			array(
834
				'Comment' => array(
835
					'id' => 1, 'article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article',
836
					'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'
837
				),
838
				'Article' => array(
839
					'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body',
840
					'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
841
					'User' => array(
842
						'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
843
						'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
844
					),
845
				),
846
			),
847
			array(
848
				'Comment' => array(
849
					'id' => 2, 'article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article',
850
					'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'
851
				),
852
				'Article' => array(
853
					'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body',
854
					'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
855
					'User' => array(
856
						'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
857
						'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
858
					),
859
				),
860
			),
861
			array(
862
				'Comment' => array(
863
					'id' => 3, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article',
864
					'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'
865
				),
866
				'Article' => array(
867
					'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body',
868
					'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
869
					'User' => array(
870
						'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
871
						'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
872
					),
873
				),
874
			),
875
			array(
876
				'Comment' => array(
877
					'id' => 4, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article',
878
					'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'
879
				),
880
				'Article' => array(
881
					'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body',
882
					'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31',
883
					'User' => array(
884
						'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
885
						'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
886
					),
887
				),
888
			),
889
			array(
890
				'Comment' => array(
891
					'id' => 5, 'article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article',
892
					'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'
893
				),
894
				'Article' => array(
895
					'id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body',
896
					'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31',
897
					'User' => array(
898
						'id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
899
						'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'
900
					),
901
				),
902
			),
903
			array(
904
				'Comment' => array(
905
					'id' => 6, 'article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article',
906
					'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31'
907
				),
908
				'Article' => array(
909
					'id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body',
910
					'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31',
911
					'User' => array(
912
						'id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
913
						'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'
914
					),
915
				),
916
			),
917
			// }}}
918
		);
919
920
		$options = array('contain' => 'Article.User');
921
922
		$Comment = ClassRegistry::init('Comment');
923
		$Comment->Behaviors->load('QueryCounter');
924
925
		$results = $Comment->find('all', $options);
926
		$this->assertEquals(1, $Comment->queryCount());
927
		$this->assertEquals($expected, $results);
928
929
		$Comment->Behaviors->load('Containable');
930
		$results = $Comment->find('all', $options);
931
		$this->assertEquals(1, $Comment->queryCount());
932
		$this->assertEquals($expected, $results);
933
934
		$Comment->Behaviors->disable('EagerLoader');
935
		$results = $Comment->find('all', $options);
936
		$this->assertEquals(7, $Comment->queryCount()); // ContainableBehavior cannot load deep associations eagerly
937
		$this->assertEquals($expected, $results);
938
	}
939
940
/**
941
 * Tests caling find method in afterFind
942
 *
943
 * @return void
944
 */
945
	public function testCallingFindInAfterFind() {
946
		$this->loadFixtures('Apple', 'Sample');
947
948
		$Apple = ClassRegistry::init('Apple');
949
950
		$options = array(
951
			'fields' => array(
952
				'Apple.id',
953
			),
954
			'contain' => 'SampleA',
955
			'conditions' => array('Apple.id' => 3),
956
		);
957
958
		$expected = array(
959
			// {{{
960
			'Apple' => array(
961
				'id' => '3',
962
			),
963
			'SampleA' => array(
964
				'id' => '1',
965
				'apple_id' => '3',
966
				'name' => 'sample1',
967
				'Apple' => array(
968
					'id' => '1',
969
					'apple_id' => '2',
970
					'ParentApple' => array(
971
						'id' => '2',
972
						'name' => 'Bright Red Apple'
973
					)
974
				)
975
			)
976
			// }}}
977
		);
978
		$result = $Apple->find('first', $options);
979
		$this->assertEquals($expected, $result);
980
	}
981
982
/**
983
 * Tests non-existent belongsTo association
984
 *
985
 * @return void
986
 */
987
	public function testNonExistentBelongsTo() {
988
		$this->loadFixtures('Category');
989
990
		$Category = ClassRegistry::init('Category');
991
		$result = $Category->find('first', array(
992
			'contain' => array(
993
				'ParentCategory'
994
			),
995
		));
996
997
		$expected = array(
998
			'Category' => array(
999
				'id' => '1',
1000
				'parent_id' => '0',
1001
				'name' => 'Category 1',
1002
				'created' => '2007-03-18 15:30:23',
1003
				'updated' => '2007-03-18 15:32:31',
1004
				'is_root' => 1,
1005
			),
1006
			'ParentCategory' => array(),
1007
		);
1008
1009
		$this->assertEquals($expected, $result);
1010
	}
1011
1012
/**
1013
 * Tests duplicated aliases
1014
 *
1015
 * @return void
1016
 */
1017
	public function testDuplicatedAliases() {
1018
		$this->loadFixtures('Comment', 'Article', 'User', 'Profile');
1019
1020
		$Comment = ClassRegistry::init('Comment');
1021
		$Comment->Behaviors->load('QueryCounter');
1022
1023
		$result = $Comment->find('first', array(
1024
			'contain' => array(
1025
				'Article.User.Profile',
1026
				'User',
1027
			),
1028
			'conditions' => array('Comment.id' => 5),
1029
		));
1030
1031
		$expected = array(
1032
			// {{{
1033
			'Comment' => array(
1034
				'id' => '5',
1035
				'article_id' => '2',
1036
				'user_id' => '1',
1037
				'comment' => 'First Comment for Second Article',
1038
				'published' => 'Y',
1039
				'created' => '2007-03-18 10:53:23',
1040
				'updated' => '2007-03-18 10:55:31'
1041
			),
1042
			'Article' => array(
1043
				'id' => '2',
1044
				'user_id' => '3',
1045
				'title' => 'Second Article',
1046
				'body' => 'Second Article Body',
1047
				'published' => 'Y',
1048
				'created' => '2007-03-18 10:41:23',
1049
				'updated' => '2007-03-18 10:43:31',
1050
				'User' => array(
1051
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1052
					'id' => '3',
1053
					'user' => 'larry',
1054
					'created' => '2007-03-17 01:20:23',
1055
					'updated' => '2007-03-17 01:22:31',
1056
					'Profile' => array(
1057
						'id' => '1',
1058
						'user_id' => '3',
1059
						'nickname' => 'phpnut',
1060
						'company' => 'Cake Software Foundation, Inc.'
1061
					)
1062
				)
1063
			),
1064
			'User' => array(
1065
				'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1066
				'id' => '1',
1067
				'user' => 'mariano',
1068
				'created' => '2007-03-17 01:16:23',
1069
				'updated' => '2007-03-17 01:18:31'
1070
			)
1071
			// }}}
1072
		);
1073
1074
		$this->assertEquals(2, $Comment->queryCount());
1075
		$this->assertEquals($expected, $result);
1076
	}
1077
1078
/**
1079
 * Tests virtual fields
1080
 *
1081
 * @return void
1082
 */
1083
	public function testVirtualFields() {
1084
		$this->loadFixtures('Category');
1085
1086
		$Category = ClassRegistry::init('Category');
1087
		$results = $Category->find('all', array(
1088
			'fields' => array(
1089
				'Category.id',
1090
				'Category.parent_id',
1091
				'Category.is_root',
1092
			),
1093
			'contain' => array(
1094
				'ParentCategory' => array(
1095
					'fields' => array('id', 'parent_id', 'is_root'),
1096
					'conditions' => array(),
1097
				),
1098
			),
1099
			'conditions' => array(
1100
				'Category.is_root' => 0,
1101
				'ParentCategory.is_root' => 1,
1102
			),
1103
			'order' => array(
1104
				'ParentCategory.is_root',
1105
			),
1106
		));
1107
1108
		$expected = array(
1109
			// {{{
1110
			array(
1111
				'Category' => array(
1112
					'id' => 2,
1113
					'parent_id' => 1,
1114
					'is_root' => 0,
1115
				),
1116
				'ParentCategory' => array(
1117
					'id' => 1,
1118
					'parent_id' => 0,
1119
					'is_root' => 1,
1120
				),
1121
			),
1122
			array(
1123
				'Category' => array(
1124
					'id' => 3,
1125
					'parent_id' => 1,
1126
					'is_root' => 0,
1127
				),
1128
				'ParentCategory' => array(
1129
					'id' => 1,
1130
					'parent_id' => 0,
1131
					'is_root' => 1,
1132
				),
1133
			),
1134
			array(
1135
				'Category' => array(
1136
					'id' => 6,
1137
					'parent_id' => 5,
1138
					'is_root' => 0,
1139
				),
1140
				'ParentCategory' => array(
1141
					'id' => 5,
1142
					'parent_id' => 0,
1143
					'is_root' => 1,
1144
				),
1145
			),
1146
			// }}}
1147
		);
1148
1149
		$this->assertEquals($expected, $results);
1150
	}
1151
}
1152