      1: <?php
      2:
      3: /**
      4:  *
      5:  * Test case for the Enum data type
      6:  *
      7:  * @package alpha::tests
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: Enum_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 Enum_Test extends PHPUnit_Framework_TestCase {
     49:     /**
     50:      * An Enum for testing
     51:      *
     52:      * @var Enum
     53:      * @since 1.0
     54:      */
     55:     private $enum1;
     56:
     57:     /**
     58:      * A person for testing
     59:      *
     60:      * @var PersonObject
     61:      * @since 1.0
     62:      */
     63:     private $person;
     64:
     65:     /**
     66:      * Called before the test functions will be executed
     67:      * this function is defined in PHPUnit_TestCase and overwritten
     68:      * here
     69:      *
     70:      * @since 1.0
     71:      */
     72:     protected function setUp() {
     73:         $this->enum1 = new Enum();
     74:
     75:         $rights = new RightsObject();
     76:         $rights->rebuildTable();
     77:
     78:         $this->person = new PersonObject();
     79:         $this->person->set('displayName', $_SESSION['currentUser']->getDisplayName());
     80:         $this->person->set('email', $_SESSION['currentUser']->get('email'));
     81:         $this->person->set('password', 'password');
     82:         $this->person->rebuildTable();
     83:         $this->person->save();
     84:     }
     85:
     86:     /**
     87:      * Called after the test functions are executed
     88:      * this function is defined in PHPUnit_TestCase and overwritten
     89:      * here
     90:      *
     91:      * @since 1.0
     92:      */
     93:     protected function tearDown() {
     94:         unset($this->enum1);
     95:         $this->person->dropTable();
     96:         $rights = new RightsObject();
     97:         $rights->dropTable();
     98:         $rights->dropTable('Person2Rights');
     99:         unset($this->person);
    100:     }
    101:
    102:     /**
    103:      * Testing that enum options are loaded correctly from the database
    104:      *
    105:      * @since 1.0
    106:      */
    107:     public function testLoadEnumOptions() {
    108:         $this->person->loadByAttribute('displayName', $_SESSION['currentUser']->getDisplayName(), true);
    109:
    110:         $this->assertEquals('Active', $this->person->getPropObject('state')->getValue(), "testing that enum options are loaded correctly from the database");
    111:     }
    112:
    113:     /**
    114:      * Testing the set/get enum option methods
    115:      *
    116:      * @since 1.0
    117:      */
    118:     public function testSetEnumOptions() {
    119:         $this->enum1->setOptions(array('a','b','c'));
    120:
    121:         $this->assertEquals($this->enum1->getOptions(), array('a','b','c'), "testing the set/get enum option methods");
    122:     }
    123:
    124:     /**
    125:      * Testing the setValue method with good and bad values
    126:      *
    127:      * @since 1.0
    128:      */
    129:     public function testSetValue() {
    130:         $this->enum1->setOptions(array('a','b','c'));
    131:
    132:         try {
    133:             $this->enum1->setValue('b');
    134:         }catch (AlphaFrameworkException $e) {
    135:             $this->fail('testing the setValue method with a good value');
    136:         }
    137:
    138:         try {
    139:             $this->enum1->setValue('z');
    140:             $this->fail('testing the setValue method with a good value');
    141:         }catch (AlphaException $e) {
    142:             $this->assertEquals('Not a valid enum option!'
    143:                 , $e->getMessage()
    144:                 , 'testing the setValue method with a bad value');
    145:         }
    146:     }
    147:
    148:     /**
    149:      * Testing the getValue method
    150:      *
    151:      * @since 1.0
    152:      */
    153:     public function testGetValue() {
    154:         $this->enum1->setOptions(array('a','b','c'));
    155:
    156:         try {
    157:             $this->enum1->setValue('b');
    158:         }catch (AlphaFrameworkException $e) {
    159:             $this->fail('testing the getValue method');
    160:         }
    161:
    162:         $this->assertEquals('b', $this->enum1->getValue(), 'testing the getValue method');
    163:     }
    164:
    165:     /**
    166:      * Test the constructor failing when a bad array is provided
    167:      *
    168:      * @since 1.0
    169:      */
    170:     public function testConstructorFail() {
    171:         try {
    172:             $enum = new Enum('blah');
    173:             $this->fail('test the constructor failing when a bad array is provided');
    174:         }catch (AlphaException $e) {
    175:             $this->assertEquals('Not a valid enum option array!'
    176:                 , $e->getMessage()
    177:                 , 'test the constructor failing when a bad array is provided');
    178:         }
    179:     }
    180:
    181:     /**
    182:      * Testing the default (non-alphabetical) sort order on the enum
    183:      *
    184:      * @since 1.0
    185:      */
    186:     public function testDefaultSortOrder() {
    187:         $this->enum1 = new Enum(array("alpha","gamma","beta"));
    188:
    189:         $options = $this->enum1->getOptions();
    190:
    191:         $this->assertEquals($options[1], 'gamma', 'testing the default (non-alphabetical) sort order on the enum');
    192:     }
    193:
    194:     /**
    195:      * Testing the alphabetical sort order on the enum
    196:      *
    197:      * @since 1.0
    198:      */
    199:     public function testAlphaSortOrder() {
    200:         $this->enum1 = new Enum(array("alpha","gamma","beta"));
    201:
    202:         $options = $this->enum1->getOptions(true);
    203:
    204:         $this->assertEquals($options[1], 'beta', 'testing the alphabetical sort order on the enum');
    205:     }
    206: }
    207:
    208: ?>