      1: <?php
      2:
      3: /**
      4:  *
      5:  * Test case for the Date data type
      6:  *
      7:  * @package alpha::tests
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: Date_Test.php 1605 2012-12-13 15:41:06Z 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 Date_Test extends PHPUnit_Framework_TestCase {
     49:
     50:     /**
     51:      * An Date for testing
     52:      *
     53:      * @var Date
     54:      * @since 1.0
     55:      */
     56:     private $date1;
     57:
     58:     /**
     59:      * Called before the test functions will be executed
     60:      * this function is defined in PHPUnit_TestCase and overwritten
     61:      * here
     62:      *
     63:      * @since 1.0
     64:      */
     65:     protected function setUp() {
     66:
     67:         global $config;
     68:
     69:         // override setting to ensure dates default to now
     70:         $config->set('app.default.datetime', 'now');
     71:
     72:         $this->date1 = new Date();
     73:     }
     74:
     75:     /**
     76:      * Called after the test functions are executed
     77:      * this function is defined in PHPUnit_TestCase and overwritten
     78:      * here
     79:      *
     80:      * @since 1.0
     81:      */
     82:     protected function tearDown() {
     83:         unset($this->date1);
     84:     }
     85:
     86:     /**
     87:      * Testing the constructor has set the Date to today by default
     88:      *
     89:      * @since 1.0
     90:      */
     91:     public function testDefaultDateValue() {
     92:         $this->assertEquals(date("Y-m-d"), $this->date1->getValue(), "testing the constructor has set the Date to today by default");
     93:     }
     94:
     95:     /**
     96:      * Testing the setValue method
     97:      *
     98:      * @since 1.0
     99:      */
    100:     public function testSetValuePass() {
    101:         $this->date1->setDateValue(2000, 1, 1);
    102:
    103:         $this->assertEquals("2000-01-01", $this->date1->getValue(), "testing the setValue method");
    104:     }
    105:
    106:     /**
    107:      * Testing the setValue method with a bad month
    108:      *
    109:      * @since 1.0
    110:      */
    111:     public function testSetValueInvalidMonth() {
    112:         try {
    113:             $this->date1->setDateValue(2000, 'blah', 1);
    114:             $this->fail("testing the setValue method with a bad month");
    115:         }catch (AlphaException $e) {
    116:             $this->assertEquals('Error: the month value blah provided is invalid!'
    117:                 , $e->getMessage()
    118:                 , "testing the setValue method with a bad month");
    119:         }
    120:     }
    121:
    122:     /**
    123:      * Testing the setValue method with a bad date value (out of range)
    124:      *
    125:      * @since 1.0
    126:      */
    127:     public function testSetValueInvalidValue() {
    128:         try {
    129:             $this->date1->setDateValue(2000, 13, 1);
    130:             $this->fail("testing the setValue method with a bad date value (out of range)");
    131:         }catch (AlphaException $e) {
    132:             $this->assertEquals('Error: the day value 2000-13-1 provided is invalid!'
    133:                 , $e->getMessage()
    134:                 , "testing the setValue method with a bad date value (out of range)");
    135:         }
    136:     }
    137:
    138:     /**
    139:      * Testing the populateFromString method
    140:      *
    141:      * @since 1.0
    142:      */
    143:     public function testPopulateFromString() {
    144:         $this->date1->populateFromString("2007-08-13");
    145:
    146:         $this->assertEquals("2007-08-13", $this->date1->getValue(), "testing the populateFromString method");
    147:
    148:         try {
    149:             $this->date1->populateFromString("2007-08-40");
    150:             $this->fail("testing the populateFromString method with a bad date value");
    151:         }catch (AlphaException $e) {
    152:             $this->assertEquals('Error: the date value 2007-08-40 provided is invalid!'
    153:                 , $e->getMessage()
    154:                 , "testing the populateFromString method with a bad date value");
    155:         }
    156:
    157:         try {
    158:             $this->date1->populateFromString("2007-08-aa");
    159:             $this->fail("testing the populateFromString method with a bad date value");
    160:         }catch (AlphaException $e) {
    161:             $this->assertEquals('Error: the day value aa provided is invalid!'
    162:                 , $e->getMessage()
    163:                 , "testing the populateFromString method with a bad date value");
    164:         }
    165:
    166:         try {
    167:             $this->date1->populateFromString("bad");
    168:             $this->fail("testing the populateFromString method with a bad date value");
    169:         }catch (AlphaException $e) {
    170:             $this->assertEquals('Invalid Date value [bad] provided!'
    171:                 , $e->getMessage()
    172:                 , "testing the populateFromString method with a bad date value");
    173:         }
    174:     }
    175:
    176:     /**
    177:      * Testing that the validation will cause an invalid date to fail on the constructor
    178:      *
    179:      * @since 1.0
    180:      */
    181:     public function testValidationOnConstructor() {
    182:         try {
    183:             $date = new Date("blah");
    184:             $this->fail("testing that the validation will cause an invalid date to fail on the constructor");
    185:         }catch (AlphaException $e) {
    186:             $this->assertTrue(true, "testing that the validation will cause an invalid date to fail on the constructor");
    187:         }
    188:     }
    189:
    190:     /**
    191:      * Testing the getEuroValue method for converting to European date format
    192:      *
    193:      * @since 1.0
    194:      */
    195:     public function testGetEuroValue() {
    196:         $this->assertEquals(date("d/m/y"), $this->date1->getEuroValue(), "testing the getEuroValue method for converting to European date format");
    197:     }
    198:
    199:     /**
    200:      * Testing the getWeekday() method when the default constructor is used
    201:      *
    202:      * @since 1.0
    203:      */
    204:     public function testGetWeekday() {
    205:         $this->assertEquals(date('l'), $this->date1->getWeekday(), "testing the getWeekday() method when the default constructor is used");
    206:     }
    207:
    208:     /**
    209:      * Testing the getUnixValue() method
    210:      *
    211:      * @since 1.2.1
    212:      */
    213:     public function testGetUnixValue() {
    214:         $date = new Date('2012-12-10');
    215:
    216:         $this->assertEquals('1355097600', $date->getUnixValue(), 'testing the getUnixValue() method');
    217:     }
    218:
    219:     /**
    220:      * Testing the getUSValue method for converting to European date format
    221:      *
    222:      * @since 1.2.1
    223:      */
    224:     public function testGetUSValue() {
    225:         $this->assertEquals(date("m/d/y"), $this->date1->getUSValue(), "testing the getUSValue method for converting to US date format");
    226:     }
    227: }
    228:
    229: ?>