Our coding style omits the PHP closing tag at EOF
[awl.git] / tests / myComponentTest.php
blob4a427dba0ce06ef8c466a453498a4818e9521221
1 <?php
2 /**
3 * Created by JetBrains PhpStorm.
4 * User: milan
5 * Date: 7/5/13
6 * Time: 2:01 PM
7 * To change this template use File | Settings | File Templates.
8 */
9 require_once 'inc/vComponent.php';
11 class vComponentTest extends PHPUnit_Framework_TestCase {
12 public $testdata = <<<EOBODY1
13 BEGIN:VCALENDAR
14 PRODID:-//davical.org//NONSGML AWL Calendar//EN
15 VERSION:2.0
16 CALSCALE:GREGORIAN
17 EOBODY1;
19 public $simpledata1 = <<<EOSIMPLEDATA1
20 BEGIN:VCALENDAR
21 SUMARY:12234567890122345678901223456789012234567890122345678901223467890
22 122345678901223456789012
23 END:VCALENDAR
24 EOSIMPLEDATA1;
26 protected function setUp()
28 //$this->markTestIncomplete( );
31 function testParseComponents() {
33 // $lines = new HeapLines((new bigtestdata)->testdata2);
34 $data = $this->getData('tests/data/tzid_data.test');
35 $component = new vComponent($data);
37 $this->assertEquals(8, $component->ComponentCount());
38 $type = $component->GetType();
39 $this->assertStringStartsWith('VCALENDAR', $type );
40 $child = $component->getComponentAt(0);
41 $this->assertNotNull($child);
43 $childtype = $child->GetType();
45 $this->assertStringStartsWith('VEVENT', $childtype );
46 $this->assertEquals(1, $child->ComponentCount());
50 function testParseProperties(){
51 //$lines = new HeapLines((new bigtestdata)->testdata2);
52 $data = $this->getData('tests/data/inner_valarmdata.test');
53 $component = new vComponent($data);
54 //$component = new vComponent();
56 $this->assertEquals(4, $component->propertiesCount());
57 $property1 = $component->getPropertyAt(0);
58 $this->assertNotNull($property1);
62 function testGetNameProperties(){
63 //$lines = new HeapLines((new bigtestdata)->testdata2);
64 $data = $this->getData('tests/data/inner_valarmdata.test');
65 $component = new vComponent($data);
67 $this->assertEquals(4, $component->propertiesCount());
70 function te1stGetNameProperties2(){
71 $lines = new HeapLines((new bigtestdata)->data);
72 for($i = 0; $i < 100; $i++)
73 $component = new vComponent($lines);
75 //$this->assertEquals(4, $component->propertiesCount());
78 function testCollectParameterValues(){
79 // /$lines = new HeapLines((new bigtestdata)->testdata2);
80 $data = $this->getData('tests/data/tzid_data.test');
81 $component = new vComponent($data);
83 $parameters = $component->CollectParameterValues('TZID');
85 $this->equalTo(4, count($parameters));
86 $this->assertArrayHasKey('America/Los_Angeles', $parameters );
89 function testGetProperty(){
90 //$lines = new HeapLines((new bigtestdata)->testdata2);
91 $data = $this->getData('tests/data/inner_valarmdata.test');
92 $component = new vComponent($data);
94 $property = $component->GetProperty("VERSION");
95 $this->assertNotNull($property);
96 $this->assertInstanceOf('vProperty', $property);
99 function testGetPropertyAndChange(){
100 //$lines = new HeapLines((new bigtestdata)->testdata2);
101 $data = $this->getData('tests/data/inner_valarmdata.test');
102 $component = new vComponent($data);
103 $this->assertTrue($component->isValid());
104 $property = $component->GetProperty("VERSION");
105 $this->assertTrue($component->isValid());
106 $this->assertNotNull($property);
107 $this->assertInstanceOf('vProperty', $property);
108 $this->assertTrue($component->isValid());
109 $property->Name("ahoj");
110 $this->assertFalse($component->isValid());
114 function testGetNestedPropertyAndChange(){
115 //$lines = new HeapLines((new bigtestdata)->testdata2);
116 $data = $this->getData('tests/data/inner_valarmdata.test');
117 $component = new vComponent($data);
119 $nestedComponent = $component->getComponentAt(0);
120 $nestedProperty = $nestedComponent->getPropertyAt(0);
122 $name = $nestedProperty->Name();
123 $this->assertTrue($component->isValid());
124 $nestedProperty->Name($name . "TEST");
125 $this->assertFalse($component->isValid());
130 function testGetPValue(){
131 // /$lines = new HeapLines((new bigtestdata)->testdata2);
132 $data = $this->getData('tests/data/inner_valarmdata.test');
133 $component = new vComponent($data);
135 $property = $component->GetPValue("VERSION");
136 $this->assertNotNull($property);
137 $this->assertStringStartsWith('2.0', $property);
140 function testGetPropertiesWithName(){
141 //$lines = new HeapLines((new bigtestdata)->testdata2);
142 $data = $this->getData('tests/data/inner_valarmdata.test');
143 $component = new vComponent($data);
145 $properties = $component->GetProperties("VERSION");
146 $this->assertNotNull($properties);
147 $this->assertCount(1, $properties);
148 $this->assertStringStartsWith('2.0', $properties[0]->Value());
151 function testGetPropertiesWithArray(){
152 //$lines = new HeapLines((new bigtestdata)->testdata2);
153 $data = $this->getData('tests/data/inner_valarmdata.test');
154 $component = new vComponent($data);
156 $properties = $component->GetProperties(["VERSION" => true, "CALSCALE" => true]);
157 $this->assertNotNull($properties);
158 $this->assertCount(2, $properties);
159 $this->assertStringStartsWith('2.0', $properties[0]->Value());
162 function testGetPropertiesNoParam(){
163 //$lines = new HeapLines((new bigtestdata)->testdata2);
164 $data = $this->getData('tests/data/inner_valarmdata.test');
165 $component = new vComponent($data);
167 $properties = $component->GetProperties();
168 $this->assertNotNull($properties);
169 $this->assertCount(4, $properties);
170 //$this->assertStringStartsWith('2.0', $properties[0]->Value());
175 function testClearPropertiesNoParam(){
176 //$lines = new HeapLines((new bigtestdata)->testdata2);
177 $data = $this->getData('tests/data/inner_valarmdata.test');
178 $component = new vComponent($data);
180 $this->assertEquals(4, $component->propertiesCount());
181 $component->ClearProperties();
182 $this->assertEquals(0, $component->propertiesCount());
183 //$this->assertStringStartsWith('2.0', $properties[0]->Value());
186 function testClearPropertiesStringParam(){
187 //$lines = new HeapLines((new bigtestdata)->testdata2);
188 $data = $this->getData('tests/data/inner_valarmdata.test');
189 $component = new vComponent($data);
191 $this->assertEquals(4, $component->propertiesCount());
192 $component->ClearProperties('VERSION');
193 $this->assertEquals(3, $component->propertiesCount());
194 //$this->assertStringStartsWith('2.0', $properties[0]->Value());
197 function testClearPropertiesArrayParam(){
198 //$lines = new HeapLines((new bigtestdata)->testdata2);
199 $data = $this->getData('tests/data/inner_valarmdata.test');
200 $component = new vComponent($data);
202 $this->assertEquals(4, $component->propertiesCount());
203 $component->ClearProperties(["VERSION" => true, "CALSCALE" => true]);
204 $this->assertEquals(2, $component->propertiesCount());
205 //$this->assertStringStartsWith('2.0', $properties[0]->Value());
209 function testAddPropertyLikeProperty(){
210 //$lines = new HeapLines((new bigtestdata)->testdata2);
211 $data = $this->getData('tests/data/inner_valarmdata.test');
212 $component = new vComponent($data);
214 $property = new vProperty('HELLO');
216 $this->assertEquals(4, $component->propertiesCount());
217 $component->AddProperty($property);
218 $this->assertEquals(5, $component->propertiesCount());
219 $this->assertFalse($component->isValid());
220 $p = $component->GetProperties(["HELLO" => true ]);
221 $this->assertNotNull($p);
223 //$this->assertStringStartsWith('2.0', $properties[0]->Value());
226 function testAddPropertyLikeNameAndValue(){
227 //$lines = new HeapLines((new bigtestdata)->testdata2);
228 $data = $this->getData('tests/data/inner_valarmdata.test');
229 $component = new vComponent($data);
232 $this->assertEquals(4, $component->propertiesCount());
233 $component->AddProperty("HELLO", "WORLD");
234 $this->assertEquals(5, $component->propertiesCount());
235 $this->assertFalse($component->isValid());
236 $p = $component->GetProperties(["HELLO" => true ]);
237 $this->assertNotNull($p);
239 //$this->assertStringStartsWith('2.0', $properties[0]->Value());
242 function testComponentsNoParam(){
243 //$lines = new HeapLines((new bigtestdata)->testdata2);
244 $data = $this->getData('tests/data/tzid_data.test');
245 $component = new vComponent($data);
248 $p = $component->GetComponents();
249 $this->assertNotNull($p);
250 $this->assertCount(8, $p);
252 //$this->assertStringStartsWith('2.0', $properties[0]->Value());
255 function testComponentsByName(){
256 //$lines = new HeapLines((new bigtestdata)->testdata2);
257 $data = $this->getData('tests/data/tzid_data.test');
258 $component = new vComponent($data);
261 $p = $component->GetComponents("VEVENT");
262 $this->assertNotNull($p);
263 $this->assertCount(8, $p);
264 foreach($p as $item){
265 $this->assertStringStartsWith("VEVENT", $item->GetType());
268 //$this->assertStringStartsWith('2.0', $properties[0]->Value());
271 function testComponentsByArray(){
272 //$lines = new HeapLines((new bigtestdata)->testdata2);
273 $data = $this->getData('tests/data/tzid_data.test');
274 $component = new vComponent($data);
277 $p = $component->GetComponents(["VEVENT" => true]);
278 $this->assertNotNull($p);
279 $this->assertCount(8, $p);
280 foreach($p as $item){
281 $this->assertStringStartsWith("VEVENT", $item->GetType());
284 //$this->assertStringStartsWith('2.0', $properties[0]->Value());
288 function testClearComponentsNoParam(){
289 //$lines = new HeapLines((new bigtestdata)->testdata2);
290 $data = $this->getData('tests/data/inner_valarmdata.test');
291 $component = new vComponent($data);
294 $component->ClearComponents();
295 $this->assertEquals(0, $component->ComponentCount());
296 $this->assertFalse($component->isValid());
300 function testClearComponentsArrayParam(){
301 //$lines = new HeapLines((new bigtestdata)->testdata2);
302 $data = $this->getData('tests/data/inner_valarmdata.test');
303 $component = new vComponent($data);
306 $component->ClearComponents(["VEVENT" => true]);
307 $this->assertEquals(0, $component->ComponentCount());
308 $this->assertFalse($component->isValid());
312 function testClearComponentsStringParam(){
313 //$lines = new HeapLines((new bigtestdata)->testdata2);
314 $data = $this->getData('tests/data/inner_valarmdata.test');
315 $component = new vComponent($data);
318 $component->ClearComponents("VEVENT");
319 $this->assertEquals(0, $component->ComponentCount());
320 $this->assertFalse($component->isValid());
324 function testSetComponent(){
325 //$lines = new HeapLines((new bigtestdata)->testdata2);
326 $data = $this->getData('tests/data/inner_valarmdata.test');
327 $component = new vComponent($data);
329 $newcomponent = new vComponent("BEGIN:VEVENT");
330 $newcomponent2 = new vComponent("BEGIN:VEVENT");
332 $component->SetComponents([$newcomponent, $newcomponent2]);
333 $this->assertEquals(2, $component->ComponentCount());
334 $this->assertFalse($component->isValid());
338 function testAddComponents(){
339 //$lines = new HeapLines((new bigtestdata)->testdata2);
340 $data = $this->getData('tests/data/inner_valarmdata.test');
341 $component = new vComponent($data);
343 $newcomponent = new vComponent("BEGIN:VEVENT");
344 $newcomponent2 = new vComponent("BEGIN:VEVENT");
345 $newcomponent3 = new vComponent("BEGIN:VEVENT");
348 $component->SetComponents([$newcomponent]);
349 $component->AddComponent([$newcomponent2, $newcomponent3]);
350 $this->assertEquals(3, $component->ComponentCount());
351 $this->assertFalse($component->isValid());
355 function testAddComponentsWithEmptyComponents(){
356 //$lines = new HeapLines((new bigtestdata)->testdata2);
357 $component = new vComponent("VCALENDAR");
359 $newcomponent = new vComponent("VEVENT");
360 $newcomponent2 = new vComponent("VEVENT");
361 $newcomponent3 = new vComponent("VEVENT");
363 $this->assertEquals(0, $component->ComponentCount());
364 $component->AddComponent([$newcomponent]);
365 $this->assertEquals(1, $component->ComponentCount());
367 $component->AddComponent([$newcomponent2, $newcomponent3]);
368 $this->assertEquals(3, $component->ComponentCount());
369 $this->assertFalse($component->isValid());
373 function testMaskComponents(){
374 //$lines = new HeapLines((new bigtestdata)->testdata2);
375 $component = new vComponent("VCALENDAR");
377 $newcomponent = new vComponent("BEGIN:VEVENT");
378 $newcomponent2 = new vComponent("BEGIN:MASK");
379 $newcomponent3 = new vComponent("BEGIN:VEVENT");
381 $component->AddComponent([$newcomponent, $newcomponent2, $newcomponent3]);
382 $this->assertEquals(3, $component->ComponentCount());
383 $component->MaskComponents(["MASK" => true]);
384 $this->assertEquals(1, $component->ComponentCount());
385 $this->assertFalse($component->isValid());
390 function testMaskProperties(){
391 //$lines = new HeapLines((new bigtestdata)->testdata2);
392 $component = new vComponent("BEGIN:VCALENDAR\nEND:VCALENDAR");
394 $newcomponent = new vProperty("VEVENT");
395 $newcomponent2 = new vProperty("MASK");
396 $newcomponent3 = new vProperty("VEVENT");
398 $component->AddProperty($newcomponent);
399 $component->AddProperty($newcomponent2);
400 $component->AddProperty($newcomponent3);
401 $this->assertEquals(3, $component->propertiesCount());
402 $component->MaskProperties(["MASK" => true]);
403 $this->assertEquals(1, $component->propertiesCount());
404 $this->assertFalse($component->isValid());
410 function te2stRenderCreated(){
412 $component = new vComponent("BEGIN:VCALENDAR");
413 $component->AddProperty("SUMARY", "12234567890122345678901223456789012234567890122345678901223467890122345678901223456789012");
414 $text = $component->Render();
416 //$this->assertStringStartsWith($this->simpledata1, $text);
419 function testRenderNoChanges2(){
420 // $testdata = new bigtestdata();
421 // //$lines = new HeapLines($this->simpledata1);
422 // $component = new vComponent((new bigtestdata)->testdata2);
423 // //$component->AddProperty("SUMARY", "12234567890122345678901223456789012234567890122345678901223467890122345678901223456789012");
424 // $text = $component->Render();
426 //$this->assertStringStartsWith($this->simpledata1, $text);
429 function getData($filename){
430 $file = fopen($filename, 'r');
431 $data = fread($file, filesize($filename));
432 fclose($file);
433 return $data;
436 function testInnerAlarmPresent(){
437 $data = $this->getData('tests/data/inner_valarmdata.test');
438 $vComponent = new vComponent($data);
439 $this->assertEquals(4, $vComponent->propertiesCount());
440 $components = $vComponent->GetComponents();
442 $this->assertEquals(1, count($components));
443 $inner = $components[0];
444 $this->assertEquals(1, $inner->ComponentCount());
445 $this->assertEquals(20, $inner->propertiesCount());
446 $this->assertEquals(5, $inner->GetComponents()[0]->propertiesCount());