      1: <?php
      2:
      3: /**
      4:  *
      5:  * Test case for the AlphaDAO class
      6:  *
      7:  * @package alpha::tests
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: AlphaDAO_Test.php 1563 2012-08-04 14:36:54Z alphadevx $
     11:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     12:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     13:  * All rights reserved.
     14:  *
     15:  * <pre>
     16:  * Redistribution and use in source and binary forms, with or
     17:  * without modification, are permitted provided that the
     18:  * following conditions are met:
     19:  *
     20:  * * Redistributions of source code must retain the above
     21:  *   copyright notice, this list of conditions and the
     22:  *   following disclaimer.
     23:  * * Redistributions in binary form must reproduce the above
     24:  *   copyright notice, this list of conditions and the
     25:  *   following disclaimer in the documentation and/or other
     26:  *   materials provided with the distribution.
     27:  * * Neither the name of the Alpha Framework nor the names
     28:  *   of its contributors may be used to endorse or promote
     29:  *   products derived from this software without specific
     30:  *   prior written permission.
     31:  *
     32:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     33:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     34:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     35:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     36:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     37:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     38:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     39:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     40:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     43:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     44:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     45:  * </pre>
     46:  *
     47:  */
     48: class AlphaDAO_Test extends PHPUnit_Framework_TestCase {
     49:     /**
     50:      * A PersonObject for Testing (any business object will do)
     51:      *
     52:      * @var PersonObject
     53:      * @since 1.0
     54:      */
     55:     private $person;
     56:
     57:     /**
     58:      * Called before the test functions will be executed
     59:      * this function is defined in PHPUnit_TestCase and overwritten
     60:      * here
     61:      *
     62:      * @since 1.0
     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:         // just making sure no previous test user is in the DB
     74:         $this->person->deleteAllByAttribute('URL', 'http://unitTestUser/');
     75:         $this->person->deleteAllByAttribute('displayName', 'unitTestUser');
     76:     }
     77:
     78:     /**
     79:      * Called after the test functions are executed
     80:      * this function is defined in PHPUnit_TestCase and overwritten
     81:      * here
     82:      *
     83:      * @since 1.0
     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:      * Creates a person object for Testing
     96:      *
     97:      * @return PersonObject
     98:      * @since 1.0
     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:      * Test that the constructor sets the correct values of the "house keeping" attributes
    112:      *
    113:      * @since 1.0
    114:      */
    115:     public function testDefaultHouseKeepingValues() {
    116:         // make sure the person logged in is the same person to create/update the object
    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:         // as it is a new object, make sure the version number is zero
    122:         $this->assertEquals(0, $this->person->getVersionNumber()->getValue(),
    123:             'test that the constructor sets the correct values of the "house keeping" attributes');
    124:
    125:         // check that the date created and updated equal to today
    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:         // make sure the object is transient
    133:         $this->assertTrue($this->person->isTransient(), 'test that the constructor sets the correct values of the "house keeping" attributes');
    134:     }
    135:
    136:     /**
    137:      * Testing the basic load/save functionality
    138:      *
    139:      * @since 1.0
    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:      * Testing the checkRecordExists method
    150:      *
    151:      * @since 1.0
    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:      * Testing the loadByAttribute method
    161:      *
    162:      * @since 1.0
    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:      * Testing loadAll method
    174:      *
    175:      * @since 1.0
    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:         // only load 1
    183:         $people = $this->person->loadAll(0, 1);
    184:         $this->assertEquals(1, count($people), 'Testing loadAll method');
    185:     }
    186:
    187:     /**
    188:      * Testing the loadAllByAttribute method
    189:      *
    190:      * @since 1.0
    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:      * Testing the loadAllByAttributes method
    202:      *
    203:      * @since 1.0
    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:      * Testing the loadAllByDayUpdated method
    215:      *
    216:      * @since 1.0
    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:      * Testing the loadAllFieldValuesByAttribute method
    227:      *
    228:      * @since 1.0
    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:      * Testing the save method on transient and non-transient objects
    238:      *
    239:      * @since 1.0
    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:      * Testing to ensure that a transient object, once saved, will have an OID
    253:      *
    254:      * @since 1.0
    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:      * Testing optimistic locking mechanism#
    265:      *
    266:      * @since 1.0
    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:      * Testing the validation method
    289:      *
    290:      * @since 1.0
    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:      * Testing the delete method
    306:      *
    307:      * @since 1.0
    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:         // gone from memory (all attributes null)
    315:         $this->assertEquals(0, count(get_object_vars($this->person)), 'Testing the delete method');
    316:         // gone from the database
    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:      * Testing the deleteAllByAttribute method
    330:      *
    331:      * @since 1.0
    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:      * Testing the version numbers of business objects
    360:      *
    361:      * @since 1.0
    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:      * Testing the getMAX method
    376:      *
    377:      * @since 1.0
    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:      * Testing the getCount method
    389:      *
    390:      * @since 1.0
    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:      * Testing the setEnumOptions method is loading enum options correctly
    400:      *
    401:      * @since 1.0
    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:      * Testing that checkTableExists returns true for the person BO
    413:      *
    414:      * @since 1.0
    415:      */
    416:     public function testCheckTableExists() {
    417:         $this->assertTrue($this->person->checkTableExists(), 'Testing that checkTableExists returns true for the person BO');
    418:     }
    419:
    420:     /**
    421:      * Testing that checkTableNeedsUpdate returns false for the person BO
    422:      *
    423:      * @since 1.0
    424:      */
    425:     public function testCheckTableNeedsUpdate() {
    426:         $this->assertFalse($this->person->checkTableNeedsUpdate(), 'Testing that checkTableNeedsUpdate returns false for the person BO');
    427:     }
    428:
    429:     /**
    430:      * Testing to ensure that the getTableName method can read the TABLE_NAME constant declared in the child class
    431:      *
    432:      * @since 1.0
    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:      * Testing the getDataLabel method
    441:      *
    442:      * @since 1.0
    443:      */
    444:     public function testGetDataLabel() {
    445:         $this->assertEquals('E-mail Address', $this->person->getDataLabel('email'), 'Testing the getDataLabel method');
    446:     }
    447:
    448:     /**
    449:      * Testing get on a String attribute with no child get method available
    450:      *
    451:      * @since 1.0
    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:      * Testing get on an Enum attribute with a child method available, with $noChildMethods disabled (default)
    461:      *
    462:      * @since 1.0
    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:      * Testing get on an Enum attribute with a child method available, with $noChildMethods enabled
    475:      *
    476:      * @since 1.0
    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:      * Testing get on a simple data type
    486:      *
    487:      * @since 1.0
    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:      * Testing set on a String attribute with no child get method available
    497:      *
    498:      * @since 1.0
    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:      * Testing set on an Enum attribute with a child method available, with $noChildMethods disabled (default)
    508:      *
    509:      * @since 1.0
    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:      * Testing set on an Enum attribute with a child method available, with $noChildMethods enabled
    520:      *
    521:      * @since 1.0
    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:      * Testing set on a simple data type
    532:      *
    533:      * @since 1.0
    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:      * Testing getPropObject on a complex type
    546:      *
    547:      * @since 1.0
    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:      * Testing getPropObject on a simple type
    558:      *
    559:      * @since 1.0
    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:      * Testing that markTransient and markPersistent methods
    570:      *
    571:      * @since 1.0
    572:      */
    573:     public function testMarkTransientPersistent() {
    574:         // initial save
    575:         $this->person->save();
    576:
    577:         // now mark the URL transient, and save again (old URL value should not be overwritten)
    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:         // used to ensure that we attempt to reload it from the DB
    584:         $this->person->markPersistent('URL');
    585:         $this->assertFalse(in_array('URL', $this->person->getTransientAttributes()), 'Testing that markTransient and markPersistent methods');
    586:         // reload from DB
    587:         $this->person->reload();
    588:
    589:         $this->assertEquals('http://unitTestUser/', $this->person->get('URL'), 'Testing that markTransient and markPersistent methods');
    590:     }
    591:
    592:     /**
    593:      * Testing the getDataLabels method
    594:      *
    595:      * @since 1.0
    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:      * Testing the getTransientAttributes method in conjunction with markTransient/markPersistent
    606:      *
    607:      * @since 1.0
    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:      * Testing isTransient before and after save
    622:      *
    623:      * @since 1.0
    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:      * Testing the getLastQuery method after various persistance calls
    633:      *
    634:      * @since 1.0
    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:      * Testing the clear method for unsetting the attributes of an object
    680:      *
    681:      * @since 1.0
    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:         // delete will invoke clear(), which is private
    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:      * Testing the saveAttribute method
    725:      *
    726:      * @since 1.0
    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: ?>