2012-06-03 14:19:28 -04:00
|
|
|
<?php
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
namespace Sabre\VObject;
|
|
|
|
|
|
|
|
class ComponentTest extends \PHPUnit_Framework_TestCase {
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
function testIterate() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$sub = new Component('VEVENT');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children[] = $sub;
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$sub = new Component('VTODO');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children[] = $sub;
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
foreach($comp->children() as $key=>$subcomponent) {
|
|
|
|
|
|
|
|
$count++;
|
2012-08-11 04:07:19 -04:00
|
|
|
$this->assertInstanceOf('Sabre\\VObject\\Component',$subcomponent);
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
$this->assertEquals(2,$count);
|
|
|
|
$this->assertEquals(1,$key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMagicGet() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$sub = new Component('VEVENT');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children[] = $sub;
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$sub = new Component('VTODO');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children[] = $sub;
|
|
|
|
|
|
|
|
$event = $comp->vevent;
|
2012-08-11 04:07:19 -04:00
|
|
|
$this->assertInstanceOf('Sabre\\VObject\\Component', $event);
|
2012-06-03 14:19:28 -04:00
|
|
|
$this->assertEquals('VEVENT', $event->name);
|
|
|
|
|
|
|
|
$this->assertInternalType('null', $comp->vjournal);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMagicGetGroups() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCARD');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$sub = new Property('GROUP1.EMAIL','1@1.com');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children[] = $sub;
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$sub = new Property('GROUP2.EMAIL','2@2.com');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children[] = $sub;
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$sub = new Property('EMAIL','3@3.com');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children[] = $sub;
|
|
|
|
|
|
|
|
$emails = $comp->email;
|
|
|
|
$this->assertEquals(3, count($emails));
|
|
|
|
|
|
|
|
$email1 = $comp->{"group1.email"};
|
|
|
|
$this->assertEquals('EMAIL', $email1[0]->name);
|
|
|
|
$this->assertEquals('GROUP1', $email1[0]->group);
|
|
|
|
|
|
|
|
$email3 = $comp->{".email"};
|
|
|
|
$this->assertEquals('EMAIL', $email3[0]->name);
|
|
|
|
$this->assertEquals(null, $email3[0]->group);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMagicIsset() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$sub = new Component('VEVENT');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children[] = $sub;
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$sub = new Component('VTODO');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children[] = $sub;
|
|
|
|
|
|
|
|
$this->assertTrue(isset($comp->vevent));
|
|
|
|
$this->assertTrue(isset($comp->vtodo));
|
|
|
|
$this->assertFalse(isset($comp->vjournal));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMagicSetScalar() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->myProp = 'myValue';
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$this->assertInstanceOf('Sabre\\VObject\\Property',$comp->MYPROP);
|
2012-06-03 14:19:28 -04:00
|
|
|
$this->assertEquals('myValue',$comp->MYPROP->value);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMagicSetScalarTwice() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->myProp = 'myValue';
|
|
|
|
$comp->myProp = 'myValue';
|
|
|
|
|
|
|
|
$this->assertEquals(1,count($comp->children));
|
2012-08-11 04:07:19 -04:00
|
|
|
$this->assertInstanceOf('Sabre\\VObject\\Property',$comp->MYPROP);
|
2012-06-03 14:19:28 -04:00
|
|
|
$this->assertEquals('myValue',$comp->MYPROP->value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMagicSetComponent() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
// Note that 'myProp' is ignored here.
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp->myProp = new Component('VEVENT');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$this->assertEquals(1, count($comp->children));
|
|
|
|
|
|
|
|
$this->assertEquals('VEVENT',$comp->VEVENT->name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMagicSetTwice() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp->VEVENT = new Component('VEVENT');
|
|
|
|
$comp->VEVENT = new Component('VEVENT');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$this->assertEquals(1, count($comp->children));
|
|
|
|
|
|
|
|
$this->assertEquals('VEVENT',$comp->VEVENT->name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testArrayAccessGet() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$event = new Component('VEVENT');
|
2012-06-03 14:19:28 -04:00
|
|
|
$event->summary = 'Event 1';
|
|
|
|
|
|
|
|
$comp->add($event);
|
|
|
|
|
|
|
|
$event2 = clone $event;
|
|
|
|
$event2->summary = 'Event 2';
|
|
|
|
|
|
|
|
$comp->add($event2);
|
|
|
|
|
|
|
|
$this->assertEquals(2,count($comp->children()));
|
2012-08-11 04:07:19 -04:00
|
|
|
$this->assertTrue($comp->vevent[1] instanceof Component);
|
2012-06-03 14:19:28 -04:00
|
|
|
$this->assertEquals('Event 2', (string)$comp->vevent[1]->summary);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testArrayAccessExists() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$event = new Component('VEVENT');
|
2012-06-03 14:19:28 -04:00
|
|
|
$event->summary = 'Event 1';
|
|
|
|
|
|
|
|
$comp->add($event);
|
|
|
|
|
|
|
|
$event2 = clone $event;
|
|
|
|
$event2->summary = 'Event 2';
|
|
|
|
|
|
|
|
$comp->add($event2);
|
|
|
|
|
|
|
|
$this->assertTrue(isset($comp->vevent[0]));
|
|
|
|
$this->assertTrue(isset($comp->vevent[1]));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException LogicException
|
|
|
|
*/
|
|
|
|
function testArrayAccessSet() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp['hey'] = 'hi there';
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @expectedException LogicException
|
|
|
|
*/
|
|
|
|
function testArrayAccessUnset() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
unset($comp[0]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testAddScalar() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$comp->add('myprop','value');
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($comp->children));
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$this->assertTrue($comp->children[0] instanceof Property);
|
|
|
|
$this->assertEquals('MYPROP',$comp->children[0]->name);
|
|
|
|
$this->assertEquals('value',$comp->children[0]->value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testAddScalarParams() {
|
|
|
|
|
|
|
|
$comp = Component::create('VCALENDAR');
|
|
|
|
|
|
|
|
$comp->add('myprop','value',array('param1'=>'value1'));
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($comp->children));
|
|
|
|
|
|
|
|
$this->assertTrue($comp->children[0] instanceof Property);
|
2012-06-03 14:19:28 -04:00
|
|
|
$this->assertEquals('MYPROP',$comp->children[0]->name);
|
|
|
|
$this->assertEquals('value',$comp->children[0]->value);
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$this->assertEquals(1, count($comp->children[0]->parameters));
|
|
|
|
|
|
|
|
$this->assertTrue($comp->children[0]->parameters[0] instanceof Parameter);
|
|
|
|
$this->assertEquals('PARAM1',$comp->children[0]->parameters[0]->name);
|
|
|
|
$this->assertEquals('value1',$comp->children[0]->parameters[0]->value);
|
|
|
|
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
|
2012-06-03 14:19:28 -04:00
|
|
|
function testAddComponent() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp->add(new Component('VEVENT'));
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$this->assertEquals(1, count($comp->children));
|
|
|
|
|
|
|
|
$this->assertEquals('VEVENT',$comp->VEVENT->name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testAddComponentTwice() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp->add(new Component('VEVENT'));
|
|
|
|
$comp->add(new Component('VEVENT'));
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$this->assertEquals(2, count($comp->children));
|
|
|
|
|
|
|
|
$this->assertEquals('VEVENT',$comp->VEVENT->name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function testAddArgFail() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
|
|
|
$comp->add(new Component('VEVENT'),'hello');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function testAddArgFail2() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->add(array());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function testAddArgFail3() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->add('hello',array());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function testMagicSetInvalid() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
// Note that 'myProp' is ignored here.
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp->myProp = new \StdClass();
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$this->assertEquals(1, count($comp->children));
|
|
|
|
|
|
|
|
$this->assertEquals('VEVENT',$comp->VEVENT->name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMagicUnset() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
|
|
|
$comp->add(new Component('VEVENT'));
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
unset($comp->vevent);
|
|
|
|
|
|
|
|
$this->assertEquals(array(), $comp->children);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testCount() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
$this->assertEquals(1,$comp->count());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testChildren() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
// Note that 'myProp' is ignored here.
|
|
|
|
$comp->children = array(
|
2012-08-11 04:07:19 -04:00
|
|
|
new Component('VEVENT'),
|
|
|
|
new Component('VTODO')
|
2012-06-03 14:19:28 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
$r = $comp->children();
|
2012-08-11 04:07:19 -04:00
|
|
|
$this->assertTrue($r instanceof ElementList);
|
2012-06-03 14:19:28 -04:00
|
|
|
$this->assertEquals(2,count($r));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetComponents() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
// Note that 'myProp' is ignored here.
|
|
|
|
$comp->children = array(
|
2012-08-11 04:07:19 -04:00
|
|
|
new Property('FOO','BAR'),
|
|
|
|
new Component('VTODO')
|
2012-06-03 14:19:28 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
$r = $comp->getComponents();
|
|
|
|
$this->assertInternalType('array', $r);
|
|
|
|
$this->assertEquals(1, count($r));
|
|
|
|
$this->assertEquals('VTODO', $r[0]->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSerialize() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
$this->assertEquals("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n", $comp->serialize());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSerializeChildren() {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->children = array(
|
2012-08-11 04:07:19 -04:00
|
|
|
new Component('VEVENT'),
|
|
|
|
new Component('VTODO')
|
2012-06-03 14:19:28 -04:00
|
|
|
);
|
2012-07-23 17:15:47 -04:00
|
|
|
|
|
|
|
$str = $comp->serialize();
|
|
|
|
|
|
|
|
$this->assertEquals("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n", $str);
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-23 17:15:47 -04:00
|
|
|
function testSerializeOrderCompAndProp() {
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCALENDAR');
|
|
|
|
$comp->add(new Component('VEVENT'));
|
2012-06-03 14:19:28 -04:00
|
|
|
$comp->add('PROP1','BLABLA');
|
|
|
|
$comp->add('VERSION','2.0');
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp->add(new Component('VTIMEZONE'));
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$str = $comp->serialize();
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-23 17:15:47 -04:00
|
|
|
function testAnotherSerializeOrderProp() {
|
|
|
|
|
|
|
|
$prop4s=array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$comp = new Component('VCARD');
|
2012-07-23 17:15:47 -04:00
|
|
|
$comp->__set('SOMEPROP','FOO');
|
|
|
|
$comp->__set('ANOTHERPROP','FOO');
|
|
|
|
$comp->__set('THIRDPROP','FOO');
|
|
|
|
foreach ($prop4s as $prop4) {
|
|
|
|
$comp->add('PROP4', 'FOO '.$prop4);
|
|
|
|
}
|
|
|
|
$comp->__set('PROPNUMBERFIVE', 'FOO');
|
|
|
|
$comp->__set('PROPNUMBERSIX', 'FOO');
|
|
|
|
$comp->__set('PROPNUMBERSEVEN', 'FOO');
|
|
|
|
$comp->__set('PROPNUMBEREIGHT', 'FOO');
|
|
|
|
$comp->__set('PROPNUMBERNINE', 'FOO');
|
|
|
|
$comp->__set('PROPNUMBERTEN', 'FOO');
|
|
|
|
$comp->__set('VERSION','2.0');
|
|
|
|
$comp->__set('UID', 'FOO');
|
|
|
|
|
|
|
|
$str = $comp->serialize();
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|