      1: <?php
      2:
      3: /**
      4:  * Test case for the Timestamp data type
      5:  *
      6:  * @package alpha::tests
      7:  * @since 1.0
      8:  * @author John Collins <dev@alphaframework.org>
      9:  * @version $Id: Timestamp_Test.php 1611 2012-12-18 11:21:25Z alphadevx $
     10:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     11:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     12:  * All rights reserved.
     13:  *
     14:  * <pre>
     15:  * Redistribution and use in source and binary forms, with or
     16:  * without modification, are permitted provided that the
     17:  * following conditions are met:
     18:  *
     19:  * * Redistributions of source code must retain the above
     20:  *   copyright notice, this list of conditions and the
     21:  *   following disclaimer.
     22:  * * Redistributions in binary form must reproduce the above
     23:  *   copyright notice, this list of conditions and the
     24:  *   following disclaimer in the documentation and/or other
     25:  *   materials provided with the distribution.
     26:  * * Neither the name of the Alpha Framework nor the names
     27:  *   of its contributors may be used to endorse or promote
     28:  *   products derived from this software without specific
     29:  *   prior written permission.
     30:  *
     31:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     32:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     33:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     34:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     35:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     36:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     37:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     39:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     41:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     42:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     43:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     44:  * </pre>
     45:  *
     46:  */
     47: class Timestamp_Test extends PHPUnit_Framework_TestCase {
     48:     /**
     49:      * An Timestamp for testing
     50:      *
     51:      * @var Timestamp
     52:      * @since 1.0
     53:      */
     54:     private $timestamp1;
     55:
     56:     /**
     57:      * Called before the test functions will be executed
     58:      * this function is defined in PHPUnit_TestCase and overwritten
     59:      * here
     60:      *
     61:      * @since 1.0
     62:      */
     63:     protected function setUp() {
     64:         global $config;
     65:
     66:         $config->set('app.default.datetime', 'now');
     67:         $this->timestamp1 = new Timestamp();
     68:     }
     69:
     70:     /**
     71:      * Called after the test functions are executed
     72:      * this function is defined in PHPUnit_TestCase and overwritten
     73:      * here
     74:      *
     75:      * @since 1.0
     76:      */
     77:     protected function tearDown() {
     78:         unset($this->timestamp1);
     79:     }
     80:
     81:     /**
     82:      * Testing the constructor has set the Timestamp to today by default
     83:      *
     84:      * @since 1.0
     85:      */
     86:     public function testDefaultTimestampValue() {
     87:         $this->assertEquals(date("Y-m-d H:i:s"), $this->timestamp1->getValue(), "testing the constructor has set the Timestamp to now by default");
     88:     }
     89:
     90:     /**
     91:      * Testing the setValue method
     92:      *
     93:      * @since 1.0
     94:      */
     95:     public function testSetValuePass() {
     96:         $this->timestamp1->setTimestampValue(2000, 1, 1, 23, 33, 5);
     97:
     98:         $this->assertEquals("2000-01-01 23:33:05", $this->timestamp1->getValue(), "testing the setValue method");
     99:     }
    100:
    101:     /**
    102:      * Testing the setValue method with a bad month
    103:      *
    104:      * @since 1.0
    105:      */
    106:     public function testSetValueInvalidMonth() {
    107:         try {
    108:             $this->timestamp1->setTimestampValue(2000, 'blah', 1, 0, 0, 0);
    109:             $this->fail("testing the setValue method with a bad month");
    110:         }catch (AlphaException $e) {
    111:             $this->assertEquals('The month value blah provided is invalid!'
    112:                 , $e->getMessage()
    113:                 , "testing the setValue method with a bad month");
    114:         }
    115:     }
    116:
    117:     /**
    118:      * Testing the setValue method with a bad timestamp value (out of range)
    119:      *
    120:      * @since 1.0
    121:      */
    122:     public function testSetValueInvalidValue() {
    123:         try {
    124:             $this->timestamp1->setTimestampValue(2000, 13, 1, 0, 0, 0);
    125:             $this->fail("testing the setValue method with a bad timestamp value (out of range)");
    126:         }catch (AlphaException $e) {
    127:             $this->assertEquals('The day value 2000-13-1 provided is invalid!'
    128:                 , $e->getMessage()
    129:                 , "testing the setValue method with a bad timestamp value (out of range)");
    130:         }
    131:     }
    132:
    133:     /**
    134:      * Testing the populate_from_string method
    135:      *
    136:      * @since 1.0
    137:      */
    138:     public function testPopulateFromString() {
    139:         $this->timestamp1->populateFromString("2007-08-13 23:44:07");
    140:
    141:         $this->assertEquals("2007-08-13 23:44:07", $this->timestamp1->getValue(), "testing the populateFromString method");
    142:     }
    143:
    144:     /**
    145:      * Testing that the validation will cause an invalid timestamp to fail on the constructor
    146:      *
    147:      * @since 1.0
    148:      */
    149:     public function testValidationOnConstructor() {
    150:         try {
    151:             $timestamp = new Timestamp("blah");
    152:             $this->fail("testing that the validation will cause an invalid timestamp to fail on the constructor");
    153:         }catch (AlphaException $e) {
    154:             $this->assertTrue(true, "testing that the validation will cause an invalid timestamp to fail on the constructor");
    155:         }
    156:     }
    157:
    158:     /**
    159:      * Testing the get_euro_value method for converting to European timestamp format
    160:      *
    161:      * @since 1.0
    162:      */
    163:     public function testGetEuroValue() {
    164:         $this->assertEquals(date("d/m/y"), $this->timestamp1->getEuroValue(), "testing the get_euro_value method for converting to European timestamp format");
    165:     }
    166:
    167:     /**
    168:      * Testing the getWeekday() method when the default constructor is used
    169:      *
    170:      * @since 1.0
    171:      */
    172:     public function testGetWeekday() {
    173:         $this->assertEquals(date('l'), $this->timestamp1->getWeekday(), "testing the getWeekday() method when the default constructor is used");
    174:     }
    175:
    176:     /**
    177:      * Testing the getUnixValue() method
    178:      *
    179:      * @since 1.2.1
    180:      */
    181:     public function testGetUnixValue() {
    182:         $timestamp = new Timestamp('2012-12-18 11:30:00');
    183:
    184:         $this->assertEquals(1355830200, $timestamp->getUnixValue(), 'testing the getUnixValue() method');
    185:     }
    186: }
    187:
    188: ?>