      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 1624 2012-12-21 12:17:55Z 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:         $request = new BadRequestObject();
     71:         $request->rebuildTable();
     72:
     73:         $this->person = $this->createPersonObject('unitTestUser');
     74:         $this->person->rebuildTable();
     75:
     76:         // just making sure no previous test user is in the DB
     77:         $this->person->deleteAllByAttribute('URL', 'http://unitTestUser/');
     78:         $this->person->deleteAllByAttribute('displayName', 'unitTestUser');
     79:     }
     80:
     81:     /**
     82:      * Called after the test functions are executed
     83:      * this function is defined in PHPUnit_TestCase and overwritten
     84:      * here
     85:      *
     86:      * @since 1.0
     87:      */
     88:     protected function tearDown() {
     89:         AlphaDAO::rollback();
     90:         $this->person->dropTable();
     91:         unset($this->person);
     92:         $rights = new RightsObject();
     93:         $rights->dropTable();
     94:         $rights->dropTable('Person2Rights');
     95:         $request = new BadRequestObject();
     96:         $request->dropTable();
     97:     }
     98:
     99:     /**
    100:      * Creates a person object for Testing
    101:      *
    102:      * @return PersonObject
    103:      * @since 1.0
    104:      */
    105:     private function createPersonObject($name) {
    106:         $person = new PersonObject();
    107:         $person->setDisplayname($name);
    108:         $person->set('email', $name.'@test.com');
    109:         $person->set('password', 'passwordTest');
    110:         $person->set('URL', 'http://unitTestUser/');
    111:
    112:         return $person;
    113:     }
    114:
    115:     /**
    116:      * Test that the constructor sets the correct values of the "house keeping" attributes
    117:      *
    118:      * @since 1.0
    119:      */
    120:     public function testDefaultHouseKeepingValues() {
    121:         // make sure the person logged in is the same person to create/update the object
    122:         $this->assertEquals($_SESSION['currentUser']->getID(), $this->person->getCreatorId()->getValue(),
    123:             'test that the constructor sets the correct values of the "house keeping" attributes');
    124:         $this->assertEquals($_SESSION['currentUser']->getID(), $this->person->getUpdatorId()->getValue(),
    125:             'test that the constructor sets the correct values of the "house keeping" attributes');
    126:         // as it is a new object, make sure the version number is zero
    127:         $this->assertEquals(0, $this->person->getVersionNumber()->getValue(),
    128:             'test that the constructor sets the correct values of the "house keeping" attributes');
    129:
    130:         // check that the date created and updated equal to today
    131:         $today = date('Y-m-d');
    132:         $this->assertEquals($today, $this->person->getCreateTS()->getDate(),
    133:             'test that the constructor sets the correct values of the "house keeping" attributes');
    134:         $this->assertEquals($today, $this->person->getUpdateTS()->getDate(),
    135:             'test that the constructor sets the correct values of the "house keeping" attributes');
    136:
    137:         // make sure the object is transient
    138:         $this->assertTrue($this->person->isTransient(), 'test that the constructor sets the correct values of the "house keeping" attributes');
    139:     }
    140:
    141:     /**
    142:      * Testing the basic load/save functionality
    143:      *
    144:      * @since 1.0
    145:      */
    146:     public function testBasicLoadSave() {
    147:         $this->person->save();
    148:         $id = $this->person->getMAX();
    149:         $this->person->load($id);
    150:         $this->assertEquals('unitTestUser', $this->person->getDisplayname()->getValue(), 'Testing the basic load/save functionality');
    151:     }
    152:
    153:     /**
    154:      * Testing the checkRecordExists method
    155:      *
    156:      * @since 1.0
    157:      */
    158:     public function testCheckRecordExists() {
    159:         $this->person->save();
    160:         $person = new PersonObject();
    161:         $this->assertTrue($person->checkRecordExists($this->person->getOID()), 'Testing the checkRecordExists method');
    162:     }
    163:
    164:     /**
    165:      * Testing the loadByAttribute method
    166:      *
    167:      * @since 1.0
    168:      */
    169:     public function testLoadByAttribute() {
    170:         $this->person->save();
    171:         $this->person->loadByAttribute('displayName','unitTestUser');
    172:         $this->assertEquals('unitTestUser@test.com', $this->person->get('email'), 'Testing the loadByAttribute method');
    173:         $this->person->loadByAttribute('email','unitTestUser@test.com');
    174:         $this->assertEquals('unitTestUser', $this->person->getDisplayname()->getValue(), 'Testing the loadByAttribute method');
    175:     }
    176:
    177:     /**
    178:      * Testing loadAll method
    179:      *
    180:      * @since 1.0
    181:      */
    182:     public function testLoadAll() {
    183:         $this->person->save();
    184:         $peopleCount = $this->person->getCount();
    185:         $people = $this->person->loadAll();
    186:         $this->assertEquals($peopleCount, count($people), 'Testing loadAll method');
    187:         // only load 1
    188:         $people = $this->person->loadAll(0, 1);
    189:         $this->assertEquals(1, count($people), 'Testing loadAll method');
    190:     }
    191:
    192:     /**
    193:      * Testing the loadAllByAttribute method
    194:      *
    195:      * @since 1.0
    196:      */
    197:     public function testLoadAllByAttribute() {
    198:         $this->person->save();
    199:         $people = $this->person->loadAllByAttribute('email','unitTestUser@test.com');
    200:         $this->assertEquals(1, count($people), 'Testing the loadAllByAttribute method');
    201:         $this->assertEquals('unitTestUser', $people[0]->getDisplayname()->getValue(), 'Testing the loadAllByAttribute method');
    202:         $people[0]->delete();
    203:     }
    204:
    205:     /**
    206:      * Testing the loadAllByAttributes method
    207:      *
    208:      * @since 1.0
    209:      */
    210:     public function testLoadAllByAttributes() {
    211:         $this->person->save();
    212:         $people = $this->person->loadAllByAttributes(array('OID'),array($this->person->getOID()));
    213:         $this->assertEquals(1, count($people), 'Testing the loadAllByAttribute method');
    214:         $this->assertEquals('unitTestUser', $people[0]->getDisplayname()->getValue(), 'Testing the loadAllByAttributes method');
    215:         $people[0]->delete();
    216:     }
    217:
    218:     /**
    219:      * Testing the loadAllByDayUpdated method
    220:      *
    221:      * @since 1.0
    222:      */
    223:     public function testLoadAllByDayUpdated() {
    224:         $this->person->save();
    225:         $people = $this->person->loadAllByDayUpdated(date('Y-m-d'));
    226:         $this->assertGreaterThan(0, count($people), 'Testing the loadAllByDayUpdated method');
    227:         $people[0]->delete();
    228:     }
    229:
    230:     /**
    231:      * Testing the loadAllFieldValuesByAttribute method
    232:      *
    233:      * @since 1.0
    234:      */
    235:     public function testLoadAllFieldValuesByAttribute() {
    236:         $this->person->save();
    237:         $emails = $this->person->loadAllFieldValuesByAttribute('email', $this->person->get('email'), 'email');
    238:         $this->assertEquals($this->person->get('email'), $emails[0], 'Testing the loadAllFieldValuesByAttribute method');
    239:     }
    240:
    241:     /**
    242:      * Testing the save method on transient and non-transient objects
    243:      *
    244:      * @since 1.0
    245:      */
    246:     public function testSaveTransientOrPersistent() {
    247:         $this->assertTrue($this->person->isTransient(), 'Testing the save method on transient and non-transient objects');
    248:         $this->assertEquals(0, $this->person->getVersionNumber()->getValue(), 'Testing the save method on transient and non-transient objects');
    249:
    250:         $this->person->save();
    251:
    252:         $this->assertFalse($this->person->isTransient(), 'Testing the save method on transient and non-transient objects');
    253:         $this->assertEquals(1, $this->person->getVersionNumber()->getValue(), 'Testing the save method on transient and non-transient objects');
    254:     }
    255:
    256:     /**
    257:      * Testing to ensure that a transient object, once saved, will have an OID
    258:      *
    259:      * @since 1.0
    260:      */
    261:     public function testSaveTransientOID() {
    262:         $this->assertTrue($this->person->isTransient(), 'Testing to ensure that a transient object, once saved, will have an OID');
    263:         $this->person->save();
    264:         $this->assertGreaterThan(0, $this->person->getID(), 'Testing to ensure that a transient object, once saved, will have an OID');
    265:         $this->assertFalse($this->person->isTransient(), 'Testing to ensure that a transient object, once saved, will have an OID');
    266:     }
    267:
    268:     /**
    269:      * Testing optimistic locking mechanism#
    270:      *
    271:      * @since 1.0
    272:      */
    273:     public function testSaveObjectLocking() {
    274:         try {
    275:             $this->person->save();
    276:
    277:             $personInstance1 = new PersonObject();
    278:             $personInstance1->load($this->person->getID());
    279:             $personInstance2 = new PersonObject();
    280:             $personInstance2->load($this->person->getID());
    281:
    282:             $personInstance1->save();
    283:             $personInstance2->save();
    284:             $this->fail('Testing optimistic locking mechanism');
    285:         }catch (LockingException $e) {
    286:             $this->assertEquals('Could not save the object as it has been updated by another user.  Please try saving again.',
    287:                             $e->getMessage(),
    288:                             'Testing optimistic locking mechanism');
    289:         }
    290:     }
    291:
    292:     /**
    293:      * Testing the validation method
    294:      *
    295:      * @since 1.0
    296:      */
    297:     public function testValidation() {
    298:         try {
    299:             $person = new PersonObject();
    300:             $person->save();
    301:             $this->fail('Testing the validation method');
    302:         }catch (ValidationException $e) {
    303:             $this->assertEquals('Failed to save, validation error is:',
    304:                             substr($e->getMessage(), 0, 36),
    305:                             'Testing the validation method');
    306:         }
    307:     }
    308:
    309:     /**
    310:      * Testing the delete method
    311:      *
    312:      * @since 1.0
    313:      */
    314:     public function testDelete() {
    315:         $this->person->save();
    316:         $this->assertFalse($this->person->isTransient(), 'Testing the delete method');
    317:         $id = $this->person->getID();
    318:         $this->person->delete();
    319:         // gone from memory (all attributes null)
    320:         $this->assertEquals(0, count(get_object_vars($this->person)), 'Testing the delete method');
    321:         // gone from the database
    322:         try {
    323:             $this->person = new PersonObject();
    324:             $this->person->load($id);
    325:             $this->fail('Testing the delete method');
    326:         }catch (BONotFoundException $e) {
    327:             $this->assertEquals('Failed to load object',
    328:                             substr($e->getMessage(), 0, 21),
    329:                             'Testing the delete method');
    330:         }
    331:     }
    332:
    333:     /**
    334:      * Testing the deleteAllByAttribute method
    335:      *
    336:      * @since 1.0
    337:      */
    338:     public function testDeleteAllByAttribute() {
    339:         $person1 = new PersonObject();
    340:         $person1->setDisplayname('unitTestUser1');
    341:         $person1->set('email', 'unitTestUser1@test.com');
    342:         $person1->set('password', 'passwordTest');
    343:         $person1->set('URL', 'http://unitTestUser/');
    344:
    345:         $person2 = new PersonObject();
    346:         $person2->setDisplayname('unitTestUser2');
    347:         $person2->set('email', 'unitTestUser2@test.com');
    348:         $person2->set('password', 'passwordTest');
    349:         $person2->set('URL', 'http://unitTestUser/');
    350:
    351:         $person3 = new PersonObject();
    352:         $person3->setDisplayname('unitTestUser3');
    353:         $person3->set('email', 'unitTestUser3@test.com');
    354:         $person3->set('password', 'passwordTest');
    355:         $person3->set('URL', 'http://unitTestUser/');
    356:
    357:         $person1->save();
    358:         $person2->save();
    359:         $person3->save();
    360:         $this->assertEquals(3, $this->person->deleteAllByAttribute('URL', 'http://unitTestUser/'), 'Testing the deleteAllByAttribute method');
    361:     }
    362:
    363:     /**
    364:      * Testing the version numbers of business objects
    365:      *
    366:      * @since 1.0
    367:      */
    368:     public function testGetVersion() {
    369:         $this->assertEquals(0, $this->person->getVersion(), 'Testing the version numbers of business objects');
    370:         $this->assertEquals(0, $this->person->getVersionNumber()->getValue(), 'Testing the version numbers of business objects');
    371:         $this->person->save();
    372:         $this->assertEquals(1, $this->person->getVersion(), 'Testing the version numbers of business objects');
    373:         $this->assertEquals(1, $this->person->getVersionNumber()->getValue(), 'Testing the version numbers of business objects');
    374:         $this->person->save();
    375:         $this->assertEquals(2, $this->person->getVersion(), 'Testing the version numbers of business objects');
    376:         $this->assertEquals(2, $this->person->getVersionNumber()->getValue(), 'Testing the version numbers of business objects');
    377:     }
    378:
    379:     /**
    380:      * Testing the getMAX method
    381:      *
    382:      * @since 1.0
    383:      */
    384:     public function testGetMAX() {
    385:         $this->person->save();
    386:         $max = $this->person->getMAX();
    387:         $person2 = $this->createPersonObject('unitTestUser2');
    388:         $person2->save();
    389:         $this->assertEquals($max+1, $this->person->getMAX(), 'Testing the getMAX method');
    390:     }
    391:
    392:     /**
    393:      * Testing the getCount method
    394:      *
    395:      * @since 1.0
    396:      */
    397:     public function testGetCount() {
    398:         $count = $this->person->getCount();
    399:         $this->person->save();
    400:         $this->assertEquals($count+1, $this->person->getCount(), 'Testing the getCount method');
    401:     }
    402:
    403:     /**
    404:      * Testing the setEnumOptions method is loading enum options correctly
    405:      *
    406:      * @since 1.0
    407:      */
    408:     public function testSetEnumOptions() {
    409:         $this->person->save();
    410:         $id = $this->person->getMAX();
    411:         $this->person->load($id);
    412:         $this->assertTrue(in_array('Active', $this->person->getPropObject('state')->getOptions()),
    413:             'Testing the setEnumOptions method is loading enum options correctly');
    414:     }
    415:
    416:     /**
    417:      * Testing that checkTableExists returns true for the person BO
    418:      *
    419:      * @since 1.0
    420:      */
    421:     public function testCheckTableExists() {
    422:         $this->assertTrue($this->person->checkTableExists(), 'Testing that checkTableExists returns true for the person BO');
    423:     }
    424:
    425:     /**
    426:      * Testing that checkTableNeedsUpdate returns false for the person BO
    427:      *
    428:      * @since 1.0
    429:      */
    430:     public function testCheckTableNeedsUpdate() {
    431:         $this->assertFalse($this->person->checkTableNeedsUpdate(), 'Testing that checkTableNeedsUpdate returns false for the person BO');
    432:     }
    433:
    434:     /**
    435:      * Testing to ensure that the getTableName method can read the TABLE_NAME constant declared in the child class
    436:      *
    437:      * @since 1.0
    438:      */
    439:     public function testGetTableName() {
    440:         $this->assertEquals('Person', $this->person->getTableName(),
    441:             'Testing to ensure that the getTableName method can read the TABLE_NAME constant declared in the child class');
    442:     }
    443:
    444:     /**
    445:      * Testing the getDataLabel method
    446:      *
    447:      * @since 1.0
    448:      */
    449:     public function testGetDataLabel() {
    450:         $this->assertEquals('E-mail Address', $this->person->getDataLabel('email'), 'Testing the getDataLabel method');
    451:     }
    452:
    453:     /**
    454:      * Testing get on a String attribute with no child get method available
    455:      *
    456:      * @since 1.0
    457:      */
    458:     public function testGetNoChildMethod() {
    459:         $email = $this->person->get('email');
    460:
    461:         $this->assertEquals('unitTestUser@test.com', $email, 'Testing get on a String attribute with no child get method available');
    462:     }
    463:
    464:     /**
    465:      * Testing get on an Enum attribute with a child method available, with $noChildMethods disabled (default)
    466:      *
    467:      * @since 1.0
    468:      */
    469:     public function testGetNoChildMethodsDisabled() {
    470:         $state = $this->person->getPropObject('state');
    471:
    472:         $this->assertEquals('Enum', get_class($state),
    473:             'Testing get on an Enum attribute with a child method avaialble, with $noChildMethods disabled (default)');
    474:         $this->assertEquals('Active', $state->getValue(),
    475:             'Testing get on an Enum attribute with a child method avaialble, with $noChildMethods disabled (default)');
    476:     }
    477:
    478:     /**
    479:      * Testing get on an Enum attribute with a child method available, with $noChildMethods enabled
    480:      *
    481:      * @since 1.0
    482:      */
    483:     public function testGetNoChildMethodsEnabled() {
    484:         $state = $this->person->get('state', true);
    485:
    486:         $this->assertEquals('Active', $state, 'Testing get on an Enum attribute with a child method avaialble, with $noChildMethods enabled');
    487:     }
    488:
    489:     /**
    490:      * Testing get on a simple data type
    491:      *
    492:      * @since 1.0
    493:      */
    494:     public function testGetSimpleType() {
    495:         $labels = $this->person->get('dataLabels');
    496:
    497:         $this->assertTrue(is_array($labels), 'Testing get on a simple data type');
    498:     }
    499:
    500:     /**
    501:      * Testing set on a String attribute with no child get method available
    502:      *
    503:      * @since 1.0
    504:      */
    505:     public function testSetNoChildMethod() {
    506:         $this->person->set('email','test@test.com');
    507:
    508:         $this->assertEquals('test@test.com', $this->person->get('email'), 'Testing set on a String attribute with no child get method available');
    509:     }
    510:
    511:     /**
    512:      * Testing set on an Enum attribute with a child method available, with $noChildMethods disabled (default)
    513:      *
    514:      * @since 1.0
    515:      */
    516:     public function testSetNoChildMethodsDisabled() {
    517:         $this->person->set('state','Active');
    518:
    519:         $this->assertEquals('Active', $this->person->get('state'),
    520:             'Testing set on an Enum attribute with a child method avaialble, with $noChildMethods disabled (default)');
    521:     }
    522:
    523:     /**
    524:      * Testing set on an Enum attribute with a child method available, with $noChildMethods enabled
    525:      *
    526:      * @since 1.0
    527:      */
    528:     public function testSetNoChildMethodsEnabled() {
    529:         $this->person->set('state','Active', true);
    530:
    531:         $this->assertEquals('Active', $this->person->get('state'),
    532:             'Testing set on an Enum attribute with a child method avaialble, with $noChildMethods enabled');
    533:     }
    534:
    535:     /**
    536:      * Testing set on a simple data type
    537:      *
    538:      * @since 1.0
    539:      */
    540:     public function testSetSimpleType() {
    541:         $this->person->set('dataLabels', array('key'=>'value'));
    542:
    543:         $labels = $this->person->get('dataLabels');
    544:
    545:         $this->assertTrue(is_array($labels), 'Testing set on a simple data type');
    546:         $this->assertEquals('value', $labels['key'], 'Testing set on a simple data type');
    547:     }
    548:
    549:     /**
    550:      * Testing getPropObject on a complex type
    551:      *
    552:      * @since 1.0
    553:      */
    554:     public function testGetPropObjectComplexType() {
    555:         $state = $this->person->getPropObject('state');
    556:
    557:         $this->assertEquals('Enum', get_class($state), 'Testing getPropObject on a complex type');
    558:         $this->assertEquals('Active', $state->getValue(), 'Testing getPropObject on a complex type');
    559:     }
    560:
    561:     /**
    562:      * Testing getPropObject on a simple type
    563:      *
    564:      * @since 1.0
    565:      */
    566:     public function testGetPropObjectSimpleType() {
    567:         $labels = $this->person->getPropObject('dataLabels');
    568:
    569:         $this->assertTrue(is_array($labels), 'Testing getPropObject on a simple type');
    570:         $this->assertEquals('E-mail Address', $labels['email'], 'Testing getPropObject on a simple type');
    571:     }
    572:
    573:     /**
    574:      * Testing that markTransient and markPersistent methods
    575:      *
    576:      * @since 1.0
    577:      */
    578:     public function testMarkTransientPersistent() {
    579:         // initial save
    580:         $this->person->save();
    581:
    582:         // now mark the URL transient, and save again (old URL value should not be overwritten)
    583:         $this->person->markTransient('URL');
    584:         $this->assertTrue(in_array('URL', $this->person->getTransientAttributes()), 'Testing that markTransient and markPersistent methods');
    585:         $this->person->set('URL','http://www.alphaframework.org/');
    586:         $this->person->save();
    587:
    588:         // used to ensure that we attempt to reload it from the DB
    589:         $this->person->markPersistent('URL');
    590:         $this->assertFalse(in_array('URL', $this->person->getTransientAttributes()), 'Testing that markTransient and markPersistent methods');
    591:         // reload from DB
    592:         $this->person->reload();
    593:
    594:         $this->assertEquals('http://unitTestUser/', $this->person->get('URL'), 'Testing that markTransient and markPersistent methods');
    595:     }
    596:
    597:     /**
    598:      * Testing the getDataLabels method
    599:      *
    600:      * @since 1.0
    601:      */
    602:     public function testGetDataLabels() {
    603:         $this->assertTrue(is_array($this->person->getDataLabels()), 'Testing the getDataLabels method');
    604:         $labels = $this->person->getDataLabels();
    605:         $this->assertTrue(in_array('OID', array_keys($labels)), 'Testing the getDataLabels method');
    606:         $this->assertTrue(in_array('E-mail Address', $labels), 'Testing the getDataLabels method');
    607:     }
    608:
    609:     /**
    610:      * Testing the getTransientAttributes method in conjunction with markTransient/markPersistent
    611:      *
    612:      * @since 1.0
    613:      */
    614:     public function testGetTransientAttributes() {
    615:         $this->assertTrue(is_array($this->person->getTransientAttributes()),
    616:             'Testing the getTransientAttributes method in conjunction with markTransient/markPersistent');
    617:         $this->person->markTransient('URL');
    618:         $this->assertTrue(in_array('URL', $this->person->getTransientAttributes()),
    619:             'Testing the getTransientAttributes method in conjunction with markTransient/markPersistent');
    620:         $this->person->markPersistent('URL');
    621:         $this->assertFalse(in_array('URL', $this->person->getTransientAttributes()),
    622:             'Testing the getTransientAttributes method in conjunction with markTransient/markPersistent');
    623:     }
    624:
    625:     /**
    626:      * Testing isTransient before and after save
    627:      *
    628:      * @since 1.0
    629:      */
    630:     public function testIsTransient() {
    631:         $this->assertTrue($this->person->isTransient(), 'Testing isTransient before and after save');
    632:         $this->person->save();
    633:         $this->assertFalse($this->person->isTransient(), 'Testing isTransient before and after save');
    634:     }
    635:
    636:     /**
    637:      * Testing the getLastQuery method after various persistance calls
    638:      *
    639:      * @since 1.0
    640:      */
    641:     public function testGetLastQuery() {
    642:
    643:         global $config;
    644:
    645:         $this->person->save();
    646:
    647:         if($config->get('db.provider.name') == 'AlphaDAOProviderMySQL') {
    648:             $this->assertEquals('INSERT INTO Person', substr($this->person->getLastQuery(), 0, 18),
    649:                 'Testing the getLastQuery method after various persistance calls');
    650:             $this->person->checkTableNeedsUpdate();
    651:             $this->assertEquals('SHOW INDEX FROM Person', substr($this->person->getLastQuery(), 0, 22),
    652:                 'Testing the getLastQuery method after various persistance calls');
    653:             $this->person->getCount();
    654:             $this->assertEquals('SELECT COUNT(OID)', substr($this->person->getLastQuery(), 0, 17),
    655:                 'Testing the getLastQuery method after various persistance calls');
    656:             $this->person->getMAX();
    657:             $this->assertEquals('SELECT MAX(OID)', substr($this->person->getLastQuery(), 0, 15),
    658:                 'Testing the getLastQuery method after various persistance calls');
    659:             $this->person->load($this->person->getID());
    660:             $this->assertEquals('SHOW COLUMNS FROM Person', substr($this->person->getLastQuery(), 0, 24),
    661:                 'Testing the getLastQuery method after various persistance calls');
    662:         }
    663:
    664:         if($config->get('db.provider.name') == 'AlphaDAOProviderSQLite') {
    665:             $this->assertEquals('PRAGMA table_info(Person)', substr($this->person->getLastQuery(), 0, 25),
    666:                     'Testing the getLastQuery method after various persistance calls');
    667:             $this->person->checkTableNeedsUpdate();
    668:             $this->assertEquals('SELECT name FROM sqlite_master WHERE type=\'index\'', substr($this->person->getLastQuery(), 0, 49),
    669:                     'Testing the getLastQuery method after various persistance calls');
    670:             $this->person->getCount();
    671:             $this->assertEquals('SELECT COUNT(OID)', substr($this->person->getLastQuery(), 0, 17),
    672:                     'Testing the getLastQuery method after various persistance calls');
    673:             $this->person->getMAX();
    674:             $this->assertEquals('SELECT MAX(OID)', substr($this->person->getLastQuery(), 0, 15),
    675:                     'Testing the getLastQuery method after various persistance calls');
    676:             $this->person->load($this->person->getID());
    677:             $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;',
    678:                     substr($this->person->getLastQuery(), 0, 150),
    679:                     'Testing the getLastQuery method after various persistance calls');
    680:         }
    681:     }
    682:
    683:     /**
    684:      * Testing the clear method for unsetting the attributes of an object
    685:      *
    686:      * @since 1.0
    687:      */
    688:     public function testClear() {
    689:         $state = $this->person->get('state');
    690:         $this->assertTrue(!empty($state), 'Testing the clear method for unsetting the attributes of an object');
    691:
    692:         $reflection = new ReflectionClass(get_class($this->person));
    693:         $properties = $reflection->getProperties();
    694:
    695:         foreach($properties as $propObj) {
    696:             $propName = $propObj->name;
    697:             if(!in_array($propName, $this->person->getDefaultAttributes()) && !in_array($propName, $this->person->getTransientAttributes())) {
    698:                 $this->assertNotNull($this->person->get($propName), 'Testing the clear method for unsetting the attributes of an object');
    699:             }
    700:         }
    701:
    702:         // delete will invoke clear(), which is private
    703:         $this->person->delete();
    704:
    705:         try {
    706:             $state = $this->person->get('state');
    707:             $this->fail('Testing the clear method for unsetting the attributes of an object');
    708:         } catch (AlphaException $e) {
    709:             $reflection = new ReflectionClass(get_class($this->person));
    710:             $properties = $reflection->getProperties();
    711:
    712:             foreach($properties as $propObj) {
    713:                 $propName = $propObj->name;
    714:
    715:                 try {
    716:                     $this->person->get($propName);
    717:                 } catch (PHPException $e) {
    718:                     $this->assertEquals(preg_match("/Undefined property/", $e->getMessage()), 1,
    719:                         'Testing the clear method for unsetting the attributes of an object');
    720:                 } catch (AlphaException $e) {
    721:                     $this->assertEquals('Could not access the property ['.$propName.'] on the object of class [PersonObject]', $e->getMessage(),
    722:                         'Testing the clear method for unsetting the attributes of an object');
    723:                 }
    724:             }
    725:         }
    726:     }
    727:
    728:     /**
    729:      * Testing the saveAttribute method
    730:      *
    731:      * @since 1.0
    732:      */
    733:     public function testSaveAttribute() {
    734:         $this->person->save();
    735:         $this->person->saveAttribute('displayName', 'unitTestUserNew');
    736:
    737:         $this->assertEquals('unitTestUserNew', $this->person->getDisplayName()->getValue(),
    738:             'Testing that the value was set on the object in memory along with saving to the database');
    739:
    740:         $person = new PersonObject();
    741:
    742:         try {
    743:             $person->loadByAttribute('displayName', 'unitTestUserNew');
    744:             $this->assertEquals('unitTestUserNew', $person->getDisplayName()->getValue(), 'Testing that the value was saved to the database');
    745:         } catch (BONotFoundException $e) {
    746:             $this->fail('Failed to load the BO that was updated with the saveAttribute method');
    747:         }
    748:     }
    749:
    750:     /**
    751:      * Testing to ensure that a history table was created automatically
    752:      *
    753:      * @since 1.2.1
    754:      */
    755:     public function testHistoryTableCreated() {
    756:         $this->person->setMaintainHistory(true);
    757:         $this->person->rebuildTable(); // this should result in the _history table being created
    758:
    759:         $this->assertTrue($this->person->checkTableExists(true), 'Testing to ensure that a history table was created automatically');
    760:
    761:         $this->person->dropTable('Person_history');
    762:     }
    763:
    764:     /**
    765:      * Testing that the saveHistory() method is automatically invoked when it should be
    766:      *
    767:      * @since 1.2.1
    768:      */
    769:     public function testSaveHistory() {
    770:         $this->person->setMaintainHistory(true);
    771:         $this->person->rebuildTable(); // this should result in the _history table being created
    772:
    773:         $this->person->set('password', 'passwordhist1');
    774:         $this->person->save();
    775:
    776:         $this->assertEquals(1, $this->person->getHistoryCount(), 'Testing that a normal save is propegated to the history table for this class');
    777:         $this->person->setMaintainHistory(true);
    778:         $this->person->saveAttribute('password', 'passwordhist2');
    779:
    780:         $this->assertEquals(2, $this->person->getHistoryCount(), 'Testing that an attribute save is propegated to the history table for this class');
    781:
    782:         $this->person->dropTable('Person_history');
    783:     }
    784:
    785:     /**
    786:      * Testing the hasAttribute method
    787:      *
    788:      * @since 1.2.1
    789:      */
    790:     public function testHasAttribute() {
    791:         $this->assertTrue($this->person->hasAttribute('password'), 'testing the hasAttribute method for true');
    792:         $this->assertFalse($this->person->hasAttribute('doesnotexist'), 'testing the hasAttribute method for false');
    793:     }
    794:
    795:     /**
    796:      * Testing that a saved record is subsequently retrievable from the cache
    797:      *
    798:      * @since 1.2.1
    799:      */
    800:     public function testLoadFromCache() {
    801:
    802:         global $config;
    803:         $oldSetting = $config->get('cache.provider.name');
    804:         $config->set('cache.provider.name', 'AlphaCacheProviderArray');
    805:
    806:         $this->person->save();
    807:
    808:         $fromCache = new PersonObject();
    809:         $fromCache->setOID($this->person->getOID());
    810:
    811:         $this->assertTrue($fromCache->loadFromCache(), 'testing that the item loads from the cache');
    812:         $this->assertEquals('unitTestUser', $fromCache->get('displayName', true), 'testing that a saved record is subsequently retrievable from the cache');
    813:
    814:         $config->set('cache.provider.name', $oldSetting);
    815:     }
    816:
    817:     /**
    818:      * Testing the removeFromCache method
    819:      *
    820:      * @since 1.2.1
    821:      */
    822:     public function testRemoveFromCache() {
    823:
    824:         global $config;
    825:         $oldSetting = $config->get('cache.provider.name');
    826:         $config->set('cache.provider.name', 'AlphaCacheProviderArray');
    827:
    828:         $this->person->save();
    829:
    830:         $fromCache = new PersonObject();
    831:         $fromCache->setOID($this->person->getOID());
    832:
    833:         $this->assertTrue($fromCache->loadFromCache(), 'testing that the item loads from the cache');
    834:
    835:         $fromCache->removeFromCache();
    836:
    837:         $this->assertFalse($fromCache->loadFromCache(), 'testing that the item is gone from the cache');
    838:
    839:         $config->set('cache.provider.name', $oldSetting);
    840:     }
    841:
    842:     /**
    843:      * Testing the getFriendlyClassName() method
    844:      *
    845:      * @since 1.2.1
    846:      */
    847:     public function testGetFriendlyClassName() {
    848:         $person = new PersonObject();
    849:         $article = new ArticleObject();
    850:         $comment = new ArticleCommentObject();
    851:
    852:         $this->assertEquals('Person', $person->getFriendlyClassName(), 'testing the getFriendlyClassName() method');
    853:         $this->assertEquals('Article', $article->getFriendlyClassName(), 'testing the getFriendlyClassName() method');
    854:         $this->assertEquals('ArticleComment', $comment->getFriendlyClassName(), 'testing the getFriendlyClassName() method');
    855:     }
    856:
    857:     /**
    858:      * Testing the cast() method
    859:      *
    860:      * @since 1.2.1
    861:      */
    862:     public function testCast() {
    863:         $original = new BadRequestObject();
    864:         $original->set('IP', '127.0.0.1');
    865:         $copy = $original->cast('BlacklistedIPObject', $original);
    866:
    867:         $this->assertTrue($copy instanceof BlacklistedIPObject, 'testing the cast() method');
    868:         $this->assertTrue($copy->hasAttribute('IP'), 'testing the cast() method');
    869:         $this->assertEquals($original->get('IP'), $copy->get('IP'), 'testing the cast() method');
    870:     }
    871: }
    872:
    873: ?>