      1: <?php
      2:
      3: /**
      4:  *
      5:  * Test case for the Integer data type
      6:  *
      7:  * @package alpha::tests
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: Integer_Test.php 1496 2012-02-12 20:32:21Z alphadev $
     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 Integer_Test extends PHPUnit_Framework_TestCase {
     49:     /**
     50:      * An Integer for testing
     51:      *
     52:      * @var Integer
     53:      * @since 1.0
     54:      */
     55:     private $int1;
     56:
     57:     /**
     58:      * An Integer for testing
     59:      *
     60:      * @var Integer
     61:      * @since 1.0
     62:      */
     63:     private $int2;
     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->int1 = new Integer();
     74:         $this->int2 = new Integer();
     75:     }
     76:
     77:     /**
     78:      * Called after the test functions are executed
     79:      * this function is defined in PHPUnit_TestCase and overwritten
     80:      * here
     81:      *
     82:      * @since 1.0
     83:      */
     84:     protected function tearDown() {
     85:         unset($this->int1);
     86:         unset($this->int2);
     87:     }
     88:
     89:     /**
     90:      * Testing the int constructor for acceptance of correct data
     91:      *
     92:      * @since 1.0
     93:      */
     94:     public function testConstructorPass() {
     95:         $this->int1 = new Integer(25);
     96:
     97:         $this->assertEquals(25, $this->int1->getValue(), "testing the Integer constructor for pass");
     98:     }
     99:
    100:     /**
    101:      * Testing passing invalid data to setValue
    102:      *
    103:      * @since 1.0
    104:      */
    105:     public function testSetValueInvalid() {
    106:         try {
    107:             $this->int1->setValue("blah");
    108:             $this->fail('testing passing invalid data to setValue');
    109:         }catch (AlphaException $e) {
    110:             $this->assertEquals('Not a valid integer value!'
    111:                 , $e->getMessage()
    112:                 , 'testing passing invalid data to setValue');
    113:         }
    114:     }
    115:
    116:     /**
    117:      * Testing passing valid data to setValue
    118:      *
    119:      * @since 1.0
    120:      */
    121:     public function testSetValueValid() {
    122:         $this->int1->setValue(7);
    123:
    124:         $this->assertEquals(7, $this->int1->getValue(), 'testing passing valid data to setValue');
    125:     }
    126:
    127:     /**
    128:      * Testing the setSize method to see if validation fails
    129:      *
    130:      * @since 1.0
    131:      */
    132:     public function testSetSizeInvalid() {
    133:         $this->int1 = new Integer();
    134:         $this->int1->setSize(2);
    135:
    136:         try {
    137:             $this->int1->setValue(200);
    138:         }catch (AlphaException $e) {
    139:             $this->assertEquals('Not a valid integer value!  A maximum of 2 characters is allowed'
    140:                 , $e->getMessage()
    141:                 , 'testing the setSize method to see if validation fails');
    142:         }
    143:     }
    144:
    145:     /**
    146:      * Testing addition of two Integer values
    147:      *
    148:      * @since 1.0
    149:      */
    150:     public function testAddIntegers() {
    151:         $this->int1 = new Integer(1500);
    152:         $this->int2 = new Integer(3577);
    153:
    154:         $this->assertEquals(5077, ($this->int1->getValue()+$this->int2->getValue()), 'testing addition of two Integer values');
    155:     }
    156:
    157:     /**
    158:      * Testing the __toString method
    159:      *
    160:      * @since 1.0
    161:      */
    162:     public function testToString() {
    163:         $this->int1 = new Integer(2008);
    164:
    165:         $this->assertEquals('The year is 2008', 'The year is '.$this->int1, 'testing the __toString method');
    166:     }
    167: }
    168:
    169: ?>