public
Description: CakePHP Lazy Load implementation
Homepage: http://rafaelbandeira3.wordpress.com/2008/11/21/lazy-loader-behavior-what-you-need-when-you-need-the-way-you-want/
Clone URL: git://github.com/rafaelbandeira3/lazy_loading.git
lazy_loading / tests / cases / behaviors / lazy_loader.test.php
100644 182 lines (152 sloc) 6.534 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/*
* Unit test for LazyLoaderBehavior v1.0
*
* LazyLoaderBehavior. What you need, When you need, The way you want.
* RafaelBandeira <rafaelbandeira3(at)gmail(dot)com>
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
*/
 
App::import('Behavior', 'LazyLoading.LazyLoader');
App::import('Model', 'App');
include CAKE_TESTS . 'cases' . DS . 'libs' . DS . 'model' . DS . 'models.php';
 
class LazyLoaderBehaviorTest extends CakeTestCase {
 
var $fixtures = array(
'core.portfolio', 'core.item', 'core.items_portfolio',
'core.syfile', 'core.image', 'core.message',
'core.thread', 'core.bid', 'core.project'
);
 
function start() {
parent::start();
$this->Portfolio = ClassRegistry::init('Portfolio');
$this->Item = ClassRegistry::init('Item');
$this->Syfile = ClassRegistry::init('Syfile');
$this->Image = ClassRegistry::init('Image');
$this->Message = ClassRegistry::init('Message');
$this->Thread = ClassRegistry::init('Thread');
 
$this->Portfolio->Behaviors->attach('LazyLoading.LazyLoader');
$this->Item->Behaviors->attach('LazyLoading.LazyLoader');
$this->Syfile->Behaviors->attach('LazyLoading.LazyLoader');
$this->Image->Behaviors->attach('LazyLoading.LazyLoader');
$this->Message->Behaviors->attach('LazyLoading.LazyLoader');
$this->Thread->Behaviors->attach('LazyLoading.LazyLoader');
 
$this->Image->bind('Syfile', array('hasOne'));
}
 
function testBelongsTo() {
$this->Syfile->id = 1;
$result = $this->Syfile->getImage();
$expected = array('Image' => array('id' => 1, 'name' => 'Image 1'));
$this->assertEqual($result, $expected);
}
 
function testBelongsToLoadInstance() {
$this->Syfile->id = 1;
$result = $this->Syfile->getImage(true);
$this->assertIsA($result, 'Image');
}
 
function testHasOne() {
$this->Image->id = 1;
$result = $this->Image->getSyfile();
$expected = array('Syfile' => array('id' => 1, 'image_id' => 1, 'name' => 'Syfile 1', 'item_count' => null));
$this->assertEqual($result, $expected);
}
 
function testHasOneLoadInstance() {
$this->Image->id = 1;
$result = $this->Image->getSyfile(true);
$this->assertIsA($result, 'Syfile');
}
 
function testHasAndBelongsToMany() {
$this->Portfolio->id = 1;
$result = $this->Portfolio->getItems();
$expected = array(1 => 'Item 1', 3 => 'Item 3', 4 => 'Item 4', 5 => 'Item 5');
$this->assertEqual($result, $expected);
}
 
function testHasMany() {
$this->Thread->id = 1;
$result = $this->Thread->getMessages();
$expected = array(1 => 'Thread 1, Message 1');
$this->assertEqual($result, $expected);
}
 
function testAlternativeFindType() {
$this->Portfolio->id = 1;
$result = $this->Portfolio->getItems('all');
$expected = array(
array('Item' => array('id' => 1, 'syfile_id' => 1, 'published' => 0, 'name' => 'Item 1')),
array('Item' => array('id' => 3, 'syfile_id' => 3, 'published' => 0, 'name' => 'Item 3')),
array('Item' => array('id' => 4, 'syfile_id' => 4, 'published' => 0, 'name' => 'Item 4')),
array('Item' => array('id' => 5, 'syfile_id' => 5, 'published' => 0, 'name' => 'Item 5'))
);
$this->assertEqual($result, $expected);
 
$result = $this->Portfolio->getItems('count');
$expected = 4;
$this->assertEqual($result, $expected);
}
 
function testUnderscoredStyle() {
$this->Portfolio->id = 1;
$result = $this->Portfolio->get_items();
$expected = array(1 => 'Item 1', 3 => 'Item 3', 4 => 'Item 4', 5 => 'Item 5');
$this->assertEqual($result, $expected);
 
$this->Image->id = 1;
$result = $this->Image->get_syfile();
$expected = array('Syfile' => array('id' => 1, 'image_id' => 1, 'name' => 'Syfile 1', 'item_count' => null));
$this->assertEqual($result, $expected);
}
 
function testUninstantiatedModel() {
$this->expectException();
$this->Image->id = false;
$result = $this->Image->getSyfile();
$this->assertFalse($result);
}
 
function testUnexistantAssociation() {
$this->expectException();
$this->Image->id = 1;
$result = $this->Image->getACherryFromTheTopOfTheCakeForMePlease();
$this->assertFalse($result);
}
 
function testOnTheFlyAssociations() {
$this->Message->bind('Thread');
$this->Message->id = 1;
$result = $this->Message->getThread();
$expected = array('Thread' => array('id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1'));
$this->assertEqual($result, $expected);
 
$result = $this->Message->getThread(true);
$this->assertIsA($result, 'Thread');
}
 
function testComposedNameAssociation() {
$this->Image->unbindModel(array('hasOne' => array('Syfile')));
$this->Image->bind('SystemFile', array('hasOne', 'className' => 'Syfile'), false);
 
$this->Image->id = 1;
$result = $this->Image->getSystemFile();
$expected = array('SystemFile' => array('id' => 1, 'image_id' => 1, 'name' => 'Syfile 1', 'item_count' => null));
$this->assertEqual($result, $expected);
 
$this->Portfolio->unbindModel(array('hasAndBelongsToMany' => array('Item')));
$this->Portfolio->bind('RelatedItem', array('hasAndBelongsToMany', 'className' => 'Item'), false);
 
$this->Portfolio->id = 1;
$result = $this->Portfolio->getRelatedItems();
$expected = array(1 => 'Item 1', 3 => 'Item 3', 4 => 'Item 4', 5 => 'Item 5');
$this->assertEqual($result, $expected);
}
 
function testUnderscoredStyleComposedNameAssociation() {
$this->Image->unbindModel(array('hasOne' => array('Syfile')));
$this->Image->bind('SystemFile', array('hasOne', 'className' => 'Syfile'), false);
 
$this->Image->id = 1;
$result = $this->Image->get_system_file();
$expected = array('SystemFile' => array('id' => 1, 'image_id' => 1, 'name' => 'Syfile 1', 'item_count' => null));
$this->assertEqual($result, $expected);
 
$this->Portfolio->unbindModel(array('hasAndBelongsToMany' => array('Item')));
$this->Portfolio->bind('RelatedItem', array('hasAndBelongsToMany', 'className' => 'Item'), false);
 
$this->Portfolio->id = 1;
$result = $this->Portfolio->get_related_items();
$expected = array(1 => 'Item 1', 3 => 'Item 3', 4 => 'Item 4', 5 => 'Item 5');
$this->assertEqual($result, $expected);
}
 
function testErroneousRegexpMatch() {
$this->Syfile->recursive = -1;
$result = $this->Syfile->findById(1);
$expected = array('Syfile' => array('id' => 1, 'image_id' => 1, 'name' => 'Syfile 1', 'item_count' => null));
$this->assertEqual($result, $expected);
 
$this->expectError();
$this->Syfile->someMethodThatWillNeverBeHandledAsItIsNotOnlyNotImplementedInTheModelAsItIsNotImplementedInAnyOfItsBehaviors();
}
}