1<?php 2 3namespace Sabre\VObject; 4 5class BirthdayCalendarGeneratorTest extends \PHPUnit_Framework_TestCase { 6 7 use PHPUnitAssertions; 8 9 function testVcardStringWithValidBirthday() { 10 11 $generator = new BirthdayCalendarGenerator(); 12 $input = <<<VCF 13BEGIN:VCARD 14VERSION:3.0 15N:Gump;Forrest;;Mr. 16FN:Forrest Gump 17BDAY:19850407 18UID:foo 19END:VCARD 20VCF; 21 22 $expected = <<<ICS 23BEGIN:VCALENDAR 24VERSION:2.0 25BEGIN:VEVENT 26UID:**ANY** 27DTSTAMP:**ANY** 28SUMMARY:Forrest Gump's Birthday 29DTSTART;VALUE=DATE:19850407 30RRULE:FREQ=YEARLY 31TRANSP:TRANSPARENT 32X-SABRE-BDAY;X-SABRE-VCARD-UID=foo;X-SABRE-VCARD-FN=Forrest Gump:BDAY 33END:VEVENT 34END:VCALENDAR 35ICS; 36 37 $generator->setObjects($input); 38 $output = $generator->getResult(); 39 40 $this->assertVObjectEqualsVObject( 41 $expected, 42 $output 43 ); 44 45 } 46 47 function testArrayOfVcardStringsWithValidBirthdays() { 48 49 $generator = new BirthdayCalendarGenerator(); 50 $input = []; 51 52 $input[] = <<<VCF 53BEGIN:VCARD 54VERSION:3.0 55N:Gump;Forrest;;Mr. 56FN:Forrest Gump 57BDAY:19850407 58UID:foo 59END:VCARD 60VCF; 61 62 $input[] = <<<VCF 63BEGIN:VCARD 64VERSION:3.0 65N:Doe;John;;Mr. 66FN:John Doe 67BDAY:19820210 68UID:bar 69END:VCARD 70VCF; 71 72 $expected = <<<ICS 73BEGIN:VCALENDAR 74VERSION:2.0 75BEGIN:VEVENT 76UID:**ANY** 77DTSTAMP:**ANY** 78SUMMARY:Forrest Gump's Birthday 79DTSTART;VALUE=DATE:19850407 80RRULE:FREQ=YEARLY 81TRANSP:TRANSPARENT 82X-SABRE-BDAY;X-SABRE-VCARD-UID=foo;X-SABRE-VCARD-FN=Forrest Gump:BDAY 83END:VEVENT 84BEGIN:VEVENT 85UID:**ANY** 86DTSTAMP:**ANY** 87SUMMARY:John Doe's Birthday 88DTSTART;VALUE=DATE:19820210 89RRULE:FREQ=YEARLY 90TRANSP:TRANSPARENT 91X-SABRE-BDAY;X-SABRE-VCARD-UID=bar;X-SABRE-VCARD-FN=John Doe:BDAY 92END:VEVENT 93END:VCALENDAR 94ICS; 95 96 $generator->setObjects($input); 97 $output = $generator->getResult(); 98 99 $this->assertVObjectEqualsVObject( 100 $expected, 101 $output 102 ); 103 104 } 105 106 function testArrayOfVcardStringsWithValidBirthdaysViaConstructor() { 107 108 $input = []; 109 110 $input[] = <<<VCF 111BEGIN:VCARD 112VERSION:3.0 113N:Gump;Forrest;;Mr. 114FN:Forrest Gump 115BDAY:19850407 116UID:foo 117END:VCARD 118VCF; 119 120 $input[] = <<<VCF 121BEGIN:VCARD 122VERSION:3.0 123N:Doe;John;;Mr. 124FN:John Doe 125BDAY:19820210 126UID:bar 127END:VCARD 128VCF; 129 130 $generator = new BirthdayCalendarGenerator($input); 131 132 $expected = <<<ICS 133BEGIN:VCALENDAR 134VERSION:2.0 135BEGIN:VEVENT 136UID:**ANY** 137DTSTAMP:**ANY** 138SUMMARY:Forrest Gump's Birthday 139DTSTART;VALUE=DATE:19850407 140RRULE:FREQ=YEARLY 141TRANSP:TRANSPARENT 142X-SABRE-BDAY;X-SABRE-VCARD-UID=foo;X-SABRE-VCARD-FN=Forrest Gump:BDAY 143END:VEVENT 144BEGIN:VEVENT 145UID:**ANY** 146DTSTAMP:**ANY** 147SUMMARY:John Doe's Birthday 148DTSTART;VALUE=DATE:19820210 149RRULE:FREQ=YEARLY 150TRANSP:TRANSPARENT 151X-SABRE-BDAY;X-SABRE-VCARD-UID=bar;X-SABRE-VCARD-FN=John Doe:BDAY 152END:VEVENT 153END:VCALENDAR 154ICS; 155 156 $generator->setObjects($input); 157 $output = $generator->getResult(); 158 159 $this->assertVObjectEqualsVObject( 160 $expected, 161 $output 162 ); 163 164 } 165 166 function testVcardObjectWithValidBirthday() { 167 168 $generator = new BirthdayCalendarGenerator(); 169 $input = <<<VCF 170BEGIN:VCARD 171VERSION:3.0 172N:Gump;Forrest;;Mr. 173FN:Forrest Gump 174BDAY:19850407 175UID:foo 176END:VCARD 177VCF; 178 179 $input = Reader::read($input); 180 181 $expected = <<<ICS 182BEGIN:VCALENDAR 183VERSION:2.0 184BEGIN:VEVENT 185UID:**ANY** 186DTSTAMP:**ANY** 187SUMMARY:Forrest Gump's Birthday 188DTSTART;VALUE=DATE:19850407 189RRULE:FREQ=YEARLY 190TRANSP:TRANSPARENT 191X-SABRE-BDAY;X-SABRE-VCARD-UID=foo;X-SABRE-VCARD-FN=Forrest Gump:BDAY 192END:VEVENT 193END:VCALENDAR 194ICS; 195 196 $generator->setObjects($input); 197 $output = $generator->getResult(); 198 199 $this->assertVObjectEqualsVObject( 200 $expected, 201 $output 202 ); 203 204 } 205 206 function testArrayOfVcardObjectsWithValidBirthdays() { 207 208 $generator = new BirthdayCalendarGenerator(); 209 $input = []; 210 211 $input[] = <<<VCF 212BEGIN:VCARD 213VERSION:3.0 214N:Gump;Forrest;;Mr. 215FN:Forrest Gump 216BDAY:19850407 217UID:foo 218END:VCARD 219VCF; 220 221 $input[] = <<<VCF 222BEGIN:VCARD 223VERSION:3.0 224N:Doe;John;;Mr. 225FN:John Doe 226BDAY:19820210 227UID:bar 228END:VCARD 229VCF; 230 231 foreach ($input as $key => $value) { 232 $input[$key] = Reader::read($value); 233 } 234 235 $expected = <<<ICS 236BEGIN:VCALENDAR 237VERSION:2.0 238BEGIN:VEVENT 239UID:**ANY** 240DTSTAMP:**ANY** 241SUMMARY:Forrest Gump's Birthday 242DTSTART;VALUE=DATE:19850407 243RRULE:FREQ=YEARLY 244TRANSP:TRANSPARENT 245X-SABRE-BDAY;X-SABRE-VCARD-UID=foo;X-SABRE-VCARD-FN=Forrest Gump:BDAY 246END:VEVENT 247BEGIN:VEVENT 248UID:**ANY** 249DTSTAMP:**ANY** 250SUMMARY:John Doe's Birthday 251DTSTART;VALUE=DATE:19820210 252RRULE:FREQ=YEARLY 253TRANSP:TRANSPARENT 254X-SABRE-BDAY;X-SABRE-VCARD-UID=bar;X-SABRE-VCARD-FN=John Doe:BDAY 255END:VEVENT 256END:VCALENDAR 257ICS; 258 259 $generator->setObjects($input); 260 $output = $generator->getResult(); 261 262 $this->assertVObjectEqualsVObject( 263 $expected, 264 $output 265 ); 266 267 } 268 269 function testVcardStringWithValidBirthdayWithXAppleOmitYear() { 270 271 $generator = new BirthdayCalendarGenerator(); 272 $input = <<<VCF 273BEGIN:VCARD 274VERSION:3.0 275N:Gump;Forrest;;Mr. 276FN:Forrest Gump 277BDAY;X-APPLE-OMIT-YEAR=1604:1604-04-07 278UID:foo 279END:VCARD 280VCF; 281 282 $expected = <<<ICS 283BEGIN:VCALENDAR 284VERSION:2.0 285BEGIN:VEVENT 286UID:**ANY** 287DTSTAMP:**ANY** 288SUMMARY:Forrest Gump's Birthday 289DTSTART;VALUE=DATE:20000407 290RRULE:FREQ=YEARLY 291TRANSP:TRANSPARENT 292X-SABRE-BDAY;X-SABRE-VCARD-UID=foo;X-SABRE-VCARD-FN=Forrest Gump;X-SABRE-OMIT-YEAR=2000:BDAY 293END:VEVENT 294END:VCALENDAR 295ICS; 296 297 $generator->setObjects($input); 298 $output = $generator->getResult(); 299 300 $this->assertVObjectEqualsVObject( 301 $expected, 302 $output 303 ); 304 305 } 306 307 function testVcardStringWithValidBirthdayWithoutYear() { 308 309 $generator = new BirthdayCalendarGenerator(); 310 $input = <<<VCF 311BEGIN:VCARD 312VERSION:4.0 313N:Gump;Forrest;;Mr. 314FN:Forrest Gump 315BDAY:--04-07 316UID:foo 317END:VCARD 318VCF; 319 320 $expected = <<<ICS 321BEGIN:VCALENDAR 322VERSION:2.0 323BEGIN:VEVENT 324UID:**ANY** 325DTSTAMP:**ANY** 326SUMMARY:Forrest Gump's Birthday 327DTSTART;VALUE=DATE:20000407 328RRULE:FREQ=YEARLY 329TRANSP:TRANSPARENT 330X-SABRE-BDAY;X-SABRE-VCARD-UID=foo;X-SABRE-VCARD-FN=Forrest Gump;X-SABRE-OMIT-YEAR=2000:BDAY 331END:VEVENT 332END:VCALENDAR 333ICS; 334 335 $generator->setObjects($input); 336 $output = $generator->getResult(); 337 338 $this->assertVObjectEqualsVObject( 339 $expected, 340 $output 341 ); 342 343 } 344 345 function testVcardStringWithInvalidBirthday() { 346 347 $generator = new BirthdayCalendarGenerator(); 348 $input = <<<VCF 349BEGIN:VCARD 350VERSION:3.0 351N:Gump;Forrest;;Mr. 352FN:Forrest Gump 353BDAY:foo 354X-SABRE-BDAY;X-SABRE-VCARD-UID=foo;X-SABRE-VCARD-FN=Forrest Gump:BDAY 355END:VCARD 356VCF; 357 358 $expected = <<<ICS 359BEGIN:VCALENDAR 360VERSION:2.0 361END:VCALENDAR 362ICS; 363 364 $generator->setObjects($input); 365 $output = $generator->getResult(); 366 367 $this->assertVObjectEqualsVObject( 368 $expected, 369 $output 370 ); 371 372 } 373 374 function testVcardStringWithNoBirthday() { 375 376 $generator = new BirthdayCalendarGenerator(); 377 $input = <<<VCF 378BEGIN:VCARD 379VERSION:3.0 380N:Gump;Forrest;;Mr. 381FN:Forrest Gump 382UID:foo 383END:VCARD 384VCF; 385 386 $expected = <<<ICS 387BEGIN:VCALENDAR 388VERSION:2.0 389END:VCALENDAR 390ICS; 391 392 $generator->setObjects($input); 393 $output = $generator->getResult(); 394 395 $this->assertVObjectEqualsVObject( 396 $expected, 397 $output 398 ); 399 400 } 401 402 function testVcardStringWithValidBirthdayLocalized() { 403 404 $generator = new BirthdayCalendarGenerator(); 405 $input = <<<VCF 406BEGIN:VCARD 407VERSION:3.0 408N:Gump;Forrest;;Mr. 409FN:Forrest Gump 410BDAY:19850407 411UID:foo 412END:VCARD 413VCF; 414 415 $expected = <<<ICS 416BEGIN:VCALENDAR 417VERSION:2.0 418BEGIN:VEVENT 419UID:**ANY** 420DTSTAMP:**ANY** 421SUMMARY:Forrest Gump's Geburtstag 422DTSTART;VALUE=DATE:19850407 423RRULE:FREQ=YEARLY 424TRANSP:TRANSPARENT 425X-SABRE-BDAY;X-SABRE-VCARD-UID=foo;X-SABRE-VCARD-FN=Forrest Gump:BDAY 426END:VEVENT 427END:VCALENDAR 428ICS; 429 430 $generator->setObjects($input); 431 $generator->setFormat('%1$s\'s Geburtstag'); 432 $output = $generator->getResult(); 433 434 $this->assertVObjectEqualsVObject( 435 $expected, 436 $output 437 ); 438 439 } 440 441 function testVcardStringWithEmptyBirthdayProperty() { 442 443 $generator = new BirthdayCalendarGenerator(); 444 $input = <<<VCF 445BEGIN:VCARD 446VERSION:3.0 447N:Gump;Forrest;;Mr. 448FN:Forrest Gump 449BDAY: 450UID:foo 451END:VCARD 452VCF; 453 454 $expected = <<<ICS 455BEGIN:VCALENDAR 456VERSION:2.0 457END:VCALENDAR 458ICS; 459 460 $generator->setObjects($input); 461 $output = $generator->getResult(); 462 463 $this->assertVObjectEqualsVObject( 464 $expected, 465 $output 466 ); 467 468 } 469 470 /** 471 * @expectedException \Sabre\VObject\ParseException 472 */ 473 function testParseException() { 474 475 $generator = new BirthdayCalendarGenerator(); 476 $input = <<<FOO 477BEGIN:FOO 478FOO:Bar 479END:FOO 480FOO; 481 482 $generator->setObjects($input); 483 484 } 485 486 /** 487 * @expectedException \InvalidArgumentException 488 */ 489 function testInvalidArgumentException() { 490 491 $generator = new BirthdayCalendarGenerator(); 492 $input = <<<ICS 493BEGIN:VCALENDAR 494VERSION:2.0 495BEGIN:VEVENT 496SUMMARY:Foo 497DTSTART;VALUE=DATE:19850407 498END:VEVENT 499END:VCALENDAR 500ICS; 501 502 $generator->setObjects($input); 503 504 } 505 506 /** 507 * @expectedException \InvalidArgumentException 508 */ 509 function testInvalidArgumentExceptionForPartiallyInvalidArray() { 510 511 $generator = new BirthdayCalendarGenerator(); 512 $input = []; 513 514 $input[] = <<<VCF 515BEGIN:VCARD 516VERSION:3.0 517N:Gump;Forrest;;Mr. 518FN:Forrest Gump 519BDAY:19850407 520UID:foo 521END:VCARD 522VCF; 523 $calendar = new Component\VCalendar(); 524 525 $input = $calendar->add('VEVENT', [ 526 'SUMMARY' => 'Foo', 527 'DTSTART' => new \DateTime('NOW'), 528 ]); 529 530 $generator->setObjects($input); 531 532 } 533 534 function testBrokenVcardWithoutFN() { 535 536 $generator = new BirthdayCalendarGenerator(); 537 $input = <<<VCF 538BEGIN:VCARD 539VERSION:3.0 540N:Gump;Forrest;;Mr. 541BDAY:19850407 542UID:foo 543END:VCARD 544VCF; 545 546 $expected = <<<ICS 547BEGIN:VCALENDAR 548VERSION:2.0 549END:VCALENDAR 550ICS; 551 552 $generator->setObjects($input); 553 $output = $generator->getResult(); 554 555 $this->assertVObjectEqualsVObject( 556 $expected, 557 $output 558 ); 559 560 } 561 562} 563