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 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 Date_Test extends PHPUnit_Framework_TestCase {
49: /**
50: * An Date for testing
51: *
52: * @var Date
53: * @since 1.0
54: */
55: private $date1;
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: // override setting to ensure dates default to now
67: $config->set('app.default.datetime', 'now');
68:
69: $this->date1 = new Date();
70: }
71:
72: /**
73: * Called after the test functions are executed
74: * this function is defined in PHPUnit_TestCase and overwritten
75: * here
76: *
77: * @since 1.0
78: */
79: protected function tearDown() {
80: unset($this->date1);
81: }
82:
83: /**
84: * Testing the constructor has set the Date to today by default
85: *
86: * @since 1.0
87: */
88: public function testDefaultDateValue() {
89: $this->assertEquals(date("Y-m-d"), $this->date1->getValue(), "testing the constructor has set the Date to today by default");
90: }
91:
92: /**
93: * Testing the setValue method
94: *
95: * @since 1.0
96: */
97: public function testSetValuePass() {
98: $this->date1->setDateValue(2000, 1, 1);
99:
100: $this->assertEquals("2000-01-01", $this->date1->getValue(), "testing the setValue method");
101: }
102:
103: /**
104: * Testing the setValue method with a bad month
105: *
106: * @since 1.0
107: */
108: public function testSetValueInvalidMonth() {
109: try {
110: $this->date1->setDateValue(2000, 'blah', 1);
111: $this->fail("testing the setValue method with a bad month");
112: }catch (AlphaException $e) {
113: $this->assertEquals('Error: the month value blah provided is invalid!'
114: , $e->getMessage()
115: , "testing the setValue method with a bad month");
116: }
117: }
118:
119: /**
120: * Testing the setValue method with a bad date value (out of range)
121: *
122: * @since 1.0
123: */
124: public function testSetValueInvalidValue() {
125: try {
126: $this->date1->setDateValue(2000, 13, 1);
127: $this->fail("testing the setValue method with a bad date value (out of range)");
128: }catch (AlphaException $e) {
129: $this->assertEquals('Error: the day value 2000-13-1 provided is invalid!'
130: , $e->getMessage()
131: , "testing the setValue method with a bad date value (out of range)");
132: }
133: }
134:
135: /**
136: * Testing the populate_from_string method
137: *
138: * @since 1.0
139: */
140: public function testPopulateFromString() {
141: $this->date1->populateFromString("2007-08-13");
142:
143: $this->assertEquals("2007-08-13", $this->date1->getValue(), "testing the populateFromString method");
144: }
145:
146: /**
147: * Testing that the validation will cause an invalid date to fail on the constructor
148: *
149: * @since 1.0
150: */
151: public function testValidationOnConstructor() {
152: try {
153: $date = new Date("blah");
154: $this->fail("testing that the validation will cause an invalid date to fail on the constructor");
155: }catch (AlphaException $e) {
156: $this->assertTrue(true, "testing that the validation will cause an invalid date to fail on the constructor");
157: }
158: }
159:
160: /**
161: * Testing the get_euro_value method for converting to European date format
162: *
163: * @since 1.0
164: */
165: public function testGetEuroValue() {
166: $this->assertEquals(date("d/m/y"), $this->date1->getEuroValue(), "testing the get_euro_value method for converting to European date format");
167: }
168:
169: /**
170: * Testing the getWeekday() method when the default constructor is used
171: *
172: * @since 1.0
173: */
174: public function testGetWeekday() {
175: $this->assertEquals(date('l'), $this->date1->getWeekday(), "testing the getWeekday() method when the default constructor is used");
176: }
177: }
178:
179: ?>