1<?php
2
3namespace Sabre\VObject\Property\VCard;
4
5use Sabre\VObject;
6use Sabre\VObject\Reader;
7
8class DateAndOrTimeTest extends \PHPUnit_Framework_TestCase {
9
10    /**
11     * @dataProvider dates
12     */
13    function testGetJsonValue($input, $output) {
14
15        $vcard = new VObject\Component\VCard();
16        $prop = $vcard->createProperty('BDAY', $input);
17
18        $this->assertEquals([$output], $prop->getJsonValue());
19
20    }
21
22    function dates() {
23
24        return [
25            [
26                "19961022T140000",
27                "1996-10-22T14:00:00",
28            ],
29            [
30                "--1022T1400",
31                "--10-22T14:00",
32            ],
33            [
34                "---22T14",
35                "---22T14",
36            ],
37            [
38                "19850412",
39                "1985-04-12",
40            ],
41            [
42                "1985-04",
43                "1985-04",
44            ],
45            [
46                "1985",
47                "1985",
48            ],
49            [
50                "--0412",
51                "--04-12",
52            ],
53            [
54                "T102200",
55                "T10:22:00",
56            ],
57            [
58                "T1022",
59                "T10:22",
60            ],
61            [
62                "T10",
63                "T10",
64            ],
65            [
66                "T-2200",
67                "T-22:00",
68            ],
69            [
70                "T102200Z",
71                "T10:22:00Z",
72            ],
73            [
74                "T102200-0800",
75                "T10:22:00-0800",
76            ],
77            [
78                "T--00",
79                "T--00",
80            ],
81        ];
82
83    }
84
85    function testSetParts() {
86
87        $vcard = new VObject\Component\VCard();
88
89        $prop = $vcard->createProperty('BDAY');
90        $prop->setParts([
91            new \DateTime('2014-04-02 18:37:00')
92        ]);
93
94        $this->assertEquals('20140402T183700Z', $prop->getValue());
95
96    }
97
98    function testSetPartsDateTimeImmutable() {
99
100        $vcard = new VObject\Component\VCard();
101
102        $prop = $vcard->createProperty('BDAY');
103        $prop->setParts([
104            new \DateTimeImmutable('2014-04-02 18:37:00')
105        ]);
106
107        $this->assertEquals('20140402T183700Z', $prop->getValue());
108
109    }
110
111    /**
112     * @expectedException InvalidArgumentException
113     */
114    function testSetPartsTooMany() {
115
116        $vcard = new VObject\Component\VCard();
117
118        $prop = $vcard->createProperty('BDAY');
119        $prop->setParts([
120            1,
121            2
122        ]);
123
124    }
125
126    function testSetPartsString() {
127
128        $vcard = new VObject\Component\VCard();
129
130        $prop = $vcard->createProperty('BDAY');
131        $prop->setParts([
132            "20140402T183700Z"
133        ]);
134
135        $this->assertEquals('20140402T183700Z', $prop->getValue());
136
137    }
138
139    function testSetValueDateTime() {
140
141        $vcard = new VObject\Component\VCard();
142
143        $prop = $vcard->createProperty('BDAY');
144        $prop->setValue(
145            new \DateTime('2014-04-02 18:37:00')
146        );
147
148        $this->assertEquals('20140402T183700Z', $prop->getValue());
149
150    }
151
152    function testSetValueDateTimeImmutable() {
153
154        $vcard = new VObject\Component\VCard();
155
156        $prop = $vcard->createProperty('BDAY');
157        $prop->setValue(
158            new \DateTimeImmutable('2014-04-02 18:37:00')
159        );
160
161        $this->assertEquals('20140402T183700Z', $prop->getValue());
162
163    }
164
165    function testSetDateTimeOffset() {
166
167        $vcard = new VObject\Component\VCard();
168
169        $prop = $vcard->createProperty('BDAY');
170        $prop->setValue(
171            new \DateTime('2014-04-02 18:37:00', new \DateTimeZone('America/Toronto'))
172        );
173
174        $this->assertEquals('20140402T183700-0400', $prop->getValue());
175
176    }
177
178    function testGetDateTime() {
179
180        $datetime = new \DateTime('2014-04-02 18:37:00', new \DateTimeZone('America/Toronto'));
181
182        $vcard = new VObject\Component\VCard();
183        $prop = $vcard->createProperty('BDAY', $datetime);
184
185        $dt = $prop->getDateTime();
186        $this->assertEquals('2014-04-02T18:37:00-04:00', $dt->format('c'), "For some reason this one failed. Current default timezone is: " . date_default_timezone_get());
187
188    }
189
190    function testGetDate() {
191
192        $datetime = new \DateTime('2014-04-02');
193
194        $vcard = new VObject\Component\VCard();
195        $prop = $vcard->createProperty('BDAY', $datetime, null, 'DATE');
196
197        $this->assertEquals('DATE', $prop->getValueType());
198        $this->assertEquals('BDAY:20140402', rtrim($prop->serialize()));
199
200    }
201
202    function testGetDateIncomplete() {
203
204        $datetime = '--0407';
205
206        $vcard = new VObject\Component\VCard();
207        $prop = $vcard->add('BDAY', $datetime);
208
209        $dt = $prop->getDateTime();
210        // Note: if the year changes between the last line and the next line of
211        // code, this test may fail.
212        //
213        // If that happens, head outside and have a drink.
214        $current = new \DateTime('now');
215        $year = $current->format('Y');
216
217        $this->assertEquals($year . '0407', $dt->format('Ymd'));
218
219    }
220
221    function testGetDateIncompleteFromVCard() {
222
223        $vcard = <<<VCF
224BEGIN:VCARD
225VERSION:4.0
226BDAY:--0407
227END:VCARD
228VCF;
229        $vcard = Reader::read($vcard);
230        $prop = $vcard->BDAY;
231
232        $dt = $prop->getDateTime();
233        // Note: if the year changes between the last line and the next line of
234        // code, this test may fail.
235        //
236        // If that happens, head outside and have a drink.
237        $current = new \DateTime('now');
238        $year = $current->format('Y');
239
240        $this->assertEquals($year . '0407', $dt->format('Ymd'));
241
242    }
243
244    function testValidate() {
245
246        $datetime = '--0407';
247
248        $vcard = new VObject\Component\VCard();
249        $prop = $vcard->add('BDAY', $datetime);
250
251        $this->assertEquals([], $prop->validate());
252
253    }
254
255    function testValidateBroken() {
256
257        $datetime = '123';
258
259        $vcard = new VObject\Component\VCard();
260        $prop = $vcard->add('BDAY', $datetime);
261
262        $this->assertEquals([[
263            'level'   => 3,
264            'message' => 'The supplied value (123) is not a correct DATE-AND-OR-TIME property',
265            'node'    => $prop,
266        ]], $prop->validate());
267
268    }
269}
270