      1: <?php
      2:
      3: /**
      4:  *
      5:  * Test case for the Relation data type
      6:  *
      7:  * @package alpha::tests
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: Relation_Test.php 1560 2012-08-02 21:41:29Z 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 Relation_Test extends PHPUnit_Framework_TestCase {
     49:     /**
     50:      * A Relation for testing
     51:      *
     52:      * @var Relation
     53:      * @since 1.0
     54:      */
     55:     private $rel1;
     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:         $this->rel1 = new Relation();
     66:
     67:         $rights = new RightsObject();
     68:         $rights->rebuildTable();
     69:
     70:         $person = new PersonObject();
     71:         $person->set('displayName', $_SESSION['currentUser']->getDisplayName());
     72:         $person->set('email', $_SESSION['currentUser']->get('email'));
     73:         $person->set('password', 'password');
     74:         $person->rebuildTable();
     75:         $person->save();
     76:     }
     77:
     78:     /**
     79:      * Called after the test functions are executed
     80:      * this function is defined in PHPUnit_TestCase and overwritten
     81:      * here
     82:      *
     83:      * @since 1.0
     84:      */
     85:     protected function tearDown() {
     86:         unset($this->rel1);
     87:         $person = new PersonObject();
     88:         $person->dropTable();
     89:
     90:         $rights = new RightsObject();
     91:         $rights->dropTable();
     92:         $rights->dropTable('Person2Rights');
     93:     }
     94:
     95:     /**
     96:      * Testing passing a valid BO name to setRelatedClass
     97:      *
     98:      * @since 1.0
     99:      */
    100:     public function testSetRelatedClassPass() {
    101:         try {
    102:             $this->rel1->setRelatedClass('ArticleObject');
    103:         }catch (AlphaException $e) {
    104:             $this->fail('Testing passing a valid BO name to setRelatedClass');
    105:         }
    106:     }
    107:
    108:     /**
    109:      * Testing passing an invalid BO name to setRelatedClass
    110:      *
    111:      * @since 1.0
    112:      */
    113:     public function testSetRelatedClassFail() {
    114:         try {
    115:             $this->rel1->setRelatedClass('XyzObject');
    116:             $this->fail('Testing passing an invalid BO name to setRelatedClass');
    117:         }catch (AlphaException $e) {
    118:             $this->assertEquals('The class [XyzObject] is not defined anywhere!'
    119:                 , $e->getMessage()
    120:                 , 'Testing passing an invalid BO name to setRelatedClass');
    121:         }
    122:     }
    123:
    124:     /**
    125:      * Testing passing a valid field name to setRelatedClassField
    126:      *
    127:      * @since 1.0
    128:      */
    129:     public function testSetRelatedClassFieldPass() {
    130:         try {
    131:             $this->rel1->setRelatedClass('PersonObject');
    132:             $this->rel1->setRelatedClassField('email');
    133:         }catch (AlphaException $e) {
    134:             $this->fail('Testing passing a valid field name to setRelatedClassField');
    135:         }
    136:     }
    137:
    138:     /**
    139:      * Testing passing an invalid field name to setRelatedClassField
    140:      *
    141:      * @since 1.0
    142:      */
    143:     public function testSetRelatedClassFieldFail() {
    144:         try {
    145:             $this->rel1->setRelatedClass('PersonObject');
    146:             $this->rel1->setRelatedClassField('doesNotExist');
    147:             $this->fail('Testing passing an invalid field name to setRelatedClassField');
    148:         }catch (AlphaException $e) {
    149:             $this->assertEquals('The field [doesNotExist] was not found in the class [PersonObject]'
    150:                 , $e->getMessage()
    151:                 , 'Testing passing an invalid field name to setRelatedClassField');
    152:         }
    153:     }
    154:
    155:     /**
    156:      * Testing passing a valid type name to setRelationType
    157:      *
    158:      * @since 1.0
    159:      */
    160:     public function testSetRelationTypePass() {
    161:         try {
    162:             $this->rel1->setRelationType('MANY-TO-ONE');
    163:         }catch (AlphaException $e) {
    164:             $this->fail('Testing passing a valid type name to setRelationType');
    165:         }
    166:     }
    167:
    168:     /**
    169:      * Testing passing an invalid type name to setRelationType
    170:      *
    171:      * @since 1.0
    172:      */
    173:     public function testSetRelationTypeFail() {
    174:         try {
    175:             $this->rel1->setRelationType('blah');
    176:             $this->fail('Testing passing an invalid type name to setRelationType');
    177:         }catch (AlphaException $e) {
    178:             $this->assertEquals('Relation type of [blah] is invalid!'
    179:                 , $e->getMessage()
    180:                 , 'Testing passing an invalid type name to setRelationType');
    181:         }
    182:     }
    183:
    184:     /**
    185:      * Testing setValue method with a valid value
    186:      *
    187:      * @since 1.0
    188:      */
    189:     public function testSetValuePass() {
    190:         try {
    191:             $this->rel1->setValue(100);
    192:             $this->rel1->setValue('2777');
    193:         }catch (AlphaException $e) {
    194:             $this->fail('Testing setValue method with a valid value');
    195:         }
    196:     }
    197:
    198:     /**
    199:      * Testing setValue method with an invalid value
    200:      *
    201:      * @since 1.0
    202:      */
    203:     public function testSetValueFail() {
    204:         try {
    205:             $this->rel1->setValue('xyz');
    206:             $this->fail('Testing setValue method with an invalid value');
    207:         }catch (AlphaException $e) {
    208:             $this->assertEquals('[xyz] not a valid Relation value!  A maximum of 11 characters is allowed.'
    209:                 , $e->getMessage()
    210:                 , 'Testing setValue method with an invalid value');
    211:         }
    212:     }
    213:
    214:     /**
    215:      * Testing that the display field value of the related class is accessed correctly
    216:      *
    217:      * @since 1.0
    218:      */
    219:     public function testSetRelatedClassDisplayFieldPass() {
    220:         try {
    221:             $this->rel1->setRelatedClass('PersonObject');
    222:             // assuming here that user #1 is the default Administrator account
    223:             $this->rel1->setValue(1);
    224:             $this->rel1->setRelatedClassDisplayField('state');
    225:             $this->assertEquals('Active', $this->rel1->getRelatedClassDisplayFieldValue(), 'Testing that the display field value of the related class is accessed correctly');
    226:         }catch (AlphaException $e) {
    227:             $this->fail('Testing that the display field value of the related class is accessed correctly');
    228:         }
    229:     }
    230:
    231:     /**
    232:      * Testing that getRelatedClassDisplayFieldValue() will fail to load an invalid class definition
    233:      *
    234:      * @since 1.0
    235:      */
    236:     public function testGetRelatedClassDisplayFieldValueFail() {
    237:         try {
    238:             $this->rel1->setRelatedClassDisplayField('someField');
    239:             $value = $this->rel1->getRelatedClassDisplayFieldValue();
    240:             $this->fail('Testing that getRelatedClassDisplayFieldValue() will fail to load an invalid class definition');
    241:         }catch (AlphaException $e) {
    242:             $this->assertEquals('The class [] is not defined anywhere!'
    243:                 , $e->getMessage()
    244:                 , 'Testing that getRelatedClassDisplayFieldValue() will fail to load an invalid class definition');
    245:         }
    246:     }
    247: }
    248:
    249: ?>