ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DateTimeTest.php
Go to the documentation of this file.
1<?php
2
4
5use PHPUnit\Framework\TestCase;
7
8class DateTimeTest extends TestCase {
9
10 protected $vcal;
11
12 function setUp() {
13
14 $this->vcal = new VCalendar();
15
16 }
17
18 function testSetDateTime() {
19
20 $tz = new \DateTimeZone('Europe/Amsterdam');
21 $dt = new \DateTime('1985-07-04 01:30:00', $tz);
22 $dt->setTimeZone($tz);
23
24 $elem = $this->vcal->createProperty('DTSTART');
25 $elem->setDateTime($dt);
26
27 $this->assertEquals('19850704T013000', (string)$elem);
28 $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
29 $this->assertNull($elem['VALUE']);
30
31 $this->assertTrue($elem->hasTime());
32
33 }
34
36
37 $tz = new \DateTimeZone('Europe/Amsterdam');
38 $dt = new \DateTime('1985-07-04 01:30:00', $tz);
39 $dt->setTimeZone($tz);
40
41 $elem = $this->vcal->createProperty('DTSTART');
42 $elem->setDateTime($dt, $isFloating = true);
43
44 $this->assertEquals('19850704T013000', (string)$elem);
45 $this->assertNull($elem['TZID']);
46
47 $this->assertTrue($elem->hasTime());
48 }
49
50 function testSetDateTimeUTC() {
51
52 $tz = new \DateTimeZone('GMT');
53 $dt = new \DateTime('1985-07-04 01:30:00', $tz);
54 $dt->setTimeZone($tz);
55
56 $elem = $this->vcal->createProperty('DTSTART');
57 $elem->setDateTime($dt);
58
59 $this->assertEquals('19850704T013000Z', (string)$elem);
60 $this->assertNull($elem['TZID']);
61
62 $this->assertTrue($elem->hasTime());
63 }
64
66
67 // When initialized from a Unix timestamp, the timezone is set to "+00:00".
68 $dt = new \DateTime('@489288600');
69
70 $elem = $this->vcal->createProperty('DTSTART');
71 $elem->setDateTime($dt);
72
73 $this->assertEquals('19850704T013000Z', (string)$elem);
74 $this->assertNull($elem['TZID']);
75
76 $this->assertTrue($elem->hasTime());
77 }
78
80
81 $tz = new \DateTimeZone('Europe/Amsterdam');
82 $dt = new \DateTime('1985-07-04 01:30:00', $tz);
83 $dt->setTimeZone($tz);
84
85 $elem = $this->vcal->createProperty('DTSTART');
86 $elem->setDateTime($dt);
87
88 $this->assertEquals('19850704T013000', (string)$elem);
89 $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
90
91 $this->assertTrue($elem->hasTime());
92 }
93
95
96 $tz = new \DateTimeZone('Europe/Amsterdam');
97 $dt = new \DateTime('1985-07-04 01:30:00', $tz);
98 $dt->setTimeZone($tz);
99
100 $elem = $this->vcal->createProperty('DTSTART');
101 $elem['VALUE'] = 'DATE';
102 $elem->setDateTime($dt);
103
104 $this->assertEquals('19850704', (string)$elem);
105 $this->assertNull($elem['TZID']);
106 $this->assertEquals('DATE', (string)$elem['VALUE']);
107
108 $this->assertFalse($elem->hasTime());
109 }
110
111 function testSetValue() {
112
113 $tz = new \DateTimeZone('Europe/Amsterdam');
114 $dt = new \DateTime('1985-07-04 01:30:00', $tz);
115 $dt->setTimeZone($tz);
116
117 $elem = $this->vcal->createProperty('DTSTART');
118 $elem->setValue($dt);
119
120 $this->assertEquals('19850704T013000', (string)$elem);
121 $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
122 $this->assertNull($elem['VALUE']);
123
124 $this->assertTrue($elem->hasTime());
125
126 }
127
128 function testSetValueArray() {
129
130 $tz = new \DateTimeZone('Europe/Amsterdam');
131 $dt1 = new \DateTime('1985-07-04 01:30:00', $tz);
132 $dt2 = new \DateTime('1985-07-04 02:30:00', $tz);
133 $dt1->setTimeZone($tz);
134 $dt2->setTimeZone($tz);
135
136 $elem = $this->vcal->createProperty('DTSTART');
137 $elem->setValue([$dt1, $dt2]);
138
139 $this->assertEquals('19850704T013000,19850704T023000', (string)$elem);
140 $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
141 $this->assertNull($elem['VALUE']);
142
143 $this->assertTrue($elem->hasTime());
144
145 }
146
147 function testSetParts() {
148
149 $tz = new \DateTimeZone('Europe/Amsterdam');
150 $dt1 = new \DateTime('1985-07-04 01:30:00', $tz);
151 $dt2 = new \DateTime('1985-07-04 02:30:00', $tz);
152 $dt1->setTimeZone($tz);
153 $dt2->setTimeZone($tz);
154
155 $elem = $this->vcal->createProperty('DTSTART');
156 $elem->setParts([$dt1, $dt2]);
157
158 $this->assertEquals('19850704T013000,19850704T023000', (string)$elem);
159 $this->assertEquals('Europe/Amsterdam', (string)$elem['TZID']);
160 $this->assertNull($elem['VALUE']);
161
162 $this->assertTrue($elem->hasTime());
163
164 }
166
167 $dt1 = '19850704T013000Z';
168 $dt2 = '19850704T023000Z';
169
170 $elem = $this->vcal->createProperty('DTSTART');
171 $elem->setParts([$dt1, $dt2]);
172
173 $this->assertEquals('19850704T013000Z,19850704T023000Z', (string)$elem);
174 $this->assertNull($elem['VALUE']);
175
176 $this->assertTrue($elem->hasTime());
177
178 }
179
180
182
183 $tz = new \DateTimeZone('Europe/Amsterdam');
184 $dt = new \DateTimeImmutable('1985-07-04 01:30:00', $tz);
185 $dt->setTimeZone($tz);
186
187 $elem = $this->vcal->createProperty('DTSTART');
188 $elem->setDateTime($dt);
189
190 $this->assertEquals($elem->getDateTime(), $dt);
191
192 }
193
195
196 $elem = $this->vcal->createProperty('DTSTART');
197 $dt = $elem->getDateTime();
198
199 $this->assertNull($dt);
200
201 }
202
204
205 $elem = $this->vcal->createProperty('DTSTART', '19850704');
206 $dt = $elem->getDateTime();
207
208 $this->assertInstanceOf('DateTimeImmutable', $dt);
209 $this->assertEquals('1985-07-04 00:00:00', $dt->format('Y-m-d H:i:s'));
210
211 }
212
214
215 $elem = $this->vcal->createProperty('DTSTART', '19850704');
216
217 $tz = new \DateTimeZone('America/Toronto');
218 $dt = $elem->getDateTime($tz);
219 $dt = $dt->setTimeZone(new \DateTimeZone('UTC'));
220
221 $this->assertInstanceOf('DateTimeImmutable', $dt);
222 $this->assertEquals('1985-07-04 04:00:00', $dt->format('Y-m-d H:i:s'));
223
224 }
225
227
228 $elem = $this->vcal->createProperty('DTSTART', '19850704T013000');
229 $dt = $elem->getDateTime();
230
231 $this->assertInstanceOf('DateTimeImmutable', $dt);
232 $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
233
234 }
235
237
238 $elem = $this->vcal->createProperty('DTSTART', '19850704T013000');
239
240 $tz = new \DateTimeZone('America/Toronto');
241 $dt = $elem->getDateTime($tz);
242 $dt = $dt->setTimeZone(new \DateTimeZone('UTC'));
243
244 $this->assertInstanceOf('DateTimeInterface', $dt);
245 $this->assertEquals('1985-07-04 05:30:00', $dt->format('Y-m-d H:i:s'));
246
247 }
248
250
251 $elem = $this->vcal->createProperty('DTSTART', '19850704T013000Z');
252 $dt = $elem->getDateTime();
253
254 $this->assertInstanceOf('DateTimeImmutable', $dt);
255 $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
256 $this->assertEquals('UTC', $dt->getTimeZone()->getName());
257
258 }
259
261
262 $elem = $this->vcal->createProperty('DTSTART', '19850704T013000');
263 $elem['TZID'] = 'Europe/Amsterdam';
264
265 $dt = $elem->getDateTime();
266
267 $this->assertInstanceOf('DateTimeImmutable', $dt);
268 $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
269 $this->assertEquals('Europe/Amsterdam', $dt->getTimeZone()->getName());
270
271 }
272
277
278 $elem = $this->vcal->createProperty('DTSTART', 'bla');
279 $dt = $elem->getDateTime();
280
281 }
282
284
285 $elem = $this->vcal->createProperty('DTSTART', '19850704T013000');
286 $elem['TZID'] = '/freeassociation.sourceforge.net/Tzfile/Europe/Amsterdam';
287
288
289 $event = $this->vcal->createComponent('VEVENT');
290 $event->add($elem);
291
292 $timezone = $this->vcal->createComponent('VTIMEZONE');
293 $timezone->TZID = '/freeassociation.sourceforge.net/Tzfile/Europe/Amsterdam';
294 $timezone->{'X-LIC-LOCATION'} = 'Europe/Amsterdam';
295
296 $this->vcal->add($event);
297 $this->vcal->add($timezone);
298
299 $dt = $elem->getDateTime();
300
301 $this->assertInstanceOf('DateTimeImmutable', $dt);
302 $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
303 $this->assertEquals('Europe/Amsterdam', $dt->getTimeZone()->getName());
304
305 }
306
308
309 $default = date_default_timezone_get();
310 date_default_timezone_set('Canada/Eastern');
311
312 $elem = $this->vcal->createProperty('DTSTART', '19850704T013000');
313 $elem['TZID'] = 'Moon';
314
315
316 $event = $this->vcal->createComponent('VEVENT');
317 $event->add($elem);
318
319 $timezone = $this->vcal->createComponent('VTIMEZONE');
320 $timezone->TZID = 'Moon';
321 $timezone->{'X-LIC-LOCATION'} = 'Moon';
322
323
324 $this->vcal->add($event);
325 $this->vcal->add($timezone);
326
327 $dt = $elem->getDateTime();
328
329 $this->assertInstanceOf('DateTimeImmutable', $dt);
330 $this->assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
331 $this->assertEquals('Canada/Eastern', $dt->getTimeZone()->getName());
332 date_default_timezone_set($default);
333
334 }
335
337
338 $dtStart = $this->vcal->createProperty('DTSTART', new \DateTime('2013-06-07 15:05:00'));
339 $dtStart['VALUE'] = 'DATE';
340
341 $this->assertEquals("DTSTART;VALUE=DATE:20130607\r\n", $dtStart->serialize());
342
343 }
344
345 function testValidate() {
346
347 $exDate = $this->vcal->createProperty('EXDATE', '-00011130T143000Z');
348 $messages = $exDate->validate();
349 $this->assertEquals(1, count($messages));
350 $this->assertEquals(3, $messages[0]['level']);
351
352 }
353
358
359 $vcal = new VCalendar();
360 $vevent = $vcal->add('VEVENT');
361
362 $dtstart = $vevent->add(
363 'DTSTART',
364 new \DateTime('2014-03-07'),
365 ['VALUE' => 'DATE']
366 );
367
368 $this->assertEquals("DTSTART;VALUE=DATE:20140307\r\n", $dtstart->serialize());
369
370 }
371
372}
$default
Definition: build.php:20
An exception for terminatinating execution or to throw for unit testing.
The VCalendar component.
Definition: VCalendar.php:23
testGetDateTimeDateInvalid()
@expectedException \Sabre\VObject\InvalidDataException
testCreateDatePropertyThroughAdd()
This issue was discovered on the sabredav mailing list.
$messages
Definition: en.php:5