| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 lmb_require('limb/active_record/src/lmbActiveRecord.class.php'); 10 lmb_require('limb/dbal/src/lmbSimpleDb.class.php'); 11 lmb_require('limb/validation/src/rule/lmbRequiredRule.class.php'); 12 13 class GroupForTest extends lmbActiveRecord 14 { 15 protected $_db_table_name = 'group_for_test'; 16 17 protected $_has_many_to_many = array('users' => array('field' => 'group_id', 18 'foreign_field' => 'user_id', 19 'table' => 'user2group_for_test', 20 'class' => 'UserForTest')); 21 22 protected $_test_validator; 23 24 function setValidator($validator) 25 { 26 $this->_test_validator = $validator; 27 } 28 29 function _createValidator() 30 { 31 if($this->_test_validator) 32 return $this->_test_validator; 33 34 return parent :: _createValidator(); 35 } 36 } 37 38 class UserForTest extends lmbActiveRecord 39 { 40 protected $_db_table_name = 'user_for_test'; 41 42 protected $_has_many_to_many = array('groups' => array('field' => 'user_id', 43 'foreign_field' => 'group_id', 44 'table' => 'user2group_for_test', 45 'class' => 'GroupForTest')); 46 } 47 48 class GroupsForTestCollectionStub extends lmbARManyToManyCollection{} 49 50 class UserForTestWithCustomCollection extends lmbActiveRecord 51 { 52 protected $_db_table_name = 'user_for_test'; 53 54 protected $_has_many_to_many = array('groups' => array('field' => 'user_id', 55 'foreign_field' => 'group_id', 56 'table' => 'user2group_for_test', 57 'class' => 'GroupForTest', 58 'collection' => 'GroupsForTestCollectionStub')); 59 } 60 61 class lmbARManyToManyRelationsTest extends UnitTestCase 62 { 63 protected $db; 64 65 function setUp() 66 { 67 $this->db = new lmbSimpleDb(lmbToolkit :: instance()->getDefaultDbConnection()); 68 $this->_dbCleanUp(); 69 } 70 71 function tearDown() 72 { 73 $this->_dbCleanUp(); 74 } 75 76 function _dbCleanUp() 77 { 78 lmbActiveRecord :: delete('GroupForTest'); 79 lmbActiveRecord :: delete('UserForTest'); 80 } 81 82 function testMapPropertyToField() 83 { 84 $group = new GroupForTest(); 85 $this->assertEqual('users', $group->mapFieldToProperty('group_id')); 86 $this->assertNull($group->mapFieldToProperty('blah')); 87 } 88 89 function testNewObjectReturnsEmptyCollection() 90 { 91 $user = new UserForTest(); 92 $groups = $user->getGroups(); 93 $groups->rewind(); 94 $this->assertFalse($groups->valid()); 95 } 96 97 function testAddFromOneSideOfRelation() 98 { 99 $user = new UserForTest(); 100 $user->setFirstName('Bob'); 101 102 $group1 = new GroupForTest(); 103 $group1->setTitle('vp1'); 104 105 $group2 = new GroupForTest(); 106 $group2->setTitle('vp2'); 107 108 $user->addToGroups($group1); 109 $user->addToGroups($group2); 110 $user->save(); 111 112 $user2 = lmbActiveRecord :: findById('UserForTest', $user->getId()); 113 $rs = $user2->getGroups(); 114 115 $rs->rewind(); 116 $this->assertTrue($rs->valid()); 117 $this->assertEqual($rs->current()->getTitle(), $group1->getTitle()); 118 $this->assertEqual($rs->current()->getId(), $group1->getId()); 119 $rs->next(); 120 $this->assertEqual($rs->current()->getTitle(), $group2->getTitle()); 121 $this->assertEqual($rs->current()->getId(), $group2->getId()); 122 } 123 124 function testLoadShouldNotMixTables() 125 { 126 $user1 = new UserForTest(); 127 $user1->setFirstName('Bob'); 128 129 $user2 = new UserForTest(); 130 $user2->setFirstName('Joe'); 131 132 $group1 = new GroupForTest(); 133 $group1->setTitle('vp1'); 134 135 $group2 = new GroupForTest(); 136 $group2->setTitle('vp2'); 137 138 $user1->addToGroups($group1); 139 $user1->addToGroups($group2); 140 $user1->save(); 141 142 $user2->addToGroups($group1); 143 $user2->addToGroups($group2); 144 $user2->save(); 145 146 $user3 = lmbActiveRecord :: findById('UserForTest', $user2->getId()); 147 $rs = $user3->getGroups(); 148 149 $rs->rewind(); 150 $this->assertTrue($rs->valid()); 151 $this->assertEqual($rs->current()->getTitle(), $group1->getTitle()); 152 $this->assertEqual($rs->current()->getId(), $group1->getId()); 153 $rs->next(); 154 $this->assertEqual($rs->current()->getTitle(), $group2->getTitle()); 155 $this->assertEqual($rs->current()->getId(), $group2->getId()); 156 } 157 158 function testSetingCollectionDirectlyCallsAddToMethod() 159 { 160 $user = new UserForTest(); 161 $user->setFirstName('Bob'); 162 163 $g1 = new GroupForTest(); 164 $g1->setTitle('vp1'); 165 $g2 = new GroupForTest(); 166 $g2->setTitle('vp2'); 167 168 $user->setGroups(array($g1, $g2)); 169 $arr = $user->getGroups()->getArray(); 170 $this->assertEqual(sizeof($arr), 2); 171 $this->assertEqual($arr[0]->getTitle(), $g1->getTitle()); 172 $this->assertEqual($arr[1]->getTitle(), $g2->getTitle()); 173 } 174 175 function testSetFlushesPreviousCollection() 176 { 177 $user = new UserForTest(); 178 $user->setFirstName('Bob'); 179 180 $g1 = new GroupForTest(); 181 $g1->setTitle('vp1'); 182 $g2 = new GroupForTest(); 183 $g2->setTitle('vp2'); 184 185 $user->addToGroups($g1); 186 $user->addToGroups($g2); 187 188 $user->setGroups(array($g1)); 189 $groups = $user->getGroups()->getArray(); 190 $this->assertEqual($groups[0]->getTitle(), $g1->getTitle()); 191 $this->assertEqual(sizeof($groups), 1); 192 } 193 194 function testUpdateRelations() 195 { 196 $user = new UserForTest(); 197 $user->setFirstName('Bob'); 198 199 $group1 = new GroupForTest(); 200 $group1->setTitle('vp1'); 201 202 $group2 = new GroupForTest(); 203 $group2->setTitle('vp2'); 204 205 $user->addToGroups($group1); 206 $user->addToGroups($group2); 207 $user->save(); 208 209 $user2 = lmbActiveRecord :: findById('UserForTest', $user->getId()); 210 $user2->setGroups(array($group2)); 211 $user2->save(); 212 213 $user3 = lmbActiveRecord :: findById('UserForTest', $user->getId()); 214 $groups = $user3->getGroups(); 215 216 $this->assertEqual($groups->at(0)->getTitle(), $group2->getTitle()); 217 $this->assertEqual($groups->count(), 1); 218 } 219 220 function testDeleteAlsoRemovesManyToManyRecords() 221 { 222 $user1 = new UserForTest(); 223 $user1->setFirstName('Bob'); 224 225 $user2 = new UserForTest(); 226 $user2->setFirstName('Bob'); 227 228 $group1 = new GroupForTest(); 229 $group1->setTitle('vp1'); 230 231 $group2 = new GroupForTest(); 232 $group2->setTitle('vp2'); 233 234 $user1->addToGroups($group1); 235 $user1->addToGroups($group2); 236 $user1->save(); 237 238 $user2->addToGroups($group1); 239 $user2->addToGroups($group2); 240 $user2->save(); 241 242 $user3 = lmbActiveRecord :: findById('UserForTest', $user1->getId()); 243 $user3->destroy(); 244 245 $this->assertEqual($this->db->count('user2group_for_test'), 2); 246 247 $user4 = lmbActiveRecord :: findById('UserForTest', $user2->getId()); 248 $groups = $user4->getGroups(); 249 $this->assertEqual($groups->at(0)->getTitle(), $group1->getTitle()); 250 $this->assertEqual($groups->at(1)->getTitle(), $group2->getTitle()); 251 $this->assertEqual($groups->count(), 2); 252 } 253 254 function testUseCustomCollection() 255 { 256 $user = new UserForTestWithCustomCollection(); 257 $this->assertTrue($user->getGroups() instanceof GroupsForTestCollectionStub); 258 } 259 260 function testErrorListIsSharedWithCollection() 261 { 262 $user = new UserForTest(); 263 $user->setFirstName('Bob'); 264 265 $group = new GroupForTest(); 266 267 $validator = new lmbValidator(); 268 $validator->addRequiredRule('title'); 269 $group->setValidator($validator); 270 271 $user->addToGroups($group); 272 $user->addToGroups($group); 273 274 $error_list = new lmbErrorList(); 275 $this->assertFalse($user->trySave($error_list)); 276 } 277 } 278 279 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Aug 29 04:49:26 2008 | Cross-referenced by PHPXref 0.7 |