3 defined('AK_TEST_DATABASE_ON') ?
null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__
).'/../../../fixtures/config/config.php');
6 class HasManyTestCase
extends AkUnitTest
11 $this->installAndIncludeModels(array('Picture', 'Thumbnail','Panorama', 'Property', 'PropertyType'));
14 function test_for_has_many()
16 $Property =& new Property();
17 $this->assertEqual($Property->picture
->getType(), 'hasMany');
18 $this->assertTrue(is_array($Property->pictures
) && count($Property->pictures
) === 0);
20 $Property->picture
->load();
21 $this->assertEqual($Property->picture
->count(), 0);
23 $SeaViews =& new Picture(array('title'=>'Sea views'));
25 $Property->picture
->add($SeaViews);
26 $this->assertEqual($Property->picture
->count(), 1);
28 $this->assertReference($Property->pictures
[0], $SeaViews);
30 $Property->picture
->add($SeaViews);
31 $this->assertEqual($Property->picture
->count(), 1);
33 $this->assertNull($Property->pictures
[0]->get('property_id'));
35 $MountainViews =& new Picture(array('title'=>'Mountain views'));
36 $this->assertTrue($MountainViews->isNewRecord());
37 $Property->picture
->add($MountainViews);
39 $this->assertEqual($Property->picture
->count(), 2);
41 $this->assertTrue($Property->save());
43 $this->assertFalse($SeaViews->isNewRecord());
44 $this->assertFalse($MountainViews->isNewRecord());
47 $this->assertEqual($SeaViews->get('property_id'), $Property->getId());
48 $this->assertEqual($MountainViews->get('property_id'), $Property->getId());
50 $this->assertReference($SeaViews, $Property->pictures
[0]);
51 $this->assertReference($MountainViews, $Property->pictures
[1]);
53 $Property =& new Property($Property->getId());
54 $Property->picture
->load();
56 $this->assertEqual($Property->picture
->association_id
, 'pictures');
57 $this->assertEqual($Property->picture
->count(), 2);
59 $Property->pictures
= array();
60 $this->assertEqual($Property->picture
->count(), 0);
62 $Property->picture
->load();
63 $this->assertEqual($Property->picture
->count(), 0);
65 $Property->picture
->load(true);
66 $this->assertEqual($Property->picture
->count(), 2);
68 $this->assertEqual($Property->pictures
[1]->getType(), 'Picture');
70 $Property->picture
->delete($Property->pictures
[1]);
72 $this->assertEqual($Property->picture
->count(), 1);
74 $Property->picture
->load(true);
75 $this->assertEqual($Property->picture
->count(), 1);
77 $Property = $Property->find('first');
79 $Picture = new Picture();
80 $Pictures = $Picture->find();
82 $Property->picture
->set($Pictures);
83 $this->assertEqual($Property->picture
->count(), count($Pictures));
85 $Property = $Property->find('first');
86 $Property->picture
->load();
87 $this->assertEqual($Property->picture
->count(), count($Pictures));
89 $Picture = $Picture->find('first');
91 $Property->picture
->set($Picture);
93 $this->assertEqual($Property->picture
->count(), 1);
95 $this->assertTrue(in_array('pictures', $Property->getAssociatedIds()));
97 $Property = $Property->find('first', array('include'=>'pictures'));
99 $this->assertIdentical($Property->picture
->count(), 1);
101 $this->assertEqual($Property->pictures
[0]->getId(), $Picture->getId());
103 $this->assertTrue($Property->picture
->delete($Property->pictures
[0]));
105 $this->assertIdentical($Property->picture
->count(), 0);
107 $Property =& $Property->find('first');
108 $this->assertIdentical($Property->picture
->count(), 0);
110 //$this->assertTrue($Property =& $Property->find('first', array('include'=>'pictures')));
111 //$this->assertIdentical($Property->picture->count(), 0);
113 $Picture =& new Picture();
114 $Alicia =& $Picture->create(array('title'=>'Alicia'));
115 $Bermi =& $Picture->create(array('title'=>'Bermi'));
116 $Hilario =& $Picture->create(array('title'=>'Hilario'));
118 $Property->picture
->setByIds(array($Alicia->getId(),$Bermi->getId(),$Hilario->getId()));
120 $Property->set('description', 'Cool house');
122 $this->assertTrue($Property->save());
124 $this->assertTrue($Property =& $Property->findFirstBy('description', 'Cool house'));
126 $Property->picture
->load();
128 $this->assertEqual($Property->picture
->count(), 3);
130 $FoundAlicia = $Property->picture
->find('first', array('conditions' => array('title = ?',"Alicia")));
131 $this->assertEqual($Alicia->get('title').$Alicia->getId(), $FoundAlicia->get('title').$FoundAlicia->getId());
133 $FoundPals = $Property->picture
->find();
135 $this->assertEqual(count($FoundPals), $Property->picture
->count());
138 foreach ($FoundPals as $FoundPal){
139 $titles[] = $FoundPal->get('title');
143 $this->assertEqual($titles, array('Alicia','Bermi','Hilario'));
145 $this->assertFalse($Property->picture
->isEmpty());
147 $this->assertEqual($Property->picture
->getSize(), 3);
149 $this->assertTrue($Property->picture
->clear());
151 $this->assertTrue($Property->picture
->isEmpty());
153 $this->assertEqual($Property->picture
->getSize(), 0);
156 $Property =& new Property();
158 $PoolPicture =& $Property->picture
->build(array('title'=>'Pool'));
160 $this->assertReference($PoolPicture, $Property->pictures
[0]);
162 $this->assertTrue($Property->pictures
[0]->isNewRecord());
164 $this->assertEqual($PoolPicture->getType(), 'Picture');
166 $Property->set('description', 'Maui Estate');
169 $this->assertTrue($Property->save());
171 $this->assertTrue($MauiEstate = $Property->findFirstBy('description', 'Maui Estate', array('include'=>'pictures')));
173 $this->assertEqual($MauiEstate->pictures
[0]->get('title'), 'Pool');
175 $Property =& new Property(array('description'=>'Villa Altea'));
176 $GardenPicture =& $Property->picture
->create(array('title'=>'Garden'));
177 $this->assertReference($GardenPicture, $Property->pictures
[0]);
178 $this->assertTrue($GardenPicture->isNewRecord());
180 $Property =& new Property(array('description'=>'Villa Altea'));
181 $this->assertTrue($Property->save());
182 $GardenPicture =& $Property->picture
->create(array('title'=>'Garden'));
183 $this->assertReference($GardenPicture, $Property->pictures
[0]);
184 $this->assertFalse($GardenPicture->isNewRecord());
186 $this->assertTrue($VillaAltea = $Property->findFirstBy('description', 'Villa Altea', array('include'=>'pictures')));
188 $this->assertEqual($VillaAltea->pictures
[0]->get('title'), 'Garden');
191 function test_clean_up_dependencies()
193 $Property =& new Property(array('description'=>'Ruins in Matamon'));
194 $this->assertTrue($Property->save());
196 $South =& $Property->picture
->create(array('title'=>'South views'));
197 $this->assertReference($South, $Property->pictures
[0]);
198 $this->assertFalse($South->isNewRecord());
200 $pic_id = $South->getId();
202 $Property =& new Property($Property->getId());
203 $this->assertTrue($Property->destroy());
205 $Picture =& new Picture();
207 $this->assertFalse($Picture->find($pic_id));
211 function _test_should_not_die_on_unincluded_model()
213 $this->installAndIncludeModels(array('Post'));
216 $Post->find('all', array('include' => array('comments')));
219 function test_should_find_owner_even_if_it_has_no_relations()
221 $this->installAndIncludeModels(array('Post', 'Comment'));
223 $Post =& new Post(array('title' => 'Post for unit testing', 'body' => 'This is a post for testing the model'));
228 $expected_id = $Post->getId();
230 $this->assertTrue($Result =& $Post->find($expected_id, array('include' => array('comments'))));
231 $this->assertEqual($Result->getId(), $expected_id);
234 function test_should_find_owner_using_related_conditions()
236 $this->installAndIncludeModels(array('Post', 'Comment'));
238 $Post =& new Post(array('title' => 'Post for unit testing', 'body' => 'This is a post for testing the model'));
239 $Post->comment
->create(array('body' => 'hello', 'name' => 'Aditya'));
243 $expected_id = $Post->getId();
245 $this->assertTrue($Result =& $Post->find($expected_id, array('include' => array('comments'), 'conditions' => "name = 'Aditya'")));
247 $this->assertEqual($Result->comments
[0]->get('name'), 'Aditya');
251 function test_remove_existing_associates_before_setting_by_id()
253 $this->installAndIncludeModels(array('Post', 'Comment'));
255 foreach (range(1,10) as $i){
256 $Post =& new Post(array('title' => 'Post '.$i));
257 $Post->comment
->create(array('name' => 'Comment '.$i));
261 $Post11 =& new Post(array('name' => 'Post 11'));
262 $this->assertTrue($Post11->save());
264 $Post->comment
->setByIds(1,2,3,4,5);
266 $this->assertTrue($Post =& $Post->find(10, array('include' => 'comments')));
268 // order cannot be guaranteed!
269 $expected_ids = array(1,2,3,4,5); // on my postgreSQL $Post->comment->associated_ids = array(5,4,3,2,1);
270 foreach (array_keys($Post->comments
) as $k){
271 $this->assertTrue(in_array($Post->comments
[$k]->getId(),$expected_ids));
272 unset($expected_ids[$Post->comments
[$k]->getId()-1]);
274 $this->assertTrue(empty($expected_ids));
276 // Comment 10 should exist but unrelated to a post
277 $this->assertTrue($Comment =& $Post->comments
[$k]->find(10));
278 $this->assertNull($Comment->get('post_id'));
280 $Post11->comment
->setByIds(array(10,1));
282 $this->assertTrue($Comment =& $Comment->find(10));
283 $this->assertEqual($Comment->get('post_id'), 11);
286 function test_find_with_include_on_associated_record()
288 $this->installAndIncludeModels('Property','Picture','Landlord');
289 $Property =& $this->Property
->create(array('description'=>'A Property'));
290 $Picture =& $this->Picture
->create(array('title'=>'With a picture'));
291 $Landlord =& $this->Landlord
->create(array('name'=>'and a landlord'));
293 $Picture->landlord
->assign($Landlord);
294 $Property->picture
->add($Picture);
296 $Property =& $this->Property
->find('first',array('description'=>'A Property'));
297 $Loaded =& $Property->picture
->find('all');
298 $this->assertEqual('With a picture',$Loaded[0]->title
);
300 $Property =& $this->Property
->find('first',array('description'=>'A Property'));
301 $Loaded =& $Property->picture
->find('all',array('include'=>'landlord'));
302 $this->assertEqual('and a landlord',$Loaded[0]->landlord
->name
);
306 ak_test('HasManyTestCase',true);