1: <?php
2: 3: 4: 5:
6:
7: if(!class_exists('TypeTest')){
8: require_once __INCLUDES__ .'/model/TypeTest.class.php';
9: }
10:
11: class QDateTimeTests extends QUnitTestCaseBase {
12: protected function setUp()
13: {
14: TypeTest::DeleteAll();
15: }
16:
17: public function testNow() {
18: $obj1 = QDateTime::Now();
19: $this->assertFalse($obj1->IsNull());
20: $this->assertFalse($obj1->IsDateNull());
21: $this->assertFalse($obj1->IsTimeNull());
22:
23: $obj2 = QDateTime::Now(false);
24: $this->assertFalse($obj2->IsNull());
25: $this->assertFalse($obj2->IsDateNull());
26: $this->assertTrue($obj2->IsTimeNull());
27: }
28:
29: public function testIncompleteDates() {
30: $obj1 = new QDateTime("Feb 12");
31: $this->assertFalse($obj1->IsNull());
32: $this->assertFalse($obj1->IsDateNull());
33:
34:
35: $obj2 = new QDateTime("March 2003");
36: $this->assertFalse($obj2->IsNull());
37: $this->assertFalse($obj2->IsDateNull());
38:
39: }
40:
41: public function testConstructor() {
42: $dtNow = QDateTime::Now();
43:
44: $timestamp = time() + 100;
45: $obj2 = QDateTime::FromTimestamp($timestamp);
46:
47: $this->assertNotEquals($dtNow, $obj2);
48:
49: $diff = $obj2->Difference($dtNow);
50:
51: $this->assertTrue($diff->IsPositive());
52: $this->assertFalse($diff->IsNegative());
53: $this->assertFalse($diff->IsZero());
54: $this->assertEquals(1, $diff->Minutes);
55:
56:
57: $this->assertTrue($diff->Seconds > 95);
58: $this->assertTrue($diff->Seconds < 105);
59:
60: $dt2 = QDateTime::FromTimestamp($dtNow->Timestamp);
61: $dt3 = new QDateTime ($dtNow);
62: $this->assertTrue ($dt2->IsEqualTo($dt3));
63:
64:
65: $dt2 = new QDateTime('last Monday');
66: $this->assertEquals ($dt2->format('N'), 1);
67:
68:
69: $dt2 = new QDateTime('0:00:00');
70: $this->assertEquals ($dt2->Hour, 0);
71: $this->assertFalse ($dt2->IsTimeNull());
72:
73:
74: $dt2 = new QDateTime ('@' . $dtNow->Timestamp);
75: $this->assertEquals($dtNow->Hour, $dt2->Hour);
76: }
77:
78:
79: public function testLimitedConstructors() {
80: $dt1 = QDateTime::Now();
81: $ts = $dt1->Timestamp;
82: $strIso = $dt1->qFormat(QDateTime::FormatIsoCompressed);
83:
84: $dt2 = new QDateTime($dt1, null, QDateTime::DateAndTimeType);
85: $dt3 = new QDateTime ($dt1);
86: $this->assertTrue ($dt2->IsEqualTo($dt3));
87:
88: $dt2 = new QDateTime('@' . $ts, null, QDateTime::DateAndTimeType);
89: $this->assertTrue ($dt2->IsEqualTo($dt1));
90: $this->assertFalse($dt2->IsDateNull());
91: $this->assertFalse($dt2->IsTimeNull());
92:
93: $dt2 = new QDateTime('@' . $ts, null, QDateTime::DateOnlyType);
94: $this->assertFalse($dt2->IsDateNull());
95: $this->assertTrue($dt2->IsTimeNull());
96:
97: $dt2 = new QDateTime('@' . $ts, null, QDateTime::TimeOnlyType);
98: $this->assertTrue($dt2->IsDateNull());
99: $this->assertFalse($dt2->IsTimeNull());
100:
101: $dt2 = new QDateTime($strIso, null, QDateTime::DateAndTimeType);
102: $this->assertTrue ($dt2->IsEqualTo($dt1));
103: $this->assertFalse($dt2->IsDateNull());
104: $this->assertFalse($dt2->IsTimeNull());
105:
106: $dt2 = new QDateTime($strIso, null, QDateTime::DateOnlyType);
107: $this->assertFalse($dt2->IsDateNull());
108: $this->assertTrue($dt2->IsTimeNull());
109:
110: $dt2 = new QDateTime($strIso, null, QDateTime::TimeOnlyType);
111: $this->assertTrue($dt2->IsDateNull());
112: $this->assertFalse($dt2->IsTimeNull());
113:
114:
115:
116: $dt2 = new QDateTime(null);
117: $this->assertTrue($dt2->IsDateNull());
118: $this->assertTrue($dt2->IsTimeNull());
119:
120:
121: $dt2 = new QDateTime(null, null, QDateTime::DateOnlyType);
122: $this->assertTrue($dt2->IsDateNull());
123: $this->assertTrue($dt2->IsTimeNull());
124:
125: $dt2 = new QDateTime(null, null, QDateTime::TimeOnlyType);
126: $this->assertTrue($dt2->IsDateNull());
127: $this->assertTrue($dt2->IsTimeNull());
128:
129: $dt2 = new QDateTime(null, null, QDateTime::DateAndTimeType);
130: $this->assertFalse($dt2->IsDateNull());
131: $this->assertFalse($dt2->IsTimeNull());
132:
133:
134: }
135:
136: public function testTimeZoneIssues() {
137: $tz = new DateTimeZone('America/Los_Angeles');
138: $dt1 = new QDateTime ('11/02/14', $tz);
139: $this->assertEquals('America/Los_Angeles', $dt1->getTimezone()->getName());
140:
141: $dt2 = new QDateTime ($dt1, null, QDateTime::DateOnlyType);
142: $this->assertEquals('America/Los_Angeles', $dt2->getTimezone()->getName());
143: $this->assertTrue($dt2->IsTimeNull());
144:
145: $dt2->setTime(7,0,0);
146: $this->assertEquals(7, $dt2->Hour);
147: $this->assertEquals('America/Los_Angeles', $dt2->getTimezone()->getName());
148:
149:
150: $dt2 = new QDateTime ($dt1->format (DateTime::ISO8601), null, QDateTime::DateOnlyType);
151: $dt2->setTime(7,0,0);
152: $this->assertEquals(7, $dt2->Hour);
153:
154: $dt2 = new QDateTime('1/1/14', new DateTimeZone('America/Los_Angeles'));
155: $dt2->Timestamp = 1288486753;
156: $this->assertEquals('America/Los_Angeles', $dt2->getTimezone()->getName());
157: $this->assertEquals(1288486753, $dt2->Timestamp);
158:
159: }
160:
161: 162: 163: 164: 165: 166: 167: 168: 169: 170:
171:
172: public function testOperations() {
173: $obj1 = QDateTime::Now();
174: $obj1->AddYears(-1);
175: $obj1->AddSeconds(-10);
176:
177: $obj2 = QDateTime::Now();
178: $obj2->AddMonths(3);
179:
180: $diff = $obj2->Difference($obj1);
181: $this->assertTrue($diff->IsPositive());
182: $this->assertEquals(15, $diff->Months);
183: }
184:
185: public function testOperations2() {
186: $obj1 = QDateTime::Now();
187: $obj2 = new QDateTime($obj1);
188:
189: $obj1->Year = $obj1->Year + 1;
190: $obj1->AddDays(1);
191:
192: $diff = $obj2->Difference($obj1);
193: $this->assertTrue($diff->IsNegative());
194: $this->assertEquals(-1, $diff->Years);
195: }
196:
197: public function testRoundtrip() {
198: $obj1 = QDateTime::Now();
199: $obj2 = QDateTime::FromTimestamp($obj1->Timestamp);
200:
201: $this->assertTrue($obj1->IsEqualTo($obj2));
202: }
203:
204: public function testSetProperties() {
205: $obj1 = new QDateTime();
206: $obj1->setDate(2002, 3, 15);
207: $this->assertTrue($obj1->IsTimeNull(), "Setting only a date after null constructor keeps time null");
208:
209: $obj2 = new QDateTime("2002-03-15");
210: $obj3 = new QDateTime("2002-03-15 13:15");
211: $obj4 = new QDateTime("2002-03-16");
212:
213: $this->assertTrue($obj1->IsEqualTo($obj2));
214: $this->assertTrue($obj1->IsEqualTo($obj3));
215:
216: $this->assertFalse($obj3->IsEqualTo($obj4));
217:
218:
219: $obj5 = new QDateTime ('13:15:02', null, QDateTime::TimeOnlyType);
220: $this->assertTrue($obj5->IsDateNull(), "Setting only a date after null constructor keeps time null");
221: $obj6 = new QDateTime ('2002-03-15 13:15:02');
222:
223:
224: $obj1->SetTime($obj5);
225:
226: $this->assertFalse($obj1->IsTimeNull(), "Setting a time with object results in a change in null time status");
227: $this->assertTrue($obj1->IsEqualTo($obj6), "SetTime correctly combines date only and time only values");
228: }
229:
230: public function testFormat() {
231: $obj1 = new QDateTime("2002-3-5 13:15");
232:
233: $this->assertEquals("3/5/02 1:15 pm", $obj1->qFormat("M/D/YY h:mm z"));
234: $this->assertEquals("Tue Mar 5 2002", $obj1->qFormat("DDD MMM D YYYY"));
235: $this->assertEquals("One random Tuesday in March", $obj1->qFormat("One random DDDD in MMMM"));
236:
237:
238: $this->assertEquals($obj1->qFormat("M/D/YY h:mm z"), $obj1->qFormat("M/D/YY h:mm z"));
239: }
240:
241: public function testFirstOfMonth() {
242: $dt1 = new QDateTime("2/23/2009");
243: $this->assertEquals(new QDateTime("2/1/2009"), $dt1->FirstDayOfTheMonth);
244:
245: $dt2 = new QDateTime("12/2/2015");
246: $this->assertEquals(new QDateTime("12/1/2015"), $dt2->FirstDayOfTheMonth);
247:
248:
249: $this->assertEquals(new QDateTime("1/1/1923"), QDateTime::FirstDayOfTheMonth(1,1923));
250: }
251:
252: public function testLastOfMonth() {
253: $dt1 = new QDateTime("2/23/2009");
254: $this->assertEquals(new QDateTime("2/28/2009"), $dt1->LastDayOfTheMonth);
255:
256: $dt2 = new QDateTime("1/1/1923");
257: $this->assertEquals(new QDateTime("1/31/1923"), $dt2->LastDayOfTheMonth);
258:
259:
260: $dt3 = new QDateTime("2/4/2000");
261: $this->assertEquals(new QDateTime("2/29/2000"), $dt3->LastDayOfTheMonth);
262:
263: $dt4 = new QDateTime("2/4/2016");
264: $this->assertEquals(new QDateTime("2/29/2016"), $dt4->LastDayOfTheMonth);
265:
266:
267: $this->assertEquals(new QDateTime("12/31/2015"), QDateTime::LastDayOfTheMonth(12, 2015));
268: }
269:
270: public function testSerialize() {
271: $dt1 = QDateTime::Now();
272:
273: $str = serialize($dt1);
274: $dt2 = unserialize($str);
275:
276: $this->assertTrue ($dt1->IsEqualTo($dt2));
277: $this->assertEquals ($dt1->getTimezone()->getName(), $dt2->getTimezone()->getName());
278:
279: }
280:
281: public function testSave() {
282: $objTest = new TypeTest();
283:
284:
285: $objTest->Save();
286: $id = $objTest->Id;
287:
288: $objTest2 = TypeTest::Load($id);
289: $this->assertNull ($objTest2->Date, 'Null date is saved as a NULL in database');
290: $this->assertNull ($objTest2->Time, 'Null time is saved as a NULL in database');
291: $this->assertNull ($objTest2->DateTime, 'Null datetime is saved as a NULL in database');
292: $objTest2->Delete();
293:
294:
295: $objTest = new TypeTest();
296: $objTest->Date = new QDateTime();
297: $objTest->Time = new QDateTime();
298: $objTest->DateTime = new QDateTime();
299: $objTest->Save();
300: $id = $objTest->Id;
301: $objTest2 = TypeTest::Load($id);
302: $this->assertNull ($objTest2->Date, 'Empty date is saved as a NULL in database');
303: $this->assertNull ($objTest2->Time, 'Empty time is saved as a NULL in database');
304: $this->assertNull ($objTest2->DateTime, 'Empty datetime is saved as a NULL in database');
305: $objTest2->Delete();
306:
307:
308: $objTest = new TypeTest();
309: $objTest->Date = new QDateTime('2002-3-5', null, QDateTime::DateOnlyType);
310: $objTest->Time = new QDateTime('13:15:02', null, QDateTime::TimeOnlyType);
311: $objTest->DateTime = new QDateTime('2002-3-5 13:15:02');
312: $objTest->Save();
313:
314: $id = $objTest->Id;
315: $objTest2 = TypeTest::Load($id);
316: $dt = clone($objTest2->Date);
317: $dt->SetTime($objTest2->Time);
318: $this->assertTrue ($dt->IsEqualTo($objTest2->DateTime), 'Combined dates and times after a save are correct.');
319: $objTest2->Delete();
320: }
321: }