1<?php
2
3namespace Sabre\VObject;
4
5class FreeBusyGeneratorTest extends \PHPUnit_Framework_TestCase {
6
7    function getInput() {
8
9        $tests = array();
10
11        $blob = <<<ICS
12BEGIN:VCALENDAR
13BEGIN:VEVENT
14UID:foobar
15DTSTART:20110101T120000Z
16DTEND:20110101T130000Z
17END:VEVENT
18END:VCALENDAR
19ICS;
20
21        $tests[] = array(
22            $blob,
23            "20110101T120000Z/20110101T130000Z"
24        );
25
26        // opaque, shows up
27        $blob = <<<ICS
28BEGIN:VCALENDAR
29BEGIN:VEVENT
30UID:foobar2
31TRANSP:OPAQUE
32DTSTART:20110101T130000Z
33DTEND:20110101T140000Z
34END:VEVENT
35END:VCALENDAR
36ICS;
37
38        $tests[] = array(
39            $blob,
40            "20110101T130000Z/20110101T140000Z"
41        );
42
43        // transparent, hidden
44        $blob = <<<ICS
45BEGIN:VCALENDAR
46BEGIN:VEVENT
47UID:foobar3
48TRANSP:TRANSPARENT
49DTSTART:20110101T140000Z
50DTEND:20110101T150000Z
51END:VEVENT
52END:VCALENDAR
53ICS;
54
55        $tests[] = array(
56            $blob,
57            null,
58        );
59
60        // cancelled, hidden
61        $blob = <<<ICS
62BEGIN:VCALENDAR
63BEGIN:VEVENT
64UID:foobar4
65STATUS:CANCELLED
66DTSTART:20110101T160000Z
67DTEND:20110101T170000Z
68END:VEVENT
69END:VCALENDAR
70ICS;
71
72        $tests[] = array(
73            $blob,
74            null,
75        );
76
77        // tentative, shows up
78        $blob = <<<ICS
79BEGIN:VCALENDAR
80BEGIN:VEVENT
81UID:foobar5
82STATUS:TENTATIVE
83DTSTART:20110101T180000Z
84DTEND:20110101T190000Z
85END:VEVENT
86END:VCALENDAR
87ICS;
88
89        $tests[] = array(
90            $blob,
91            '20110101T180000Z/20110101T190000Z',
92        );
93
94        // outside of time-range, hidden
95        $blob = <<<ICS
96BEGIN:VCALENDAR
97BEGIN:VEVENT
98UID:foobar6
99DTSTART:20110101T090000Z
100DTEND:20110101T100000Z
101END:VEVENT
102END:VCALENDAR
103ICS;
104
105        $tests[] = array(
106            $blob,
107            null,
108        );
109
110        // outside of time-range, hidden
111        $blob = <<<ICS
112BEGIN:VCALENDAR
113BEGIN:VEVENT
114UID:foobar7
115DTSTART:20110104T090000Z
116DTEND:20110104T100000Z
117END:VEVENT
118END:VCALENDAR
119ICS;
120
121        $tests[] = array(
122            $blob,
123            null,
124        );
125
126        // using duration, shows up
127        $blob = <<<ICS
128BEGIN:VCALENDAR
129BEGIN:VEVENT
130UID:foobar8
131DTSTART:20110101T190000Z
132DURATION:PT1H
133END:VEVENT
134END:VCALENDAR
135ICS;
136
137        $tests[] = array(
138            $blob,
139            '20110101T190000Z/20110101T200000Z',
140        );
141
142        // Day-long event, shows up
143        $blob = <<<ICS
144BEGIN:VCALENDAR
145BEGIN:VEVENT
146UID:foobar9
147DTSTART;VALUE=DATE:20110102
148END:VEVENT
149END:VCALENDAR
150ICS;
151
152        $tests[] = array(
153            $blob,
154            '20110102T000000Z/20110103T000000Z',
155        );
156
157
158        // No duration, does not show up
159        $blob = <<<ICS
160BEGIN:VCALENDAR
161BEGIN:VEVENT
162UID:foobar10
163DTSTART:20110101T200000Z
164END:VEVENT
165END:VCALENDAR
166ICS;
167
168        $tests[] = array(
169            $blob,
170            null,
171        );
172
173        // encoded as object, shows up
174        $blob = <<<ICS
175BEGIN:VCALENDAR
176BEGIN:VEVENT
177UID:foobar11
178DTSTART:20110101T210000Z
179DURATION:PT1H
180END:VEVENT
181END:VCALENDAR
182ICS;
183
184        $tests[] = array(
185            Reader::read($blob),
186            '20110101T210000Z/20110101T220000Z',
187        );
188
189        // Freebusy. Some parts show up
190        $blob = <<<ICS
191BEGIN:VCALENDAR
192BEGIN:VFREEBUSY
193FREEBUSY:20110103T010000Z/20110103T020000Z
194FREEBUSY;FBTYPE=FREE:20110103T020000Z/20110103T030000Z
195FREEBUSY:20110103T030000Z/20110103T040000Z,20110103T040000Z/20110103T050000Z
196FREEBUSY:20120101T000000Z/20120101T010000Z
197FREEBUSY:20110103T050000Z/PT1H
198END:VFREEBUSY
199END:VCALENDAR
200ICS;
201
202        $tests[] = array(
203            Reader::read($blob),
204            array(
205                '20110103T010000Z/20110103T020000Z',
206                '20110103T030000Z/20110103T040000Z',
207                '20110103T040000Z/20110103T050000Z',
208                '20110103T050000Z/20110103T060000Z',
209            )
210        );
211
212
213        // Yearly recurrence rule, shows up
214        $blob = <<<ICS
215BEGIN:VCALENDAR
216BEGIN:VEVENT
217UID:foobar13
218DTSTART:20100101T220000Z
219DTEND:20100101T230000Z
220RRULE:FREQ=YEARLY
221END:VEVENT
222END:VCALENDAR
223ICS;
224
225        $tests[] = array(
226            Reader::read($blob),
227            '20110101T220000Z/20110101T230000Z',
228        );
229
230
231        // Yearly recurrence rule + duration, shows up
232        $blob = <<<ICS
233BEGIN:VCALENDAR
234BEGIN:VEVENT
235UID:foobar14
236DTSTART:20100101T230000Z
237DURATION:PT1H
238RRULE:FREQ=YEARLY
239END:VEVENT
240END:VCALENDAR
241ICS;
242
243        $tests[] = array(
244            Reader::read($blob),
245            '20110101T230000Z/20110102T000000Z',
246        );
247
248        // Floating time, no timezone
249        $blob = <<<ICS
250BEGIN:VCALENDAR
251BEGIN:VEVENT
252UID:foobar
253DTSTART:20110101T120000
254DTEND:20110101T130000
255END:VEVENT
256END:VCALENDAR
257ICS;
258
259        $tests[] = array(
260            $blob,
261            "20110101T120000Z/20110101T130000Z"
262        );
263
264        // Floating time + reference timezone
265        $blob = <<<ICS
266BEGIN:VCALENDAR
267BEGIN:VEVENT
268UID:foobar
269DTSTART:20110101T120000
270DTEND:20110101T130000
271END:VEVENT
272END:VCALENDAR
273ICS;
274
275        $tests[] = array(
276            $blob,
277            "20110101T170000Z/20110101T180000Z",
278            new \DateTimeZone('America/Toronto')
279        );
280
281        // All-day event
282        $blob = <<<ICS
283BEGIN:VCALENDAR
284BEGIN:VEVENT
285UID:foobar
286DTSTART;VALUE=DATE:20110101
287END:VEVENT
288END:VCALENDAR
289ICS;
290
291        $tests[] = array(
292            $blob,
293            "20110101T000000Z/20110102T000000Z"
294        );
295
296        // All-day event + reference timezone
297        $blob = <<<ICS
298BEGIN:VCALENDAR
299BEGIN:VEVENT
300UID:foobar
301DTSTART;VALUE=DATE:20110101
302END:VEVENT
303END:VCALENDAR
304ICS;
305
306        $tests[] = array(
307            $blob,
308            "20110101T050000Z/20110102T050000Z",
309            new \DateTimeZone('America/Toronto')
310        );
311
312        // Recurrence rule with no valid instances
313        $blob = <<<ICS
314BEGIN:VCALENDAR
315BEGIN:VEVENT
316UID:foobar
317DTSTART:20110101T100000Z
318DTEND:20110103T120000Z
319RRULE:FREQ=WEEKLY;COUNT=1
320EXDATE:20110101T100000Z
321END:VEVENT
322END:VCALENDAR
323ICS;
324
325        $tests[] = array(
326            $blob,
327            array()
328            );
329        return $tests;
330
331    }
332
333    /**
334     * @dataProvider getInput
335     */
336    function testGenerator($input, $expected, $timeZone = null) {
337
338        $gen = new FreeBusyGenerator(
339            new \DateTime('20110101T110000Z', new \DateTimeZone('UTC')),
340            new \DateTime('20110103T110000Z', new \DateTimeZone('UTC')),
341            $input,
342            $timeZone
343        );
344
345        $result = $gen->getResult();
346
347        $expected = (array)$expected;
348
349        $freebusy = $result->VFREEBUSY->select('FREEBUSY');
350
351        foreach($freebusy as $fb) {
352
353            $this->assertContains((string)$fb, $expected, "$fb did not appear in our list of expected freebusy strings. This is concerning!");
354
355            $k = array_search((string)$fb, $expected);
356            unset($expected[$k]);
357
358        }
359        $this->assertTrue(
360            count($expected) === 0,
361            'There were elements in the expected array that were not found in the output: ' . "\n"  . print_r($expected,true) . "\n" . $result->serialize()
362        );
363
364    }
365
366    function testGeneratorBaseObject() {
367
368        $obj = new Component\VCalendar();
369        $obj->METHOD = 'PUBLISH';
370
371        $gen = new FreeBusyGenerator();
372        $gen->setObjects(array());
373        $gen->setBaseObject($obj);
374
375        $result = $gen->getResult();
376
377        $this->assertEquals('PUBLISH', $result->METHOD->getValue());
378
379    }
380
381    /**
382     * @expectedException InvalidArgumentException
383     */
384    function testInvalidArg() {
385
386        $gen = new FreeBusyGenerator(
387            new \DateTime('2012-01-01'),
388            new \DateTime('2012-12-31'),
389            new \StdClass()
390        );
391
392    }
393
394}
395