| [ 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/lmbARManyToManyCollection.class.php'); 10 lmb_require('limb/dbal/src/lmbSimpleDb.class.php'); 11 require_once(dirname(__FILE__) . '/lmbARManyToManyRelationsTest.class.php'); 12 13 Mock :: generate('GroupForTest', 'MockGroupForTest'); 14 15 class GroupForTestStub extends GroupForTest 16 { 17 var $save_calls = 0; 18 19 function save($error_list = null) 20 { 21 parent :: save($error_list); 22 $this->save_calls++; 23 } 24 } 25 26 class UserForTestWithSpecialRelationTable extends lmbActiveRecord 27 { 28 protected $_db_table_name = 'user_for_test'; 29 30 protected $_has_many_to_many = array('groups' => array('field' => 'user_id', 31 'foreign_field' => 'group_id', 32 'table' => 'extended_user2group_for_test', 33 'class' => 'GroupForTest')); 34 } 35 36 class lmbARManyToManyCollectionTest extends UnitTestCase 37 { 38 protected $db; 39 40 function setUp() 41 { 42 $this->db = new lmbSimpleDb(lmbToolkit :: instance()->getDefaultDbConnection()); 43 $this->_dbCleanUp(); 44 } 45 46 function tearDown() 47 { 48 $this->_dbCleanUp(); 49 } 50 51 function _dbCleanUp() 52 { 53 $this->db->delete('group_for_test'); 54 $this->db->delete('user_for_test'); 55 $this->db->delete('user2group_for_test'); 56 $this->db->delete('extended_user2group_for_test'); 57 } 58 59 function testAddToWithExistingOwner() 60 { 61 $user = $this->_createUserAndSave(); 62 63 $group1 = $this->_initGroup(); 64 $group2 = $this->_initGroup(); 65 66 $collection = new lmbARManyToManyCollection('groups', $user); 67 $collection->add($group1); 68 $collection->add($group2); 69 70 $arr = $collection->getArray(); 71 72 $this->assertEqual($arr[0]->getTitle(), $group1->getTitle()); 73 $this->assertEqual($arr[1]->getTitle(), $group2->getTitle()); 74 $this->assertEqual(sizeof($arr), 2); 75 76 $collection2 = new lmbARManyToManyCollection('groups', $user); 77 $arr = $collection2->getArray(); 78 79 $this->assertEqual($arr[0]->getTitle(), $group1->getTitle()); 80 $this->assertEqual($arr[1]->getTitle(), $group2->getTitle()); 81 $this->assertEqual(sizeof($arr), 2); 82 } 83 84 function testAddToWithNonSavedOwner() 85 { 86 $user = $this->_initUser(); 87 88 $group1 = $this->_initGroup(); 89 $group2 = $this->_initGroup(); 90 91 $collection = new lmbARManyToManyCollection('groups', $user); 92 $collection->add($group1); 93 $collection->add($group2); 94 95 $arr = $collection->getArray(); 96 $this->assertEqual(sizeof($arr), 2); 97 $this->assertEqual($arr[0]->getTitle(), $group1->getTitle()); 98 $this->assertEqual($arr[1]->getTitle(), $group2->getTitle()); 99 100 $collection2 = new lmbARManyToManyCollection('groups', $user); 101 $arr = $collection2->getArray(); 102 103 $this->assertEqual(sizeof($arr), 0); 104 } 105 106 function testSaveWithExistingOwnerDoesNothing() 107 { 108 $group1 = new MockGroupForTest(); 109 $group2 = new MockGroupForTest(); 110 111 $user = $this->_createUserAndSave(); 112 113 $collection = new lmbARManyToManyCollection('groups', $user); 114 115 $collection->add($group1); 116 $collection->add($group2); 117 118 $group1->expectNever('save'); 119 $group2->expectNever('save'); 120 121 $collection->save(); 122 } 123 124 function testSaveWithNonSavedOwner() 125 { 126 $group1 = $this->_initGroup(); 127 $group2 = $this->_initGroup(); 128 129 $user = $this->_initUser(); 130 131 $collection = new lmbARManyToManyCollection('groups', $user); 132 $collection->add($group1); 133 $collection->add($group2); 134 135 $collection2 = new lmbARManyToManyCollection('groups', $user); 136 $this->assertEqual(sizeof($collection2->getArray()), 0); 137 138 $user->save(); 139 $collection->save(); 140 141 $collection3 = new lmbARManyToManyCollection('groups', $user); 142 $arr = $collection3->getArray(); 143 $this->assertEqual(sizeof($arr), 2); 144 $this->assertEqual($arr[0]->getTitle(), $group1->getTitle()); 145 $this->assertEqual($arr[1]->getTitle(), $group2->getTitle()); 146 } 147 148 function testSavingOwnerDoesntAffectCollection() 149 { 150 $group1 = new GroupForTestStub(); 151 $group1->setTitle('Group1'); 152 $group2 = new GroupForTestStub(); 153 $group2->setTitle('Group2'); 154 155 $user = $this->_initUser(); 156 157 $collection = new lmbARManyToManyCollection('groups', $user); 158 $collection->add($group1); 159 160 $user->save(); 161 162 $collection->add($group2); 163 164 //items in memory 165 $arr = $collection->getArray(); 166 $this->assertEqual(sizeof($arr), 2); 167 $this->assertEqual($arr[0]->getTitle(), $group1->getTitle()); 168 $this->assertEqual($arr[1]->getTitle(), $group2->getTitle()); 169 $this->assertEqual($group1->save_calls, 0); 170 $this->assertEqual($group2->save_calls, 0); 171 172 //...and not db yet 173 $collection2 = new lmbARManyToManyCollection('groups', $user); 174 $this->assertEqual(sizeof($collection2->getArray()), 0); 175 176 $collection->save(); 177 178 $collection3 = new lmbARManyToManyCollection('groups', $user); 179 $arr = $collection3->getArray(); 180 $this->assertEqual(sizeof($arr), 2); 181 $this->assertEqual($arr[0]->getTitle(), $group1->getTitle()); 182 $this->assertEqual($arr[1]->getTitle(), $group2->getTitle()); 183 184 //check items not saved twice 185 $collection->save(); 186 187 $this->assertEqual($group1->save_calls, 1); 188 $this->assertEqual($group2->save_calls, 1); 189 190 $collection4 = new lmbARManyToManyCollection('groups', $user); 191 $arr = $collection4->getArray(); 192 $this->assertEqual(sizeof($arr), 2); 193 $this->assertEqual($arr[0]->getTitle(), $group1->getTitle()); 194 $this->assertEqual($arr[1]->getTitle(), $group2->getTitle()); 195 } 196 197 function testLoadOnlyProperRecordsWithExistingOwner() 198 { 199 $g1 = $this->_initGroup(); 200 $g2 = $this->_initGroup(); 201 202 $user1 = $this->_createUserAndSave(array($g1, $g2)); 203 204 $g3 = $this->_initGroup(); 205 $g4 = $this->_initGroup(); 206 207 $user2 = $this->_createUserAndSave(array($g3, $g4)); 208 209 $collection1 = new lmbARManyToManyCollection('groups', $user1); 210 $this->assertEqual($collection1->count(), 2); 211 $arr = $collection1->getArray(); 212 $this->assertEqual(sizeof($arr), 2); 213 $this->assertEqual($arr[0]->getTitle(), $g1->getTitle()); 214 $this->assertEqual($arr[1]->getTitle(), $g2->getTitle()); 215 216 $collection2 = new lmbARManyToManyCollection('groups', $user2); 217 $this->assertEqual($collection2->count(), 2); 218 $arr = $collection2->getArray(); 219 $this->assertEqual(sizeof($arr), 2); 220 $this->assertEqual($arr[0]->getTitle(), $g3->getTitle()); 221 $this->assertEqual($arr[1]->getTitle(), $g4->getTitle()); 222 } 223 224 function testCountWithExistingOwner() 225 { 226 $group1 = $this->_initGroup(); 227 $group2 = $this->_initGroup(); 228 229 $user = $this->_createUserAndSave(); 230 231 $collection = new lmbARManyToManyCollection('groups', $user); 232 $this->assertEqual($collection->count(), 0); 233 $collection->add($group1); 234 $collection->add($group2); 235 236 $this->assertEqual($collection->count(), 2); 237 } 238 239 function testCountWithNonSavedOwner() 240 { 241 $group1 = $this->_initGroup(); 242 $group2 = $this->_initGroup(); 243 244 $user = new UserForTest(); 245 246 $collection = new lmbARManyToManyCollection('groups', $user); 247 $this->assertEqual($collection->count(), 0); 248 249 $collection->add($group1); 250 $collection->add($group2); 251 252 $this->assertEqual($collection->count(), 2); 253 } 254 255 function testImplementsCountable() 256 { 257 $group1 = $this->_initGroup(); 258 $group2 = $this->_initGroup(); 259 260 $user = $this->_createUserAndSave(); 261 262 $collection = new lmbARManyToManyCollection('groups', $user); 263 $this->assertEqual(sizeof($collection), 0); 264 265 $collection->add($group1); 266 $collection->add($group2); 267 268 $this->assertEqual(sizeof($collection), 2); 269 } 270 271 function testPartiallyImplementsArrayAccess() 272 { 273 $group1 = $this->_initGroup(); 274 $group2 = $this->_initGroup(); 275 276 $user = $this->_createUserAndSave(); 277 278 $collection = new lmbARManyToManyCollection('groups', $user); 279 280 $collection[] = $group1; 281 $collection[] = $group2; 282 283 $this->assertEqual($collection[0]->getId(), $group1->getId()); 284 $this->assertEqual($collection[1]->getId(), $group2->getId()); 285 $this->assertNull($collection[2]); 286 287 $this->assertTrue(isset($collection[0])); 288 $this->assertTrue(isset($collection[1])); 289 $this->assertFalse(isset($collection[2])); 290 291 //we can't really implement just every php array use case 292 $this->assertNull($collection['foo']); 293 $this->assertFalse(isset($collection['foo'])); 294 $collection[3] = 'foo'; 295 $this->assertNull($collection[3]); 296 } 297 298 function testRemoveAllWithExistingOwner() 299 { 300 $group1 = $this->_initGroup(); 301 $group2 = $this->_initGroup(); 302 303 $user = $this->_createUserAndSave(array($group1, $group2)); 304 305 $collection = new lmbARManyToManyCollection('groups', $user); 306 $collection->removeAll(); 307 308 $user2 = lmbActiveRecord :: findById('UserForTest', $user->getId()); 309 310 $collection = new lmbARManyToManyCollection('groups', $user2); 311 $this->assertEqual(sizeof($collection->getArray()), 0); 312 } 313 314 function testRemoveAllWithNonSavedOwner() 315 { 316 $group1 = $this->_initGroup(); 317 $group2 = $this->_initGroup(); 318 319 $user = $this->_initUser(); 320 321 $collection = new lmbARManyToManyCollection('groups', $user); 322 $collection->add($group1); 323 $collection->add($group2); 324 $collection->removeAll(); 325 326 $this->assertEqual($collection->count(), 0); 327 } 328 329 function testRemoveAllDeletesOnlyProperRecordsFromTable() 330 { 331 $group1 = $this->_initGroup(); 332 $group2 = $this->_initGroup(); 333 334 $user = new UserForTestWithSpecialRelationTable(); 335 $user->setFirstName('User' . mt_rand()); 336 $user->save(); 337 338 $collection = new lmbARManyToManyCollection('groups', $user); 339 $collection->add($group1); 340 $collection->add($group2); 341 342 $db_table = new lmbTableGateway('extended_user2group_for_test'); 343 $db_table->insert(array('user_id' => $user->getId(), 344 'other_id' => 100)); 345 346 $collection->removeAll(); 347 348 $this->assertEqual($db_table->select()->count(), 1); 349 } 350 351 function testPaginateWithNonSavedOwner() 352 { 353 $group1 = $this->_initGroup(); 354 $group2 = $this->_initGroup(); 355 $group3 = $this->_initGroup(); 356 357 $user = $this->_initUser(); 358 359 $collection = new