1: <?php
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: class AlphaDAO_Test extends PHPUnit_Framework_TestCase {
49: 50: 51: 52: 53: 54:
55: private $person;
56:
57: 58: 59: 60: 61: 62: 63:
64: protected function setUp() {
65: AlphaDAO::begin();
66:
67: $rights = new RightsObject();
68: $rights->rebuildTable();
69:
70: $this->person = $this->createPersonObject('unitTestUser');
71: $this->person->rebuildTable();
72:
73:
74: $this->person->deleteAllByAttribute('URL', 'http://unitTestUser/');
75: $this->person->deleteAllByAttribute('displayName', 'unitTestUser');
76: }
77:
78: 79: 80: 81: 82: 83: 84:
85: protected function tearDown() {
86: AlphaDAO::rollback();
87: $this->person->dropTable();
88: unset($this->person);
89: $rights = new RightsObject();
90: $rights->dropTable();
91: $rights->dropTable('Person2Rights');
92: }
93:
94: 95: 96: 97: 98: 99:
100: private function createPersonObject($name) {
101: $person = new PersonObject();
102: $person->setDisplayname($name);
103: $person->set('email', $name.'@test.com');
104: $person->set('password', 'passwordTest');
105: $person->set('URL', 'http://unitTestUser/');
106:
107: return $person;
108: }
109:
110: 111: 112: 113: 114:
115: public function testDefaultHouseKeepingValues() {
116:
117: $this->assertEquals($_SESSION['currentUser']->getID(), $this->person->getCreatorId()->getValue(),
118: 'test that the constructor sets the correct values of the "house keeping" attributes');
119: $this->assertEquals($_SESSION['currentUser']->getID(), $this->person->getUpdatorId()->getValue(),
120: 'test that the constructor sets the correct values of the "house keeping" attributes');
121:
122: $this->assertEquals(0, $this->person->getVersionNumber()->getValue(),
123: 'test that the constructor sets the correct values of the "house keeping" attributes');
124:
125:
126: $today = date('Y-m-d');
127: $this->assertEquals($today, $this->person->getCreateTS()->getDate(),
128: 'test that the constructor sets the correct values of the "house keeping" attributes');
129: $this->assertEquals($today, $this->person->getUpdateTS()->getDate(),
130: 'test that the constructor sets the correct values of the "house keeping" attributes');
131:
132:
133: $this->assertTrue($this->person->isTransient(), 'test that the constructor sets the correct values of the "house keeping" attributes');
134: }
135:
136: 137: 138: 139: 140:
141: public function testBasicLoadSave() {
142: $this->person->save();
143: $id = $this->person->getMAX();
144: $this->person->load($id);
145: $this->assertEquals('unitTestUser', $this->person->getDisplayname()->getValue(), 'Testing the basic load/save functionality');
146: }
147:
148: 149: 150: 151: 152:
153: public function testCheckRecordExists() {
154: $this->person->save();
155: $person = new PersonObject();
156: $this->assertTrue($person->checkRecordExists($this->person->getOID()), 'Testing the checkRecordExists method');
157: }
158:
159: 160: 161: 162: 163:
164: public function testLoadByAttribute() {
165: $this->person->save();
166: $this->person->loadByAttribute('displayName','unitTestUser');
167: $this->assertEquals('unitTestUser@test.com', $this->person->get('email'), 'Testing the loadByAttribute method');
168: $this->person->loadByAttribute('email','unitTestUser@test.com');
169: $this->assertEquals('unitTestUser', $this->person->getDisplayname()->getValue(), 'Testing the loadByAttribute method');
170: }
171:
172: 173: 174: 175: 176:
177: public function testLoadAll() {
178: $this->person->save();
179: $peopleCount = $this->person->getCount();
180: $people = $this->person->loadAll();
181: $this->assertEquals($peopleCount, count($people), 'Testing loadAll method');
182:
183: $people = $this->person->loadAll(0, 1);
184: $this->assertEquals(1, count($people), 'Testing loadAll method');
185: }
186:
187: 188: 189: 190: 191:
192: public function testLoadAllByAttribute() {
193: $this->person->save();
194: $people = $this->person->loadAllByAttribute('email','unitTestUser@test.com');
195: $this->assertEquals(1, count($people), 'Testing the loadAllByAttribute method');
196: $this->assertEquals('unitTestUser', $people[0]->getDisplayname()->getValue(), 'Testing the loadAllByAttribute method');
197: $people[0]->delete();
198: }
199:
200: 201: 202: 203: 204:
205: public function testLoadAllByAttributes() {
206: $this->person->save();
207: $people = $this->person->loadAllByAttributes(array('OID'),array($this->person->getOID()));
208: $this->assertEquals(1, count($people), 'Testing the loadAllByAttribute method');
209: $this->assertEquals('unitTestUser', $people[0]->getDisplayname()->getValue(), 'Testing the loadAllByAttributes method');
210: $people[0]->delete();
211: }
212:
213: 214: 215: 216: 217:
218: public function testLoadAllByDayUpdated() {
219: $this->person->save();
220: $people = $this->person->loadAllByDayUpdated(date('Y-m-d'));
221: $this->assertGreaterThan(0, count($people), 'Testing the loadAllByDayUpdated method');
222: $people[0]->delete();
223: }
224:
225: 226: 227: 228: 229:
230: public function testLoadAllFieldValuesByAttribute() {
231: $this->person->save();
232: $emails = $this->person->loadAllFieldValuesByAttribute('email', $this->person->get('email'), 'email');
233: $this->assertEquals($this->person->get('email'), $emails[0], 'Testing the loadAllFieldValuesByAttribute method');
234: }
235:
236: 237: 238: 239: 240:
241: public function testSaveTransientOrPersistent() {
242: $this->assertTrue($this->person->isTransient(), 'Testing the save method on transient and non-transient objects');
243: $this->assertEquals(0, $this->person->getVersionNumber()->getValue(), 'Testing the save method on transient and non-transient objects');
244:
245: $this->person->save();
246:
247: $this->assertFalse($this->person->isTransient(), 'Testing the save method on transient and non-transient objects');
248: $this->assertEquals(1, $this->person->getVersionNumber()->getValue(), 'Testing the save method on transient and non-transient objects');
249: }
250:
251: 252: 253: 254: 255:
256: public function testSaveTransientOID() {
257: $this->assertTrue($this->person->isTransient(), 'Testing to ensure that a transient object, once saved, will have an OID');
258: $this->person->save();
259: $this->assertGreaterThan(0, $this->person->getID(), 'Testing to ensure that a transient object, once saved, will have an OID');
260: $this->assertFalse($this->person->isTransient(), 'Testing to ensure that a transient object, once saved, will have an OID');
261: }
262:
263: 264: 265: 266: 267:
268: public function testSaveObjectLocking() {
269: try {
270: $this->person->save();
271:
272: $personInstance1 = new PersonObject();
273: $personInstance1->load($this->person->getID());
274: $personInstance2 = new PersonObject();
275: $personInstance2->load($this->person->getID());
276:
277: $personInstance1->save();
278: $personInstance2->save();
279: $this->fail('Testing optimistic locking mechanism');
280: }catch (LockingException $e) {
281: $this->assertEquals('Could not save the object as it has been updated by another user. Please try saving again.',
282: $e->getMessage(),
283: 'Testing optimistic locking mechanism');
284: }
285: }
286:
287: 288: 289: 290: 291:
292: public function testValidation() {
293: try {
294: $person = new PersonObject();
295: $person->save();
296: $this->fail('Testing the validation method');
297: }catch (ValidationException $e) {
298: $this->assertEquals('Failed to save, validation error is:',
299: substr($e->getMessage(), 0, 36),
300: 'Testing the validation method');
301: }
302: }
303:
304: 305: 306: 307: 308:
309: public function testDelete() {
310: $this->person->save();
311: $this->assertFalse($this->person->isTransient(), 'Testing the delete method');
312: $id = $this->person->getID();
313: $this->person->delete();
314:
315: $this->assertEquals(0, count(get_object_vars($this->person)), 'Testing the delete method');
316:
317: try {
318: $this->person = new PersonObject();
319: $this->person->load($id);
320: $this->fail('Testing the delete method');
321: }catch (BONotFoundException $e) {
322: $this->assertEquals('Failed to load object',
323: substr($e->getMessage(), 0, 21),
324: 'Testing the delete method');
325: }
326: }
327:
328: 329: 330: 331: 332:
333: public function testDeleteAllByAttribute() {
334: $person1 = new PersonObject();
335: $person1->setDisplayname('unitTestUser1');
336: $person1->set('email', 'unitTestUser1@test.com');
337: $person1->set('password', 'passwordTest');
338: $person1->set('URL', 'http://unitTestUser/');
339:
340: $person2 = new PersonObject();
341: $person2->setDisplayname('unitTestUser2');
342: $person2->set('email', 'unitTestUser2@test.com');
343: $person2->set('password', 'passwordTest');
344: $person2->set('URL', 'http://unitTestUser/');
345:
346: $person3 = new PersonObject();
347: $person3->setDisplayname('unitTestUser3');
348: $person3->set('email', 'unitTestUser3@test.com');
349: $person3->set('password', 'passwordTest');
350: $person3->set('URL', 'http://unitTestUser/');
351:
352: $person1->save();
353: $person2->save();
354: $person3->save();
355: $this->assertEquals(3, $this->person->deleteAllByAttribute('URL', 'http://unitTestUser/'), 'Testing the deleteAllByAttribute method');
356: }
357:
358: 359: 360: 361: 362:
363: public function testGetVersion() {
364: $this->assertEquals(0, $this->person->getVersion(), 'Testing the version numbers of business objects');
365: $this->assertEquals(0, $this->person->getVersionNumber()->getValue(), 'Testing the version numbers of business objects');
366: $this->person->save();
367: $this->assertEquals(1, $this->person->getVersion(), 'Testing the version numbers of business objects');
368: $this->assertEquals(1, $this->person->getVersionNumber()->getValue(), 'Testing the version numbers of business objects');
369: $this->person->save();
370: $this->assertEquals(2, $this->person->getVersion(), 'Testing the version numbers of business objects');
371: $this->assertEquals(2, $this->person->getVersionNumber()->getValue(), 'Testing the version numbers of business objects');
372: }
373:
374: 375: 376: 377: 378:
379: public function testGetMAX() {
380: $this->person->save();
381: $max = $this->person->getMAX();
382: $person2 = $this->createPersonObject('unitTestUser2');
383: $person2->save();
384: $this->assertEquals($max+1, $this->person->getMAX(), 'Testing the getMAX method');
385: }
386:
387: 388: 389: 390: 391:
392: public function testGetCount() {
393: $count = $this->person->getCount();
394: $this->person->save();
395: $this->assertEquals($count+1, $this->person->getCount(), 'Testing the getCount method');
396: }
397:
398: 399: 400: 401: 402:
403: public function testSetEnumOptions() {
404: $this->person->save();
405: $id = $this->person->getMAX();
406: $this->person->load($id);
407: $this->assertTrue(in_array('Active', $this->person->getPropObject('state')->getOptions()),
408: 'Testing the setEnumOptions method is loading enum options correctly');
409: }
410:
411: 412: 413: 414: 415:
416: public function testCheckTableExists() {
417: $this->assertTrue($this->person->checkTableExists(), 'Testing that checkTableExists returns true for the person BO');
418: }
419:
420: 421: 422: 423: 424:
425: public function testCheckTableNeedsUpdate() {
426: $this->assertFalse($this->person->checkTableNeedsUpdate(), 'Testing that checkTableNeedsUpdate returns false for the person BO');
427: }
428:
429: 430: 431: 432: 433:
434: public function testGetTableName() {
435: $this->assertEquals('Person', $this->person->getTableName(),
436: 'Testing to ensure that the getTableName method can read the TABLE_NAME constant declared in the child class');
437: }
438:
439: 440: 441: 442: 443:
444: public function testGetDataLabel() {
445: $this->assertEquals('E-mail Address', $this->person->getDataLabel('email'), 'Testing the getDataLabel method');
446: }
447:
448: 449: 450: 451: 452:
453: public function testGetNoChildMethod() {
454: $email = $this->person->get('email');
455:
456: $this->assertEquals('unitTestUser@test.com', $email, 'Testing get on a String attribute with no child get method available');
457: }
458:
459: 460: 461: 462: 463:
464: public function testGetNoChildMethodsDisabled() {
465: $state = $this->person->getPropObject('state');
466:
467: $this->assertEquals('Enum', get_class($state),
468: 'Testing get on an Enum attribute with a child method avaialble, with $noChildMethods disabled (default)');
469: $this->assertEquals('Active', $state->getValue(),
470: 'Testing get on an Enum attribute with a child method avaialble, with $noChildMethods disabled (default)');
471: }
472:
473: 474: 475: 476: 477:
478: public function testGetNoChildMethodsEnabled() {
479: $state = $this->person->get('state', true);
480:
481: $this->assertEquals('Active', $state, 'Testing get on an Enum attribute with a child method avaialble, with $noChildMethods enabled');
482: }
483:
484: 485: 486: 487: 488:
489: public function testGetSimpleType() {
490: $labels = $this->person->get('dataLabels');
491:
492: $this->assertTrue(is_array($labels), 'Testing get on a simple data type');
493: }
494:
495: 496: 497: 498: 499:
500: public function testSetNoChildMethod() {
501: $this->person->set('email','test@test.com');
502:
503: $this->assertEquals('test@test.com', $this->person->get('email'), 'Testing set on a String attribute with no child get method available');
504: }
505:
506: 507: 508: 509: 510:
511: public function testSetNoChildMethodsDisabled() {
512: $this->person->set('state','Active');
513:
514: $this->assertEquals('Active', $this->person->get('state'),
515: 'Testing set on an Enum attribute with a child method avaialble, with $noChildMethods disabled (default)');
516: }
517:
518: 519: 520: 521: 522:
523: public function testSetNoChildMethodsEnabled() {
524: $this->person->set('state','Active', true);
525:
526: $this->assertEquals('Active', $this->person->get('state'),
527: 'Testing set on an Enum attribute with a child method avaialble, with $noChildMethods enabled');
528: }
529:
530: 531: 532: 533: 534:
535: public function testSetSimpleType() {
536: $this->person->set('dataLabels', array('key'=>'value'));
537:
538: $labels = $this->person->get('dataLabels');
539:
540: $this->assertTrue(is_array($labels), 'Testing set on a simple data type');
541: $this->assertEquals('value', $labels['key'], 'Testing set on a simple data type');
542: }
543:
544: 545: 546: 547: 548:
549: public function testGetPropObjectComplexType() {
550: $state = $this->person->getPropObject('state');
551:
552: $this->assertEquals('Enum', get_class($state), 'Testing getPropObject on a complex type');
553: $this->assertEquals('Active', $state->getValue(), 'Testing getPropObject on a complex type');
554: }
555:
556: 557: 558: 559: 560:
561: public function testGetPropObjectSimpleType() {
562: $labels = $this->person->getPropObject('dataLabels');
563:
564: $this->assertTrue(is_array($labels), 'Testing getPropObject on a simple type');
565: $this->assertEquals('E-mail Address', $labels['email'], 'Testing getPropObject on a simple type');
566: }
567:
568: 569: 570: 571: 572:
573: public function testMarkTransientPersistent() {
574:
575: $this->person->save();
576:
577:
578: $this->person->markTransient('URL');
579: $this->assertTrue(in_array('URL', $this->person->getTransientAttributes()), 'Testing that markTransient and markPersistent methods');
580: $this->person->set('URL','http://www.alphaframework.org/');
581: $this->person->save();
582:
583:
584: $this->person->markPersistent('URL');
585: $this->assertFalse(in_array('URL', $this->person->getTransientAttributes()), 'Testing that markTransient and markPersistent methods');
586:
587: $this->person->reload();
588:
589: $this->assertEquals('http://unitTestUser/', $this->person->get('URL'), 'Testing that markTransient and markPersistent methods');
590: }
591:
592: 593: 594: 595: 596:
597: public function testGetDataLabels() {
598: $this->assertTrue(is_array($this->person->getDataLabels()), 'Testing the getDataLabels method');
599: $labels = $this->person->getDataLabels();
600: $this->assertTrue(in_array('OID', array_keys($labels)), 'Testing the getDataLabels method');
601: $this->assertTrue(in_array('E-mail Address', $labels), 'Testing the getDataLabels method');
602: }
603:
604: 605: 606: 607: 608:
609: public function testGetTransientAttributes() {
610: $this->assertTrue(is_array($this->person->getTransientAttributes()),
611: 'Testing the getTransientAttributes method in conjunction with markTransient/markPersistent');
612: $this->person->markTransient('URL');
613: $this->assertTrue(in_array('URL', $this->person->getTransientAttributes()),
614: 'Testing the getTransientAttributes method in conjunction with markTransient/markPersistent');
615: $this->person->markPersistent('URL');
616: $this->assertFalse(in_array('URL', $this->person->getTransientAttributes()),
617: 'Testing the getTransientAttributes method in conjunction with markTransient/markPersistent');
618: }
619:
620: 621: 622: 623: 624:
625: public function testIsTransient() {
626: $this->assertTrue($this->person->isTransient(), 'Testing isTransient before and after save');
627: $this->person->save();
628: $this->assertFalse($this->person->isTransient(), 'Testing isTransient before and after save');
629: }
630:
631: 632: 633: 634: 635:
636: public function testGetLastQuery() {
637:
638: global $config;
639:
640: $this->person->save();
641:
642: if($config->get('db.provider.name') == 'AlphaDAOProviderMySQL') {
643: $this->assertEquals('INSERT INTO Person', substr($this->person->getLastQuery(), 0, 18),
644: 'Testing the getLastQuery method after various persistance calls');
645: $this->person->checkTableNeedsUpdate();
646: $this->assertEquals('SHOW INDEX FROM Person', substr($this->person->getLastQuery(), 0, 22),
647: 'Testing the getLastQuery method after various persistance calls');
648: $this->person->getCount();
649: $this->assertEquals('SELECT COUNT(OID)', substr($this->person->getLastQuery(), 0, 17),
650: 'Testing the getLastQuery method after various persistance calls');
651: $this->person->getMAX();
652: $this->assertEquals('SELECT MAX(OID)', substr($this->person->getLastQuery(), 0, 15),
653: 'Testing the getLastQuery method after various persistance calls');
654: $this->person->load($this->person->getID());
655: $this->assertEquals('SHOW COLUMNS FROM Person', substr($this->person->getLastQuery(), 0, 24),
656: 'Testing the getLastQuery method after various persistance calls');
657: }
658:
659: if($config->get('db.provider.name') == 'AlphaDAOProviderSQLite') {
660: $this->assertEquals('PRAGMA table_info(Person)', substr($this->person->getLastQuery(), 0, 25),
661: 'Testing the getLastQuery method after various persistance calls');
662: $this->person->checkTableNeedsUpdate();
663: $this->assertEquals('SELECT name FROM sqlite_master WHERE type=\'index\'', substr($this->person->getLastQuery(), 0, 49),
664: 'Testing the getLastQuery method after various persistance calls');
665: $this->person->getCount();
666: $this->assertEquals('SELECT COUNT(OID)', substr($this->person->getLastQuery(), 0, 17),
667: 'Testing the getLastQuery method after various persistance calls');
668: $this->person->getMAX();
669: $this->assertEquals('SELECT MAX(OID)', substr($this->person->getLastQuery(), 0, 15),
670: 'Testing the getLastQuery method after various persistance calls');
671: $this->person->load($this->person->getID());
672: $this->assertEquals('SELECT displayName,email,password,state,URL,OID,version_num,created_ts,created_by,updated_ts,updated_by FROM Person WHERE OID = :OID LIMIT 1;',
673: substr($this->person->getLastQuery(), 0, 150),
674: 'Testing the getLastQuery method after various persistance calls');
675: }
676: }
677:
678: 679: 680: 681: 682:
683: public function testClear() {
684: $state = $this->person->get('state');
685: $this->assertTrue(!empty($state), 'Testing the clear method for unsetting the attributes of an object');
686:
687: $reflection = new ReflectionClass(get_class($this->person));
688: $properties = $reflection->getProperties();
689:
690: foreach($properties as $propObj) {
691: $propName = $propObj->name;
692: if(!in_array($propName, $this->person->getDefaultAttributes()) && !in_array($propName, $this->person->getTransientAttributes())) {
693: $this->assertNotNull($this->person->get($propName), 'Testing the clear method for unsetting the attributes of an object');
694: }
695: }
696:
697:
698: $this->person->delete();
699:
700: try {
701: $state = $this->person->get('state');
702: $this->fail('Testing the clear method for unsetting the attributes of an object');
703: } catch (AlphaException $e) {
704: $reflection = new ReflectionClass(get_class($this->person));
705: $properties = $reflection->getProperties();
706:
707: foreach($properties as $propObj) {
708: $propName = $propObj->name;
709:
710: try {
711: $this->person->get($propName);
712: } catch (PHPException $e) {
713: $this->assertEquals(preg_match("/Undefined property/", $e->getMessage()), 1,
714: 'Testing the clear method for unsetting the attributes of an object');
715: } catch (AlphaException $e) {
716: $this->assertEquals('Could not access the property ['.$propName.'] on the object of class [PersonObject]', $e->getMessage(),
717: 'Testing the clear method for unsetting the attributes of an object');
718: }
719: }
720: }
721: }
722:
723: 724: 725: 726: 727:
728: public function testSaveAttribute() {
729: $this->person->save();
730: $this->person->saveAttribute('displayName', 'unitTestUserNew');
731:
732: $this->assertEquals('unitTestUserNew', $this->person->getDisplayName()->getValue(),
733: 'Testing that the value was set on the object in memory along with saving to the database');
734:
735: $person = new PersonObject();
736:
737: try {
738: $person->loadByAttribute('displayName', 'unitTestUserNew');
739: $this->assertEquals('unitTestUserNew', $person->getDisplayName()->getValue(), 'Testing that the value was saved to the database');
740: } catch (BONotFoundException $e) {
741: $this->fail('Failed to load the BO that was updated with the saveAttribute method');
742: }
743: }
744: }
745:
746: ?>