Alpha Framework alpha--tests
[ class tree: alpha--tests ] [ index: alpha--tests ] [ all elements ]

Source for file AlphaDAO_Test.php

Documentation is available at AlphaDAO_Test.php

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

Documentation generated on Thu, 17 Mar 2011 16:43:28 +0000 by phpDocumentor 1.4.3