1: <?php
2:
3: /**
4: *
5: * Test case for the Timestamp data type
6: *
7: * @package alpha::tests
8: * @since 1.0
9: * @author John Collins <dev@alphaframework.org>
10: * @version $Id: Timestamp_Test.php 1548 2012-07-29 17:07:07Z 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 Timestamp_Test extends PHPUnit_Framework_TestCase {
49: /**
50: * An Timestamp for testing
51: *
52: * @var Timestamp
53: * @since 1.0
54: */
55: private $timestamp1;
56:
57: /**
58: * Called before the test functions will be executed
59: * this function is defined in PHPUnit_TestCase and overwritten
60: * here
61: *
62: * @since 1.0
63: */
64: protected function setUp() {
65: global $config;
66:
67: $config->set('app.default.datetime', 'now');
68: $this->timestamp1 = new Timestamp();
69: }
70:
71: /**
72: * Called after the test functions are executed
73: * this function is defined in PHPUnit_TestCase and overwritten
74: * here
75: *
76: * @since 1.0
77: */
78: protected function tearDown() {
79: unset($this->timestamp1);
80: }
81:
82: /**
83: * Testing the constructor has set the Timestamp to today by default
84: *
85: * @since 1.0
86: */
87: public function testDefaultTimestampValue() {
88: $this->assertEquals(date("Y-m-d H:i:s"), $this->timestamp1->getValue(), "testing the constructor has set the Timestamp to now by default");
89: }
90:
91: /**
92: * Testing the setValue method
93: *
94: * @since 1.0
95: */
96: public function testSetValuePass() {
97: $this->timestamp1->setTimestampValue(2000, 1, 1, 23, 33, 5);
98:
99: $this->assertEquals("2000-01-01 23:33:05", $this->timestamp1->getValue(), "testing the setValue method");
100: }
101:
102: /**
103: * Testing the setValue method with a bad month
104: *
105: * @since 1.0
106: */
107: public function testSetValueInvalidMonth() {
108: try {
109: $this->timestamp1->setTimestampValue(2000, 'blah', 1, 0, 0, 0);
110: $this->fail("testing the setValue method with a bad month");
111: }catch (AlphaException $e) {
112: $this->assertEquals('The month value blah provided is invalid!'
113: , $e->getMessage()
114: , "testing the setValue method with a bad month");
115: }
116: }
117:
118: /**
119: * Testing the setValue method with a bad timestamp value (out of range)
120: *
121: * @since 1.0
122: */
123: public function testSetValueInvalidValue() {
124: try {
125: $this->timestamp1->setTimestampValue(2000, 13, 1, 0, 0, 0);
126: $this->fail("testing the setValue method with a bad timestamp value (out of range)");
127: }catch (AlphaException $e) {
128: $this->assertEquals('The day value 2000-13-1 provided is invalid!'
129: , $e->getMessage()
130: , "testing the setValue method with a bad timestamp value (out of range)");
131: }
132: }
133:
134: /**
135: * Testing the populate_from_string method
136: *
137: * @since 1.0
138: */
139: public function testPopulateFromString() {
140: $this->timestamp1->populateFromString("2007-08-13 23:44:07");
141:
142: $this->assertEquals("2007-08-13 23:44:07", $this->timestamp1->getValue(), "testing the populateFromString method");
143: }
144:
145: /**
146: * Testing that the validation will cause an invalid timestamp to fail on the constructor
147: *
148: * @since 1.0
149: */
150: public function testValidationOnConstructor() {
151: try {
152: $timestamp = new Timestamp("blah");
153: $this->fail("testing that the validation will cause an invalid timestamp to fail on the constructor");
154: }catch (AlphaException $e) {
155: $this->assertTrue(true, "testing that the validation will cause an invalid timestamp to fail on the constructor");
156: }
157: }
158:
159: /**
160: * Testing the get_euro_value method for converting to European timestamp format
161: *
162: * @since 1.0
163: */
164: public function testGetEuroValue() {
165: $this->assertEquals(date("d/m/y"), $this->timestamp1->getEuroValue(), "testing the get_euro_value method for converting to European timestamp format");
166: }
167:
168: /**
169: * Testing the getWeekday() method when the default constructor is used
170: *
171: * @since 1.0
172: */
173: public function testGetWeekday() {
174: $this->assertEquals(date('l'), $this->timestamp1->getWeekday(), "testing the getWeekday() method when the default constructor is used");
175: }
176: }
177:
178: ?>