3 namespace Sabre\VObject;
5 class ComponentTest extends \PHPUnit_Framework_TestCase {
7 function testIterate() {
9 $comp = new Component('VCALENDAR');
11 $sub = new Component('VEVENT');
12 $comp->children[] = $sub;
14 $sub = new Component('VTODO');
15 $comp->children[] = $sub;
18 foreach($comp->children() as $key=>$subcomponent) {
21 $this->assertInstanceOf('Sabre\\VObject\\Component',$subcomponent);
24 $this->assertEquals(2,$count);
25 $this->assertEquals(1,$key);
29 function testMagicGet() {
31 $comp = new Component('VCALENDAR');
33 $sub = new Component('VEVENT');
34 $comp->children[] = $sub;
36 $sub = new Component('VTODO');
37 $comp->children[] = $sub;
39 $event = $comp->vevent;
40 $this->assertInstanceOf('Sabre\\VObject\\Component', $event);
41 $this->assertEquals('VEVENT', $event->name);
43 $this->assertInternalType('null', $comp->vjournal);
47 function testMagicGetGroups() {
49 $comp = new Component('VCARD');
51 $sub = new Property('GROUP1.EMAIL','1@1.com');
52 $comp->children[] = $sub;
54 $sub = new Property('GROUP2.EMAIL','2@2.com');
55 $comp->children[] = $sub;
57 $sub = new Property('EMAIL','3@3.com');
58 $comp->children[] = $sub;
60 $emails = $comp->email;
61 $this->assertEquals(3, count($emails));
63 $email1 = $comp->{"group1.email"};
64 $this->assertEquals('EMAIL', $email1[0]->name);
65 $this->assertEquals('GROUP1', $email1[0]->group);
67 $email3 = $comp->{".email"};
68 $this->assertEquals('EMAIL', $email3[0]->name);
69 $this->assertEquals(null, $email3[0]->group);
73 function testMagicIsset() {
75 $comp = new Component('VCALENDAR');
77 $sub = new Component('VEVENT');
78 $comp->children[] = $sub;
80 $sub = new Component('VTODO');
81 $comp->children[] = $sub;
83 $this->assertTrue(isset($comp->vevent));
84 $this->assertTrue(isset($comp->vtodo));
85 $this->assertFalse(isset($comp->vjournal));
89 function testMagicSetScalar() {
91 $comp = new Component('VCALENDAR');
92 $comp->myProp = 'myValue';
94 $this->assertInstanceOf('Sabre\\VObject\\Property',$comp->MYPROP);
95 $this->assertEquals('myValue',$comp->MYPROP->value);
100 function testMagicSetScalarTwice() {
102 $comp = new Component('VCALENDAR');
103 $comp->myProp = 'myValue';
104 $comp->myProp = 'myValue';
106 $this->assertEquals(1,count($comp->children));
107 $this->assertInstanceOf('Sabre\\VObject\\Property',$comp->MYPROP);
108 $this->assertEquals('myValue',$comp->MYPROP->value);
112 function testMagicSetComponent() {
114 $comp = new Component('VCALENDAR');
116 // Note that 'myProp' is ignored here.
117 $comp->myProp = new Component('VEVENT');
119 $this->assertEquals(1, count($comp->children));
121 $this->assertEquals('VEVENT',$comp->VEVENT->name);
125 function testMagicSetTwice() {
127 $comp = new Component('VCALENDAR');
129 $comp->VEVENT = new Component('VEVENT');
130 $comp->VEVENT = new Component('VEVENT');
132 $this->assertEquals(1, count($comp->children));
134 $this->assertEquals('VEVENT',$comp->VEVENT->name);
138 function testArrayAccessGet() {
140 $comp = new Component('VCALENDAR');
142 $event = new Component('VEVENT');
143 $event->summary = 'Event 1';
147 $event2 = clone $event;
148 $event2->summary = 'Event 2';
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);
158 function testArrayAccessExists() {
160 $comp = new Component('VCALENDAR');
162 $event = new Component('VEVENT');
163 $event->summary = 'Event 1';
167 $event2 = clone $event;
168 $event2->summary = 'Event 2';
172 $this->assertTrue(isset($comp->vevent[0]));
173 $this->assertTrue(isset($comp->vevent[1]));
178 * @expectedException LogicException
180 function testArrayAccessSet() {
182 $comp = new Component('VCALENDAR');
183 $comp['hey'] = 'hi there';
187 * @expectedException LogicException
189 function testArrayAccessUnset() {
191 $comp = new Component('VCALENDAR');
196 function testAddScalar() {
198 $comp = new Component('VCALENDAR');
200 $comp->add('myprop','value');
202 $this->assertEquals(1, count($comp->children));
204 $this->assertTrue($comp->children[0] instanceof Property);
205 $this->assertEquals('MYPROP',$comp->children[0]->name);
206 $this->assertEquals('value',$comp->children[0]->value);
210 function testAddScalarParams() {
212 $comp = Component::create('VCALENDAR');
214 $comp->add('myprop','value',array('param1'=>'value1'));
216 $this->assertEquals(1, count($comp->children));
218 $this->assertTrue($comp->children[0] instanceof Property);
219 $this->assertEquals('MYPROP',$comp->children[0]->name);
220 $this->assertEquals('value',$comp->children[0]->value);
222 $this->assertEquals(1, count($comp->children[0]->parameters));
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);
231 function testAddComponent() {
233 $comp = new Component('VCALENDAR');
235 $comp->add(new Component('VEVENT'));
237 $this->assertEquals(1, count($comp->children));
239 $this->assertEquals('VEVENT',$comp->VEVENT->name);
243 function testAddComponentTwice() {
245 $comp = new Component('VCALENDAR');
247 $comp->add(new Component('VEVENT'));
248 $comp->add(new Component('VEVENT'));
250 $this->assertEquals(2, count($comp->children));
252 $this->assertEquals('VEVENT',$comp->VEVENT->name);
257 * @expectedException InvalidArgumentException
259 function testAddArgFail() {
261 $comp = new Component('VCALENDAR');
262 $comp->add(new Component('VEVENT'),'hello');
267 * @expectedException InvalidArgumentException
269 function testAddArgFail2() {
271 $comp = new Component('VCALENDAR');
277 * @expectedException InvalidArgumentException
279 function testAddArgFail3() {
281 $comp = new Component('VCALENDAR');
282 $comp->add('hello',array());
287 * @expectedException InvalidArgumentException
289 function testMagicSetInvalid() {
291 $comp = new Component('VCALENDAR');
293 // Note that 'myProp' is ignored here.
294 $comp->myProp = new \StdClass();
296 $this->assertEquals(1, count($comp->children));
298 $this->assertEquals('VEVENT',$comp->VEVENT->name);
302 function testMagicUnset() {
304 $comp = new Component('VCALENDAR');
305 $comp->add(new Component('VEVENT'));
307 unset($comp->vevent);
309 $this->assertEquals(array(), $comp->children);
314 function testCount() {
316 $comp = new Component('VCALENDAR');
317 $this->assertEquals(1,$comp->count());
321 function testChildren() {
323 $comp = new Component('VCALENDAR');
325 // Note that 'myProp' is ignored here.
326 $comp->children = array(
327 new Component('VEVENT'),
328 new Component('VTODO')
331 $r = $comp->children();
332 $this->assertTrue($r instanceof ElementList);
333 $this->assertEquals(2,count($r));
336 function testGetComponents() {
338 $comp = new Component('VCALENDAR');
340 // Note that 'myProp' is ignored here.
341 $comp->children = array(
342 new Property('FOO','BAR'),
343 new Component('VTODO')
346 $r = $comp->getComponents();
347 $this->assertInternalType('array', $r);
348 $this->assertEquals(1, count($r));
349 $this->assertEquals('VTODO', $r[0]->name);
352 function testSerialize() {
354 $comp = new Component('VCALENDAR');
355 $this->assertEquals("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n", $comp->serialize());
359 function testSerializeChildren() {
361 $comp = new Component('VCALENDAR');
362 $comp->children = array(
363 new Component('VEVENT'),
364 new Component('VTODO')
367 $str = $comp->serialize();
369 $this->assertEquals("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n", $str);
373 function testSerializeOrderCompAndProp() {
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'));
381 $str = $comp->serialize();
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);
387 function testAnotherSerializeOrderProp() {
389 $prop4s=array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
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);
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');
407 $str = $comp->serialize();
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);