Source for file AlphaDAO_Test.php
Documentation is available at AlphaDAO_Test.php
* Test case for the AlphaDAO class
* @author John Collins <dev@alphaframework.org>
* @version $Id: AlphaDAO_Test.php 1341 2011-03-17 15:02:02Z johnc $
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* A PersonObject for Testing (any business object will do)
* Called before the test functions will be executed
* this function is defined in PHPUnit_TestCase and overwritten
protected function setUp() {
$this->person = $this->createPersonObject('unitTestUser');
$this->person->rebuildTable();
// just making sure no previous test user is in the DB
$this->person->deleteAllByAttribute('URL', 'http://unitTestUser/');
$this->person->deleteAllByAttribute('displayName', 'unitTestUser');
* Called after the test functions are executed
* this function is defined in PHPUnit_TestCase and overwritten
$this->person->dropTable();
$rights->dropTable('Person2Rights');
* Creates a person object for Testing
private function createPersonObject($name) {
$person->setDisplayname($name);
$person->set('email', $name. '@test.com');
$person->set('password', 'passwordTest');
$person->set('URL', 'http://unitTestUser/');
* Test that the constructor sets the correct values of the "house keeping" attributes
// make sure the person logged in is the same person to create/update the object
$this->assertEquals($_SESSION['currentUser']->getID(), $this->person->getCreatorId()->getValue(),
'test that the constructor sets the correct values of the "house keeping" attributes');
$this->assertEquals($_SESSION['currentUser']->getID(), $this->person->getUpdatorId()->getValue(),
'test that the constructor sets the correct values of the "house keeping" attributes');
// as it is a new object, make sure the version number is zero
$this->assertEquals(0, $this->person->getVersionNumber()->getValue(),
'test that the constructor sets the correct values of the "house keeping" attributes');
// check that the date created and updated equal to today
$this->assertEquals($today, $this->person->getCreateTS()->getDate(),
'test that the constructor sets the correct values of the "house keeping" attributes');
$this->assertEquals($today, $this->person->getUpdateTS()->getDate(),
'test that the constructor sets the correct values of the "house keeping" attributes');
// make sure the object is transient
$this->assertTrue($this->person->isTransient(), 'test that the constructor sets the correct values of the "house keeping" attributes');
* Testing the basic load/save functionality
$id = $this->person->getMAX();
$this->person->load($id);
$this->assertEquals('unitTestUser', $this->person->getDisplayname()->getValue(), 'Testing the basic load/save functionality');
* Testing the checkRecordExists method
$this->assertTrue($person->checkRecordExists($this->person->getOID()), 'Testing the checkRecordExists method');
* Testing the loadByAttribute method
$this->person->loadByAttribute('displayName','unitTestUser');
$this->assertEquals('unitTestUser@test.com', $this->person->get('email'), 'Testing the loadByAttribute method');
$this->person->loadByAttribute('email','unitTestUser@test.com');
$this->assertEquals('unitTestUser', $this->person->getDisplayname()->getValue(), 'Testing the loadByAttribute method');
$peopleCount = $this->person->getCount();
$people = $this->person->loadAll();
$this->assertEquals($peopleCount, count($people), 'Testing loadAll method');
$people = $this->person->loadAll(0, 1);
$this->assertEquals(1, count($people), 'Testing loadAll method');
* Testing the loadAllByAttribute method
$people = $this->person->loadAllByAttribute('email','unitTestUser@test.com');
$this->assertEquals(1, count($people), 'Testing the loadAllByAttribute method');
$this->assertEquals('unitTestUser', $people[0]->getDisplayname()->getValue(), 'Testing the loadAllByAttribute method');
* Testing the loadAllByAttributes method
$people = $this->person->loadAllByAttributes(array('OID'),array($this->person->getOID()));
$this->assertEquals(1, count($people), 'Testing the loadAllByAttribute method');
$this->assertEquals('unitTestUser', $people[0]->getDisplayname()->getValue(), 'Testing the loadAllByAttributes method');
* Testing the loadAllByDayUpdated method
$people = $this->person->loadAllByDayUpdated(date('Y-m-d'));
$this->assertGreaterThan(0, count($people), 'Testing the loadAllByDayUpdated method');
* Testing the loadAllFieldValuesByAttribute method
$emails = $this->person->loadAllFieldValuesByAttribute('email', $this->person->get('email'), 'email');
$this->assertEquals($this->person->get('email'), $emails[0], 'Testing the loadAllFieldValuesByAttribute method');
* Testing the save method on transient and non-transient objects
// its transient, so query will insert
$this->assertEquals('INSERT', substr($this->person->getLastQuery(), 0, 6),
'Testing the save method on transient and non-transient objects');
// its now persistent, so query will update
$this->assertEquals('UPDATE', substr($this->person->getLastQuery(), 0, 6),
'Testing the save method on transient and non-transient objects');
* Testing to ensure that a transient object, once saved, will have an OID
$this->assertTrue($this->person->isTransient(), 'Testing to ensure that a transient object, once saved, will have an OID');
$this->assertGreaterThan(0, $this->person->getID(), 'Testing to ensure that a transient object, once saved, will have an OID');
$this->assertFalse($this->person->isTransient(), 'Testing to ensure that a transient object, once saved, will have an OID');
* Testing optimistic locking mechanism#
$personInstance1->load($this->person->getID());
$personInstance2->load($this->person->getID());
$personInstance1->save();
$personInstance2->save();
$this->fail('Testing optimistic locking mechanism');
$this->assertEquals('Could not save the object as it has been updated by another user. Please try saving again.',
'Testing optimistic locking mechanism');
* Testing the validation method
$this->fail('Testing the validation method');
$this->assertEquals('Failed to save, validation error is:',
substr($e->getMessage(), 0, 36),
'Testing the validation method');
* Testing the delete method
$this->assertFalse($this->person->isTransient(), 'Testing the delete method');
$id = $this->person->getID();
// gone from memory (all attributes null)
// gone from the database
$this->person->load($id);
$this->fail('Testing the delete method');
$this->assertEquals('Failed to load object',
substr($e->getMessage(), 0, 21),
'Testing the delete method');
* Testing the deleteAllByAttribute method
$person1->setDisplayname('unitTestUser1');
$person1->set('email', 'unitTestUser1@test.com');
$person1->set('password', 'passwordTest');
$person1->set('URL', 'http://unitTestUser/');
$person2->setDisplayname('unitTestUser2');
$person2->set('email', 'unitTestUser2@test.com');
$person2->set('password', 'passwordTest');
$person2->set('URL', 'http://unitTestUser/');
$person3->setDisplayname('unitTestUser3');
$person3->set('email', 'unitTestUser3@test.com');
$person3->set('password', 'passwordTest');
$person3->set('URL', 'http://unitTestUser/');
$this->assertEquals(3, $this->person->deleteAllByAttribute('URL', 'http://unitTestUser/'), 'Testing the deleteAllByAttribute method');
* Testing the version numbers of business objects
$this->assertEquals(0, $this->person->getVersion(), 'Testing the version numbers of business objects');
$this->assertEquals(0, $this->person->getVersionNumber()->getValue(), 'Testing the version numbers of business objects');
$this->assertEquals(1, $this->person->getVersion(), 'Testing the version numbers of business objects');
$this->assertEquals(1, $this->person->getVersionNumber()->getValue(), 'Testing the version numbers of business objects');
$this->assertEquals(2, $this->person->getVersion(), 'Testing the version numbers of business objects');
$this->assertEquals(2, $this->person->getVersionNumber()->getValue(), 'Testing the version numbers of business objects');
* Testing the getMAX method
$max = $this->person->getMAX();
$person2 = $this->createPersonObject('unitTestUser2');
$this->assertEquals($max+ 1, $this->person->getMAX(), 'Testing the getMAX method');
* Testing the getCount method
$count = $this->person->getCount();
$this->assertEquals($count+ 1, $this->person->getCount(), 'Testing the getCount method');
* Testing the setEnumOptions method is loading enum options correctly
$id = $this->person->getMAX();
$this->person->load($id);
$this->assertTrue(in_array('Active', $this->person->getPropObject('state')->getOptions()),
'Testing the setEnumOptions method is loading enum options correctly');
* Testing that checkTableExists returns true for the person BO
$this->assertTrue($this->person->checkTableExists(), 'Testing that checkTableExists returns true for the person BO');
* Testing that checkTableNeedsUpdate returns false for the person BO
$this->assertFalse($this->person->checkTableNeedsUpdate(), 'Testing that checkTableNeedsUpdate returns false for the person BO');
* Testing to ensure that the getTableName method can read the TABLE_NAME constant declared in the child class
$this->assertEquals('Person', $this->person->getTableName(),
'Testing to ensure that the getTableName method can read the TABLE_NAME constant declared in the child class');
* Testing the getDataLabel method
$this->assertEquals('E-mail Address', $this->person->getDataLabel('email'), 'Testing the getDataLabel method');
* Testing get on a String attribute with no child get method available
$email = $this->person->get('email');
$this->assertEquals('unitTestUser@test.com', $email, 'Testing get on a String attribute with no child get method available');
* Testing get on an Enum attribute with a child method available, with $noChildMethods disabled (default)
$state = $this->person->getPropObject('state');
$this->assertEquals('Enum', get_class($state),
'Testing get on an Enum attribute with a child method avaialble, with $noChildMethods disabled (default)');
$this->assertEquals('Active', $state->getValue(),
'Testing get on an Enum attribute with a child method avaialble, with $noChildMethods disabled (default)');
* Testing get on an Enum attribute with a child method available, with $noChildMethods enabled
$state = $this->person->get('state', true);
$this->assertEquals('Active', $state, 'Testing get on an Enum attribute with a child method avaialble, with $noChildMethods enabled');
* Testing get on a simple data type
$labels = $this->person->get('dataLabels');
$this->assertTrue(is_array($labels), 'Testing get on a simple data type');
* Testing set on a String attribute with no child get method available
$this->person->set('email','test@test.com');
$this->assertEquals('test@test.com', $this->person->get('email'), 'Testing set on a String attribute with no child get method available');
* Testing set on an Enum attribute with a child method available, with $noChildMethods disabled (default)
$this->person->set('state','Active');
$this->assertEquals('Active', $this->person->get('state'),
'Testing set on an Enum attribute with a child method avaialble, with $noChildMethods disabled (default)');
* Testing set on an Enum attribute with a child method available, with $noChildMethods enabled
$this->person->set('state','Active', true);
$this->assertEquals('Active', $this->person->get('state'),
'Testing set on an Enum attribute with a child method avaialble, with $noChildMethods enabled');
* Testing set on a simple data type
$this->person->set('dataLabels', array('key'=> 'value'));
$labels = $this->person->get('dataLabels');
$this->assertTrue(is_array($labels), 'Testing set on a simple data type');
$this->assertEquals('value', $labels['key'], 'Testing set on a simple data type');
* Testing getPropObject on a complex type
$state = $this->person->getPropObject('state');
$this->assertEquals('Enum', get_class($state), 'Testing getPropObject on a complex type');
$this->assertEquals('Active', $state->getValue(), 'Testing getPropObject on a complex type');
* Testing getPropObject on a simple type
$labels = $this->person->getPropObject('dataLabels');
$this->assertTrue(is_array($labels), 'Testing getPropObject on a simple type');
$this->assertEquals('E-mail Address', $labels['email'], 'Testing getPropObject on a simple type');
* Testing that markTransient and markPersistent methods
// now mark the URL transient, and save again (old URL value should not be overwritten)
$this->person->markTransient('URL');
$this->assertTrue(in_array('URL', $this->person->getTransientAttributes()), 'Testing that markTransient and markPersistent methods');
$this->person->set('URL','http://www.alphaframework.org/');
// used to ensure that we attempt to reload it from the DB
$this->person->markPersistent('URL');
$this->assertFalse(in_array('URL', $this->person->getTransientAttributes()), 'Testing that markTransient and markPersistent methods');
$this->assertEquals('http://unitTestUser/', $this->person->get('URL'), 'Testing that markTransient and markPersistent methods');
* Testing the getDataLabels method
$this->assertTrue(is_array($this->person->getDataLabels()), 'Testing the getDataLabels method');
$labels = $this->person->getDataLabels();
$this->assertTrue(in_array('OID', array_keys($labels)), 'Testing the getDataLabels method');
$this->assertTrue(in_array('E-mail Address', $labels), 'Testing the getDataLabels method');
* Testing the getTransientAttributes method in conjunction with markTransient/markPersistent
$this->assertTrue(is_array($this->person->getTransientAttributes()),
'Testing the getTransientAttributes method in conjunction with markTransient/markPersistent');
$this->person->markTransient('URL');
$this->assertTrue(in_array('URL', $this->person->getTransientAttributes()),
'Testing the getTransientAttributes method in conjunction with markTransient/markPersistent');
$this->person->markPersistent('URL');
$this->assertFalse(in_array('URL', $this->person->getTransientAttributes()),
'Testing the getTransientAttributes method in conjunction with markTransient/markPersistent');
* Testing isTransient before and after save
$this->assertTrue($this->person->isTransient(), 'Testing isTransient before and after save');
$this->assertFalse($this->person->isTransient(), 'Testing isTransient before and after save');
* Testing the getLastQuery method after various persistance calls
$this->assertEquals('INSERT INTO Person', substr($this->person->getLastQuery(), 0, 18),
'Testing the getLastQuery method after various persistance calls');
$this->person->checkTableNeedsUpdate();
$this->assertEquals('SHOW INDEX FROM Person', substr($this->person->getLastQuery(), 0, 22),
'Testing the getLastQuery method after various persistance calls');
$this->person->getCount();
$this->assertEquals('SELECT COUNT(OID)', substr($this->person->getLastQuery(), 0, 17),
'Testing the getLastQuery method after various persistance calls');
$this->assertEquals('SELECT MAX(OID)', substr($this->person->getLastQuery(), 0, 15),
'Testing the getLastQuery method after various persistance calls');
$this->person->load($this->person->getID());
$this->assertEquals('SHOW COLUMNS FROM Person', substr($this->person->getLastQuery(), 0, 24),
'Testing the getLastQuery method after various persistance calls');
* Testing the clear method for unsetting the attributes of an object
$state = $this->person->get('state');
$this->assertTrue(!empty($state), 'Testing the clear method for unsetting the attributes of an object');
$reflection = new ReflectionClass(get_class($this->person));
$properties = $reflection->getProperties();
foreach($properties as $propObj) {
$propName = $propObj->name;
if(!in_array($propName, $this->person->getDefaultAttributes()) && !in_array($propName, $this->person->getTransientAttributes())) {
$this->assertNotNull($this->person->get($propName), 'Testing the clear method for unsetting the attributes of an object');
// delete will invoke clear(), which is private
$state = $this->person->get('state');
$this->fail('Testing the clear method for unsetting the attributes of an object');
$reflection = new ReflectionClass(get_class($this->person));
$properties = $reflection->getProperties();
foreach($properties as $propObj) {
$propName = $propObj->name;
$this->person->get($propName);
$this->assertEquals(preg_match("/Undefined property/", $e->getMessage()), 1,
'Testing the clear method for unsetting the attributes of an object');
$this->assertEquals('Could not access the property ['. $propName. '] on the object of class [PersonObject]', $e->getMessage(),
'Testing the clear method for unsetting the attributes of an object');
* Testing the saveAttribute method
$this->person->saveAttribute('displayName', 'unitTestUserNew');
$this->assertEquals('unitTestUserNew', $this->person->getDisplayName()->getValue(),
'Testing that the value was set on the object in memory along with saving to the database');
$person->loadByAttribute('displayName', 'unitTestUserNew');
$this->assertEquals('unitTestUserNew', $person->getDisplayName()->getValue(), 'Testing that the value was saved to the database');
$this->fail('Failed to load the BO that was updated with the saveAttribute method');
|