]> git.mxchange.org Git - friendica-addons.git/blob - dav/sabre-vobject/tests/Sabre/VObject/ComponentTest.php
Second part of refactoring; should be runnable again, yet not thoroughly tested
[friendica-addons.git] / dav / sabre-vobject / tests / Sabre / VObject / ComponentTest.php
1 <?php
2
3 namespace Sabre\VObject;
4
5 class ComponentTest extends \PHPUnit_Framework_TestCase {
6
7     function testIterate() {
8
9         $comp = new Component('VCALENDAR');
10
11         $sub = new Component('VEVENT');
12         $comp->children[] = $sub;
13
14         $sub = new Component('VTODO');
15         $comp->children[] = $sub;
16
17         $count = 0;
18         foreach($comp->children() as $key=>$subcomponent) {
19
20            $count++;
21            $this->assertInstanceOf('Sabre\\VObject\\Component',$subcomponent);
22
23         }
24         $this->assertEquals(2,$count);
25         $this->assertEquals(1,$key);
26
27     }
28
29     function testMagicGet() {
30
31         $comp = new Component('VCALENDAR');
32
33         $sub = new Component('VEVENT');
34         $comp->children[] = $sub;
35
36         $sub = new Component('VTODO');
37         $comp->children[] = $sub;
38
39         $event = $comp->vevent;
40         $this->assertInstanceOf('Sabre\\VObject\\Component', $event);
41         $this->assertEquals('VEVENT', $event->name);
42
43         $this->assertInternalType('null', $comp->vjournal);
44
45     }
46
47     function testMagicGetGroups() {
48
49         $comp = new Component('VCARD');
50
51         $sub = new Property('GROUP1.EMAIL','1@1.com');
52         $comp->children[] = $sub;
53
54         $sub = new Property('GROUP2.EMAIL','2@2.com');
55         $comp->children[] = $sub;
56
57         $sub = new Property('EMAIL','3@3.com');
58         $comp->children[] = $sub;
59
60         $emails = $comp->email;
61         $this->assertEquals(3, count($emails));
62
63         $email1 = $comp->{"group1.email"};
64         $this->assertEquals('EMAIL', $email1[0]->name);
65         $this->assertEquals('GROUP1', $email1[0]->group);
66
67         $email3 = $comp->{".email"};
68         $this->assertEquals('EMAIL', $email3[0]->name);
69         $this->assertEquals(null, $email3[0]->group);
70
71     }
72
73     function testMagicIsset() {
74
75         $comp = new Component('VCALENDAR');
76
77         $sub = new Component('VEVENT');
78         $comp->children[] = $sub;
79
80         $sub = new Component('VTODO');
81         $comp->children[] = $sub;
82
83         $this->assertTrue(isset($comp->vevent));
84         $this->assertTrue(isset($comp->vtodo));
85         $this->assertFalse(isset($comp->vjournal));
86
87     }
88
89     function testMagicSetScalar() {
90
91         $comp = new Component('VCALENDAR');
92         $comp->myProp = 'myValue';
93
94         $this->assertInstanceOf('Sabre\\VObject\\Property',$comp->MYPROP);
95         $this->assertEquals('myValue',$comp->MYPROP->value);
96
97
98     }
99
100     function testMagicSetScalarTwice() {
101
102         $comp = new Component('VCALENDAR');
103         $comp->myProp = 'myValue';
104         $comp->myProp = 'myValue';
105
106         $this->assertEquals(1,count($comp->children));
107         $this->assertInstanceOf('Sabre\\VObject\\Property',$comp->MYPROP);
108         $this->assertEquals('myValue',$comp->MYPROP->value);
109
110     }
111
112     function testMagicSetComponent() {
113
114         $comp = new Component('VCALENDAR');
115
116         // Note that 'myProp' is ignored here.
117         $comp->myProp = new Component('VEVENT');
118
119         $this->assertEquals(1, count($comp->children));
120
121         $this->assertEquals('VEVENT',$comp->VEVENT->name);
122
123     }
124
125     function testMagicSetTwice() {
126
127         $comp = new Component('VCALENDAR');
128
129         $comp->VEVENT = new Component('VEVENT');
130         $comp->VEVENT = new Component('VEVENT');
131
132         $this->assertEquals(1, count($comp->children));
133
134         $this->assertEquals('VEVENT',$comp->VEVENT->name);
135
136     }
137
138     function testArrayAccessGet() {
139
140         $comp = new Component('VCALENDAR');
141
142         $event = new Component('VEVENT');
143         $event->summary = 'Event 1';
144
145         $comp->add($event);
146
147         $event2 = clone $event;
148         $event2->summary = 'Event 2';
149
150         $comp->add($event2);
151
152         $this->assertEquals(2,count($comp->children()));
153         $this->assertTrue($comp->vevent[1] instanceof Component);
154         $this->assertEquals('Event 2', (string)$comp->vevent[1]->summary);
155
156     }
157
158     function testArrayAccessExists() {
159
160         $comp = new Component('VCALENDAR');
161
162         $event = new Component('VEVENT');
163         $event->summary = 'Event 1';
164
165         $comp->add($event);
166
167         $event2 = clone $event;
168         $event2->summary = 'Event 2';
169
170         $comp->add($event2);
171
172         $this->assertTrue(isset($comp->vevent[0]));
173         $this->assertTrue(isset($comp->vevent[1]));
174
175     }
176
177     /**
178      * @expectedException LogicException
179      */
180     function testArrayAccessSet() {
181
182         $comp = new Component('VCALENDAR');
183         $comp['hey'] = 'hi there';
184
185     }
186     /**
187      * @expectedException LogicException
188      */
189     function testArrayAccessUnset() {
190
191         $comp = new Component('VCALENDAR');
192         unset($comp[0]);
193
194     }
195
196     function testAddScalar() {
197
198         $comp = new Component('VCALENDAR');
199
200         $comp->add('myprop','value');
201
202         $this->assertEquals(1, count($comp->children));
203
204         $this->assertTrue($comp->children[0] instanceof Property);
205         $this->assertEquals('MYPROP',$comp->children[0]->name);
206         $this->assertEquals('value',$comp->children[0]->value);
207
208     }
209
210     function testAddScalarParams() {
211
212         $comp = Component::create('VCALENDAR');
213
214         $comp->add('myprop','value',array('param1'=>'value1'));
215
216         $this->assertEquals(1, count($comp->children));
217
218         $this->assertTrue($comp->children[0] instanceof Property);
219         $this->assertEquals('MYPROP',$comp->children[0]->name);
220         $this->assertEquals('value',$comp->children[0]->value);
221
222         $this->assertEquals(1, count($comp->children[0]->parameters));
223
224         $this->assertTrue($comp->children[0]->parameters[0] instanceof Parameter);
225         $this->assertEquals('PARAM1',$comp->children[0]->parameters[0]->name);
226         $this->assertEquals('value1',$comp->children[0]->parameters[0]->value);
227
228     }
229
230
231     function testAddComponent() {
232
233         $comp = new Component('VCALENDAR');
234
235         $comp->add(new Component('VEVENT'));
236
237         $this->assertEquals(1, count($comp->children));
238
239         $this->assertEquals('VEVENT',$comp->VEVENT->name);
240
241     }
242
243     function testAddComponentTwice() {
244
245         $comp = new Component('VCALENDAR');
246
247         $comp->add(new Component('VEVENT'));
248         $comp->add(new Component('VEVENT'));
249
250         $this->assertEquals(2, count($comp->children));
251
252         $this->assertEquals('VEVENT',$comp->VEVENT->name);
253
254     }
255
256     /**
257      * @expectedException InvalidArgumentException
258      */
259     function testAddArgFail() {
260
261         $comp = new Component('VCALENDAR');
262         $comp->add(new Component('VEVENT'),'hello');
263
264     }
265
266     /**
267      * @expectedException InvalidArgumentException
268      */
269     function testAddArgFail2() {
270
271         $comp = new Component('VCALENDAR');
272         $comp->add(array());
273
274     }
275
276     /**
277      * @expectedException InvalidArgumentException
278      */
279     function testAddArgFail3() {
280
281         $comp = new Component('VCALENDAR');
282         $comp->add('hello',array());
283
284     }
285
286     /**
287      * @expectedException InvalidArgumentException
288      */
289     function testMagicSetInvalid() {
290
291         $comp = new Component('VCALENDAR');
292
293         // Note that 'myProp' is ignored here.
294         $comp->myProp = new \StdClass();
295
296         $this->assertEquals(1, count($comp->children));
297
298         $this->assertEquals('VEVENT',$comp->VEVENT->name);
299
300     }
301
302     function testMagicUnset() {
303
304         $comp = new Component('VCALENDAR');
305         $comp->add(new Component('VEVENT'));
306
307         unset($comp->vevent);
308
309         $this->assertEquals(array(), $comp->children);
310
311     }
312
313
314     function testCount() {
315
316         $comp = new Component('VCALENDAR');
317         $this->assertEquals(1,$comp->count());
318
319     }
320
321     function testChildren() {
322
323         $comp = new Component('VCALENDAR');
324
325         // Note that 'myProp' is ignored here.
326         $comp->children = array(
327             new Component('VEVENT'),
328             new Component('VTODO')
329         );
330
331         $r = $comp->children();
332         $this->assertTrue($r instanceof ElementList);
333         $this->assertEquals(2,count($r));
334     }
335
336     function testGetComponents() {
337
338         $comp = new Component('VCALENDAR');
339
340         // Note that 'myProp' is ignored here.
341         $comp->children = array(
342             new Property('FOO','BAR'),
343             new Component('VTODO')
344         );
345
346         $r = $comp->getComponents();
347         $this->assertInternalType('array', $r);
348         $this->assertEquals(1, count($r));
349         $this->assertEquals('VTODO', $r[0]->name);
350     }
351
352     function testSerialize() {
353
354         $comp = new Component('VCALENDAR');
355         $this->assertEquals("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n", $comp->serialize());
356
357     }
358
359     function testSerializeChildren() {
360
361         $comp = new Component('VCALENDAR');
362         $comp->children = array(
363             new Component('VEVENT'),
364             new Component('VTODO')
365         );
366
367         $str = $comp->serialize();
368
369         $this->assertEquals("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n", $str);
370
371     }
372
373     function testSerializeOrderCompAndProp() {
374         
375         $comp = new Component('VCALENDAR');
376         $comp->add(new Component('VEVENT'));
377         $comp->add('PROP1','BLABLA');
378         $comp->add('VERSION','2.0');
379         $comp->add(new Component('VTIMEZONE'));
380
381         $str = $comp->serialize();
382
383         $this->assertEquals("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPROP1:BLABLA\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", $str);
384
385     }
386
387     function testAnotherSerializeOrderProp() {
388
389         $prop4s=array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
390
391         $comp = new Component('VCARD');
392         $comp->__set('SOMEPROP','FOO');
393         $comp->__set('ANOTHERPROP','FOO');
394         $comp->__set('THIRDPROP','FOO');
395         foreach ($prop4s as $prop4) {
396             $comp->add('PROP4', 'FOO '.$prop4);
397         }
398         $comp->__set('PROPNUMBERFIVE', 'FOO');
399         $comp->__set('PROPNUMBERSIX', 'FOO');
400         $comp->__set('PROPNUMBERSEVEN', 'FOO');
401         $comp->__set('PROPNUMBEREIGHT', 'FOO');
402         $comp->__set('PROPNUMBERNINE', 'FOO');
403         $comp->__set('PROPNUMBERTEN', 'FOO');
404         $comp->__set('VERSION','2.0');
405         $comp->__set('UID', 'FOO');
406
407         $str = $comp->serialize();
408
409         $this->assertEquals("BEGIN:VCARD\r\nVERSION:2.0\r\nSOMEPROP:FOO\r\nANOTHERPROP:FOO\r\nTHIRDPROP:FOO\r\nPROP4:FOO 1\r\nPROP4:FOO 2\r\nPROP4:FOO 3\r\nPROP4:FOO 4\r\nPROP4:FOO 5\r\nPROP4:FOO 6\r\nPROP4:FOO 7\r\nPROP4:FOO 8\r\nPROP4:FOO 9\r\nPROP4:FOO 10\r\nPROPNUMBERFIVE:FOO\r\nPROPNUMBERSIX:FOO\r\nPROPNUMBERSEVEN:FOO\r\nPROPNUMBEREIGHT:FOO\r\nPROPNUMBERNINE:FOO\r\nPROPNUMBERTEN:FOO\r\nUID:FOO\r\nEND:VCARD\r\n", $str);
410
411     }
412
413 }