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

Source for file Enum_Test.php

Documentation is available at Enum_Test.php

  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 1453 2011-12-04 15:12:54Z 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 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.         $this->person new PersonObject();
  75.         $this->person->set('displayName'$_SESSION['currentUser']->getDisplayName());
  76.         $this->person->set('email'$_SESSION['currentUser']->get('email'));
  77.         $this->person->set('password''password');
  78.         $this->person->rebuildTable();
  79.         $this->person->save();
  80.     }
  81.     
  82.     /** 
  83.      * Called after the test functions are executed
  84.      * this function is defined in PHPUnit_TestCase and overwritten
  85.      * here
  86.      * 
  87.      * @since 1.0
  88.      */    
  89.     protected function tearDown({        
  90.         unset($this->enum1);
  91.         $this->person->dropTable();
  92.         $rights new RightsObject();
  93.         $rights->dropTable();
  94.         $rights->dropTable('Person2Rights');
  95.         unset($this->person);
  96.     }    
  97.     
  98.     /**
  99.      * Testing that enum options are loaded correctly from the database
  100.      * 
  101.      * @since 1.0
  102.      */
  103.     public function testLoadEnumOptions({
  104.         $this->person->loadByAttribute('displayName'$_SESSION['currentUser']->getDisplayName()true);
  105.         
  106.         $this->assertEquals('Active'$this->person->getPropObject('state')->getValue()"testing that enum options are loaded correctly from the database");
  107.     }
  108.     
  109.     /**
  110.      * Testing the set/get enum option methods
  111.      * 
  112.      * @since 1.0
  113.      */
  114.     public function testSetEnumOptions({
  115.         $this->enum1->setOptions(array('a','b','c'));
  116.         
  117.         $this->assertEquals($this->enum1->getOptions()array('a','b','c')"testing the set/get enum option methods");
  118.     }
  119.     
  120.     /**
  121.      * Testing the setValue method with good and bad values
  122.      * 
  123.      * @since 1.0
  124.      */
  125.     public function testSetValue({
  126.         $this->enum1->setOptions(array('a','b','c'));
  127.         
  128.         try {        
  129.             $this->enum1->setValue('b');
  130.         }catch (AlphaFrameworkException $e{
  131.             $this->fail('testing the setValue method with a good value');
  132.         }
  133.         
  134.         try {        
  135.             $this->enum1->setValue('z');
  136.             $this->fail('testing the setValue method with a good value');
  137.         }catch (AlphaException $e{
  138.             $this->assertEquals('Not a valid enum option!'
  139.                 $e->getMessage()
  140.                 'testing the setValue method with a bad value');
  141.         }
  142.     }
  143.     
  144.     /**
  145.      * Testing the getValue method
  146.      * 
  147.      * @since 1.0
  148.      */
  149.     public function testGetValue({
  150.         $this->enum1->setOptions(array('a','b','c'));
  151.         
  152.         try {        
  153.             $this->enum1->setValue('b');
  154.         }catch (AlphaFrameworkException $e{
  155.             $this->fail('testing the getValue method');
  156.         }
  157.         
  158.         $this->assertEquals('b'$this->enum1->getValue()'testing the getValue method');
  159.     }
  160.     
  161.     /**
  162.      * Test the constructor failing when a bad array is provided
  163.      * 
  164.      * @since 1.0
  165.      */
  166.     public function testConstructorFail({
  167.         try {        
  168.             $enum new Enum('blah');
  169.             $this->fail('test the constructor failing when a bad array is provided');
  170.         }catch (AlphaException $e{
  171.             $this->assertEquals('Not a valid enum option array!'
  172.                 $e->getMessage()
  173.                 'test the constructor failing when a bad array is provided');
  174.         }
  175.     }
  176.     
  177.     /**
  178.      * Testing the default (non-alphabetical) sort order on the enum
  179.      * 
  180.      * @since 1.0
  181.      */
  182.     public function testDefaultSortOrder({
  183.         $this->enum1 new Enum(array("alpha","gamma","beta"));
  184.         
  185.         $options $this->enum1->getOptions();
  186.          
  187.         $this->assertEquals($options[1]'gamma''testing the default (non-alphabetical) sort order on the enum');
  188.     }
  189.     
  190.     /**
  191.      * Testing the alphabetical sort order on the enum
  192.      * 
  193.      * @since 1.0
  194.      */
  195.     public function testAlphaSortOrder({
  196.         $this->enum1 new Enum(array("alpha","gamma","beta"));
  197.         
  198.         $options $this->enum1->getOptions(true);
  199.          
  200.         $this->assertEquals($options[1]'beta''testing the alphabetical sort order on the enum');
  201.     }
  202. }
  203.  
  204. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:51 +0000 by phpDocumentor 1.4.3