| [ 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 12 class PersonForTest extends lmbActiveRecord 13 { 14 public $save_count = 0; 15 protected $_has_one = array('social_security' => array('field' => 'ss_id', 16 'class' => 'SocialSecurityForTest', 17 'can_be_null' => true)); 18 19 function _onSave() 20 { 21 $this->save_count++; 22 } 23 24 } 25 26 class PersonForTestNoCascadeDelete extends lmbActiveRecord 27 { 28 protected $_db_table_name = 'person_for_test'; 29 protected $_has_one = array('social_security' => array('field' => 'ss_id', 30 'class' => 'SocialSecurityForTest', 31 'can_be_null' => true, 32 'cascade_delete' => false)); 33 } 34 35 class PersonForTestWithRequiredSocialSecurity extends lmbActiveRecord 36 { 37 protected $_db_table_name = 'person_for_test'; 38 protected $_has_one = array('social_security' => array('field' => 'ss_id', 39 'class' => 'SocialSecurityForTest', 40 'can_be_null' => true)); 41 42 function _createValidator() 43 { 44 $validator = new lmbValidator(); 45 $validator->addRequiredObjectRule('social_security'); 46 return $validator; 47 } 48 } 49 50 class SocialSecurityForTest extends lmbActiveRecord 51 { 52 protected $_belongs_to = array('person' => array('field' => 'ss_id', 53 'class' => 'PersonForTest')); 54 } 55 56 class lmbAROneToOneRelationsTest extends UnitTestCase 57 { 58 protected $db; 59 60 function setUp() 61 { 62 $this->db = new lmbSimpleDb(lmbToolkit :: instance()->getDefaultDbConnection()); 63 $this->_dbCleanUp(); 64 } 65 66 function tearDown() 67 { 68 $this->_dbCleanUp(); 69 } 70 71 function _dbCleanUp() 72 { 73 $this->db->delete('person_for_test'); 74 $this->db->delete('social_security_for_test'); 75 } 76 77 function testNewObjectReturnsNullChild() 78 { 79 $person = new PersonForTest(); 80 $this->assertNull($person->getSocialSecurity()); 81 } 82 83 function testNewObjectReturnsNullParent() 84 { 85 $number = new SocialSecurityForTest(); 86 $this->assertNull($number->getPerson()); 87 } 88 89 function testSaveChild() 90 { 91 $person = new PersonForTest(); 92 $person->setName('Jim'); 93 $number = new SocialSecurityForTest(); 94 $number->setCode('099123'); 95 96 $person->setSocialSecurity($number); 97 98 $this->assertNull($number->getId()); 99 100 $person->save(); 101 102 $this->assertNotNull($number->getId()); 103 } 104 105 function testSaveParent() 106 { 107 $person = new PersonForTest(); 108 $person->setName('Jim'); 109 110 $number = new SocialSecurityForTest(); 111 $number->setCode('099123'); 112 113 $number->setPerson($person); 114 115 $this->assertNull($person->getId()); 116 117 $number->save(); 118 119 $this->assertNotNull($person->getId()); 120 } 121 122 function testDontSaveParentSecondTimeIfChildWasChanged() 123 { 124 $person = new PersonForTest(); 125 $person->setName('Jim'); 126 127 $number = new SocialSecurityForTest(); 128 $number->setCode('099123'); 129 130 $person->setSocialSecurity($number); 131 $person->save(); 132 133 $this->assertEqual($person->save_count, 1); 134 135 $person->save(); 136 137 $this->assertEqual($person->save_count, 1); 138 } 139 140 function testSavingParentSavesChildAsWell() 141 { 142 $person = new PersonForTest(); 143 $person->setName('Jim'); 144 145 $number = new SocialSecurityForTest(); 146 $number->setCode('099123'); 147 148 $person->setSocialSecurity($number); 149 $person->save(); 150 151 $number->setCode($new_code = '0022112'); 152 $person->save(); 153 154 $loaded_number = new SocialSecurityForTest($number->getId()); 155 $this->assertEqual($loaded_number->getCode(), $new_code); 156 } 157 158 function testChangingChildObjectIdDirectly() 159 { 160 $person = new PersonForTest(); 161 $person->setName('Jim'); 162 163 $number1 = new SocialSecurityForTest(); 164 $number1->setCode('099123'); 165 166 $person->setSocialSecurity($number1); 167 $person->save(); 168 169 $number2 = new SocialSecurityForTest(); 170 $number2->setCode('143453'); 171 $number2->save(); 172 173 $person2 = new PersonForTest($person->getId()); 174 $this->assertEqual($person2->getSocialSecurity()->getId(), $number1->getId()); 175 176 $person2->set('ss_id', $number2->getId()); 177 $person2->save(); 178 179 $person3 = new PersonForTest($person->getId()); 180 $this->assertEqual($person3->getSocialSecurity()->getId(), $number2->getId()); 181 } 182 183 function testChangingChildIdRelationFieldDirectlyHasNoAffectIfChildObjectPropertyIsDirty() 184 { 185 $person = new PersonForTest(); 186 $person->setName('Jim'); 187 188 $number1 = new SocialSecurityForTest(); 189 $number1->setCode('099123'); 190 191 $person->setSocialSecurity($number1); 192 $person->save(); 193 194 $number2 = new SocialSecurityForTest(); 195 $number2->setCode('143453'); 196 $number2->save(); 197 198 $person2 = new PersonForTest($person->getId()); 199 $this->assertEqual($person2->getSocialSecurity()->getId(), $number1->getId()); 200 201 $person2->set('ss_id', $number2->getId()); // changing child relation field directly 202 $person2->setSocialSecurity($number1); // and making child object dirty 203 $person2->save(); 204 205 $person3 = new PersonForTest($person->getId()); 206 $this->assertEqual($person3->getSocialSecurity()->getId(), $number1->getId()); 207 } 208 209 function testLoadParentObject() 210 { 211 $person = new PersonForTest(); 212 $person->setName('Jim'); 213 $number = new SocialSecurityForTest(); 214 $number->setCode('099123'); 215 216 $person->setSocialSecurity($number); 217 218 $person->save(); 219 220 $number2 = lmbActiveRecord :: findById('SocialSecurityForTest', $number->getId()); 221 222 $person2 = $number2->getPerson(); 223 224 $this->assertEqual($person2->getId(), $person->getId()); 225 $this->assertEqual($person2->getName(), 'Jim'); 226 } 227 228 function testGenericGetLoadsChildObject() 229 { 230 $person = new PersonForTest(); 231 $person->setName('Jim'); 232 $number = new SocialSecurityForTest(); 233 $number->setCode('099123'); 234 235 $person->setSocialSecurity($number); 236 237 $person->save(); 238 239 $number2 = lmbActiveRecord :: findById('SocialSecurityForTest', $number->getId()); 240 241 $person2 = $number2->getPerson(); 242 243 $this->assertEqual($person2->getId(), $person->getId()); 244 $this->assertEqual($person2->getName(), 'Jim'); 245 } 246 247 function testLoadChildObject() 248 { 249 $person = new PersonForTest(); 250 $person->setName('Jim'); 251 $number = new SocialSecurityForTest(); 252 $number->setCode('099123'); 253 254 $person->setSocialSecurity($number); 255 256 $person_id = $person->save(); 257 258 $person2 = lmbActiveRecord :: findById('PersonForTest', $person_id); 259 $number2 = $person2->getSocialSecurity(); 260 261 $this->assertEqual($person2->getId(), $person_id); 262 $this->assertEqual($person2->getName(), 'Jim'); 263 $this->assertEqual($number2->getId(), $number->getId()); 264 $this->assertEqual($number2->getCode(), '099123'); 265 } 266 267 function testGenericGetLoadsParentObject() 268 { 269 $person = new PersonForTest(); 270 $person->setName('Jim'); 271 $number = new SocialSecurityForTest(); 272 $number->setCode('099123'); 273 274 $person->setSocialSecurity($number); 275 276 $person_id = $person->save(); 277 278 $person2 = lmbActiveRecord :: findById('PersonForTest', $person_id); 279 $number2 = $person2->get('social_security'); 280 281 $this->assertEqual($person2->getId(), $person_id); 282 $this->assertEqual($person2->getName(), 'Jim'); 283 $this->assertEqual($number2->getId(), $number->getId()); 284 $this->assertEqual($number2->getCode(), '099123'); 285 } 286 287 function testParentRemovalDeletesChildren() 288 { 289 $person = new PersonForTest(); 290 $person->setName('Jim'); 291 $number = new SocialSecurityForTest(); 292 $number->setCode('099123'); 293 294 $person->setSocialSecurity($number); 295 296 $person_id = $person->save(); 297 $this->assertTrue($number_id = $number->getId()); 298 299 $person->destroy(); 300 301 $this->assertNull(lmbActiveRecord :: findFirst('SocialSecurityForTest', array('criteria' => 'id = ' . $number_id))); 302 $this->assertNull(lmbActiveRecord :: findFirst('PersonForTest', array('criteria' => 'id = ' . $person_id))); 303 } 304 305 function testParentDeleteAllDeletesChildren() 306 { 307 $person = new PersonForTest(); 308 $person->setName('Jim'); 309 $number = new SocialSecurityForTest(); 310 $number->setCode('099123'); 311 312 $person->setSocialSecurity($number); 313 314 $person_id = $person->save(); 315 $number_id = $number->getId(); 316 317 //this one should stay 318 $untouched_number = new SocialSecurityForTest(); 319 $untouched_number->setCode('0893127'); 320 $untouched_number->save(); 321 322 lmbActiveRecord :: delete('PersonForTest'); 323 324 $this->assertNull(lmbActiveRecord :: findFirst('SocialSecurityForTest', array('criteria' => 'id = ' . $number_id))); 325 $this->assertNull(lmbActiveRecord :: findFirst('PersonForTest', array('criteria' => 'id = ' . $person_id))); 326 327 $number2 = lmbActiveRecord :: findById('SocialSecurityForTest', $untouched_number->getId()); 328 $this->assertEqual($number2->getCode(), '0893127'); 329 } 330 331 function testParentRemovalWithNoCascadeDeleteChildren() 332 { 333 $person = new PersonForTestNoCascadeDelete(); 334 $person->setName('Jim'); 335 $number = new SocialSecurityForTest(); 336 $number->setCode('099123'); 337 338 $person->setSocialSecurity($number); 339 340 $person_id = $person->save(); 341 $this->assertTrue($number_id = $number->getId()); 342 343 $person->destroy(); 344 345 $ss2 = lmbActiveRecord :: findFirst('SocialSecurityForTest', array('criteria' => 'id = ' . $number_id)); 346 $this->assertEqual($ss2->getCode(), $number->getCode()); 347 } 348 349 function testChildRemovalNullifyParentField() 350 { 351 $number = new SocialSecurityForTest(); 352 $number->setCode('099123'); 353 354 $person = new PersonForTest(); 355 $person->setName('Jim'); 356 357 $person->setSocialSecurity($number); 358 $number->setPerson($person); 359 $person->setNumber($number); 360 $person->save(); 361 362 $number->destroy(); 363 364 $person2 = new PersonForTest($person->getId()); 365 $this->assertNull($person2->get('ss_id')); 366 } 367 368 function testChildRemovalWithRequiredObjectInParentRelationDefinitionThrowsValidationException() 369 { 370 $number = new SocialSecurityForTest(); 371 $number->setCode('099123'); 372 373 $person = new PersonForTestWithRequiredSocialSecurity(); 374 $person->setName('Jim'); 375 376 $person->setSocialSecurity($number); 377 $number->setPerson($person); 378 $person->save(); 379 380 try 381 { 382 $number->destroy(); 383 $this->assertTrue(false); 384 } 385 catch(lmbValidationException $e) 386 { 387 $this->assertTrue(true); 388 } 389 390 $number2 = lmbActiveRecord :: findFirst('SocialSecurityForTest'); 391 $this->assertNotNull($number2, 'Removal should not be finished'); 392 $this->assertEqual($number2->getId(), $number->getId()); 393 } 394 395 function testSettingNullDetachesChildObject() 396 { 397 $person = new PersonForTest(); 398 $person->setName('Jim'); 399 $number = new SocialSecurityForTest(); 400 $number->setCode('099123'); 401 402 $person->setSocialSecurity($number); 403 $person->save(); 404 405 $person->setSocialSecurity(null); 406 $person_id = $person->save(); 407 408 $person2 = new PersonForTest($person_id); 409 $this->assertNull($person2->getSocialSecurity()); 410 411 $number2 = new SocialSecurityForTest($number->getId()); 412 $this->assertEqual($number2->getCode(), $number->getCode()); 413 } 414 415 function testDontResetParentIfChildImport() 416 { 417 $person = new PersonForTest(); 418 $person->setName('Jim'); 419 $number = new SocialSecurityForTest(); 420 $number->setCode('099123'); 421 $person->setSocialSecurity($number); 422 $person->save(); 423 424 $source = array('name' => $person->getName()); 425 426 $person2 = new PersonForTest($person->getid()); 427 $person2->save(); 428 429 $this->assertEqual($person2->getName(), $person->getName()); 430 $this->assertEqual($person2->getSoc